Skip to content

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

Add webhook level so that the capable webhook provider can include better text like emoji

天舟3ae34b8438b5
diff --git a/plugin/webhook/discord.go b/plugin/webhook/discord.go
index 81b1923628..b57b9b4d66 100644
--- a/plugin/webhook/discord.go
+++ b/plugin/webhook/discord.go
@@ -52,8 +52,16 @@ func (receiver *DiscordReceiver) post(context WebhookContext) error {
fieldList = append(fieldList, DiscordWebhookEmbedField(meta))
}
+ status := ""
+ if context.Level == WebhookSuccess {
+ status = ":white_check_mark: "
+ } else if context.Level == WebhookWarn {
+ status = ":warning: "
+ } else if context.Level == WebhookError {
+ status = ":exclamation: "
+ }
embedList = append(embedList, DiscordWebhookEmbed{
- Title: context.Title,
+ Title: fmt.Sprintf("%s%s", status, context.Title),
Type: "rich",
Description: context.Description,
URL: context.Link,
diff --git a/plugin/webhook/slack.go b/plugin/webhook/slack.go
index a6b365a51b..19a8f240a0 100644
--- a/plugin/webhook/slack.go
+++ b/plugin/webhook/slack.go
@@ -45,11 +45,20 @@ type SlackReceiver struct {
func (receiver *SlackReceiver) post(context WebhookContext) error {
blockList := []SlackWebhookBlock{}
+
+ status := ""
+ if context.Level == WebhookSuccess {
+ status = ":white_check_mark: "
+ } else if context.Level == WebhookWarn {
+ status = ":warning: "
+ } else if context.Level == WebhookError {
+ status = ":exclamation: "
+ }
blockList = append(blockList, SlackWebhookBlock{
Type: "section",
Text: &SlackWebhookBlockMarkdown{
Type: "mrkdwn",
- Text: fmt.Sprintf("*%s*", context.Title),
+ Text: fmt.Sprintf("*%s%s*", status, context.Title),
},
})
diff --git a/plugin/webhook/webhook.go b/plugin/webhook/webhook.go
index 272672af04..8da809a0ab 100644
--- a/plugin/webhook/webhook.go
+++ b/plugin/webhook/webhook.go
@@ -19,8 +19,18 @@ type WebhookMeta struct {
Value string
}
+type WebhookLevel string
+
+const (
+ WebhookInfo WebhookLevel = "INFO"
+ WebhookSuccess WebhookLevel = "SUCCESS"
+ WebhookWarn WebhookLevel = "WARN"
+ WebhookError WebhookLevel = "ERROR"
+)
+
type WebhookContext struct {
URL string
+ Level WebhookLevel
Title string
Description string
Link string
diff --git a/plugin/webhook/wecom.go b/plugin/webhook/wecom.go
index 01c1d26261..ded5c8ce6f 100644
--- a/plugin/webhook/wecom.go
+++ b/plugin/webhook/wecom.go
@@ -39,9 +39,17 @@ func (receiver *WeComReceiver) post(context WebhookContext) error {
metaStrList = append(metaStrList, fmt.Sprintf("By: <font color=\"comment\">%s (%s)</font>", context.CreatorName, context.CreatorEmail))
metaStrList = append(metaStrList, fmt.Sprintf("At: <font color=\"comment\">%s</font>", time.Unix(context.CreatedTs, 0).Format(timeFormat)))
- content := fmt.Sprintf("# %s\n\n%s\n[View in Bytebase](%s)", context.Title, strings.Join(metaStrList, "\n"), context.Link)
+ status := ""
+ if context.Level == WebhookSuccess {
+ status = "<font color=\"green\">Success</font> "
+ } else if context.Level == WebhookWarn {
+ status = "<font color=\"yellow\">Warn</font> "
+ } else if context.Level == 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)
if context.Description != "" {
- content = fmt.Sprintf("# %s\n> %s\n\n%s\n[View in Bytebase](%s)", context.Title, context.Description, strings.Join(metaStrList, "\n"), context.Link)
+ content = fmt.Sprintf("# %s%s\n> %s\n\n%s\n[View in Bytebase](%s)", status, context.Title, context.Description, strings.Join(metaStrList, "\n"), context.Link)
}
post := WeComWebhook{
diff --git a/server/activity_manager.go b/server/activity_manager.go
index 77905f1889..819fcf04a6 100644
--- a/server/activity_manager.go
+++ b/server/activity_manager.go
@@ -92,6 +92,7 @@ func (m *ActivityManager) CreateActivity(ctx context.Context, create *api.Activi
// Call exteranl webhook endpoint in Go routine to avoid blocking web serveing thread.
go func() {
for _, hook := range hookList {
+ level := webhook.WebhookInfo
title := ""
link := fmt.Sprintf("%s:%d/issue/%s", m.s.frontendHost, m.s.frontendPort, api.IssueSlug(meta.issue))
metaList := []webhook.WebhookMeta{}
@@ -103,6 +104,7 @@ func (m *ActivityManager) CreateActivity(ctx context.Context, create *api.Activi
case "OPEN":
title = fmt.Sprintf("Issue reopened - %s", meta.issue.Name)
case "DONE":
+ level = webhook.WebhookSuccess
title = fmt.Sprintf("Issue resolved - %s", meta.issue.Name)
case "CANCELED":
title = fmt.Sprintf("Issue canceled - %s", meta.issue.Name)
@@ -213,8 +215,10 @@ func (m *ActivityManager) CreateActivity(ctx context.Context, create *api.Activi
case api.TaskRunning:
title = fmt.Sprintf("Task started - %s", task.Name)
case api.TaskDone:
+ level = webhook.WebhookSuccess
title = fmt.Sprintf("Task completed - %s", task.Name)
case api.TaskFailed:
+ level = webhook.WebhookError
title = fmt.Sprintf("Task failed - %s", task.Name)
}
}
@@ -232,6 +236,7 @@ func (m *ActivityManager) CreateActivity(ctx context.Context, create *api.Activi
hook.Type,
webhook.WebhookContext{
URL: hook.URL,
+ Level: level,
Title: title,
Description: create.Comment,
Link: link,
diff --git a/server/project_webhook.go b/server/project_webhook.go
index 2922600950..4db0004d95 100644
--- a/server/project_webhook.go
+++ b/server/project_webhook.go
@@ -215,6 +215,7 @@ func (s *Server) registerProjectWebhookRoutes(g *echo.Group) {
hook.Type,
webhook.WebhookContext{
URL: hook.URL,
+ Level: webhook.WebhookInfo,
Title: fmt.Sprintf("Test webhook '%s'", hook.Name),
Description: "This is a test",
Link: fmt.Sprintf("%s:%d/project/%s/webhook/%s", s.frontendHost, s.frontendPort, api.ProjectSlug(project), api.ProjectWebhookSlug(hook)),