MCPcopy Create free account
hub / github.com/bytebase/bytebase / postMessage

Function postMessage

backend/plugin/webhook/feishu/feishu.go:179–223  ·  view source on GitHub ↗
(context webhook.Context)

Source from the content-addressed store, hash-verified

177}
178
179func postMessage(context webhook.Context) error {
180 post := Webhook{
181 MessageType: "interactive",
182 Card: getMessageCard(context),
183 }
184 body, err := json.Marshal(post)
185 if err != nil {
186 return errors.Wrapf(err, "failed to marshal webhook POST request to %s", context.URL)
187 }
188 req, err := http.NewRequest("POST",
189 context.URL, bytes.NewBuffer(body))
190 if err != nil {
191 return errors.Wrapf(err, "failed to construct webhook POST request to %s", context.URL)
192 }
193
194 req.Header.Set("Content-Type", "application/json")
195 client := &http.Client{
196 Timeout: webhook.Timeout,
197 }
198 resp, err := client.Do(req)
199 if err != nil {
200 return errors.Wrapf(err, "failed to POST webhook to %s", context.URL)
201 }
202
203 b, err := io.ReadAll(resp.Body)
204 if err != nil {
205 return errors.Wrapf(err, "failed to read POST webhook response from %s", context.URL)
206 }
207 defer resp.Body.Close()
208
209 if resp.StatusCode != http.StatusOK {
210 return errors.Errorf("failed to POST webhook %s, status code: %d, response body: %s", context.URL, resp.StatusCode, b)
211 }
212
213 webhookResponse := &WebhookResponse{}
214 if err := json.Unmarshal(b, webhookResponse); err != nil {
215 return errors.Wrapf(err, "malformed webhook response from %s", context.URL)
216 }
217
218 if webhookResponse.Code != 0 {
219 return errors.Errorf("%s", webhookResponse.Message)
220 }
221
222 return nil
223}
224
225func getMessageCard(context webhook.Context) *WebhookCard {
226 var markdownBuf strings.Builder

Callers 1

PostMethod · 0.70

Calls 6

DoMethod · 0.80
ErrorfMethod · 0.80
UnmarshalMethod · 0.80
getMessageCardFunction · 0.70
CloseMethod · 0.65
SetMethod · 0.45

Tested by

no test coverage detected