| 213 | } |
| 214 | |
| 215 | func postMessage(context webhook.Context) error { |
| 216 | post := BuildMessage(context) |
| 217 | body, err := json.Marshal(post) |
| 218 | if err != nil { |
| 219 | return errors.Wrapf(err, "failed to marshal webhook POST request to %s", context.URL) |
| 220 | } |
| 221 | req, err := http.NewRequest("POST", |
| 222 | context.URL, bytes.NewBuffer(body)) |
| 223 | if err != nil { |
| 224 | return errors.Wrapf(err, "failed to construct webhook POST request to %s", context.URL) |
| 225 | } |
| 226 | |
| 227 | req.Header.Set("Content-Type", "application/json") |
| 228 | client := &http.Client{ |
| 229 | Timeout: webhook.Timeout, |
| 230 | } |
| 231 | resp, err := client.Do(req) |
| 232 | if err != nil { |
| 233 | return errors.Wrapf(err, "failed to POST webhook to %s", context.URL) |
| 234 | } |
| 235 | |
| 236 | b, err := io.ReadAll(resp.Body) |
| 237 | if err != nil { |
| 238 | return errors.Wrapf(err, "failed to read POST webhook response from %s", context.URL) |
| 239 | } |
| 240 | defer resp.Body.Close() |
| 241 | |
| 242 | if resp.StatusCode != http.StatusOK { |
| 243 | return errors.Errorf("failed to POST webhook to %s, status code: %d, response body: %s", context.URL, resp.StatusCode, b) |
| 244 | } |
| 245 | |
| 246 | if string(b) != "ok" { |
| 247 | return errors.Errorf("%.100s", string(b)) |
| 248 | } |
| 249 | |
| 250 | return nil |
| 251 | } |
| 252 | |
| 253 | func postDirectMessage(webhookCtx webhook.Context) bool { |
| 254 | ctx := context.Background() |