Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat: implement feishu bot api callers (#3514)
p0ny35c19885a3ff
diff --git a/plugin/app/feishu/feishu.go b/plugin/app/feishu/feishu.goindex 0b2b4668d7..fb92eefa59 100644--- a/plugin/app/feishu/feishu.go+++ b/plugin/app/feishu/feishu.go@@ -133,6 +133,15 @@ type CancelExternalApprovalResponse struct {Msg string `json:"msg"`}+// GetBotIDResponse is the response of GetBotID.+type GetBotIDResponse struct {+ Code int `json:"code"`+ Msg string `json:"msg"`+ Bot struct {+ OpenID string `json:"open_id"`+ } `json:"bot"`+}+// CreateApprovalInstanceRequest is the request of CreateApprovalInstance.type CreateApprovalInstanceRequest struct {ApprovalCode string `json:"approval_code"`@@ -552,6 +561,29 @@ func (p *Provider) GetIDByEmail(ctx context.Context, tokenCtx TokenCtx, emails [return userID, nil}+// GetBotID gets the id of the bot.+// https://open.feishu.cn/document/ukTMukTMukTM/uAjMxEjLwITMx4CMyETM+func (p *Provider) GetBotID(ctx context.Context, tokenCtx TokenCtx) (string, error) {+ url := fmt.Sprintf("%s/bot/v3/info", p.APIPath)+ code, _, b, err := p.do(ctx, p.client, http.MethodGet, url, nil, p.tokenRefresher(tokenCtx))+ if err != nil {+ return "", errors.Wrapf(err, "POST %s", url)+ }+ if code != http.StatusOK {+ return "", errors.Errorf("non-200 POST status code %d with body %q", code, b)+ }++ var response GetBotIDResponse+ if err := json.Unmarshal([]byte(b), &response); err != nil {+ return "", err+ }+ if response.Code != 0 {+ return "", errors.Errorf("failed to create external approval, code %d, msg %s", response.Code, response.Msg)+ }++ return response.Bot.OpenID, nil+}+func formatForm(content Content) (string, error) {type form struct {ID string `json:"id"`diff --git a/plugin/app/feishu/feishu_test.go b/plugin/app/feishu/feishu_test.goindex dfb18cba7a..0a8072e737 100644--- a/plugin/app/feishu/feishu_test.go+++ b/plugin/app/feishu/feishu_test.go@@ -227,3 +227,36 @@ func TestProvider_GetIDByEmail(t *testing.T) {a.Equal("ou_919112245678741d29069abcdef096af", user["[email protected]"])})}++func TestProvider_GetBotID(t *testing.T) {+ a := require.New(t)+ p := NewProvider(APIPath)+ p.client = &http.Client{+ Transport: &common.MockRoundTripper{+ MockRoundTrip: func(r *http.Request) (*http.Response, error) {+ return &http.Response{+ StatusCode: http.StatusOK,+ Body: io.NopCloser(strings.NewReader(`+{+ "code":0,+ "msg":"ok",+ "bot":{+ "activate_status":2,+ "app_name":"name",+ "avatar_url":"https://s1-imfile.feishucdn.com/static-resource/v1/da5xxxx14b16113",+ "ip_white_list":[++ ],+ "open_id":"ou_e6e14f667cfe239d7b129b521dce0569"+ }+}`)),+ }, nil+ },+ },+ }+ ctx := context.Background()+ botID, err := p.GetBotID(ctx, TokenCtx{})+ a.NoError(err)+ want := "ou_e6e14f667cfe239d7b129b521dce0569"+ a.Equal(want, botID)+}diff --git a/server/setting.go b/server/setting.goindex 518bf8626c..4a496b1e9e 100644--- a/server/setting.go+++ b/server/setting.go@@ -78,6 +78,16 @@ func (s *Server) registerSettingRoutes(g *echo.Group) {p := s.ApplicationRunner.p// clear token cache so that we won't use the previous token.p.ClearTokenCache()++ // check bot info+ if _, err := p.GetBotID(ctx, feishu.TokenCtx{+ AppID: value.AppID,+ AppSecret: value.AppSecret,+ }); err != nil {+ return echo.NewHTTPError(http.StatusBadRequest, "Failed to get bot id. Hint: check if bot is enabled.").SetInternal(err)+ }++ // create approval definitionapprovalDefinitionID, err := p.CreateApprovalDefinition(ctx, feishu.TokenCtx{AppID: value.AppID,AppSecret: value.AppSecret,@@ -85,6 +95,7 @@ func (s *Server) registerSettingRoutes(g *echo.Group) {if err != nil {return echo.NewHTTPError(http.StatusBadRequest, "Failed to create approval definition").SetInternal(err)}+value.ExternalApproval.ApprovalDefinitionID = approvalDefinitionIDb, err := json.Marshal(value)if err != nil {diff --git a/tests/fake/feishu.go b/tests/fake/feishu.goindex a114ae61f2..bc5b03536e 100644--- a/tests/fake/feishu.go+++ b/tests/fake/feishu.go@@ -18,13 +18,16 @@ import (// Feishu is a fake feishu API server implementation for testing.type Feishu struct {- port int- echo *echo.Echo+ port int+ echo *echo.Echo+ botID string++ mutex sync.Mutex+ // mutex protects everything below.approvalDefinition map[string]boolapprovalInstance map[string]*approval- // email to user id mapping+ // email to user id mapping.users map[string]string- mutex sync.Mutex}// FeishuProviderCreator is the function to create a fake feishu provider.@@ -52,6 +55,7 @@ func NewFeishu(port int) *Feishu {approvalInstance: map[string]*approval{},users: map[string]string{},mutex: sync.Mutex{},+ botID: uuid.NewString(),}// Routes@@ -61,6 +65,7 @@ func NewFeishu(port int) *Feishu {g.GET("/approval/v4/instances/:id", f.getApprovalInstanceStatus)g.POST("/approval/v4/instances/cancel", f.cancelApprovalInstance)g.POST("/contact/v3/users/batch_get_id", f.getIDByEmail)+ g.GET("/bot/v3/info", f.getBotID)return f}@@ -256,3 +261,15 @@ func (f *Feishu) getIDByEmail(c echo.Context) error {return c.JSON(http.StatusOK, resp)}++func (f *Feishu) getBotID(c echo.Context) error {+ return c.JSON(http.StatusOK, &feishu.GetBotIDResponse{+ Code: 0,+ Msg: "ok",+ Bot: struct {+ OpenID string `json:"open_id"`+ }{+ OpenID: f.botID,+ },+ })+}