/
github_repository.go
680 lines (585 loc) · 24.3 KB
/
github_repository.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
// Copyright The Linux Foundation and each contributor to CommunityBridge.
// SPDX-License-Identifier: MIT
package github
import (
"context"
"errors"
"fmt"
"net/http"
"strconv"
"strings"
log "github.com/communitybridge/easycla/cla-backend-go/logging"
"github.com/communitybridge/easycla/cla-backend-go/utils"
"github.com/sirupsen/logrus"
"github.com/communitybridge/easycla/cla-backend-go/logging"
"github.com/google/go-github/v37/github"
)
var (
// ErrGitHubRepositoryNotFound is returned when github repository is not found
ErrGitHubRepositoryNotFound = errors.New("github repository not found")
)
const (
help = "https://help.github.com/en/github/committing-changes-to-your-project/why-are-my-commits-linked-to-the-wrong-user"
unknown = "Unknown"
failureState = "failure"
successState = "success"
)
func GetGitHubRepository(ctx context.Context, installationID, githubRepositoryID int64) (*github.Repository, error) {
f := logrus.Fields{
"functionName": "github.github_repository.GetGitHubRepository",
"installationID": installationID,
"githubRepositoryID": githubRepositoryID,
}
client, clientErr := NewGithubAppClient(installationID)
if clientErr != nil {
log.WithFields(f).WithError(clientErr).Warnf("problem loading github client for installation ID: %d", installationID)
return nil, clientErr
}
log.WithFields(f).Debugf("getting github repository by id: %d", githubRepositoryID)
repository, httpResponse, repoErr := client.Repositories.GetByID(ctx, githubRepositoryID)
if repoErr != nil {
log.WithFields(f).WithError(repoErr).Warnf("unable to fetch repository by ID: %d", githubRepositoryID)
return nil, repoErr
}
if httpResponse.StatusCode != http.StatusOK {
log.WithFields(f).Warnf("unexpected status code: %d", httpResponse.StatusCode)
return nil, ErrGitHubRepositoryNotFound
}
//log.WithFields(f).Debugf("successfully retrieved github repository by id: %d - repository object: %+v", githubRepositoryID, repository)
return repository, nil
}
func GetPullRequest(ctx context.Context, pullRequestID int, owner, repo string, client *github.Client) (*github.PullRequest, error) {
f := logrus.Fields{
"functionName": "github.github_repository.GetPullRequest",
"pullRequestID": pullRequestID,
"owner": owner,
"repo": repo,
}
pullRequest, _, err := client.PullRequests.Get(ctx, owner, repo, pullRequestID)
if err != nil {
logging.WithFields(f).WithError(err).Warn("unable to get pull request")
return nil, err
}
return pullRequest, nil
}
// UserCommitSummary data model
type UserCommitSummary struct {
SHA string
CommitAuthor *github.User
Affiliated bool
Authorized bool
}
// GetCommitAuthorID commit author username ID (numeric value as a string) if available, otherwise returns empty string
func (u UserCommitSummary) GetCommitAuthorID() string {
if u.CommitAuthor != nil && u.CommitAuthor.ID != nil {
return strconv.Itoa(int(*u.CommitAuthor.ID))
}
return ""
}
// GetCommitAuthorUsername returns commit author username if available, otherwise returns empty string
func (u UserCommitSummary) GetCommitAuthorUsername() string {
if u.CommitAuthor != nil {
if u.CommitAuthor.Login != nil {
return *u.CommitAuthor.Login
}
if u.CommitAuthor.Name != nil {
return *u.CommitAuthor.Name
}
}
return ""
}
// GetCommitAuthorEmail returns commit author email if available, otherwise returns empty string
func (u UserCommitSummary) GetCommitAuthorEmail() string {
if u.CommitAuthor != nil && u.CommitAuthor.Email != nil {
return *u.CommitAuthor.Email
}
return ""
}
// IsValid returns true if the commit author information is available
func (u UserCommitSummary) IsValid() bool {
valid := false
if u.CommitAuthor != nil {
valid = u.CommitAuthor.ID != nil && (u.CommitAuthor.Login != nil || u.CommitAuthor.Name != nil)
}
return valid
}
// GetDisplayText returns the display text for the user commit summary
func (u UserCommitSummary) GetDisplayText(tagUser bool) string {
if !u.IsValid() {
return "Invalid author details.\n"
}
if u.Affiliated && u.Authorized {
return fmt.Sprintf("%s is authorized.\n ", u.getUserInfo(tagUser))
}
if u.Affiliated {
return fmt.Sprintf("%s is associated with a company, but not an approval list.\n", u.getUserInfo(tagUser))
} else {
return fmt.Sprintf("%s is not associated with a company.\n", u.getUserInfo(tagUser))
}
}
func (u UserCommitSummary) getUserInfo(tagUser bool) string {
f := logrus.Fields{
"functionName": "github.github_repository.getUserInfo",
"tagUser": tagUser,
}
userInfo := ""
tagValue := ""
var sb strings.Builder
sb.WriteString(userInfo)
log.WithFields(f).Debugf("author: %+v", u.CommitAuthor)
if tagUser {
tagValue = "@"
}
if u.CommitAuthor != nil {
if *u.CommitAuthor.Login != "" {
sb.WriteString(fmt.Sprintf("login: %s%s / ", tagValue, *u.CommitAuthor.Login))
}
if u.CommitAuthor.Name != nil {
sb.WriteString(fmt.Sprintf("%sname: %s / ", userInfo, utils.StringValue(u.CommitAuthor.Name)))
}
}
return strings.Replace(sb.String(), "/ $", "", -1)
}
func GetPullRequestCommitAuthors(ctx context.Context, installationID int64, pullRequestID int, owner, repo string) ([]*UserCommitSummary, *string, error) {
f := logrus.Fields{
"functionName": "github.github_repository.GetPullRequestCommitAuthors",
"pullRequestID": pullRequestID,
}
var userCommitSummary []*UserCommitSummary
client, err := NewGithubAppClient(installationID)
if err != nil {
log.WithFields(f).WithError(err).Warn("unable to create Github client")
return nil, nil, err
}
commits, resp, comErr := client.PullRequests.ListCommits(ctx, owner, repo, pullRequestID, &github.ListOptions{})
if comErr != nil {
log.WithFields(f).WithError(comErr).Warnf("problem listing commits for repo: %s/%s pull request: %d", owner, repo, pullRequestID)
return nil, nil, comErr
}
if resp.StatusCode != http.StatusOK {
msg := fmt.Sprintf("unexpected status code: %d - expected: %d", resp.StatusCode, http.StatusOK)
log.WithFields(f).Warn(msg)
return nil, nil, errors.New(msg)
}
log.WithFields(f).Debugf("found %d commits for pull request: %d", len(commits), pullRequestID)
for _, commit := range commits {
log.WithFields(f).Debugf("loaded commit: %+v", commit)
commitAuthor := ""
if commit.Commit != nil && commit.Commit.Author != nil && commit.Commit.Author.Login != nil {
log.WithFields(f).Debugf("commit.Commit.Author: %s", utils.StringValue(commit.Commit.Author.Login))
commitAuthor = utils.StringValue(commit.Commit.Author.Login)
} else if commit.Author != nil && commit.Author.Login != nil {
log.WithFields(f).Debugf("commit.Author.Login: %s", utils.StringValue(commit.Author.Login))
commitAuthor = utils.StringValue(commit.Author.Login)
}
log.WithFields(f).Debugf("commitAuthor: %s", commitAuthor)
userCommitSummary = append(userCommitSummary, &UserCommitSummary{
SHA: *commit.SHA,
CommitAuthor: commit.Author,
Affiliated: false,
Authorized: false,
})
}
// get latest commit SHA
latestCommitSHA := commits[len(commits)-1].SHA
return userCommitSummary, latestCommitSHA, nil
}
func UpdatePullRequest(ctx context.Context, installationID int64, pullRequestID int, owner, repo string, repoID *int64, latestSHA string, signed []*UserCommitSummary, missing []*UserCommitSummary, CLABaseAPIURL, CLALandingPage, CLALogoURL string) error {
f := logrus.Fields{
"functionName": "github.github_repository.UpdatePullRequest",
"installationID": installationID,
"owner": owner,
"repo": repo,
"SHA": latestSHA,
"pullRequestID": pullRequestID,
}
client, err := NewGithubAppClient(installationID)
if err != nil || client == nil {
log.WithFields(f).WithError(err).Warn("unable to create Github client")
return err
}
// Update comments as necessary
log.WithFields(f).Debugf("updating comment for PR: %d... ", pullRequestID)
previouslyFailed, comment, failedErr := hasCheckPreviouslyFailed(ctx, client, owner, repo, pullRequestID)
if failedErr != nil {
log.WithFields(f).WithError(failedErr).Debugf("unable to check previously failed PR: %d", pullRequestID)
return failedErr
}
previouslySucceeded, previousSucceededComment, succeedErr := hasCheckPreviouslySucceeded(ctx, client, owner, repo, pullRequestID)
if succeedErr != nil {
log.WithFields(f).WithError(succeedErr).Debugf("unable to check previously succeeded PR: %d", pullRequestID)
return failedErr
}
body := assembleCLAComment(ctx, int(installationID), pullRequestID, repoID, signed, missing, CLABaseAPIURL, CLALogoURL, CLALandingPage)
if len(missing) == 0 {
// All contributors are passing
// If we have previously failed, we need to update the comment
if previouslyFailed {
log.WithFields(f).Debugf("Found previously failed checks - updating the CLA comment in the PR : %d", pullRequestID)
comment.Body = &body
_, _, err = client.Issues.EditComment(ctx, owner, repo, *comment.ID, comment)
if err != nil {
log.WithFields(f).Debug("unable to edit comment ")
return err
}
}
} else {
// One or more contributors are failing
// If we have previously failed, we need to update the comment
if previouslyFailed {
log.WithFields(f).Debugf("Found previously failed checks - updating the CLA comment in the PR : %d", pullRequestID)
comment.Body = &body
_, _, err = client.Issues.EditComment(ctx, owner, repo, *comment.ID, comment)
if err != nil {
log.WithFields(f).Debug("unable to edit comment ")
return err
}
} else if previouslySucceeded {
// If we have previously succeeded, then we also need to update the comment (pass => fail)
log.WithFields(f).Debugf("Found previously succeeeded checks - updating the CLA comment in the PR : %d", pullRequestID)
// Generate a new comment with all the failed CLA info
failedComment := assembleCLAComment(ctx, int(installationID), pullRequestID, repoID, signed, missing, CLABaseAPIURL, CLALogoURL, CLALandingPage)
previousSucceededComment.Body = &failedComment
_, _, err = client.Issues.EditComment(ctx, owner, repo, *previousSucceededComment.ID, previousSucceededComment)
if err != nil {
log.WithFields(f).Debug("unable to edit comment ")
return err
}
} else {
// no previous comment - need to create a new comment
_, _, err = client.Issues.CreateComment(ctx, owner, repo, pullRequestID, comment)
if err != nil {
log.WithFields(f).Debug("unable to create comment")
}
log.WithFields(f).Debugf(`EasyCLA App checks fail for PR: %d.
CLA signatures with signed authors: %+v and with missing authors: %+v`, pullRequestID, signed, missing)
}
}
// Update/Create the status
context := "EasyCLA"
var statusBody string
var state string
var signURL string
if len(missing) > 0 {
state = failureState
context, statusBody = assembleCLAStatus(context, false)
signURL = getFullSignURL("github", strconv.Itoa(int(installationID)), strconv.Itoa(int(*repoID)), strconv.Itoa(pullRequestID), CLABaseAPIURL)
log.WithFields(f).Debugf("Creating new CLA %s status - %d passed, %d missing, signing url %s", state, len(signed), len(missing), signURL)
} else if len(signed) > 0 {
state = successState
context, statusBody = assembleCLAStatus(context, true)
signURL = fmt.Sprintf("%s/#/?version=2", CLALandingPage)
log.WithFields(f).Debugf("Creating new CLA %s status - %d passed, %d missing, signing url %s", state, len(signed), len(missing), signURL)
} else {
state = failureState
context, statusBody = assembleCLAStatus(context, false)
signURL = getFullSignURL("github", strconv.Itoa(int(installationID)), strconv.Itoa(int(*repoID)), strconv.Itoa(pullRequestID), CLABaseAPIURL)
log.WithFields(f).Debugf("Creating new CLA %s status - %d passed, %d missing, signing url %s", state, len(signed), len(missing), signURL)
log.WithFields(f).Debugf("This is an error condition - should have at least one committer in one of these lists: signed : %+v passed, %+v", signed, missing)
}
status := Status{
State: &state,
TargetURL: &signURL,
Context: &context,
Description: &statusBody,
}
log.WithFields(f).Debugf("Creating status: %+v", status)
_, _, err = CreateStatus(ctx, client, owner, repo, latestSHA, &status)
if err != nil {
log.WithFields(f).Debugf("unable to create status: %v", status)
return err
}
return nil
}
func hasCheckPreviouslyFailed(ctx context.Context, client *github.Client, owner, repo string, pullRequestID int) (bool, *github.IssueComment, error) {
f := logrus.Fields{
"functionName": "github.github_repository.hasCheckPreviouslyFailed",
}
comments, _, err := client.Issues.ListComments(ctx, owner, repo, pullRequestID, &github.IssueListCommentsOptions{})
if err != nil {
log.WithFields(f).WithError(err).Warnf("unable to get fetch comments for repo: %s, pr: %d", repo, pullRequestID)
return false, nil, err
}
for _, comment := range comments {
if strings.Contains(*comment.Body, "is not authorized under a signed CLA") {
return true, comment, nil
}
if strings.Contains(*comment.Body, "they must confirm their affiliation") {
return true, comment, nil
}
if strings.Contains(*comment.Body, "is missing the User") {
return true, comment, nil
}
}
return false, nil, nil
}
func hasCheckPreviouslySucceeded(ctx context.Context, client *github.Client, owner, repo string, pullRequestID int) (bool, *github.IssueComment, error) {
f := logrus.Fields{
"functionName": "github.github_repository.hasCheckPreviouslySucceeded",
}
comments, _, err := client.Issues.ListComments(ctx, owner, repo, pullRequestID, &github.IssueListCommentsOptions{})
if err != nil {
log.WithFields(f).WithError(err).Warnf("unable to get fetch comments for repo: %s, pr: %d", repo, pullRequestID)
return false, nil, err
}
for _, comment := range comments {
if strings.Contains(*comment.Body, "The committers listed above are authorized under a signed CLA.") {
return true, comment, nil
}
}
return false, nil, nil
}
func assembleCLAStatus(authorName string, signed bool) (string, string) {
if authorName == "" {
authorName = unknown
}
if signed {
return authorName, "EasyCLA check passed. You are authorized to contribute."
}
return authorName, "Missing CLA Authorization."
}
func assembleCLAComment(ctx context.Context, installationID, pullRequestID int, repositoryID *int64, signed, missing []*UserCommitSummary, apiBaseURL, CLALogoURL, CLALandingPage string) string {
f := logrus.Fields{
"functionName": "github.github_repository.assembleCLAComment",
utils.XREQUESTID: ctx.Value(utils.XREQUESTID),
"installationID": installationID,
"repositoryID": repositoryID,
"pullRequestID": pullRequestID,
"repoID": *repositoryID,
}
repositoryType := "github"
missingID := false
for _, userSummary := range missing {
if userSummary.GetCommitAuthorID() == "" {
missingID = true
}
}
log.WithFields(f).Debug("Building CLAComment body ")
signURL := getFullSignURL(repositoryType, strconv.Itoa(installationID), strconv.Itoa(int(*repositoryID)), strconv.Itoa(pullRequestID), apiBaseURL)
commentBody := getCommentBody(repositoryType, signURL, signed, missing)
allSigned := len(missing) == 0
badge := getCommentBadge(allSigned, signURL, missingID, false, CLALandingPage, CLALogoURL)
return fmt.Sprintf("%s<br >%s", badge, commentBody)
}
func getCommentBody(repositoryType, signURL string, signed, missing []*UserCommitSummary) string {
f := logrus.Fields{
"functionName": "github.github_repository:getCommentBody",
"repositoryType": repositoryType,
"signURL": signURL,
}
failed := ":x:"
success := ":white_check_mark:"
committersComment := strings.Builder{}
text := ""
if len(missing) > 0 || len(signed) > 0 {
committersComment.WriteString("<ul>")
}
if len(signed) > 0 {
committers := getAuthorInfoCommits(signed, false)
for k, v := range committers {
var shas []string
for _, summary := range v {
shas = append(shas, summary.SHA)
log.WithFields(f).Debugf("SHAS for signed users: %s", shas)
committersComment.WriteString(fmt.Sprintf("<li>%s%s(%s)</li>", success, k, strings.Join(shas, ", ")))
}
}
}
if len(missing) > 0 {
log.WithFields(f).Debugf("processing %d missing contributors", len(missing))
supportURL := "https://jira.linuxfoundation.org/servicedesk/customer/portal/4"
committers := getAuthorInfoCommits(missing, true)
helpURL := help
for k, v := range committers {
var shas []string
for _, summary := range v {
shas = append(shas, summary.SHA)
}
if k == unknown {
committersComment.WriteString(fmt.Sprintf(`<li>%s The commit (%s). This user is missing the User's ID, preventing the EasyCLA check. <a href='%s' target='_blank'>Consult GitHub Help</a> to resolve. For further assistance with EasyCLA, <a href='%s' target='_blank'>please submit a support request ticket</a>.</li>`,
failed, strings.Join(shas, ", "), helpURL, supportURL))
} else {
var missingAffiliations []*UserCommitSummary
for _, summary := range v {
if !summary.Affiliated && !summary.Authorized {
missingAffiliations = append(missingAffiliations, summary)
}
}
if len(missingAffiliations) > 0 {
log.WithFields(f).Debugf("SHAs for users with missing company affiliations: %+v", shas)
committersComment.WriteString(
fmt.Sprintf(`<li>%s %s The commit (%s). This user is authorized, but they must confirm their affiliation with their company. Start the authorization process <a href='%s' target='_blank'> by clicking here</a>, click \"Corporate\", select the appropriate company from the list, then confirm your affiliation on the page that appears. For further assistance with EasyCLA, <a href='%s' target='_blank'>please submit a support request ticket</a>.</li>`,
failed, k, strings.Join(shas, ", "), signURL, supportURL))
} else {
committersComment.WriteString(
fmt.Sprintf(`<li><a href='%s' target='_blank'>%s</a> - %s The commit (%s) is not authorized under a signed CLA. "<a href='%s' target='_blank'>Please click here to be authorized</a>. For further assistance with EasyCLA, <a href='%s' target='_blank'>please submit a support request ticket</a>.</li>`,
signURL, failed, k, strings.Join(shas, ", "), signURL, supportURL))
}
}
}
}
if len(signed) > 0 || len(missing) > 0 {
committersComment.WriteString("</ul>")
}
if len(signed) > 0 && len(missing) == 0 {
text = "<br>The committers listed above are authorized under a signed CLA."
}
return fmt.Sprintf("%s%s", committersComment.String(), text)
}
func getCommentBadge(allSigned bool, signURL string, missingUserId, managerApproved bool, CLALandingPage, CLALogoURL string) string {
var alt string
var text string
var badgeHyperLink string
var badgeURL string
if allSigned {
badgeURL = fmt.Sprintf("%s/cla-signed.svg", CLALogoURL)
badgeHyperLink = fmt.Sprintf("%s/#/?version=2", CLALandingPage)
alt = "CLA Signed"
return fmt.Sprintf(`<a href="%s"><img src="%s" alt="%s" align="left" height="28" width="328" >`, badgeHyperLink, badgeURL, alt)
}
badgeHyperLink = signURL
if missingUserId {
badgeURL = fmt.Sprintf("%s/cla-missing-id.svg", CLALogoURL)
alt = "CLA Missing ID"
} else if managerApproved {
badgeURL = fmt.Sprintf("%s/cla-confirmation-needed.svg", CLALogoURL)
alt = "CLA Confirmation Needed"
} else {
badgeURL = fmt.Sprintf("%s/cla-not-signed.svg", CLALogoURL)
alt = "CLA Not Signed"
}
text = fmt.Sprintf(`<a href="%s"><img src="%s" alt="%s" align="left" height="28" width="328" >`, badgeHyperLink, badgeURL, alt)
return fmt.Sprintf("%s<br/>", text)
}
func getFullSignURL(repositoryType, installationID, githubRepositoryID, pullRequestID, apiBaseURL string) string {
return fmt.Sprintf("%s/v2/repository-provider/%s/sign/%s/%s/%s/#/?version=2", apiBaseURL, repositoryType, installationID, githubRepositoryID, pullRequestID)
}
func getAuthorInfoCommits(userSummary []*UserCommitSummary, tagUser bool) map[string][]*UserCommitSummary {
f := logrus.Fields{
"functioName": "github.github_repository.getAuthorInfoCommits",
}
result := make(map[string][]*UserCommitSummary)
for _, author := range userSummary {
log.WithFields(f).WithFields(f).Debugf("checking user summary for : %s", author.getUserInfo(tagUser))
if _, ok := result[author.getUserInfo(tagUser)]; !ok {
result[author.getUserInfo(tagUser)] = []*UserCommitSummary{
author,
}
} else {
result[author.getUserInfo(tagUser)] = append(result[author.getUserInfo(tagUser)], author)
}
}
return result
}
// GetRepositoryByExternalID finds github repository by github repository id
func GetRepositoryByExternalID(ctx context.Context, installationID, id int64) (*github.Repository, error) {
client, err := NewGithubAppClient(installationID)
if err != nil {
return nil, err
}
org, resp, err := client.Repositories.GetByID(ctx, id)
if err != nil {
logging.Warnf("GitHubGetRepository %v failed. error = %s", id, err.Error())
if resp.StatusCode == 404 {
return nil, ErrGitHubRepositoryNotFound
}
return nil, err
}
return org, nil
}
// GetRepositories gets github repositories by organization
func GetRepositories(ctx context.Context, organizationName string) ([]*github.Repository, error) {
f := logrus.Fields{
"functionName": "GetRepositories",
utils.XREQUESTID: ctx.Value(utils.XREQUESTID),
"organizationName": organizationName,
}
// Get the client with token
client := NewGithubOauthClient()
var responseRepoList []*github.Repository
var nextPage = 1
for {
// API https://docs.github.com/en/free-pro-team@latest/rest/reference/repos
// API Pagination: https://docs.github.com/en/free-pro-team@latest/rest/guides/traversing-with-pagination
repoList, resp, err := client.Repositories.ListByOrg(ctx, organizationName, &github.RepositoryListByOrgOptions{
Type: "public",
Sort: "full_name",
Direction: "asc",
ListOptions: github.ListOptions{
Page: nextPage,
PerPage: 100,
},
})
if err != nil {
log.WithFields(f).WithError(err).Warn("unable to list repositories for organization")
if resp != nil && resp.StatusCode == 404 {
return nil, ErrGithubOrganizationNotFound
}
return nil, err
}
if resp.StatusCode < 200 || resp.StatusCode > 299 {
msg := fmt.Sprintf("GetRepositories %s failed with no success response code %d. error = %s", organizationName, resp.StatusCode, err.Error())
log.WithFields(f).Warnf(msg)
return nil, errors.New(msg)
}
// Append our results to the response...
responseRepoList = append(responseRepoList, repoList...)
// if no more pages...
if resp.NextPage == 0 {
break
}
// update our next page value
nextPage = resp.NextPage
}
return responseRepoList, nil
}
type Status struct {
State *string `json:"state,omitempty"`
TargetURL *string `json:"target_url,omitempty"`
Description *string `json:"description,omitempty"`
Context *string `json:"context,omitempty"`
}
// CreateStatus creates a new status on the specified commit.
//
// GitHub API docs:https://docs.github.com/en/rest/commits/statuses
func CreateStatus(ctx context.Context, client *github.Client, owner, repo, sha string, status *Status) (*Status, *github.Response, error) {
u := fmt.Sprintf("repos/%v/%v/statuses/%v", owner, repo, sha)
req, err := client.NewRequest("POST", u, status)
if err != nil {
return nil, nil, err
}
c := new(Status)
resp, err := client.Do(ctx, req, c)
if err != nil {
return nil, resp, err
}
return c, resp, nil
}
func GetReturnURL(ctx context.Context, installationID, repositoryID int64, pullRequestID int) (string, error) {
f := logrus.Fields{
"functionName": "github.github_repository.GetReturnURL",
utils.XREQUESTID: ctx.Value(utils.XREQUESTID),
"installationID": installationID,
"repositoryID": repositoryID,
"pullRequestID": pullRequestID,
}
client, err := NewGithubAppClient(installationID)
if err != nil {
log.WithFields(f).WithError(err).Warn("unable to create Github client")
return "", err
}
log.WithFields(f).Debugf("getting github repository by id: %d", repositoryID)
repo, _, err := client.Repositories.GetByID(ctx, repositoryID)
if err != nil {
log.WithFields(f).WithError(err).Warnf("unable to get repository by ID: %d", repositoryID)
return "", err
}
log.WithFields(f).Debugf("getting pull request by id: %d", pullRequestID)
pullRequest, _, err := client.PullRequests.Get(ctx, *repo.Owner.Login, *repo.Name, pullRequestID)
if err != nil {
log.WithFields(f).WithError(err).Warnf("unable to get pull request by ID: %d", pullRequestID)
return "", err
}
log.WithFields(f).Debugf("returning pull request html url: %s", *pullRequest.HTMLURL)
return *pullRequest.HTMLURL, nil
}