-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.go
More file actions
74 lines (61 loc) · 2.22 KB
/
github.go
File metadata and controls
74 lines (61 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
)
type repositoryDispatchPayload struct {
EventType string `json:"event_type"`
ClientPayload map[string]interface{} `json:"client_payload"`
}
func sendRepositoryDispatch(token string, msg *ScenarioProgressMessage) error {
if token == "" {
return fmt.Errorf("github token is empty, skipping repository_dispatch")
}
if msg.CommitSHA == "" || msg.Repository == "" {
return fmt.Errorf("commit_sha=%q or repository=%q missing, skipping", msg.CommitSHA, msg.Repository)
}
payload := repositoryDispatchPayload{
EventType: "oopstest-completed",
ClientPayload: map[string]interface{}{
"run_id": msg.RunID,
"commit_sha": msg.CommitSHA,
"repository": msg.Repository,
"run_url": msg.RunURL,
"overall_status": msg.OverallStatus,
"failed_count": msg.FailedCount,
"total_scenarios": msg.TotalScenarios,
"missing_tests_in_pr": msg.MissingTestsInPR,
"should_run_tests": msg.ShouldRunTests,
"pr_number": msg.PRNumber,
},
}
body, err := json.Marshal(payload)
if err != nil {
return fmt.Errorf("json.Marshal repository_dispatch payload: %w", err)
}
url := fmt.Sprintf("https://api.github.com/repos/%s/dispatches", msg.Repository)
req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(body))
if err != nil {
return fmt.Errorf("http.NewRequest: %w", err)
}
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Accept", "application/vnd.github+json")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-GitHub-Api-Version", "2022-11-28")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("http request failed: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusNoContent {
respBody, _ := io.ReadAll(resp.Body)
return fmt.Errorf("github API returned %d: %s", resp.StatusCode, string(respBody))
}
log.Printf("repository_dispatch sent: repo=%s sha=%s run_id=%s pr_number=%s overall_status=%s missing_tests_in_pr=%v should_run_tests=%v",
msg.Repository, msg.CommitSHA, msg.RunID, msg.PRNumber, msg.OverallStatus, msg.MissingTestsInPR, msg.ShouldRunTests)
return nil
}