Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: allow to test webhook on creation (#7402)

Danny Xufc248777eb05
diff --git a/backend/api/v1/project_service.go b/backend/api/v1/project_service.go
index 71c5bbcdb5..9ddd782490 100644
--- a/backend/api/v1/project_service.go
+++ b/backend/api/v1/project_service.go
@@ -832,6 +832,110 @@ func (s *ProjectService) UpdateWebhook(ctx context.Context, request *v1pb.Update
return convertToProject(project), nil
}
+// RemoveWebhook removes a webhook from a given project.
+func (s *ProjectService) RemoveWebhook(ctx context.Context, request *v1pb.RemoveWebhookRequest) (*v1pb.Project, error) {
+ projectID, webhookID, err := common.GetProjectIDWebhookID(request.Webhook.Name)
+ if err != nil {
+ return nil, status.Errorf(codes.InvalidArgument, err.Error())
+ }
+ webhookIDInt, err := strconv.Atoi(webhookID)
+ if err != nil {
+ return nil, status.Errorf(codes.InvalidArgument, "invalid webhook id %q", webhookID)
+ }
+
+ project, err := s.store.GetProjectV2(ctx, &store.FindProjectMessage{
+ ResourceID: &projectID,
+ })
+ if err != nil {
+ return nil, status.Errorf(codes.Internal, err.Error())
+ }
+ if project == nil {
+ return nil, status.Errorf(codes.NotFound, "project %q not found", webhookID)
+ }
+ if project.Deleted {
+ return nil, status.Errorf(codes.NotFound, "project %q has been deleted", projectID)
+ }
+
+ webhook, err := s.store.GetProjectWebhookV2(ctx, &store.FindProjectWebhookMessage{
+ ProjectID: &project.UID,
+ ID: &webhookIDInt,
+ })
+ if err != nil {
+ return nil, status.Errorf(codes.Internal, err.Error())
+ }
+ if webhook == nil {
+ return nil, status.Errorf(codes.NotFound, "webhook %q not found", request.Webhook.Url)
+ }
+
+ if err := s.store.DeleteProjectWebhookV2(ctx, project.ResourceID, webhook.ID); err != nil {
+ return nil, status.Errorf(codes.Internal, err.Error())
+ }
+
+ project, err = s.store.GetProjectV2(ctx, &store.FindProjectMessage{
+ ResourceID: &projectID,
+ })
+ if err != nil {
+ return nil, status.Errorf(codes.Internal, err.Error())
+ }
+ return convertToProject(project), nil
+}
+
+// TestWebhook tests a webhook.
+func (s *ProjectService) TestWebhook(ctx context.Context, request *v1pb.TestWebhookRequest) (*v1pb.TestWebhookResponse, error) {
+ setting, err := s.store.GetWorkspaceGeneralSetting(ctx)
+ if err != nil {
+ return nil, status.Errorf(codes.Internal, "failed to get workspace setting: %v", err)
+ }
+ if setting.ExternalUrl == "" {
+ return nil, status.Errorf(codes.FailedPrecondition, setupExternalURLError)
+ }
+
+ projectID, err := common.GetProjectID(request.Project)
+ if err != nil {
+ return nil, status.Errorf(codes.InvalidArgument, err.Error())
+ }
+ project, err := s.store.GetProjectV2(ctx, &store.FindProjectMessage{
+ ResourceID: &projectID,
+ })
+ if err != nil {
+ return nil, status.Errorf(codes.Internal, err.Error())
+ }
+ if project == nil {
+ return nil, status.Errorf(codes.NotFound, "project %q not found", request.Project)
+ }
+ if project.Deleted {
+ return nil, status.Errorf(codes.NotFound, "project %q has been deleted", request.Project)
+ }
+
+ webhook, err := convertToStoreProjectWebhookMessage(request.Webhook)
+ if err != nil {
+ return nil, status.Errorf(codes.InvalidArgument, err.Error())
+ }
+
+ resp := &v1pb.TestWebhookResponse{}
+ err = webhookPlugin.Post(
+ webhook.Type,
+ webhookPlugin.Context{
+ URL: webhook.URL,
+ Level: webhookPlugin.WebhookInfo,
+ ActivityType: string(api.ActivityIssueCreate),
+ Title: fmt.Sprintf("Test webhook %q", webhook.Title),
+ Description: "This is a test",
+ Link: fmt.Sprintf("%s/project/%s/webhook/%s", setting.ExternalUrl, fmt.Sprintf("%s-%d", slug.Make(project.Title), project.UID), fmt.Sprintf("%s-%d", slug.Make(webhook.Title), webhook.ID)),
+ CreatorID: api.SystemBotID,
+ CreatorName: "Bytebase",
+ CreatorEmail: api.SystemBotEmail,
+ CreatedTs: time.Now().Unix(),
+ Project: &webhookPlugin.Project{Name: project.Title},
+ },
+ )
+ if err != nil {
+ resp.Error = err.Error()
+ }
+
+ return resp, nil
+}
+
func (s *ProjectService) findProjectRepository(ctx context.Context, projectName string) (*store.RepositoryMessage, error) {
project, err := s.getProjectMessage(ctx, projectName)
if err != nil {
@@ -1443,116 +1547,6 @@ func (s *ProjectService) createOrUpdateVCSSQLReviewFileForGitLab(
)
}
-// RemoveWebhook removes a webhook from a given project.
-func (s *ProjectService) RemoveWebhook(ctx context.Context, request *v1pb.RemoveWebhookRequest) (*v1pb.Project, error) {
- projectID, webhookID, err := common.GetProjectIDWebhookID(request.Webhook.Name)
- if err != nil {
- return nil, status.Errorf(codes.InvalidArgument, err.Error())
- }
- webhookIDInt, err := strconv.Atoi(webhookID)
- if err != nil {
- return nil, status.Errorf(codes.InvalidArgument, "invalid webhook id %q", webhookID)
- }
-
- project, err := s.store.GetProjectV2(ctx, &store.FindProjectMessage{
- ResourceID: &projectID,
- })
- if err != nil {
- return nil, status.Errorf(codes.Internal, err.Error())
- }
- if project == nil {
- return nil, status.Errorf(codes.NotFound, "project %q not found", webhookID)
- }
- if project.Deleted {
- return nil, status.Errorf(codes.NotFound, "project %q has been deleted", projectID)
- }
-
- webhook, err := s.store.GetProjectWebhookV2(ctx, &store.FindProjectWebhookMessage{
- ProjectID: &project.UID,
- ID: &webhookIDInt,
- })
- if err != nil {
- return nil, status.Errorf(codes.Internal, err.Error())
- }
- if webhook == nil {
- return nil, status.Errorf(codes.NotFound, "webhook %q not found", request.Webhook.Url)
- }
-
- if err := s.store.DeleteProjectWebhookV2(ctx, project.ResourceID, webhook.ID); err != nil {
- return nil, status.Errorf(codes.Internal, err.Error())
- }
-
- project, err = s.store.GetProjectV2(ctx, &store.FindProjectMessage{
- ResourceID: &projectID,
- })
- if err != nil {
- return nil, status.Errorf(codes.Internal, err.Error())
- }
- return convertToProject(project), nil
-}
-
-// TestWebhook tests a webhook.
-func (s *ProjectService) TestWebhook(ctx context.Context, request *v1pb.TestWebhookRequest) (*v1pb.TestWebhookResponse, error) {
- setting, err := s.store.GetWorkspaceGeneralSetting(ctx)
- if err != nil {
- return nil, status.Errorf(codes.Internal, "failed to get workspace setting: %v", err)
- }
- if setting.ExternalUrl == "" {
- return nil, status.Errorf(codes.FailedPrecondition, setupExternalURLError)
- }
-
- projectID, err := common.GetProjectID(request.Project)
- if err != nil {
- return nil, status.Errorf(codes.InvalidArgument, err.Error())
- }
- project, err := s.store.GetProjectV2(ctx, &store.FindProjectMessage{
- ResourceID: &projectID,
- })
- if err != nil {
- return nil, status.Errorf(codes.Internal, err.Error())
- }
- if project == nil {
- return nil, status.Errorf(codes.NotFound, "project %q not found", request.Project)
- }
- if project.Deleted {
- return nil, status.Errorf(codes.NotFound, "project %q has been deleted", request.Project)
- }
-
- webhook, err := s.store.GetProjectWebhookV2(ctx, &store.FindProjectWebhookMessage{
- ProjectID: &project.UID,
- URL: &request.Webhook.Url,
- })
- if err != nil {
- return nil, status.Errorf(codes.Internal, err.Error())
- }
- if webhook == nil {
- return nil, status.Errorf(codes.NotFound, "webhook %q not found", request.Webhook.Url)
- }
-
- resp := &v1pb.TestWebhookResponse{}
- err = webhookPlugin.Post(
- webhook.Type,
- webhookPlugin.Context{
- URL: webhook.URL,
- Level: webhookPlugin.WebhookInfo,
- ActivityType: string(api.ActivityIssueCreate),
- Title: fmt.Sprintf("Test webhook %q", webhook.Title),
- Description: "This is a test",
- Link: fmt.Sprintf("%s/project/%s/webhook/%s", setting.ExternalUrl, fmt.Sprintf("%s-%d", slug.Make(project.Title), project.UID), fmt.Sprintf("%s-%d", slug.Make(webhook.Title), webhook.ID)),
- CreatorID: api.SystemBotID,
- CreatorName: "Bytebase",
- CreatorEmail: api.SystemBotEmail,
- CreatedTs: time.Now().Unix(),
- Project: &webhookPlugin.Project{Name: project.Title},
- },
- )
- if err != nil {
- resp.Error = err.Error()
- }
-
- return resp, nil
-}
-
// CreateDatabaseGroup creates a database group.
func (s *ProjectService) CreateDatabaseGroup(ctx context.Context, request *v1pb.CreateDatabaseGroupRequest) (*v1pb.DatabaseGroup, error) {
if err := s.licenseService.IsFeatureEnabled(api.FeatureDatabaseGrouping); err != nil {
diff --git a/frontend/src/components/ProjectWebhookForm.vue b/frontend/src/components/ProjectWebhookForm.vue
index a6b946bb99..d21f3243e0 100644
--- a/frontend/src/components/ProjectWebhookForm.vue
+++ b/frontend/src/components/ProjectWebhookForm.vue
@@ -229,6 +229,13 @@
/>
</div>
</div>
+ <button
+ type="button"
+ class="btn-normal whitespace-nowrap items-center"
+ @click.prevent="testWebhook"
+ >
+ {{ $t("project.webhook.test-webhook") }}
+ </button>
<div
class="flex pt-5"
:class="!create && allowEdit ? 'justify-between' : 'justify-end'"
@@ -490,4 +497,31 @@ const deleteWebhook = () => {
cancel();
});
};
+
+const testWebhook = () => {
+ useGracefulRequest(async () => {
+ console.log("Barny1", props.project);
+ console.log("Barny2", state.webhook);
+ const result = await useProjectWebhookV1Store().testProjectWebhook(
+ props.project,
+ state.webhook
+ );
+
+ if (result.error) {
+ pushNotification({
+ module: "bytebase",
+ style: "CRITICAL",
+ title: t("project.webhook.fail-tested-title"),
+ description: result.error,
+ manualHide: true,
+ });
+ } else {
+ pushNotification({
+ module: "bytebase",
+ style: "SUCCESS",
+ title: t("project.webhook.success-tested-prompt"),
+ });
+ }
+ });
+};
</script>
diff --git a/frontend/src/views/ProjectWebhookDetail.vue b/frontend/src/views/ProjectWebhookDetail.vue
index 56fb3ae3b8..32027d2489 100644
--- a/frontend/src/views/ProjectWebhookDetail.vue
+++ b/frontend/src/views/ProjectWebhookDetail.vue
@@ -31,13 +31,6 @@
{{ projectWebhook.title }}
</h3>
</div>
- <button
- type="button"
- class="btn-normal whitespace-nowrap items-center"
- @click.prevent="testWebhook"
- >
- {{ $t("project.webhook.test-webhook") }}
- </button>
</div>
<ProjectWebhookForm
class="pt-4"
@@ -51,16 +44,10 @@
<script lang="ts" setup>
import { computed } from "vue";
-import { useI18n } from "vue-i18n";
import ProjectWebhookForm from "../components/ProjectWebhookForm.vue";
import { idFromSlug } from "../utils";
-import {
- pushNotification,
- useProjectV1Store,
- useProjectWebhookV1Store,
- useGracefulRequest,
-} from "@/store";
+import { useProjectV1Store, useProjectWebhookV1Store } from "@/store";
import { Webhook_Type } from "@/types/proto/v1/project_service";
import { emptyProjectWebhook } from "@/types";
@@ -79,7 +66,6 @@ const props = defineProps({
},
});
-const { t } = useI18n();
const projectV1Store = useProjectV1Store();
const projectWebhookV1Store = useProjectWebhookV1Store();
@@ -94,29 +80,4 @@ const projectWebhook = computed(() => {
emptyProjectWebhook()
);
});
-
-const testWebhook = () => {
- useGracefulRequest(async () => {
- const result = await useProjectWebhookV1Store().testProjectWebhook(
- project.value,
- projectWebhook.value
- );
-
- if (result.error) {
- pushNotification({
- module: "bytebase",
- style: "CRITICAL",
- title: t("project.webhook.fail-tested-title"),
- description: result.error,
- manualHide: true,
- });
- } else {
- pushNotification({
- module: "bytebase",
- style: "SUCCESS",
- title: t("project.webhook.success-tested-prompt"),
- });
- }
- });
-};
</script>