Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
Improve webhook plugin interface to enclose info in a single context paramater
天舟757d06245333
diff --git a/plugin/webhook/dingtalk.go b/plugin/webhook/dingtalk.goindex b1b5e3a4f0..93eff58fa8 100644--- a/plugin/webhook/dingtalk.go+++ b/plugin/webhook/dingtalk.go@@ -6,6 +6,7 @@ import ("fmt""net/http""strings"+ "time")type DingTalkWebhookMarkdown struct {@@ -25,31 +26,34 @@ func init() {type DingTalkReceiver struct {}-func (receiver *DingTalkReceiver) post(url string, title string, description string, metaList []WebHookMeta, link string) error {+func (receiver *DingTalkReceiver) post(context WebhookContext) error {metaStrList := []string{}- for _, meta := range metaList {+ for _, meta := range context.MetaList {metaStrList = append(metaStrList, fmt.Sprintf("##### **%s:** %s", meta.Name, meta.Value))}- text := fmt.Sprintf("# %s\n%s\n##### [View in Bytebase](%s)", title, strings.Join(metaStrList, "\n"), link)- if description != "" {- text = fmt.Sprintf("# %s\n> %s\n%s\n##### [View in Bytebase](%s)", title, description, strings.Join(metaStrList, "\n"), link)+ metaStrList = append(metaStrList, fmt.Sprintf("##### **By:** %s (%s)", context.CreatorName, context.CreatorEmail))+ metaStrList = append(metaStrList, fmt.Sprintf("##### **At:** %s", time.Unix(context.CreatedTs, 0).Format(timeFormat)))++ text := fmt.Sprintf("# %s\n%s\n##### [View in Bytebase](%s)", context.Title, strings.Join(metaStrList, "\n"), context.Link)+ if context.Description != "" {+ text = fmt.Sprintf("# %s\n> %s\n%s\n##### [View in Bytebase](%s)", context.Title, context.Description, strings.Join(metaStrList, "\n"), context.Link)}post := DingTalkWebhook{MessageType: "markdown",Markdown: DingTalkWebhookMarkdown{- Title: title,+ Title: context.Title,Text: text,},}body, err := json.Marshal(post)if err != nil {- return fmt.Errorf("failed to marshal webhook POST request: %v", url)+ return fmt.Errorf("failed to marshal webhook POST request: %v", context.URL)}req, err := http.NewRequest("POST",- url, bytes.NewBuffer(body))+ context.URL, bytes.NewBuffer(body))if err != nil {- return fmt.Errorf("failed to construct webhook POST request %v (%w)", url, err)+ return fmt.Errorf("failed to construct webhook POST request %v (%w)", context.URL, err)}req.Header.Set("Content-Type", "application/json")@@ -58,7 +62,7 @@ func (receiver *DingTalkReceiver) post(url string, title string, description str}_, err = client.Do(req)if err != nil {- return fmt.Errorf("failed to POST webhook %v (%w)", url, err)+ return fmt.Errorf("failed to POST webhook %v (%w)", context.URL, err)}return nildiff --git a/plugin/webhook/discord.go b/plugin/webhook/discord.goindex 15a9c189a8..5e185b2f75 100644--- a/plugin/webhook/discord.go+++ b/plugin/webhook/discord.go@@ -5,6 +5,7 @@ import ("encoding/json""fmt""net/http"+ "time")type DiscordWebhookEmbedField struct {@@ -12,11 +13,17 @@ type DiscordWebhookEmbedField struct {Value string `json:"value"`}+type DiscordWebhookEmbedAuthor struct {+ Name string `json:"name"`+}+type DiscordWebhookEmbed struct {Title string `json:"title"`Type string `json:"type"`Description string `json:"description,omitempty"`URL string `json:"url,omitempty"`+ Timestamp string `json:"timestamp"`+ Author DiscordWebhookEmbedAuthor `json:"author"`FieldList []DiscordWebhookEmbedField `json:"fields,omitempty"`}@@ -31,20 +38,24 @@ func init() {type DiscordReceiver struct {}-func (receiver *DiscordReceiver) post(url string, title string, description string, metaList []WebHookMeta, link string) error {+func (receiver *DiscordReceiver) post(context WebhookContext) error {embedList := []DiscordWebhookEmbed{}fieldList := []DiscordWebhookEmbedField{}- for _, meta := range metaList {+ for _, meta := range context.MetaList {fieldList = append(fieldList, DiscordWebhookEmbedField(meta))}embedList = append(embedList, DiscordWebhookEmbed{- Title: title,+ Title: context.Title,Type: "rich",- Description: description,- URL: link,- FieldList: fieldList,+ Description: context.Description,+ URL: context.Link,+ Timestamp: time.Unix(context.CreatedTs, 0).Format(timeFormat),+ Author: DiscordWebhookEmbedAuthor{+ Name: fmt.Sprintf("%s (%s)", context.CreatorName, context.CreatorEmail),+ },+ FieldList: fieldList,})post := DiscordWebhook{@@ -52,12 +63,12 @@ func (receiver *DiscordReceiver) post(url string, title string, description stri}body, err := json.Marshal(post)if err != nil {- return fmt.Errorf("failed to marshal webhook POST request: %v", url)+ return fmt.Errorf("failed to marshal webhook POST request: %v", context.URL)}req, err := http.NewRequest("POST",- url, bytes.NewBuffer(body))+ context.URL, bytes.NewBuffer(body))if err != nil {- return fmt.Errorf("failed to construct webhook POST request %v (%w)", url, err)+ return fmt.Errorf("failed to construct webhook POST request %v (%w)", context.URL, err)}req.Header.Set("Content-Type", "application/json")@@ -66,7 +77,7 @@ func (receiver *DiscordReceiver) post(url string, title string, description stri}_, err = client.Do(req)if err != nil {- return fmt.Errorf("failed to POST webhook %+v (%w)", url, err)+ return fmt.Errorf("failed to POST webhook %+v (%w)", context.URL, err)}return nildiff --git a/plugin/webhook/feishu.go b/plugin/webhook/feishu.goindex 4226142dd5..e9d29ab350 100644--- a/plugin/webhook/feishu.go+++ b/plugin/webhook/feishu.go@@ -5,6 +5,7 @@ import ("encoding/json""fmt""net/http"+ "time")type FeishuWebhookPostSection struct {@@ -42,13 +43,13 @@ func init() {type FeishuReceiver struct {}-func (receiver *FeishuReceiver) post(url string, title string, description string, metaList []WebHookMeta, link string) error {+func (receiver *FeishuReceiver) post(context WebhookContext) error {contentList := [][]FeishuWebhookPostSection{}- if description != "" {+ if context.Description != "" {sectionList := []FeishuWebhookPostSection{}sectionList = append(sectionList, FeishuWebhookPostSection{Tag: "text",- Text: description,+ Text: context.Description,})contentList = append(contentList, sectionList)@@ -60,7 +61,7 @@ func (receiver *FeishuReceiver) post(url string, title string, description strincontentList = append(contentList, sectionList)}- for _, meta := range metaList {+ for _, meta := range context.MetaList {sectionList := []FeishuWebhookPostSection{}sectionList = append(sectionList, FeishuWebhookPostSection{Tag: "text",@@ -69,20 +70,40 @@ func (receiver *FeishuReceiver) post(url string, title string, description strincontentList = append(contentList, sectionList)}- sectionList := []FeishuWebhookPostSection{}- sectionList = append(sectionList, FeishuWebhookPostSection{- Tag: "a",- Text: "View in Bytebase",- Href: link,- })- contentList = append(contentList, sectionList)+ {+ sectionList := []FeishuWebhookPostSection{}+ sectionList = append(sectionList, FeishuWebhookPostSection{+ Tag: "text",+ Text: fmt.Sprintf("By: %s (%s)", context.CreatorName, context.CreatorEmail),+ })+ contentList = append(contentList, sectionList)+ }++ {+ sectionList := []FeishuWebhookPostSection{}+ sectionList = append(sectionList, FeishuWebhookPostSection{+ Tag: "text",+ Text: fmt.Sprintf("At: %s", time.Unix(context.CreatedTs, 0).Format(timeFormat)),+ })+ contentList = append(contentList, sectionList)+ }++ {+ sectionList := []FeishuWebhookPostSection{}+ sectionList = append(sectionList, FeishuWebhookPostSection{+ Tag: "a",+ Text: "View in Bytebase",+ Href: context.Link,+ })+ contentList = append(contentList, sectionList)+ }post := FeishuWebhook{MessageType: "post",Content: FeishuWebhookContent{Post: FeishuWebhookPostLanguage{English: FeishuWebhookPost{- Title: title,+ Title: context.Title,ContentList: contentList,},},@@ -90,12 +111,12 @@ func (receiver *FeishuReceiver) post(url string, title string, description strin}body, err := json.Marshal(post)if err != nil {- return fmt.Errorf("failed to marshal webhook POST request: %v", url)+ return fmt.Errorf("failed to marshal webhook POST request: %v", context.URL)}req, err := http.NewRequest("POST",- url, bytes.NewBuffer(body))+ context.URL, bytes.NewBuffer(body))if err != nil {- return fmt.Errorf("failed to construct webhook POST request %v (%w)", url, err)+ return fmt.Errorf("failed to construct webhook POST request %v (%w)", context.URL, err)}req.Header.Set("Content-Type", "application/json")@@ -104,7 +125,7 @@ func (receiver *FeishuReceiver) post(url string, title string, description strin}_, err = client.Do(req)if err != nil {- return fmt.Errorf("failed to POST webhook %+v (%w)", url, err)+ return fmt.Errorf("failed to POST webhook %+v (%w)", context.URL, err)}return nildiff --git a/plugin/webhook/slack.go b/plugin/webhook/slack.goindex 7f7d0bd684..41fdd4dabf 100644--- a/plugin/webhook/slack.go+++ b/plugin/webhook/slack.go@@ -5,16 +5,29 @@ import ("encoding/json""fmt""net/http"+ "time")-type SlackWebhookBlockText struct {+type SlackWebhookBlockMarkdown struct {Type string `json:"type"`Text string `json:"text"`}+type SlackWebhookElementButton struct {+ Type string `json:"type"`+ Text string `json:"text,omitempty"`+}++type SlackWebhookElement struct {+ Type string `json:"type"`+ Button SlackWebhookElementButton `json:"text,omitempty"`+ URL string `json:"url,omitempty"`+}+type SlackWebhookBlock struct {- Type string `json:"type"`- Text SlackWebhookBlockText `json:"text"`+ Type string `json:"type"`+ Text *SlackWebhookBlockMarkdown `json:"text,omitempty"`+ ElementList []SlackWebhookElement `json:"elements,omitempty"`}type SlackWebhook struct {@@ -29,30 +42,30 @@ func init() {type SlackReceiver struct {}-func (receiver *SlackReceiver) post(url string, title string, description string, metaList []WebHookMeta, link string) error {+func (receiver *SlackReceiver) post(context WebhookContext) error {blockList := []SlackWebhookBlock{}blockList = append(blockList, SlackWebhookBlock{Type: "section",- Text: SlackWebhookBlockText{+ Text: &SlackWebhookBlockMarkdown{Type: "mrkdwn",- Text: fmt.Sprintf("*%s*", title),+ Text: fmt.Sprintf("*%s*", context.Title),},})- if description != "" {+ if context.Description != "" {blockList = append(blockList, SlackWebhookBlock{Type: "section",- Text: SlackWebhookBlockText{+ Text: &SlackWebhookBlockMarkdown{Type: "mrkdwn",- Text: fmt.Sprintf("```%s```", description),+ Text: fmt.Sprintf("```%s```", context.Description),},})}- for _, meta := range metaList {+ for _, meta := range context.MetaList {blockList = append(blockList, SlackWebhookBlock{Type: "section",- Text: SlackWebhookBlockText{+ Text: &SlackWebhookBlockMarkdown{Type: "mrkdwn",Text: fmt.Sprintf("*%s:* %s", meta.Name, meta.Value),},@@ -61,24 +74,46 @@ func (receiver *SlackReceiver) post(url string, title string, description stringblockList = append(blockList, SlackWebhookBlock{Type: "section",- Text: SlackWebhookBlockText{+ Text: &SlackWebhookBlockMarkdown{+ Type: "mrkdwn",+ Text: fmt.Sprintf("By: %s (%s)", context.CreatorName, context.CreatorEmail),+ },+ })++ blockList = append(blockList, SlackWebhookBlock{+ Type: "section",+ Text: &SlackWebhookBlockMarkdown{Type: "mrkdwn",- Text: fmt.Sprintf("<%s|View in Bytebase>", link),+ Text: fmt.Sprintf("At: %s", time.Unix(context.CreatedTs, 0).Format(timeFormat)),+ },+ })++ blockList = append(blockList, SlackWebhookBlock{+ Type: "actions",+ ElementList: []SlackWebhookElement{+ {+ Type: "button",+ Button: SlackWebhookElementButton{+ Type: "plain_text",+ Text: "View in Bytebase",+ },+ URL: context.Link,+ },},})post := SlackWebhook{- Text: title,+ Text: context.Title,BlockList: blockList,}body, err := json.Marshal(post)if err != nil {- return fmt.Errorf("failed to marshal webhook POST request: %v", url)+ return fmt.Errorf("failed to marshal webhook POST request: %v", context.URL)}req, err := http.NewRequest("POST",- url, bytes.NewBuffer(body))+ context.URL, bytes.NewBuffer(body))if err != nil {- return fmt.Errorf("failed to construct webhook POST request %v (%w)", url, err)+ return fmt.Errorf("failed to construct webhook POST request %v (%w)", context.URL, err)}req.Header.Set("Content-Type", "application/json")@@ -87,7 +122,7 @@ func (receiver *SlackReceiver) post(url string, title string, description string}_, err = client.Do(req)if err != nil {- return fmt.Errorf("failed to POST webhook %+v (%w)", url, err)+ return fmt.Errorf("failed to POST webhook %+v (%w)", context.URL, err)}return nildiff --git a/plugin/webhook/teams.go b/plugin/webhook/teams.goindex bb6961171d..609e6ebc13 100644--- a/plugin/webhook/teams.go+++ b/plugin/webhook/teams.go@@ -5,6 +5,7 @@ import ("encoding/json""fmt""net/http"+ "time")var THEME_COLOR = "4f46e5"@@ -26,8 +27,10 @@ type TeamsWebhookSectionFact struct {}type TeamsWebhookSection struct {- FactList []TeamsWebhookSectionFact `json:"facts"`- Text string `json:"text"`+ ActivityTitle string `json:"activityTitle"`+ ActivitySubtitle string `json:"activitySubtitle"`+ FactList []TeamsWebhookSectionFact `json:"facts"`+ Text string `json:"text"`}type TeamsWebhook struct {@@ -47,22 +50,24 @@ func init() {type TeamsReceiver struct {}-func (receiver *TeamsReceiver) post(url string, title string, description string, metaList []WebHookMeta, link string) error {+func (receiver *TeamsReceiver) post(context WebhookContext) error {factList := []TeamsWebhookSectionFact{}- for _, meta := range metaList {+ for _, meta := range context.MetaList {factList = append(factList, TeamsWebhookSectionFact(meta))}post := TeamsWebhook{Type: "MessageCard",Context: "https://schema.org/extensions",- Summary: title,+ Summary: context.Title,ThemeColor: THEME_COLOR,- Title: title,+ Title: context.Title,SectionList: []TeamsWebhookSection{{- FactList: factList,- Text: description,+ ActivityTitle: fmt.Sprintf("%s (%s)", context.CreatorName, context.CreatorEmail),+ ActivitySubtitle: time.Unix(context.CreatedTs, 0).Format(timeFormat),+ FactList: factList,+ Text: context.Description,},},ActionList: []TeamsWebhookAction{@@ -72,7 +77,7 @@ func (receiver *TeamsReceiver) post(url string, title string, description stringTargetList: []TeamsWebhookActionTarget{{OS: "default",- URI: link,+ URI: context.Link,},},},@@ -80,12 +85,12 @@ func (receiver *TeamsReceiver) post(url string, title string, description string}body, err := json.Marshal(post)if err != nil {- return fmt.Errorf("failed to marshal webhook POST request: %v", url)+ return fmt.Errorf("failed to marshal webhook POST request: %v", context.URL)}req, err := http.NewRequest("POST",- url, bytes.NewBuffer(body))+ context.URL, bytes.NewBuffer(body))if err != nil {- return fmt.Errorf("failed to construct webhook POST request %v (%w)", url, err)+ return fmt.Errorf("failed to construct webhook POST request %v (%w)", context.URL, err)}req.Header.Set("Content-Type", "application/json")@@ -94,7 +99,7 @@ func (receiver *TeamsReceiver) post(url string, title string, description string}_, err = client.Do(req)if err != nil {- return fmt.Errorf("failed to POST webhook %+v (%w)", url, err)+ return fmt.Errorf("failed to POST webhook %+v (%w)", context.URL, err)}return nildiff --git a/plugin/webhook/webhook.go b/plugin/webhook/webhook.goindex eddba00097..5bb3b3ff2f 100644--- a/plugin/webhook/webhook.go+++ b/plugin/webhook/webhook.go@@ -10,15 +10,27 @@ var (receiverMu sync.RWMutexreceivers = make(map[string]WebhookReceiver)timeout = 1 * time.Second+ timeFormat = "2006-01-02 15:04:05")-type WebHookMeta struct {+type WebhookMeta struct {Name stringValue string}+type WebhookContext struct {+ URL string+ Title string+ Description string+ Link string+ CreatorName string+ CreatorEmail string+ CreatedTs int64+ MetaList []WebhookMeta+}+type WebhookReceiver interface {- post(url string, title string, description string, metaList []WebHookMeta, link string) error+ post(context WebhookContext) error}// Register makes a receiver available by the url host@@ -36,7 +48,7 @@ func register(host string, r WebhookReceiver) {receivers[host] = r}-func Post(webhookType string, urlstr string, title string, description string, metaList []WebHookMeta, link string) error {+func Post(webhookType string, context WebhookContext) error {receiverMu.RLock()r, ok := receivers[webhookType]receiverMu.RUnlock()@@ -44,5 +56,5 @@ func Post(webhookType string, urlstr string, title string, description string, mreturn fmt.Errorf("webhook: no applicable receiver for webhook type: %v", webhookType)}- return r.post(urlstr, title, description, metaList, link)+ return r.post(context)}diff --git a/plugin/webhook/wecom.go b/plugin/webhook/wecom.goindex 089df2f945..617b3a2a16 100644--- a/plugin/webhook/wecom.go+++ b/plugin/webhook/wecom.go@@ -6,6 +6,7 @@ import ("fmt""net/http""strings"+ "time")type WeComWebhookMarkdown struct {@@ -24,15 +25,17 @@ func init() {type WeComReceiver struct {}-func (receiver *WeComReceiver) post(url string, title string, description string, metaList []WebHookMeta, link string) error {+func (receiver *WeComReceiver) post(context WebhookContext) error {metaStrList := []string{}- for _, meta := range metaList {+ for _, meta := range context.MetaList {metaStrList = append(metaStrList, fmt.Sprintf("%s: <font color=\"comment\">%s</font>", meta.Name, meta.Value))}+ 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)", title, strings.Join(metaStrList, "\n"), link)- if description != "" {- content = fmt.Sprintf("# %s\n> %s\n\n%s\n[View in Bytebase](%s)", title, description, strings.Join(metaStrList, "\n"), link)+ content := fmt.Sprintf("# %s\n\n%s\n[View in Bytebase](%s)", 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)}post := WeComWebhook{@@ -43,12 +46,12 @@ func (receiver *WeComReceiver) post(url string, title string, description string}body, err := json.Marshal(post)if err != nil {- return fmt.Errorf("failed to marshal webhook POST request: %v", url)+ return fmt.Errorf("failed to marshal webhook POST request: %v", context.URL)}req, err := http.NewRequest("POST",- url, bytes.NewBuffer(body))+ context.URL, bytes.NewBuffer(body))if err != nil {- return fmt.Errorf("failed to construct webhook POST request %v (%w)", url, err)+ return fmt.Errorf("failed to construct webhook POST request %v (%w)", context.URL, err)}req.Header.Set("Content-Type", "application/json")@@ -57,7 +60,7 @@ func (receiver *WeComReceiver) post(url string, title string, description string}_, err = client.Do(req)if err != nil {- return fmt.Errorf("failed to POST webhook %v (%w)", url, err)+ return fmt.Errorf("failed to POST webhook %v (%w)", context.URL, err)}return nildiff --git a/server/issue.go b/server/issue.goindex da41b64f27..56eff97461 100644--- a/server/issue.go+++ b/server/issue.go@@ -7,6 +7,7 @@ import ("net/http""strconv""strings"+ "time""github.com/bytebase/bytebase""github.com/bytebase/bytebase/api"@@ -550,20 +551,21 @@ func (s *Server) ChangeIssueStatus(ctx context.Context, issue *api.Issue, newStaerr := webhook.Post(hook.Type,- hook.URL,- title,- comment,- []webhook.WebHookMeta{- {- Name: "Project",- Value: issue.Project.Name,- },- {- Name: "By",- Value: updater.Name,+ webhook.WebhookContext{+ URL: hook.URL,+ Title: title,+ Description: comment,+ Link: fmt.Sprintf("%s:%d/issue/%s", s.frontendHost, s.frontendPort, api.IssueSlug(issue)),+ CreatorName: updater.Name,+ CreatorEmail: updater.Email,+ CreatedTs: time.Now().Unix(),+ MetaList: []webhook.WebhookMeta{+ {+ Name: "Project",+ Value: issue.Project.Name,+ },},},- fmt.Sprintf("%s:%d/issue/%s", s.frontendHost, s.frontendPort, api.IssueSlug(issue)),)if err != nil {// The external webhook endpoint might be invalid which is out of our code control, so we just emit a warning