/
client.go
357 lines (325 loc) · 11.9 KB
/
client.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package github
import (
"context"
"net/http"
"strings"
"github.com/bivas/rivi/pkg/config/client"
"github.com/bivas/rivi/pkg/types"
"github.com/bivas/rivi/pkg/util/log"
"github.com/bradleyfalzon/ghinstallation"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
)
type Client struct {
client *github.Client
secret []byte
owner string
repo string
logger log.Logger
}
func (c *Client) handleFileContentResponse(file *github.RepositoryContent, err error, fields *log.MetaFields) string {
if err != nil {
c.logger.ErrorWith(*fields, "Unable to get file")
return ""
}
content, err := file.GetContent()
if err != nil {
c.logger.ErrorWith(*fields, "Unable to get file content")
content = ""
}
return content
}
func (c *Client) SetStatus(sha string, description string, state string) {
status := &github.RepoStatus{
Context: github.String("rivi"),
Description: &description,
State: &state,
}
if _, _, err := c.client.Repositories.CreateStatus(context.Background(), c.owner, c.repo, sha, status); err != nil {
c.logger.ErrorWith(log.MetaFields{log.E(err), log.F("repo", c.repo), log.F("sha", sha)}, "Unable to set status")
}
}
func (c *Client) GetFileContentFromRef(path, owner, repo, ref string) string {
opts := &github.RepositoryContentGetOptions{Ref: ref}
file, _, _, err := c.client.Repositories.GetContents(context.Background(), owner, repo, path, opts)
return c.handleFileContentResponse(file, err,
&log.MetaFields{
log.E(err),
log.F("repo", repo),
log.F("owner", owner),
log.F("path", path),
log.F("ref", ref)})
}
func (c *Client) GetFileContent(path string) string {
file, _, _, err := c.client.Repositories.GetContents(context.Background(), c.owner, c.repo, path, nil)
return c.handleFileContentResponse(file, err,
&log.MetaFields{
log.E(err),
log.F("repo", c.repo),
log.F("owner", c.owner),
log.F("path", path)})
}
func (c *Client) GetCollaborators() []string {
users, _, err := c.client.Repositories.ListCollaborators(context.Background(), c.owner, c.repo, nil)
result := make([]string, 0)
if err != nil {
c.logger.ErrorWith(log.MetaFields{log.E(err), log.F("repo", c.repo)}, "Unable to get collaborators")
} else {
for _, user := range users {
result = append(result, strings.ToLower(*user.Login))
}
}
return result
}
func (c *Client) GetState(issue int) string {
c.logger.DebugWith(log.MetaFields{log.F("issue.id", issue)}, "Getting issue state")
response, _, err := c.client.Issues.Get(context.Background(), c.owner, c.repo, issue)
if err != nil {
c.logger.ErrorWith(log.MetaFields{log.E(err), log.F("issue.id", issue)}, "Unable to get issue state.")
return ""
}
return *response.State
}
func handleLabelsResult(labels []*github.Label, err error, logError func(error)) []string {
result := make([]string, 0)
if err != nil {
logError(err)
} else {
for _, p := range labels {
result = append(result, *p.Name)
}
}
return result
}
func (c *Client) Lock(issue int) {
c.logger.DebugWith(log.MetaFields{log.F("issue.id", issue)}, "Locking issue")
_, err := c.client.Issues.Lock(context.Background(), c.owner, c.repo, issue)
if err != nil {
c.logger.ErrorWith(log.MetaFields{log.E(err), log.F("issue.id", issue)}, "Unable to set issue lock state")
}
}
func (c *Client) Unlock(issue int) {
c.logger.DebugWith(log.MetaFields{log.F("issue.id", issue)}, "Unlocking issue")
_, err := c.client.Issues.Unlock(context.Background(), c.owner, c.repo, issue)
if err != nil {
c.logger.ErrorWith(log.MetaFields{log.E(err), log.F("issue.id", issue)}, "Unable to set issue unlock state")
}
}
func (c *Client) Locked(issue int) bool {
c.logger.DebugWith(log.MetaFields{log.F("issue.id", issue)}, "Checking lock state")
response, _, err := c.client.Issues.Get(context.Background(), c.owner, c.repo, issue)
if err != nil {
c.logger.ErrorWith(log.MetaFields{log.E(err), log.F("issue.id", issue)}, "Unable to get issue lock state")
}
return *response.Locked
}
func (c *Client) GetAvailableLabels() []string {
c.logger.Debug("Getting available labels")
labels, _, e := c.client.Issues.ListLabels(context.Background(), c.owner, c.repo, nil)
return handleLabelsResult(labels, e, func(err error) {
c.logger.ErrorWith(log.MetaFields{log.E(err)}, "Unable to get available labels")
})
}
func (c *Client) GetLabels(issue int) []string {
c.logger.DebugWith(log.MetaFields{log.F("issue.id", issue)}, "Getting labels")
labels, _, err := c.client.Issues.ListLabelsByIssue(context.Background(), c.owner, c.repo, issue, nil)
return handleLabelsResult(labels, err, func(err error) {
c.logger.ErrorWith(log.MetaFields{log.E(err), log.F("issue.id", issue)}, "Unable to get labels for issue.")
})
}
func (c *Client) AddLabel(issue int, label string) []string {
c.logger.DebugWith(log.MetaFields{log.F("issue.id", issue)}, "Adding label '%s'", label)
labels, _, err := c.client.Issues.AddLabelsToIssue(context.Background(), c.owner, c.repo, issue, []string{label})
return handleLabelsResult(labels, err, func(err error) {
c.logger.ErrorWith(log.MetaFields{log.E(err), log.F("issue.id", issue)}, "Unable to add label '%s' for issue", label)
})
}
func (c *Client) RemoveLabel(issue int, label string) []string {
c.logger.DebugWith(log.MetaFields{log.F("issue.id", issue)}, "Removing label '%s'", label)
c.client.Issues.RemoveLabelForIssue(context.Background(), c.owner, c.repo, issue, label)
return c.GetLabels(issue)
}
func (c *Client) GetAssignees(issue int) []string {
c.logger.DebugWith(log.MetaFields{log.F("issue.id", issue)}, "Getting assignees")
result := make([]string, 0)
issueObject, _, err := c.client.Issues.Get(context.Background(), c.owner, c.repo, issue)
if err != nil {
c.logger.ErrorWith(log.MetaFields{log.E(err), log.F("issue.id", issue)}, "Unable to get assignees for issue")
} else {
for _, p := range issueObject.Assignees {
result = append(result, strings.ToLower(*p.Login))
}
}
return result
}
func (c *Client) AddAssignees(issue int, assignees ...string) []string {
c.logger.DebugWith(log.MetaFields{log.F("issue.id", issue)}, "Adding assignees %s", assignees)
response, _, err := c.client.Issues.AddAssignees(context.Background(), c.owner, c.repo, issue, assignees)
result := make([]string, 0)
if err != nil {
c.logger.ErrorWith(log.MetaFields{log.E(err), log.F("issue.id", issue)}, "Unable to add assignees %s for issue", assignees)
} else {
for _, p := range response.Assignees {
result = append(result, *p.Login)
}
}
return result
}
func (c *Client) RemoveAssignees(issue int, assignees ...string) []string {
c.logger.DebugWith(log.MetaFields{log.F("issue.id", issue)}, "Removing assignees %s", assignees)
response, _, err := c.client.Issues.RemoveAssignees(context.Background(), c.owner, c.repo, issue, assignees)
result := make([]string, 0)
if err != nil {
c.logger.ErrorWith(log.MetaFields{log.E(err), log.F("issue.id", issue)}, "Unable to remove assignees %s for issue", assignees)
} else {
for _, p := range response.Assignees {
result = append(result, *p.Login)
}
}
return result
}
func (c *Client) GetPatch(issue int) map[string]*string {
c.logger.DebugWith(log.MetaFields{log.F("issue.id", issue)}, "Getting file names")
files, _, err := c.client.PullRequests.ListFiles(context.Background(), c.owner, c.repo, issue, nil)
result := make(map[string]*string, 0)
if err != nil {
c.logger.ErrorWith(log.MetaFields{log.E(err), log.F("issue.id", issue)}, "Unable to get patch for issue")
} else {
for _, p := range files {
result[*p.Filename] = p.Patch
}
}
return result
}
func (c *Client) GetFileNames(issue int) []string {
c.logger.DebugWith(log.MetaFields{log.F("issue.id", issue)}, "Getting file names")
files, _, err := c.client.PullRequests.ListFiles(context.Background(), c.owner, c.repo, issue, nil)
result := make([]string, 0)
if err != nil {
c.logger.ErrorWith(log.MetaFields{log.E(err), log.F("issue.id", issue)}, "Unable to get file names for issue")
} else {
for _, p := range files {
result = append(result, *p.Filename)
}
}
return result
}
func (c *Client) GetComments(issue int) []types.Comment {
c.logger.DebugWith(log.MetaFields{log.F("issue.id", issue)}, "Getting comments")
comments, _, err := c.client.Issues.ListComments(context.Background(), c.owner, c.repo, issue, nil)
result := make([]types.Comment, 0)
if err != nil {
c.logger.ErrorWith(log.MetaFields{log.E(err), log.F("issue.id", issue)}, "Unable to get comments for issue")
} else {
for _, p := range comments {
comment := types.Comment{
Commenter: *p.User.Login,
Comment: *p.Body,
}
result = append(result, comment)
}
}
return result
}
func (c *Client) AddComment(issue int, comment string) types.Comment {
c.logger.DebugWith(log.MetaFields{log.F("issue.id", issue), log.F("comment", comment)}, "Adding comment")
commentObject := &github.IssueComment{Body: github.String(comment)}
posted, _, err := c.client.Issues.CreateComment(context.Background(), c.owner, c.repo, issue, commentObject)
if err != nil {
c.logger.ErrorWith(log.MetaFields{log.E(err), log.F("issue.id", issue)}, "Unable to add comment for issue")
return types.Comment{}
} else {
return types.Comment{
Commenter: *posted.User.Login,
Comment: *posted.Body,
}
}
}
func (c *Client) GetReviewers(issue int) map[string]string {
c.logger.DebugWith(log.MetaFields{log.F("issue.id", issue)}, "Getting reviewers")
result := make(map[string]string)
reviews, _, err := c.client.PullRequests.ListReviews(context.Background(), c.owner, c.repo, issue, nil)
if err != nil {
c.logger.ErrorWith(log.MetaFields{log.E(err), log.F("issue.id", issue)}, "Unable to get reviewers for issue")
return result
}
for _, review := range reviews {
user := strings.ToLower(*review.User.Login)
state := strings.ToLower(*review.State)
result[user] = state
}
c.logger.DebugWith(
log.MetaFields{log.F("issue.id", issue), log.F("reviewers", result)},
"Created reviewers map")
return result
}
func (c *Client) Merge(issue int, mergeMethod string) {
c.logger.DebugWith(log.MetaFields{log.F("issue.id", issue), log.F("strategy", mergeMethod)}, "Merging")
options := &github.PullRequestOptions{MergeMethod: mergeMethod}
_, _, err := c.client.PullRequests.Merge(context.Background(), c.owner, c.repo, issue, "", options)
if err != nil {
c.logger.ErrorWith(log.MetaFields{log.E(err), log.F("issue.id", issue)}, "Error trying to merge issue")
}
}
func NewClient(config client.ClientConfig, owner, repo string) *Client {
logger := log.Get("Github.Client")
if config.GetOAuthToken() == "" {
logger.ErrorWith(
log.MetaFields{
log.F("owner", owner),
log.F("repo", repo),
}, "Client configuration missing required fields (oauth token)",
)
return nil
}
source := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: config.GetOAuthToken()},
)
c := oauth2.NewClient(context.Background(), source)
return &Client{
client: github.NewClient(c),
secret: []byte(config.GetSecret()),
owner: owner,
repo: repo,
logger: logger,
}
}
func NewAppClient(config client.ClientConfig, owner, repo string, installation int) *Client {
logger := log.Get("Github.Client")
if config.GetApplicationID() == 0 || config.GetPrivateKeyFile() == "" {
logger.ErrorWith(
log.MetaFields{
log.F("appid", config.GetApplicationID()),
log.F("installation", installation),
log.F("owner", owner),
log.F("repo", repo),
}, "Client configuration missing required fields (appid, pem file)",
)
return nil
}
c, err := ghinstallation.NewKeyFromFile(
http.DefaultTransport,
config.GetApplicationID(),
installation,
config.GetPrivateKeyFile(),
)
if err != nil {
logger.ErrorWith(
log.MetaFields{
log.E(err),
log.F("appid", config.GetApplicationID()),
log.F("installation", installation),
log.F("owner", owner),
log.F("repo", repo),
}, "Unable to create installation client",
)
return nil
}
return &Client{
client: github.NewClient(&http.Client{Transport: c}),
secret: []byte(config.GetSecret()),
owner: owner,
repo: repo,
logger: logger,
}
}