-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.go
More file actions
168 lines (142 loc) · 4.15 KB
/
Copy pathgithub.go
File metadata and controls
168 lines (142 loc) · 4.15 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package runner
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
)
const defaultGitHubBaseURL = "https://api.github.com"
// GitHubClient is the minimal GitHub API surface used by the runner.
type GitHubClient interface {
GetIssue(ctx context.Context, repo string, number int) (*Issue, error)
AddLabels(ctx context.Context, repo string, number int, labels []string) error
RemoveLabel(ctx context.Context, repo string, number int, label string) error
CreateComment(ctx context.Context, repo string, number int, body string) error
}
type Issue struct {
Number int `json:"number"`
Title string `json:"title"`
Body string `json:"body"`
State string `json:"state"`
HTMLURL string `json:"html_url"`
Labels []Label `json:"labels"`
}
type Label struct {
Name string `json:"name"`
}
func (i *Issue) IsOpen() bool {
return strings.EqualFold(i.State, "open")
}
func (i *Issue) HasLabel(name string) bool {
for _, l := range i.Labels {
if strings.EqualFold(l.Name, name) {
return true
}
}
return false
}
type httpGitHubClient struct {
baseURL string
token string
http *http.Client
}
func NewGitHubClient(cfg GitHubConfig) GitHubClient {
base := cfg.BaseURL
if base == "" {
base = defaultGitHubBaseURL
}
return &httpGitHubClient{
baseURL: strings.TrimRight(base, "/"),
token: cfg.Token,
http: &http.Client{Timeout: 30 * time.Second},
}
}
func (c *httpGitHubClient) GetIssue(ctx context.Context, repo string, number int) (*Issue, error) {
path := fmt.Sprintf("/repos/%s/issues/%d", repo, number)
resp, err := c.do(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, statusError("get issue", resp)
}
var issue Issue
if err := json.NewDecoder(resp.Body).Decode(&issue); err != nil {
return nil, fmt.Errorf("decode issue: %w", err)
}
return &issue, nil
}
func (c *httpGitHubClient) AddLabels(ctx context.Context, repo string, number int, labels []string) error {
if len(labels) == 0 {
return nil
}
path := fmt.Sprintf("/repos/%s/issues/%d/labels", repo, number)
body := map[string]any{"labels": labels}
resp, err := c.do(ctx, http.MethodPost, path, body)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return statusError("add labels", resp)
}
return nil
}
func (c *httpGitHubClient) RemoveLabel(ctx context.Context, repo string, number int, label string) error {
path := fmt.Sprintf("/repos/%s/issues/%d/labels/%s", repo, number, url.PathEscape(label))
resp, err := c.do(ctx, http.MethodDelete, path, nil)
if err != nil {
return err
}
defer resp.Body.Close()
// 404 means the label was already absent; treat as success.
if resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusNotFound {
return nil
}
return statusError("remove label", resp)
}
func (c *httpGitHubClient) CreateComment(ctx context.Context, repo string, number int, body string) error {
path := fmt.Sprintf("/repos/%s/issues/%d/comments", repo, number)
resp, err := c.do(ctx, http.MethodPost, path, map[string]string{"body": body})
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusCreated {
return statusError("create comment", resp)
}
return nil
}
func (c *httpGitHubClient) do(ctx context.Context, method, path string, body any) (*http.Response, error) {
var reader io.Reader
if body != nil {
buf, err := json.Marshal(body)
if err != nil {
return nil, fmt.Errorf("encode request: %w", err)
}
reader = bytes.NewReader(buf)
}
req, err := http.NewRequestWithContext(ctx, method, c.baseURL+path, reader)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/vnd.github+json")
req.Header.Set("X-GitHub-Api-Version", "2022-11-28")
if c.token != "" {
req.Header.Set("Authorization", "Bearer "+c.token)
}
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
return c.http.Do(req)
}
func statusError(op string, resp *http.Response) error {
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("github %s: %s: %s", op, resp.Status, strings.TrimSpace(string(body)))
}