Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: move feishu webhook to a separate pkg (#12345)
p0nya7604f5dda32
diff --git a/backend/plugin/webhook/custom.go b/backend/plugin/webhook/custom.goindex 03f5206c9d..83d533baad 100644--- a/backend/plugin/webhook/custom.go+++ b/backend/plugin/webhook/custom.go@@ -63,7 +63,7 @@ func (*CustomReceiver) Post(context Context) error {req.Header.Set("Content-Type", "application/json")client := &http.Client{- Timeout: timeout,+ Timeout: Timeout,}resp, err := client.Do(req)if err != nil {diff --git a/backend/plugin/webhook/dingtalk.go b/backend/plugin/webhook/dingtalk.goindex 70f376728b..5220eeb56c 100644--- a/backend/plugin/webhook/dingtalk.go+++ b/backend/plugin/webhook/dingtalk.go@@ -86,7 +86,7 @@ func (*DingTalkReceiver) Post(context Context) error {req.Header.Set("Content-Type", "application/json")client := &http.Client{- Timeout: timeout,+ Timeout: Timeout,}resp, err := client.Do(req)if err != nil {diff --git a/backend/plugin/webhook/discord.go b/backend/plugin/webhook/discord.goindex 211ff91bfa..6c7a789fa8 100644--- a/backend/plugin/webhook/discord.go+++ b/backend/plugin/webhook/discord.go@@ -94,7 +94,7 @@ func (*DiscordReceiver) Post(context Context) error {req.Header.Set("Content-Type", "application/json")client := &http.Client{- Timeout: timeout,+ Timeout: Timeout,}resp, err := client.Do(req)if err != nil {diff --git a/backend/plugin/webhook/feishu.go b/backend/plugin/webhook/feishu.godeleted file mode 100644index 98327b13a7..0000000000--- a/backend/plugin/webhook/feishu.go+++ /dev/null@@ -1,179 +0,0 @@-package webhook--import (- "bytes"- "encoding/json"- "fmt"- "io"- "net/http"- "strings"-- "github.com/pkg/errors"-)--// FeishuWebhookResponse is the API message for Feishu webhook response.-type FeishuWebhookResponse struct {- Code int `json:"code"`- Message string `json:"msg"`-}--// FeishuWebhookPostSection is the API message for Feishu webhook POST section.-type FeishuWebhookPostSection struct {- Tag string `json:"tag"`- Text string `json:"text"`- Href string `json:"href,omitempty"`-}--// FeishuWebhookPostLine is the API message for Feishu webhook POST line.-type FeishuWebhookPostLine struct {- SectionList []FeishuWebhookPostSection `json:""`-}--// FeishuWebhookPost is the API message for Feishu webhook POST.-type FeishuWebhookPost struct {- Title string `json:"title"`- ContentList [][]FeishuWebhookPostSection `json:"content"`-}--// FeishuWebhookPostLanguage is the API message for Feishu webhook POST language.-type FeishuWebhookPostLanguage struct {- English FeishuWebhookPost `json:"en_us"`-}--// FeishuWebhookContent is the API message for Feishu webhook content.-type FeishuWebhookContent struct {- Post FeishuWebhookPostLanguage `json:"post"`-}--// FeishuWebhookCardConfig is the API message for Feishu webhook card config.-type FeishuWebhookCardConfig struct {- WideScreenMode bool `json:"wide_screen_mode,omitempty"`- EnableForward bool `json:"enable_forward,omitempty"`-}--// FeishuWebhookMarkdownSection is the API message for Feishu webhook card i18n content markdown.-type FeishuWebhookMarkdownSection struct {- Tag string `json:"tag,omitempty"`- Content string `json:"content,omitempty"`-}--// FeishuWebhookCardI18nElements is the API message for Feishu webhook card i18n content.-type FeishuWebhookCardI18nElements struct {- English []FeishuWebhookMarkdownSection `json:"en_us"`-}--// FeishuWebhookCardHeaderTitle is the API message for Feishu webhook card header title.-type FeishuWebhookCardHeaderTitle struct {- Content string `json:"content"`- Tag string `json:"tag"`-}--// FeishuWebhookCardHeader is the API message for Feishu webhook card header.-type FeishuWebhookCardHeader struct {- Title FeishuWebhookCardHeaderTitle `json:"title"`-}--// FeishuWebhookCard is the API message for Feishu webhook card.-type FeishuWebhookCard struct {- Config FeishuWebhookCardConfig `json:"config"`- Header FeishuWebhookCardHeader `json:"header"`- I18nElements FeishuWebhookCardI18nElements `json:"i18n_elements"`-}--// FeishuWebhook is the API message for Feishu webhook.-type FeishuWebhook struct {- MessageType string `json:"msg_type"`- Content *FeishuWebhookContent `json:"content,omitempty"`- Card *FeishuWebhookCard `json:"card,omitempty"`-}--func init() {- Register("bb.plugin.webhook.feishu", &FeishuReceiver{})-}--// FeishuReceiver is the receiver for Feishu.-type FeishuReceiver struct {-}--func (*FeishuReceiver) Post(context Context) error {- var markdownBuf strings.Builder-- if context.Description != "" {- if _, err := markdownBuf.WriteString(fmt.Sprintf("%s\n", context.Description)); err != nil {- return err- }- }-- for _, meta := range context.GetMetaList() {- if _, err := markdownBuf.WriteString(fmt.Sprintf("**%s**: %s\n", meta.Name, meta.Value)); err != nil {- return err- }- }-- if _, err := markdownBuf.WriteString(fmt.Sprintf("**By**: %s (%s)\n[View in Bytebase](%s)", context.CreatorName, context.CreatorEmail, context.Link)); err != nil {- return err- }-- post := FeishuWebhook{- MessageType: "interactive",- Card: &FeishuWebhookCard{- Config: FeishuWebhookCardConfig{- WideScreenMode: true,- EnableForward: true,- },- Header: FeishuWebhookCardHeader{- Title: FeishuWebhookCardHeaderTitle{- Content: context.Title,- Tag: "plain_text",- },- },- I18nElements: FeishuWebhookCardI18nElements{- English: []FeishuWebhookMarkdownSection{- {- Tag: "markdown",- Content: markdownBuf.String(),- },- },- },- },- }- body, err := json.Marshal(post)- if err != nil {- return errors.Wrapf(err, "failed to marshal webhook POST request to %s", context.URL)- }- req, err := http.NewRequest("POST",- context.URL, bytes.NewBuffer(body))- if err != nil {- return errors.Wrapf(err, "failed to construct webhook POST request to %s", context.URL)- }-- req.Header.Set("Content-Type", "application/json")- client := &http.Client{- Timeout: timeout,- }- resp, err := client.Do(req)- if err != nil {- return errors.Wrapf(err, "failed to POST webhook to %s", context.URL)- }-- b, err := io.ReadAll(resp.Body)- if err != nil {- return errors.Wrapf(err, "failed to read POST webhook response from %s", context.URL)- }- defer resp.Body.Close()-- if resp.StatusCode != http.StatusOK {- return errors.Errorf("failed to POST webhook %s, status code: %d, response body: %s", context.URL, resp.StatusCode, b)- }-- webhookResponse := &FeishuWebhookResponse{}- if err := json.Unmarshal(b, webhookResponse); err != nil {- return errors.Wrapf(err, "malformed webhook response from %s", context.URL)- }-- if webhookResponse.Code != 0 {- return errors.Errorf("%s", webhookResponse.Message)- }-- return nil-}diff --git a/backend/plugin/webhook/feishu/feishu.go b/backend/plugin/webhook/feishu/feishu.gonew file mode 100644index 0000000000..81c50b9d35--- /dev/null+++ b/backend/plugin/webhook/feishu/feishu.go@@ -0,0 +1,181 @@+package feishu++import (+ "bytes"+ "encoding/json"+ "fmt"+ "io"+ "net/http"+ "strings"++ "github.com/pkg/errors"++ "github.com/bytebase/bytebase/backend/plugin/webhook"+)++// WebhookResponse is the API message for Feishu webhook response.+type WebhookResponse struct {+ Code int `json:"code"`+ Message string `json:"msg"`+}++// WebhookPostSection is the API message for Feishu webhook POST section.+type WebhookPostSection struct {+ Tag string `json:"tag"`+ Text string `json:"text"`+ Href string `json:"href,omitempty"`+}++// WebhookPostLine is the API message for Feishu webhook POST line.+type WebhookPostLine struct {+ SectionList []WebhookPostSection `json:""`+}++// WebhookPost is the API message for Feishu webhook POST.+type WebhookPost struct {+ Title string `json:"title"`+ ContentList [][]WebhookPostSection `json:"content"`+}++// WebhookPostLanguage is the API message for Feishu webhook POST language.+type WebhookPostLanguage struct {+ English WebhookPost `json:"en_us"`+}++// WebhookContent is the API message for Feishu webhook content.+type WebhookContent struct {+ Post WebhookPostLanguage `json:"post"`+}++// WebhookCardConfig is the API message for Feishu webhook card config.+type WebhookCardConfig struct {+ WideScreenMode bool `json:"wide_screen_mode,omitempty"`+ EnableForward bool `json:"enable_forward,omitempty"`+}++// WebhookMarkdownSection is the API message for Feishu webhook card i18n content markdown.+type WebhookMarkdownSection struct {+ Tag string `json:"tag,omitempty"`+ Content string `json:"content,omitempty"`+}++// WebhookCardI18nElements is the API message for Feishu webhook card i18n content.+type WebhookCardI18nElements struct {+ English []WebhookMarkdownSection `json:"en_us"`+}++// WebhookCardHeaderTitle is the API message for Feishu webhook card header title.+type WebhookCardHeaderTitle struct {+ Content string `json:"content"`+ Tag string `json:"tag"`+}++// WebhookCardHeader is the API message for Feishu webhook card header.+type WebhookCardHeader struct {+ Title WebhookCardHeaderTitle `json:"title"`+}++// WebhookCard is the API message for Feishu webhook card.+type WebhookCard struct {+ Config WebhookCardConfig `json:"config"`+ Header WebhookCardHeader `json:"header"`+ I18nElements WebhookCardI18nElements `json:"i18n_elements"`+}++// Webhook is the API message for Feishu webhook.+type Webhook struct {+ MessageType string `json:"msg_type"`+ Content *WebhookContent `json:"content,omitempty"`+ Card *WebhookCard `json:"card,omitempty"`+}++func init() {+ webhook.Register("bb.plugin.webhook.feishu", &feishuReceiver{})+}++// feishuReceiver is the receiver for Feishu.+type feishuReceiver struct {+}++func (*feishuReceiver) Post(context webhook.Context) error {+ var markdownBuf strings.Builder++ if context.Description != "" {+ if _, err := markdownBuf.WriteString(fmt.Sprintf("%s\n", context.Description)); err != nil {+ return err+ }+ }++ for _, meta := range context.GetMetaList() {+ if _, err := markdownBuf.WriteString(fmt.Sprintf("**%s**: %s\n", meta.Name, meta.Value)); err != nil {+ return err+ }+ }++ if _, err := markdownBuf.WriteString(fmt.Sprintf("**By**: %s (%s)\n[View in Bytebase](%s)", context.CreatorName, context.CreatorEmail, context.Link)); err != nil {+ return err+ }++ post := Webhook{+ MessageType: "interactive",+ Card: &WebhookCard{+ Config: WebhookCardConfig{+ WideScreenMode: true,+ EnableForward: true,+ },+ Header: WebhookCardHeader{+ Title: WebhookCardHeaderTitle{+ Content: context.Title,+ Tag: "plain_text",+ },+ },+ I18nElements: WebhookCardI18nElements{+ English: []WebhookMarkdownSection{+ {+ Tag: "markdown",+ Content: markdownBuf.String(),+ },+ },+ },+ },+ }+ body, err := json.Marshal(post)+ if err != nil {+ return errors.Wrapf(err, "failed to marshal webhook POST request to %s", context.URL)+ }+ req, err := http.NewRequest("POST",+ context.URL, bytes.NewBuffer(body))+ if err != nil {+ return errors.Wrapf(err, "failed to construct webhook POST request to %s", context.URL)+ }++ req.Header.Set("Content-Type", "application/json")+ client := &http.Client{+ Timeout: webhook.Timeout,+ }+ resp, err := client.Do(req)+ if err != nil {+ return errors.Wrapf(err, "failed to POST webhook to %s", context.URL)+ }++ b, err := io.ReadAll(resp.Body)+ if err != nil {+ return errors.Wrapf(err, "failed to read POST webhook response from %s", context.URL)+ }+ defer resp.Body.Close()++ if resp.StatusCode != http.StatusOK {+ return errors.Errorf("failed to POST webhook %s, status code: %d, response body: %s", context.URL, resp.StatusCode, b)+ }++ webhookResponse := &WebhookResponse{}+ if err := json.Unmarshal(b, webhookResponse); err != nil {+ return errors.Wrapf(err, "malformed webhook response from %s", context.URL)+ }++ if webhookResponse.Code != 0 {+ return errors.Errorf("%s", webhookResponse.Message)+ }++ return nil+}diff --git a/backend/plugin/webhook/slack/slack.go b/backend/plugin/webhook/slack/slack.goindex 30e7f5ff76..c75109994c 100644--- a/backend/plugin/webhook/slack/slack.go+++ b/backend/plugin/webhook/slack/slack.go@@ -9,7 +9,6 @@ import ("log/slog""net/http""strings"- "time""github.com/pkg/errors""go.uber.org/multierr"@@ -151,7 +150,7 @@ func postMessage(context webhook.Context) error {req.Header.Set("Content-Type", "application/json")client := &http.Client{- Timeout: 3 * time.Second,+ Timeout: webhook.Timeout,}resp, err := client.Do(req)if err != nil {diff --git a/backend/plugin/webhook/teams.go b/backend/plugin/webhook/teams.goindex 59363d4cd5..6713442869 100644--- a/backend/plugin/webhook/teams.go+++ b/backend/plugin/webhook/teams.go@@ -102,7 +102,7 @@ func (*TeamsReceiver) Post(context Context) error {req.Header.Set("Content-Type", "application/json")client := &http.Client{- Timeout: timeout,+ Timeout: Timeout,}resp, err := client.Do(req)if err != nil {diff --git a/backend/plugin/webhook/webhook.go b/backend/plugin/webhook/webhook.goindex e31c08f62a..a7e426f211 100644--- a/backend/plugin/webhook/webhook.go+++ b/backend/plugin/webhook/webhook.go@@ -17,7 +17,7 @@ var (receiverMu sync.RWMutexreceivers = make(map[string]Receiver)// Based on the local test, Teams sometimes cannot finish the request in 1 second, so use 3s.- timeout = 3 * time.Second+ Timeout = 3 * time.Second)// Meta is the webhook metadata.diff --git a/backend/plugin/webhook/wecom.go b/backend/plugin/webhook/wecom.goindex ac9a0babee..ef5c972fc0 100644--- a/backend/plugin/webhook/wecom.go+++ b/backend/plugin/webhook/wecom.go@@ -75,7 +75,7 @@ func (*WeComReceiver) Post(context Context) error {req.Header.Set("Content-Type", "application/json")client := &http.Client{- Timeout: timeout,+ Timeout: Timeout,}resp, err := client.Do(req)if err != nil {diff --git a/backend/server/minimal.go b/backend/server/minimal.goindex ea19c006c4..93a8d1c28f 100644--- a/backend/server/minimal.go+++ b/backend/server/minimal.go@@ -19,5 +19,6 @@ import (_ "github.com/bytebase/bytebase/backend/plugin/parser/sql/engine/pg"// IM webhooks.+ _ "github.com/bytebase/bytebase/backend/plugin/webhook/feishu"_ "github.com/bytebase/bytebase/backend/plugin/webhook/slack")diff --git a/backend/server/ultimate.go b/backend/server/ultimate.goindex 838b469d6b..90b9f2ab65 100644--- a/backend/server/ultimate.go+++ b/backend/server/ultimate.go@@ -50,5 +50,6 @@ import (_ "github.com/bytebase/bytebase/backend/plugin/parser/sql/transform/mysql"// IM webhooks.+ _ "github.com/bytebase/bytebase/backend/plugin/webhook/feishu"_ "github.com/bytebase/bytebase/backend/plugin/webhook/slack")