-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathgithub.go
More file actions
374 lines (316 loc) · 10.2 KB
/
Copy pathgithub.go
File metadata and controls
374 lines (316 loc) · 10.2 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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package github
import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"regexp"
"sync"
"time"
gh "github.com/google/go-github/v45/github"
ghauth "github.com/jferrl/go-githubauth"
log "github.com/sirupsen/logrus"
"github.com/tcnksm/go-gitconfig"
"golang.org/x/oauth2"
)
const commentIDRegex = `META\s*=\s*{(?P<meta>[^}]*)`
// if we have fewer than this threshold remaining we will report rate limited
const rateLimitThreshold = 500
const GitHubAppID = 1046118
// openshift github orgs that our app has access to
type GitHubOrg string
const OpenshiftOrg = GitHubOrg("openshift")
const OpenshiftEngOrg = GitHubOrg("openshift-eng")
type prlocator struct {
org string
repo string
number int
}
type PREntry struct {
MergedAt *time.Time
SHA string
Title *string
URL *string
Login *string
State *string
}
type Client struct {
ctx context.Context
apiClient *gh.Client
cache map[prlocator]*PREntry
cacheLock sync.RWMutex
prFetch func(org, repo string, number int) (*gh.PullRequest, error)
prCommentsFetch func(org, repo string, number int) ([]*gh.IssueComment, error)
prCommentCreate func(org, repo string, number int, comment string) (*gh.IssueComment, error)
prCommentDelete func(org, repo string, updateID int64) error
gitHubCoreRateFetch func() (*gh.Rate, error)
gitHubListClosedPRs func(org, repo string) ([]*gh.PullRequest, error)
commentMetaRegEx *regexp.Regexp
}
func New(ctx context.Context, org GitHubOrg) *Client {
client := &Client{
ctx: ctx,
cache: make(map[prlocator]*PREntry),
}
ghc := gh.NewClient(newGHAuthClient(client.ctx, org))
client.apiClient = ghc
client.prFetch = func(org, repo string, number int) (*gh.PullRequest, error) {
pr, _, err := ghc.PullRequests.Get(client.ctx, org, repo, number)
return pr, err
}
client.prCommentCreate = func(org, repo string, number int, comment string) (*gh.IssueComment, error) {
ghComment := &gh.IssueComment{Body: &comment}
commentResponse, _, err := ghc.Issues.CreateComment(client.ctx, org, repo, number, ghComment)
return commentResponse, err
}
client.prCommentDelete = func(org, repo string, updateID int64) error {
_, err := ghc.Issues.DeleteComment(client.ctx, org, repo, updateID)
return err
}
client.prCommentsFetch = func(org, repo string, number int) ([]*gh.IssueComment, error) {
issueCommentOptions := &gh.IssueListCommentsOptions{}
issueComments, _, err := ghc.Issues.ListComments(client.ctx, org, repo, number, issueCommentOptions)
return issueComments, err
}
client.gitHubCoreRateFetch = func() (*gh.Rate, error) {
rateLimits, _, err := ghc.RateLimits(client.ctx)
if err != nil {
return nil, err
}
if rateLimits == nil {
return nil, nil
}
return rateLimits.Core, nil
}
client.gitHubListClosedPRs = func(org, repo string) ([]*gh.PullRequest, error) {
since := time.Now().Add(-time.Hour * 48)
var response []*gh.PullRequest
opts := &gh.PullRequestListOptions{
State: "closed",
Sort: "updated",
Direction: "desc",
ListOptions: gh.ListOptions{PerPage: 50},
}
for {
prs, resp, err := ghc.PullRequests.List(ctx, org, repo, opts)
if err != nil {
return response, err
}
pastWindow := false
for _, pr := range prs {
if pr != nil && pr.Number != nil {
response = append(response, pr)
if pr.UpdatedAt != nil && pr.UpdatedAt.Before(since) {
pastWindow = true
}
}
}
if pastWindow || resp.NextPage == 0 {
return response, nil
}
opts.Page = resp.NextPage
}
}
client.commentMetaRegEx = regexp.MustCompile(commentIDRegex)
return client
}
func (c *Client) APIClient() *gh.Client {
return c.apiClient
}
// we could use the app token to look up github app installation ids at https://api.github.com/app/installations
// but it's not like they will change, so we can just hard code them, for one less thing to go wrong
var installationIDForOrg = map[GitHubOrg]int64{
OpenshiftOrg: 56889436,
OpenshiftEngOrg: 56889451,
}
func newGHAuthClient(ctx context.Context, org GitHubOrg) *http.Client {
if tokenSource := newAppTokenSource(); tokenSource != nil {
// create an org-specific self-renewing token source
installationTokenSource := ghauth.NewInstallationTokenSource(installationIDForOrg[org], tokenSource, ghauth.WithContext(ctx))
log.Infof("using GitHub App credentials for org %s", org)
return oauth2.NewClient(ctx, installationTokenSource)
}
// no app creds, try to use a personal access token
token := os.Getenv("GITHUB_TOKEN")
if token == "" {
log.Infof("No GitHub token environment variable, checking git config")
var err error
token, err = gitconfig.GithubToken()
if err != nil {
log.WithError(err).Warningf("unable to retrieve GitHub token from git config")
}
}
if token != "" {
log.Infof("using GitHub access token for org %s", org)
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
return oauth2.NewClient(ctx, ts)
}
// make a no-auth client if no token is available
log.Warningf("using unathenticated GitHub client, requests will be rate-limited")
return nil
}
func newAppTokenSource() oauth2.TokenSource {
// check that the environment variables are set
privateKey := os.Getenv("GITHUB_APP_CLIENT_KEY")
if privateKey == "" {
log.Warn("missing GITHUB_APP_CLIENT_KEY, will not authenticate as GitHub App")
return nil
}
// create top-level token source for the application
appTokenSource, err := ghauth.NewApplicationTokenSource(GitHubAppID, []byte(privateKey))
if err != nil {
log.Errorf("Error creating application token source: %s", err)
return nil
}
return appTokenSource
}
// ListRecentlyClosedPRs returns all PRs for the given repo that were closed
// within the last 48 hours. The caller filters to merged-only via MergedAt.
func (c *Client) ListRecentlyClosedPRs(org, repo string) ([]*gh.PullRequest, error) {
return c.gitHubListClosedPRs(org, repo)
}
func (c *Client) IsWithinRateLimitThreshold() bool {
rate, err := c.gitHubCoreRateFetch()
if err != nil {
// presume we are rate limited if we can't even get the rate limit...
return true
}
if rate == nil {
// for now assume rate limited if we can't get the rate
return true
}
log.Infof("Github Limit:%d, Remaining:%d", rate.Limit, rate.Remaining)
return rate.Remaining < rateLimitThreshold
}
func (c *Client) GetPRURL(org, repo string, number int) (*string, error) {
prEntry, err := c.GetPREntry(org, repo, number)
if err != nil {
return nil, err
}
if prEntry != nil {
return prEntry.URL, nil
}
return nil, nil
}
func (c *Client) GetPRTitle(org, repo string, number int) (*string, error) {
prEntry, err := c.GetPREntry(org, repo, number)
if err != nil {
return nil, err
}
if prEntry != nil {
return prEntry.Title, nil
}
return nil, nil
}
// GetPRSHAMerged returns the merge time for a PR/SHA combination. The caching is designed
// to minimize queries to GitHub. We basically have to handle these cases:
// - the PR doesn't exist (cache as nil)
// - the PR is unmerged (cache with nil mergedAt)
// - the PR is merged with a different SHA (cache with the merged sha, return nil)
// - the PR is merged with the same SHA (cache with the merged sha, return merged time)
func (c *Client) GetPRSHAMerged(org, repo string, number int, sha string) (*time.Time, error) {
pr, err := c.GetPREntry(org, repo, number)
if err != nil {
return nil, err
}
if pr != nil && pr.SHA == sha {
return pr.MergedAt, nil
}
// if it isn't in the cache or the sha doesn't match then return nil
return nil, nil
}
func (c *Client) GetPREntry(org, repo string, number int) (*PREntry, error) {
c.cacheLock.Lock()
defer c.cacheLock.Unlock()
prl := prlocator{org: org, repo: repo, number: number}
if val, ok := c.cache[prl]; ok {
// If it's in the cache return it
return val, nil
}
// Get PR from GitHub
pr, err := c.PRFetch(prl.org, prl.repo, prl.number)
if err != nil {
log.WithError(err).
WithField("org", prl.org).
WithField("repo", prl.repo).
WithField("number", prl.number).
Errorf("error retrieving pull request")
if resp, ok := err.(*gh.ErrorResponse); ok && resp.Response != nil && resp.Response.StatusCode == http.StatusNotFound {
// cache nil record to prevent additional fetching
c.cache[prl] = nil
return nil, nil
}
return nil, err
}
c.cache[prl] = pr
return pr, nil
}
// PRFetch is an uncached call to github to get the most up to date information
// on the PR. Use cautiously and only when necessary
func (c *Client) PRFetch(org, repo string, number int) (prEntry *PREntry, err error) {
// Get PR from GitHub
pr, err := c.prFetch(org, repo, number)
if err != nil {
return nil, err
}
if pr != nil {
// Store any pr data we have, so we don't fetch again
prEntry = &PREntry{
MergedAt: pr.MergedAt,
Title: pr.Title,
URL: pr.HTMLURL,
State: pr.State,
}
if pr.User != nil && pr.User.Login != nil {
prEntry.Login = pr.User.Login
}
if pr.Head != nil && pr.Head.SHA != nil {
prEntry.SHA = *pr.Head.SHA
}
}
return prEntry, nil
}
func (c *Client) CreatePRComment(org, repo string, number int, comment string) error {
_, err := c.prCommentCreate(org, repo, number, comment)
return err
}
func (c *Client) DeletePRComment(org, repo string, updateID int64) error {
err := c.prCommentDelete(org, repo, updateID)
return err
}
func (c *Client) FindCommentID(org, repo string, number int, commentKey, commentID string) (*int64, *string, error) {
comments, err := c.prCommentsFetch(org, repo, number)
if err != nil {
return nil, nil, err
}
for _, cmt := range comments {
if c.isCommentIDMatch(*cmt.Body, commentKey, commentID) {
return cmt.ID, cmt.Body, nil
}
}
return nil, nil, nil
}
func (c *Client) isCommentIDMatch(comment, commentKey, commentID string) bool {
match := c.commentMetaRegEx.FindStringSubmatch(comment)
if match != nil {
index := c.commentMetaRegEx.SubexpIndex("meta")
if index > -1 {
metaJSON := fmt.Sprintf("{%s}", match[index])
var result map[string]interface{}
err := json.Unmarshal([]byte(metaJSON), &result)
if err != nil {
log.WithError(err).Errorf("Error searching for commentId: %s, match", commentID)
} else {
if value, ok := result[commentKey]; ok {
if value == commentID {
return true
}
}
}
}
}
return false
}