-
Notifications
You must be signed in to change notification settings - Fork 2
/
statuschecker.go
123 lines (96 loc) · 2.69 KB
/
statuschecker.go
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package githubcheck
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
"github.com/PaloAltoNetworks/cov/internal/statuscheck"
)
type githubStatusCheck struct {
State string `json:"state"`
TargetURL string `json:"target_url"`
Description string `json:"description"`
Context string `json:"context"`
hostURL string
}
// New returns a new statuc checker for GitHub.
func New(hostURL string, targetURL string) statuscheck.StatusChecker {
return &githubStatusCheck{
TargetURL: targetURL,
hostURL: hostURL,
}
}
// Send sends the status check to github.
func (s *githubStatusCheck) Send(reportPath string, target string, token string) error {
data, err := os.ReadFile(reportPath)
if err != nil {
return fmt.Errorf("unable to read report file: %w", err)
}
if err := json.Unmarshal(data, &s); err != nil {
return fmt.Errorf("unable to unmarshal data file: %w", err)
}
return s.send(target, token)
}
// Write writes the reports file.
func (s *githubStatusCheck) Write(path string, coverage float64, threshold float64) error {
s.Context = "cov"
s.State = func() string {
if coverage >= threshold {
return "success"
}
return "failure"
}()
s.Description = func() string {
info := fmt.Sprintf("%.2f%% / %.2f%%", coverage, threshold)
if coverage >= threshold {
return fmt.Sprintf("success %s", info)
}
return fmt.Sprintf("failure %s", info)
}()
return s.write(path)
}
// SendNoop sends the noop check.
func (s *githubStatusCheck) WriteNoop(path string) error {
s.Context = "cov"
s.State = "success"
s.Description = "no change in any go files"
return s.write(path)
}
func (s *githubStatusCheck) write(path string) error {
data, err := json.MarshalIndent(s, "", " ")
if err != nil {
return fmt.Errorf("unable encode report status: %w", err)
}
return os.WriteFile(path, data, 0600)
}
func (s *githubStatusCheck) send(target string, token string) error {
parts := strings.SplitN(target, "@", 2)
repo := parts[0]
sha := parts[1]
if token == "" {
token = os.Getenv("GITHUB_TOKEN")
}
buf := bytes.NewBuffer(nil)
if err := json.NewEncoder(buf).Encode(s); err != nil {
return fmt.Errorf("unable to encode github status check: %w", err)
}
req, err := http.NewRequest(
http.MethodPost,
fmt.Sprintf("%s/repos/%s/statuses/%s", s.hostURL, repo, sha),
buf,
)
if err != nil {
return fmt.Errorf("unable to build request: %w", err)
}
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
resp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("unable to send request: %w", err)
}
if resp.StatusCode != http.StatusCreated {
return fmt.Errorf("github rejected the request: %s", resp.Status)
}
return nil
}