Skip to content

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

feat: add custom project webhook (#1184)

Cluas0e28f8520753
diff --git a/frontend/src/components/ProjectWebhookForm.vue b/frontend/src/components/ProjectWebhookForm.vue
index 44be557db8..3f75938606 100644
--- a/frontend/src/components/ProjectWebhookForm.vue
+++ b/frontend/src/components/ProjectWebhookForm.vue
@@ -4,7 +4,7 @@
<label class="textlabel">
{{ $t("project.webhook.destination") }}
</label>
- <div class="mt-1 grid grid-cols-1 gap-4 sm:grid-cols-6">
+ <div class="mt-1 grid grid-cols-1 gap-4 sm:grid-cols-7">
<template
v-for="(item, index) in PROJECT_HOOK_TYPE_ITEM_LIST()"
:key="index"
@@ -34,6 +34,9 @@
<template v-else-if="item.type == 'bb.plugin.webhook.wecom'">
<img class="h-10 w-10" src="../assets/wecom-logo.png" />
</template>
+ <template v-else-if="item.type == 'bb.plugin.webhook.custom'">
+ <heroicons-outline:puzzle class="w-10 h-10" />
+ </template>
<p class="mt-1 text-center textlabel">
{{ item.name }}
</p>
@@ -169,6 +172,23 @@
})
}}
</template>
+ <template v-else-if="state.webhook.type == 'bb.plugin.webhook.custom'">
+ {{
+ $t("project.webhook.creation.desc", {
+ destination: $t("common.custom"),
+ })
+ }}
+ <a
+ href="https://www.bytebase.com/docs/use-bytebase/webhook-integration/project-webhook#custom"
+ target="__blank"
+ class="normal-link"
+ >{{
+ $t("project.webhook.creation.view-doc", {
+ destination: $t("common.custom"),
+ })
+ }}</a
+ >.
+ </template>
</div>
<input
id="url"
@@ -314,6 +334,8 @@ export default defineComponent({
return `${t("common.feishu")} Webhook`;
} else if (state.webhook.type == "bb.plugin.webhook.wecom") {
return `${t("common.wecom")} Webhook`;
+ } else if (state.webhook.type == "bb.plugin.webhook.custom") {
+ return `${t("common.custom")} Webhook`;
}
return "My Webhook";
@@ -332,6 +354,8 @@ export default defineComponent({
return "https://open.feishu.cn/open-apis/bot/v2/hook/...";
} else if (state.webhook.type == "bb.plugin.webhook.wecom") {
return "https://qyapi.weixin.qq.com/cgi-bin/webhook/...";
+ } else if (state.webhook.type == "bb.plugin.webhook.custom") {
+ return "https://example.com/api/webhook/...";
}
return "Webhook URL";
diff --git a/frontend/src/locales/en.yml b/frontend/src/locales/en.yml
index 4bb79bc63b..73c1c1f684 100644
--- a/frontend/src/locales/en.yml
+++ b/frontend/src/locales/en.yml
@@ -9,6 +9,7 @@ common:
dingtalk: DingTalk
feishu: Feishu
wecom: WeCom
+ custom: Custom
overview: Overview
migration-history: Migration History
webhook: Webhook
diff --git a/frontend/src/locales/zh-CN.yml b/frontend/src/locales/zh-CN.yml
index 6cb290691b..795644034d 100644
--- a/frontend/src/locales/zh-CN.yml
+++ b/frontend/src/locales/zh-CN.yml
@@ -9,6 +9,7 @@ common:
dingtalk: 钉钉
feishu: 飞书
wecom: 企业微信
+ custom: 自定义
overview: 概览
migration-history: 迁移历史
webhook: Webhook
diff --git a/frontend/src/types/projectWebhook.ts b/frontend/src/types/projectWebhook.ts
index eff04a3c31..d107e6dbb2 100644
--- a/frontend/src/types/projectWebhook.ts
+++ b/frontend/src/types/projectWebhook.ts
@@ -41,6 +41,12 @@ export const PROJECT_HOOK_TYPE_ITEM_LIST: () => ProjectWebhookTypeItem[] =
name: t("common.wecom"),
urlPrefix: "https://qyapi.weixin.qq.com",
},
+ {
+ type: "bb.plugin.webhook.custom",
+ name: t("common.custom"),
+ urlPrefix:
+ "https://www.bytebase.com/docs/use-bytebase/webhook-integration/project-webhook#custom",
+ },
];
type ProjectWebhookActivityItem = {
diff --git a/frontend/src/views/ProjectWebhookDetail.vue b/frontend/src/views/ProjectWebhookDetail.vue
index 318dfe4d3d..ce08d15267 100644
--- a/frontend/src/views/ProjectWebhookDetail.vue
+++ b/frontend/src/views/ProjectWebhookDetail.vue
@@ -26,6 +26,9 @@
<template v-else-if="projectWebhook.type == 'bb.plugin.webhook.wecom'">
<img class="h-6 w-6" src="../assets/wecom-logo.png" />
</template>
+ <template v-else-if="projectWebhook.type == 'bb.plugin.webhook.custom'">
+ <heroicons-outline:puzzle class="w-6 h-6" />
+ </template>
<h3 class="text-xl leading-6 font-medium text-main">
{{ projectWebhook.name }}
</h3>
diff --git a/plugin/webhook/custom.go b/plugin/webhook/custom.go
new file mode 100644
index 0000000000..43c98bcb34
--- /dev/null
+++ b/plugin/webhook/custom.go
@@ -0,0 +1,92 @@
+package webhook
+
+import (
+ "bytes"
+ "encoding/json"
+ "fmt"
+ "io"
+ "net/http"
+)
+
+// CustomWebhookResponse is the API message for Custom webhook response.
+type CustomWebhookResponse struct {
+ Code int `json:"code"`
+ Message string `json:"message"`
+}
+
+// CustomWebhookRequest is the API message for Custom webhook request.
+type CustomWebhookRequest struct {
+ Level Level `json:"level"`
+ ActivityType string `json:"activity_type"`
+ Title string `json:"title"`
+ Description string `json:"description"`
+ Link string `json:"link"`
+ CreatorID int `json:"creator_id"`
+ CreatorName string `json:"creator_name"`
+ CreatedTS int64 `json:"created_ts"`
+ Issue Issue `json:"issue"`
+ Project Project `json:"project"`
+}
+
+func init() {
+ register("bb.plugin.webhook.custom", &CustomReceiver{})
+}
+
+// CustomReceiver is the receiver for custom.
+type CustomReceiver struct {
+}
+
+func (receiver *CustomReceiver) post(context Context) error {
+ payload := CustomWebhookRequest{
+ Level: context.Level,
+ ActivityType: context.ActivityType,
+ Title: context.Title,
+ Description: context.Description,
+ Link: context.Link,
+ CreatorID: context.CreatorID,
+ CreatorName: context.CreatorName,
+ CreatedTS: context.CreatedTs,
+ Issue: context.Issue,
+ Project: context.Project,
+ }
+
+ body, err := json.Marshal(&payload)
+ if err != nil {
+ return fmt.Errorf("failed to marshal webhook POST request: %v (%w)", context.URL, err)
+ }
+ req, err := http.NewRequest("POST",
+ context.URL, bytes.NewBuffer(body))
+ if err != nil {
+ return fmt.Errorf("failed to construct webhook POST request %v (%w)", context.URL, err)
+ }
+
+ req.Header.Set("Content-Type", "application/json")
+ client := &http.Client{
+ Timeout: timeout,
+ }
+ resp, err := client.Do(req)
+ if err != nil {
+ return fmt.Errorf("failed to POST webhook %v (%w)", context.URL, err)
+ }
+
+ b, err := io.ReadAll(resp.Body)
+ if err != nil {
+ return fmt.Errorf("failed to read POST webhook response %v (%w)", context.URL, err)
+ }
+ defer resp.Body.Close()
+
+ if resp.StatusCode != http.StatusOK {
+ return fmt.Errorf("failed to POST webhook %v, status code: %d, response body: %s", context.URL, resp.StatusCode, b)
+ }
+
+ webhookResponse := &CustomWebhookResponse{}
+ if err := json.Unmarshal(b, webhookResponse); err != nil {
+ return fmt.Errorf("malformatted webhook response %v (%w)", context.URL, err)
+ }
+
+ if webhookResponse.Code != 0 {
+ return fmt.Errorf("%s", webhookResponse.Message)
+ }
+
+ return nil
+}
diff --git a/plugin/webhook/dingtalk.go b/plugin/webhook/dingtalk.go
index e0a04e8304..23efe1980b 100644
--- a/plugin/webhook/dingtalk.go
+++ b/plugin/webhook/dingtalk.go
@@ -58,7 +58,7 @@ func (receiver *DingTalkReceiver) post(context Context) error {
}
body, err := json.Marshal(post)
if err != nil {
- return fmt.Errorf("failed to marshal webhook POST request: %v", context.URL)
+ return fmt.Errorf("failed to marshal webhook POST request: %v (%w)", context.URL, err)
}
req, err := http.NewRequest("POST",
context.URL, bytes.NewBuffer(body))
@@ -81,6 +81,10 @@ func (receiver *DingTalkReceiver) post(context Context) error {
}
defer resp.Body.Close()
+ if resp.StatusCode != http.StatusOK {
+ return fmt.Errorf("failed to POST webhook %v, status code: %d, response body: %s", context.URL, resp.StatusCode, b)
+ }
+
webhookResponse := &DingTalkWebhookResponse{}
if err := json.Unmarshal(b, webhookResponse); err != nil {
return fmt.Errorf("malformatted webhook response %v (%w)", context.URL, err)
diff --git a/plugin/webhook/discord.go b/plugin/webhook/discord.go
index eedfeaf763..25d9d5c25a 100644
--- a/plugin/webhook/discord.go
+++ b/plugin/webhook/discord.go
@@ -59,11 +59,12 @@ func (receiver *DiscordReceiver) post(context Context) error {
}
status := ""
- if context.Level == WebhookSuccess {
+ switch context.Level {
+ case WebhookSuccess:
status = ":white_check_mark: "
- } else if context.Level == WebhookWarn {
+ case WebhookWarn:
status = ":warning: "
- } else if context.Level == WebhookError {
+ case WebhookError:
status = ":exclamation: "
}
embedList = append(embedList, DiscordWebhookEmbed{
@@ -83,7 +84,7 @@ func (receiver *DiscordReceiver) post(context Context) error {
}
body, err := json.Marshal(post)
if err != nil {
- return fmt.Errorf("failed to marshal webhook POST request: %v", context.URL)
+ return fmt.Errorf("failed to marshal webhook POST request: %v (%w)", context.URL, err)
}
req, err := http.NewRequest("POST",
context.URL, bytes.NewBuffer(body))
@@ -106,6 +107,10 @@ func (receiver *DiscordReceiver) post(context Context) error {
}
defer resp.Body.Close()
+ if resp.StatusCode != http.StatusOK {
+ return fmt.Errorf("failed to POST webhook %v, status code: %d, response body: %s", context.URL, resp.StatusCode, b)
+ }
+
webhookResponse := &DiscordWebhookResponse{}
if err := json.Unmarshal(b, webhookResponse); err != nil {
return fmt.Errorf("malformatted webhook response %v (%w)", context.URL, err)
diff --git a/plugin/webhook/feishu.go b/plugin/webhook/feishu.go
index be4f85f1c3..ae72be8fef 100644
--- a/plugin/webhook/feishu.go
+++ b/plugin/webhook/feishu.go
@@ -125,7 +125,7 @@ func (receiver *FeishuReceiver) post(context Context) error {
}
body, err := json.Marshal(post)
if err != nil {
- return fmt.Errorf("failed to marshal webhook POST request: %v", context.URL)
+ return fmt.Errorf("failed to marshal webhook POST request: %v (%w)", context.URL, err)
}
req, err := http.NewRequest("POST",
context.URL, bytes.NewBuffer(body))
@@ -148,6 +148,10 @@ func (receiver *FeishuReceiver) post(context Context) error {
}
defer resp.Body.Close()
+ if resp.StatusCode != http.StatusOK {
+ return fmt.Errorf("failed to POST webhook %v, status code: %d, response body: %s", context.URL, resp.StatusCode, b)
+ }
+
webhookResponse := &FeishuWebhookResponse{}
if err := json.Unmarshal(b, webhookResponse); err != nil {
return fmt.Errorf("malformatted webhook response %v (%w)", context.URL, err)
diff --git a/plugin/webhook/slack.go b/plugin/webhook/slack.go
index ee02685eb4..c3f0260649 100644
--- a/plugin/webhook/slack.go
+++ b/plugin/webhook/slack.go
@@ -53,11 +53,12 @@ func (receiver *SlackReceiver) post(context Context) error {
blockList := []SlackWebhookBlock{}
status := ""
- if context.Level == WebhookSuccess {
+ switch context.Level {
+ case WebhookSuccess:
status = ":white_check_mark: "
- } else if context.Level == WebhookWarn {
+ case WebhookWarn:
status = ":warning: "
- } else if context.Level == WebhookError {
+ case WebhookError:
status = ":exclamation: "
}
blockList = append(blockList, SlackWebhookBlock{
@@ -124,7 +125,7 @@ func (receiver *SlackReceiver) post(context Context) error {
}
body, err := json.Marshal(post)
if err != nil {
- return fmt.Errorf("failed to marshal webhook POST request: %v", context.URL)
+ return fmt.Errorf("failed to marshal webhook POST request: %v (%w)", context.URL, err)
}
req, err := http.NewRequest("POST",
context.URL, bytes.NewBuffer(body))
@@ -138,7 +139,7 @@ func (receiver *SlackReceiver) post(context Context) error {
}
resp, err := client.Do(req)
if err != nil {
- return fmt.Errorf("failed to POST webhook %+v (%w)", context.URL, err)
+ return fmt.Errorf("failed to POST webhook %v (%w)", context.URL, err)
}
b, err := io.ReadAll(resp.Body)
@@ -147,6 +148,10 @@ func (receiver *SlackReceiver) post(context Context) error {
}
defer resp.Body.Close()
+ if resp.StatusCode != http.StatusOK {
+ return fmt.Errorf("failed to POST webhook %v, status code: %d, response body: %s", context.URL, resp.StatusCode, b)
+ }
+
if string(b) != "ok" {
return fmt.Errorf("%s", fmt.Sprintf("%.100s", string(b)))
}
diff --git a/plugin/webhook/teams.go b/plugin/webhook/teams.go
index 4796321142..c2bbd0e837 100644
--- a/plugin/webhook/teams.go
+++ b/plugin/webhook/teams.go
@@ -92,7 +92,7 @@ func (receiver *TeamsReceiver) post(context Context) error {
}
body, err := json.Marshal(post)
if err != nil {
- return fmt.Errorf("failed to marshal webhook POST request: %v", context.URL)
+ return fmt.Errorf("failed to marshal webhook POST request: %v (%w)", context.URL, err)
}
req, err := http.NewRequest("POST",
context.URL, bytes.NewBuffer(body))
@@ -106,7 +106,7 @@ func (receiver *TeamsReceiver) post(context Context) error {
}
resp, err := client.Do(req)
if err != nil {
- return fmt.Errorf("failed to POST webhook %+v (%w)", context.URL, err)
+ return fmt.Errorf("failed to POST webhook %v (%w)", context.URL, err)
}
b, err := io.ReadAll(resp.Body)
@@ -115,6 +115,10 @@ func (receiver *TeamsReceiver) post(context Context) error {
}
defer resp.Body.Close()
+ if resp.StatusCode != http.StatusOK {
+ return fmt.Errorf("failed to POST webhook %v, status code: %d, response body: %s", context.URL, resp.StatusCode, b)
+ }
+
if string(b) != "1" {
return fmt.Errorf("%s", fmt.Sprintf("%.100s", string(b)))
}
diff --git a/plugin/webhook/webhook.go b/plugin/webhook/webhook.go
index 06ba1a2396..1c8bf6721d 100644
--- a/plugin/webhook/webhook.go
+++ b/plugin/webhook/webhook.go
@@ -34,17 +34,36 @@ const (
WebhookError Level = "ERROR"
)
+// Issue object of issue
+type Issue struct {
+ ID int `json:"id"`
+ Name string `json:"name"`
+ Status string `json:"status"`
+ Type string `json:"type"`
+ Description string `json:"description"`
+}
+
+// Project object of project
+type Project struct {
+ ID int `json:"id"`
+ Name string `json:"name"`
+}
+
// Context is the context of webhook.
type Context struct {
URL string
Level Level
+ ActivityType string
Title string
Description string
Link string
+ CreatorID int
CreatorName string
CreatorEmail string
CreatedTs int64
MetaList []Meta
+ Issue Issue
+ Project Project
}
// Receiver is the webhook receiver.
diff --git a/plugin/webhook/wecom.go b/plugin/webhook/wecom.go
index 326dbbc0e6..91ed0c1779 100644
--- a/plugin/webhook/wecom.go
+++ b/plugin/webhook/wecom.go
@@ -44,11 +44,12 @@ func (receiver *WeComReceiver) post(context Context) error {
metaStrList = append(metaStrList, fmt.Sprintf("At: <font color=\"comment\">%s</font>", time.Unix(context.CreatedTs, 0).Format(timeFormat)))
status := ""
- if context.Level == WebhookSuccess {
+ switch context.Level {
+ case WebhookSuccess:
status = "<font color=\"green\">Success</font> "
- } else if context.Level == WebhookWarn {
+ case WebhookWarn:
status = "<font color=\"yellow\">Warn</font> "
- } else if context.Level == WebhookError {
+ case WebhookError:
status = "<font color=\"red\">Error</font> "
}
content := fmt.Sprintf("# %s%s\n\n%s\n[View in Bytebase](%s)", status, context.Title, strings.Join(metaStrList, "\n"), context.Link)
@@ -64,7 +65,7 @@ func (receiver *WeComReceiver) post(context Context) error {
}
body, err := json.Marshal(post)
if err != nil {
- return fmt.Errorf("failed to marshal webhook POST request: %v", context.URL)
+ return fmt.Errorf("failed to marshal webhook POST request: %v (%w)", context.URL, err)
}
req, err := http.NewRequest("POST",
context.URL, bytes.NewBuffer(body))
@@ -87,6 +88,10 @@ func (receiver *WeComReceiver) post(context Context) error {
}
defer resp.Body.Close()
+ if resp.StatusCode != http.StatusOK {
+ return fmt.Errorf("failed to POST webhook %v, status code: %d, response body: %s", context.URL, resp.StatusCode, b)
+ }
+
webhookResponse := &WeComWebhookResponse{}
if err := json.Unmarshal(b, webhookResponse); err != nil {
return fmt.Errorf("malformatted webhook response %v (%w)", context.URL, err)
diff --git a/server/activity_manager.go b/server/activity_manager.go
index 4bde10af71..714333bcf6 100644
--- a/server/activity_manager.go
+++ b/server/activity_manager.go
@@ -282,9 +282,22 @@ func (m *ActivityManager) getWebhookContext(ctx context.Context, activity *api.A
}
webhookCtx = webhook.Context{
Level: level,
+ ActivityType: string(activity.Type),
Title: title,
+ Issue: webhook.Issue{
+ ID: meta.issue.ID,
+ Name: meta.issue.Name,
+ Status: string(meta.issue.Status),
+ Type: string(meta.issue.Type),
+ Description: meta.issue.Description,
+ },
+ Project: webhook.Project{
+ ID: meta.issue.ProjectID,
+ Name: meta.issue.Project.Name,
+ },
Description: activity.Comment,
Link: link,
+ CreatorID: updater.ID,
CreatorName: updater.Name,
CreatorEmail: updater.Email,
MetaList: metaList,
diff --git a/server/project_webhook.go b/server/project_webhook.go
index 1f49f2d319..16aac2ca17 100644
--- a/server/project_webhook.go
+++ b/server/project_webhook.go
@@ -190,9 +190,11 @@ func (s *Server) registerProjectWebhookRoutes(g *echo.Group) {
webhookPlugin.Context{
URL: webhook.URL,
Level: webhookPlugin.WebhookInfo,
+ ActivityType: string(api.ActivityIssueCreate),
Title: fmt.Sprintf("Test webhook %q", webhook.Name),
Description: "This is a test",
Link: fmt.Sprintf("%s:%d/project/%s/webhook/%s", s.frontendHost, s.frontendPort, api.ProjectSlug(project), api.ProjectWebhookSlug(webhook)),
+ CreatorID: api.SystemBotID,
CreatorName: "Bytebase",
CreatorEmail: "[email protected]",
CreatedTs: time.Now().Unix(),
@@ -202,6 +204,7 @@ func (s *Server) registerProjectWebhookRoutes(g *echo.Group) {
Value: project.Name,
},
},
+ Project: webhookPlugin.Project{Name: project.Name},
},
)