Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(plugin/notifiers): improve lark notification error visibility #202

Merged
merged 4 commits into from
Feb 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions plugins/notifiers/lark/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ type tokenResponse struct {
Expire int `json:"expire"`
}

type messageResponse struct {
Code int `json:"code"`
Message string `json:"msg"`
Data map[string]interface{} `json:"data,omitempty"`
Error map[string]interface{} `json:"error,omitempty"`
}

type Payload struct {
AppID string `json:"app_id"`
AppSecret string `json:"app_secret"`
Expand Down Expand Up @@ -126,7 +133,7 @@ func (n *Notifier) sendMessage(workspace LarkWorkspace, email string, messageBlo
}
req.Header.Add("Authorization", "Bearer "+token)
req.Header.Add("Content-Type", "application/json")
_, err = n.sendRequest(req)
_, err = n.sendMessageRequest(req, payload)
return err
}

Expand All @@ -145,7 +152,7 @@ func (n *Notifier) findTenantAccessToken(clientId string, clientSecret string, w
return "", err
}
req.Header.Add("Content-Type", "application/json")
result, err := n.sendRequest(req)
result, err := n.sendTenantAccessTokenRequest(req)
if err != nil {
return "", fmt.Errorf("error get tenant access token for workspace: %s - %s", ws.WorkspaceName, err)
}
Expand All @@ -155,7 +162,7 @@ func (n *Notifier) findTenantAccessToken(clientId string, clientSecret string, w
return result.Token, nil
}

func (n *Notifier) sendRequest(req *http.Request) (*tokenResponse, error) {
func (n *Notifier) sendTenantAccessTokenRequest(req *http.Request) (*tokenResponse, error) {
resp, err := n.httpClient.Do(req)
if err != nil {
return nil, err
Expand All @@ -171,6 +178,27 @@ func (n *Notifier) sendRequest(req *http.Request) (*tokenResponse, error) {
return &result, nil
}

func (n *Notifier) sendMessageRequest(req *http.Request, reqPayload map[string]interface{}) (*messageResponse, error) {
resp, err := n.httpClient.Do(req)
if err != nil {
return nil, err
}
var result messageResponse
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
return nil, err
}
if result.Code != 0 { // non 0 code means the API has returned some error(s)
result.Data = reqPayload
d, err := json.Marshal(result)
if err != nil {
return nil, errors.New(result.Message)
}
return nil, errors.New(string(d))
}
return &result, nil
}

func getDefaultTemplate(messageType string, defaultTemplateFiles embed.FS) (string, error) {
content, err := defaultTemplateFiles.ReadFile(fmt.Sprintf("templates/%s.json", messageType))
if err != nil {
Expand Down
Loading