/
github.go
1186 lines (1042 loc) · 40.3 KB
/
github.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
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package vcsclient
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/google/go-github/v56/github"
"github.com/grokify/mogo/encoding/base64"
"github.com/jfrog/froggit-go/vcsutils"
"github.com/jfrog/gofrog/datastructures"
"github.com/mitchellh/mapstructure"
"golang.org/x/exp/slices"
"golang.org/x/oauth2"
"io"
"net/http"
"net/url"
"path/filepath"
"sort"
"strconv"
"strings"
)
const (
maxRetries = 5
retriesIntervalMilliSecs = 60000
// https://github.com/orgs/community/discussions/27190
githubPrContentSizeLimit = 65536
)
var rateLimitRetryStatuses = []int{http.StatusForbidden, http.StatusTooManyRequests}
type GitHubRateLimitExecutionHandler func() (*github.Response, error)
type GitHubRateLimitRetryExecutor struct {
vcsutils.RetryExecutor
GitHubRateLimitExecutionHandler
}
func (ghe *GitHubRateLimitRetryExecutor) Execute() error {
ghe.ExecutionHandler = func() (bool, error) {
ghResponse, err := ghe.GitHubRateLimitExecutionHandler()
return shouldRetryIfRateLimitExceeded(ghResponse, err), err
}
return ghe.RetryExecutor.Execute()
}
// GitHubClient API version 3
type GitHubClient struct {
vcsInfo VcsInfo
rateLimitRetryExecutor GitHubRateLimitRetryExecutor
logger vcsutils.Log
ghClient *github.Client
}
// NewGitHubClient create a new GitHubClient
func NewGitHubClient(vcsInfo VcsInfo, logger vcsutils.Log) (*GitHubClient, error) {
ghClient, err := buildGithubClient(vcsInfo, logger)
if err != nil {
return nil, err
}
return &GitHubClient{
vcsInfo: vcsInfo,
logger: logger,
ghClient: ghClient,
rateLimitRetryExecutor: GitHubRateLimitRetryExecutor{RetryExecutor: vcsutils.RetryExecutor{
Logger: logger,
MaxRetries: maxRetries,
RetriesIntervalMilliSecs: retriesIntervalMilliSecs},
}},
nil
}
func (client *GitHubClient) runWithRateLimitRetries(handler func() (*github.Response, error)) error {
client.rateLimitRetryExecutor.GitHubRateLimitExecutionHandler = handler
return client.rateLimitRetryExecutor.Execute()
}
// TestConnection on GitHub
func (client *GitHubClient) TestConnection(ctx context.Context) error {
_, _, err := client.ghClient.Meta.Zen(ctx)
return err
}
func buildGithubClient(vcsInfo VcsInfo, logger vcsutils.Log) (*github.Client, error) {
httpClient := &http.Client{}
if vcsInfo.Token != "" {
httpClient = oauth2.NewClient(context.Background(), oauth2.StaticTokenSource(&oauth2.Token{AccessToken: vcsInfo.Token}))
}
ghClient := github.NewClient(httpClient)
if vcsInfo.APIEndpoint != "" {
baseURL, err := url.Parse(strings.TrimSuffix(vcsInfo.APIEndpoint, "/") + "/")
if err != nil {
return nil, err
}
logger.Info("Using API endpoint:", baseURL)
ghClient.BaseURL = baseURL
}
return ghClient, nil
}
// AddSshKeyToRepository on GitHub
func (client *GitHubClient) AddSshKeyToRepository(ctx context.Context, owner, repository, keyName, publicKey string, permission Permission) error {
err := validateParametersNotBlank(map[string]string{
"owner": owner,
"repository": repository,
"key name": keyName,
"public key": publicKey,
})
if err != nil {
return err
}
readOnly := permission != ReadWrite
key := github.Key{
Key: &publicKey,
Title: &keyName,
ReadOnly: &readOnly,
}
return client.runWithRateLimitRetries(func() (*github.Response, error) {
_, ghResponse, err := client.ghClient.Repositories.CreateKey(ctx, owner, repository, &key)
return ghResponse, err
})
}
// ListRepositories on GitHub
func (client *GitHubClient) ListRepositories(ctx context.Context) (results map[string][]string, err error) {
results = make(map[string][]string)
for nextPage := 1; ; nextPage++ {
var repositoriesInPage []*github.Repository
var ghResponse *github.Response
err = client.runWithRateLimitRetries(func() (*github.Response, error) {
repositoriesInPage, ghResponse, err = client.executeListRepositoriesInPage(ctx, nextPage)
return ghResponse, err
})
if err != nil {
return
}
for _, repo := range repositoriesInPage {
results[*repo.Owner.Login] = append(results[*repo.Owner.Login], *repo.Name)
}
if nextPage+1 > ghResponse.LastPage {
break
}
}
return
}
func (client *GitHubClient) executeListRepositoriesInPage(ctx context.Context, page int) ([]*github.Repository, *github.Response, error) {
options := &github.RepositoryListOptions{ListOptions: github.ListOptions{Page: page}}
return client.ghClient.Repositories.List(ctx, "", options)
}
// ListBranches on GitHub
func (client *GitHubClient) ListBranches(ctx context.Context, owner, repository string) (branchList []string, err error) {
err = client.runWithRateLimitRetries(func() (*github.Response, error) {
var ghResponse *github.Response
branchList, ghResponse, err = client.executeListBranch(ctx, owner, repository)
return ghResponse, err
})
return
}
func (client *GitHubClient) executeListBranch(ctx context.Context, owner, repository string) ([]string, *github.Response, error) {
branches, ghResponse, err := client.ghClient.Repositories.ListBranches(ctx, owner, repository, nil)
if err != nil {
return []string{}, ghResponse, err
}
branchList := make([]string, 0, len(branches))
for _, branch := range branches {
branchList = append(branchList, *branch.Name)
}
return branchList, ghResponse, nil
}
// CreateWebhook on GitHub
func (client *GitHubClient) CreateWebhook(ctx context.Context, owner, repository, _, payloadURL string,
webhookEvents ...vcsutils.WebhookEvent) (string, string, error) {
token := vcsutils.CreateToken()
hook := createGitHubHook(token, payloadURL, webhookEvents...)
var ghResponseHook *github.Hook
var err error
if err = client.runWithRateLimitRetries(func() (*github.Response, error) {
var ghResponse *github.Response
ghResponseHook, ghResponse, err = client.ghClient.Repositories.CreateHook(ctx, owner, repository, hook)
return ghResponse, err
}); err != nil {
return "", "", err
}
return strconv.FormatInt(*ghResponseHook.ID, 10), token, nil
}
// UpdateWebhook on GitHub
func (client *GitHubClient) UpdateWebhook(ctx context.Context, owner, repository, _, payloadURL, token,
webhookID string, webhookEvents ...vcsutils.WebhookEvent) error {
webhookIDInt64, err := strconv.ParseInt(webhookID, 10, 64)
if err != nil {
return err
}
hook := createGitHubHook(token, payloadURL, webhookEvents...)
return client.runWithRateLimitRetries(func() (*github.Response, error) {
var ghResponse *github.Response
_, ghResponse, err = client.ghClient.Repositories.EditHook(ctx, owner, repository, webhookIDInt64, hook)
return ghResponse, err
})
}
// DeleteWebhook on GitHub
func (client *GitHubClient) DeleteWebhook(ctx context.Context, owner, repository, webhookID string) error {
webhookIDInt64, err := strconv.ParseInt(webhookID, 10, 64)
if err != nil {
return err
}
return client.runWithRateLimitRetries(func() (*github.Response, error) {
return client.ghClient.Repositories.DeleteHook(ctx, owner, repository, webhookIDInt64)
})
}
// SetCommitStatus on GitHub
func (client *GitHubClient) SetCommitStatus(ctx context.Context, commitStatus CommitStatus, owner, repository, ref,
title, description, detailsURL string) error {
state := getGitHubCommitState(commitStatus)
status := &github.RepoStatus{
Context: &title,
TargetURL: &detailsURL,
State: &state,
Description: &description,
}
return client.runWithRateLimitRetries(func() (*github.Response, error) {
_, ghResponse, err := client.ghClient.Repositories.CreateStatus(ctx, owner, repository, ref, status)
return ghResponse, err
})
}
// GetCommitStatuses on GitHub
func (client *GitHubClient) GetCommitStatuses(ctx context.Context, owner, repository, ref string) (statusInfoList []CommitStatusInfo, err error) {
err = client.runWithRateLimitRetries(func() (*github.Response, error) {
var ghResponse *github.Response
statusInfoList, ghResponse, err = client.executeGetCommitStatuses(ctx, owner, repository, ref)
return ghResponse, err
})
return
}
func (client *GitHubClient) executeGetCommitStatuses(ctx context.Context, owner, repository, ref string) (statusInfoList []CommitStatusInfo, ghResponse *github.Response, err error) {
statuses, ghResponse, err := client.ghClient.Repositories.GetCombinedStatus(ctx, owner, repository, ref, nil)
if err != nil {
return
}
for _, singleStatus := range statuses.Statuses {
statusInfoList = append(statusInfoList, CommitStatusInfo{
State: commitStatusAsStringToStatus(*singleStatus.State),
Description: singleStatus.GetDescription(),
DetailsUrl: singleStatus.GetTargetURL(),
Creator: singleStatus.GetCreator().GetName(),
LastUpdatedAt: singleStatus.GetUpdatedAt().Time,
CreatedAt: singleStatus.GetCreatedAt().Time,
})
}
return
}
// DownloadRepository on GitHub
func (client *GitHubClient) DownloadRepository(ctx context.Context, owner, repository, branch, localPath string) (err error) {
// Get the archive download link from GitHub
var baseURL *url.URL
err = client.runWithRateLimitRetries(func() (*github.Response, error) {
var ghResponse *github.Response
baseURL, ghResponse, err = client.executeGetArchiveLink(ctx, owner, repository, branch)
return ghResponse, err
})
if err != nil {
return
}
// Download the archive
httpResponse, err := executeDownloadArchiveFromLink(baseURL.String())
if err != nil {
return
}
defer func() { err = errors.Join(err, httpResponse.Body.Close()) }()
client.logger.Info(repository, vcsutils.SuccessfulRepoDownload)
// Untar the archive
if err = vcsutils.Untar(localPath, httpResponse.Body, true); err != nil {
return
}
client.logger.Info(vcsutils.SuccessfulRepoExtraction)
repositoryInfo, err := client.GetRepositoryInfo(ctx, owner, repository)
if err != nil {
return
}
// Create a .git folder in the archive with the remote repository HTTP clone url
err = vcsutils.CreateDotGitFolderWithRemote(localPath, vcsutils.RemoteName, repositoryInfo.CloneInfo.HTTP)
return
}
func (client *GitHubClient) executeGetArchiveLink(ctx context.Context, owner, repository, branch string) (baseURL *url.URL, ghResponse *github.Response, err error) {
client.logger.Debug("Getting GitHub archive link to download")
return client.ghClient.Repositories.GetArchiveLink(ctx, owner, repository, github.Tarball,
&github.RepositoryContentGetOptions{Ref: branch}, 5)
}
func executeDownloadArchiveFromLink(baseURL string) (*http.Response, error) {
httpClient := &http.Client{}
req, err := http.NewRequest(http.MethodGet, baseURL, nil)
if err != nil {
return nil, err
}
httpResponse, err := httpClient.Do(req)
if err != nil {
return httpResponse, err
}
return httpResponse, vcsutils.CheckResponseStatusWithBody(httpResponse, http.StatusOK)
}
func (client *GitHubClient) GetPullRequestCommentSizeLimit() int {
return githubPrContentSizeLimit
}
func (client *GitHubClient) GetPullRequestDetailsSizeLimit() int {
return githubPrContentSizeLimit
}
// CreatePullRequest on GitHub
func (client *GitHubClient) CreatePullRequest(ctx context.Context, owner, repository, sourceBranch, targetBranch, title, description string) error {
return client.runWithRateLimitRetries(func() (*github.Response, error) {
return client.executeCreatePullRequest(ctx, owner, repository, sourceBranch, targetBranch, title, description)
})
}
func (client *GitHubClient) executeCreatePullRequest(ctx context.Context, owner, repository, sourceBranch, targetBranch, title, description string) (*github.Response, error) {
head := owner + ":" + sourceBranch
client.logger.Debug(vcsutils.CreatingPullRequest, title)
_, ghResponse, err := client.ghClient.PullRequests.Create(ctx, owner, repository, &github.NewPullRequest{
Title: &title,
Body: &description,
Head: &head,
Base: &targetBranch,
})
return ghResponse, err
}
// UpdatePullRequest on GitHub
func (client *GitHubClient) UpdatePullRequest(ctx context.Context, owner, repository, title, body, targetBranchName string, id int, state vcsutils.PullRequestState) error {
client.logger.Debug(vcsutils.UpdatingPullRequest, id)
var baseRef *github.PullRequestBranch
if targetBranchName != "" {
baseRef = &github.PullRequestBranch{Ref: &targetBranchName}
}
pullRequest := &github.PullRequest{
Body: &body,
Title: &title,
State: vcsutils.MapPullRequestState(&state),
Base: baseRef,
}
return client.runWithRateLimitRetries(func() (*github.Response, error) {
_, ghResponse, err := client.ghClient.PullRequests.Edit(ctx, owner, repository, id, pullRequest)
return ghResponse, err
})
}
// ListOpenPullRequestsWithBody on GitHub
func (client *GitHubClient) ListOpenPullRequestsWithBody(ctx context.Context, owner, repository string) ([]PullRequestInfo, error) {
return client.getOpenPullRequests(ctx, owner, repository, true)
}
// ListOpenPullRequests on GitHub
func (client *GitHubClient) ListOpenPullRequests(ctx context.Context, owner, repository string) ([]PullRequestInfo, error) {
return client.getOpenPullRequests(ctx, owner, repository, false)
}
func (client *GitHubClient) getOpenPullRequests(ctx context.Context, owner, repository string, withBody bool) ([]PullRequestInfo, error) {
var pullRequests []*github.PullRequest
client.logger.Debug(vcsutils.FetchingOpenPullRequests, repository)
err := client.runWithRateLimitRetries(func() (*github.Response, error) {
var ghResponse *github.Response
var err error
pullRequests, ghResponse, err = client.ghClient.PullRequests.List(ctx, owner, repository, &github.PullRequestListOptions{State: "open"})
return ghResponse, err
})
if err != nil {
return []PullRequestInfo{}, err
}
return mapGitHubPullRequestToPullRequestInfoList(pullRequests, withBody)
}
func (client *GitHubClient) GetPullRequestByID(ctx context.Context, owner, repository string, pullRequestId int) (PullRequestInfo, error) {
var pullRequest *github.PullRequest
var ghResponse *github.Response
var err error
client.logger.Debug(vcsutils.FetchingPullRequestById, repository)
err = client.runWithRateLimitRetries(func() (*github.Response, error) {
pullRequest, ghResponse, err = client.ghClient.PullRequests.Get(ctx, owner, repository, pullRequestId)
return ghResponse, err
})
if err != nil {
return PullRequestInfo{}, err
}
if err = vcsutils.CheckResponseStatusWithBody(ghResponse.Response, http.StatusOK); err != nil {
return PullRequestInfo{}, err
}
return mapGitHubPullRequestToPullRequestInfo(pullRequest, false)
}
func mapGitHubPullRequestToPullRequestInfo(ghPullRequest *github.PullRequest, withBody bool) (PullRequestInfo, error) {
var sourceBranch, targetBranch string
var err1, err2 error
if ghPullRequest != nil && ghPullRequest.Head != nil && ghPullRequest.Base != nil {
sourceBranch, err1 = extractBranchFromLabel(vcsutils.DefaultIfNotNil(ghPullRequest.Head.Label))
targetBranch, err2 = extractBranchFromLabel(vcsutils.DefaultIfNotNil(ghPullRequest.Base.Label))
err := errors.Join(err1, err2)
if err != nil {
return PullRequestInfo{}, err
}
}
var sourceRepoName, sourceRepoOwner string
if ghPullRequest.Head.Repo == nil {
return PullRequestInfo{}, errors.New("the source repository information is missing when fetching the pull request details")
}
if ghPullRequest.Head.Repo.Owner == nil {
return PullRequestInfo{}, errors.New("the source repository owner name is missing when fetching the pull request details")
}
sourceRepoName = vcsutils.DefaultIfNotNil(ghPullRequest.Head.Repo.Name)
sourceRepoOwner = vcsutils.DefaultIfNotNil(ghPullRequest.Head.Repo.Owner.Login)
var targetRepoName, targetRepoOwner string
if ghPullRequest.Base.Repo == nil {
return PullRequestInfo{}, errors.New("the target repository information is missing when fetching the pull request details")
}
if ghPullRequest.Base.Repo.Owner == nil {
return PullRequestInfo{}, errors.New("the target repository owner name is missing when fetching the pull request details")
}
targetRepoName = vcsutils.DefaultIfNotNil(ghPullRequest.Base.Repo.Name)
targetRepoOwner = vcsutils.DefaultIfNotNil(ghPullRequest.Base.Repo.Owner.Login)
var body string
if withBody {
body = vcsutils.DefaultIfNotNil(ghPullRequest.Body)
}
return PullRequestInfo{
ID: int64(vcsutils.DefaultIfNotNil(ghPullRequest.Number)),
URL: vcsutils.DefaultIfNotNil(ghPullRequest.HTMLURL),
Body: body,
Source: BranchInfo{
Name: sourceBranch,
Repository: sourceRepoName,
Owner: sourceRepoOwner,
},
Target: BranchInfo{
Name: targetBranch,
Repository: targetRepoName,
Owner: targetRepoOwner,
},
}, nil
}
// Extracts branch name from the following expected label format repo:branch
func extractBranchFromLabel(label string) (string, error) {
split := strings.Split(label, ":")
if len(split) <= 1 {
return "", fmt.Errorf("bad label format %s", label)
}
return split[1], nil
}
// AddPullRequestComment on GitHub
func (client *GitHubClient) AddPullRequestComment(ctx context.Context, owner, repository, content string, pullRequestID int) error {
err := validateParametersNotBlank(map[string]string{"owner": owner, "repository": repository, "content": content})
if err != nil {
return err
}
return client.runWithRateLimitRetries(func() (*github.Response, error) {
var ghResponse *github.Response
// We use the Issues API to add a regular comment. The PullRequests API adds a code review comment.
_, ghResponse, err = client.ghClient.Issues.CreateComment(ctx, owner, repository, pullRequestID, &github.IssueComment{Body: &content})
return ghResponse, err
})
}
// AddPullRequestReviewComments on GitHub
func (client *GitHubClient) AddPullRequestReviewComments(ctx context.Context, owner, repository string, pullRequestID int, comments ...PullRequestComment) error {
prID := strconv.Itoa(pullRequestID)
err := validateParametersNotBlank(map[string]string{"owner": owner, "repository": repository, "pullRequestID": prID})
if err != nil {
return err
}
if len(comments) == 0 {
return errors.New(vcsutils.ErrNoCommentsProvided)
}
var commits []*github.RepositoryCommit
var ghResponse *github.Response
err = client.runWithRateLimitRetries(func() (*github.Response, error) {
commits, ghResponse, err = client.ghClient.PullRequests.ListCommits(ctx, owner, repository, pullRequestID, nil)
return ghResponse, err
})
if err != nil {
return err
}
if len(commits) == 0 {
return errors.New("could not fetch the commits list for pull request " + prID)
}
latestCommitSHA := commits[len(commits)-1].GetSHA()
for _, comment := range comments {
err = client.runWithRateLimitRetries(func() (*github.Response, error) {
ghResponse, err = client.executeCreatePullRequestReviewComment(ctx, owner, repository, latestCommitSHA, pullRequestID, comment)
return ghResponse, err
})
if err != nil {
return err
}
}
return nil
}
func (client *GitHubClient) executeCreatePullRequestReviewComment(ctx context.Context, owner, repository, latestCommitSHA string, pullRequestID int, comment PullRequestComment) (*github.Response, error) {
filePath := filepath.Clean(comment.NewFilePath)
startLine := &comment.NewStartLine
// GitHub API won't accept 'start_line' if it equals the end line
if *startLine == comment.NewEndLine {
startLine = nil
}
_, ghResponse, err := client.ghClient.PullRequests.CreateComment(ctx, owner, repository, pullRequestID, &github.PullRequestComment{
CommitID: &latestCommitSHA,
Body: &comment.Content,
StartLine: startLine,
Line: &comment.NewEndLine,
Path: &filePath,
})
if err != nil {
err = fmt.Errorf("could not create a code review comment for <%s/%s> in pull request %d. error received: %w",
owner, repository, pullRequestID, err)
}
return ghResponse, err
}
// ListPullRequestReviewComments on GitHub
func (client *GitHubClient) ListPullRequestReviewComments(ctx context.Context, owner, repository string, pullRequestID int) ([]CommentInfo, error) {
err := validateParametersNotBlank(map[string]string{"owner": owner, "repository": repository})
if err != nil {
return nil, err
}
commentsInfoList := []CommentInfo{}
err = client.runWithRateLimitRetries(func() (*github.Response, error) {
var ghResponse *github.Response
commentsInfoList, ghResponse, err = client.executeListPullRequestReviewComments(ctx, owner, repository, pullRequestID)
return ghResponse, err
})
return commentsInfoList, err
}
func (client *GitHubClient) executeListPullRequestReviewComments(ctx context.Context, owner, repository string, pullRequestID int) ([]CommentInfo, *github.Response, error) {
commentsList, ghResponse, err := client.ghClient.PullRequests.ListComments(ctx, owner, repository, pullRequestID, nil)
if err != nil {
return []CommentInfo{}, ghResponse, err
}
commentsInfoList := []CommentInfo{}
for _, comment := range commentsList {
commentsInfoList = append(commentsInfoList, CommentInfo{
ID: comment.GetID(),
Content: comment.GetBody(),
Created: comment.GetCreatedAt().Time,
})
}
return commentsInfoList, ghResponse, nil
}
// ListPullRequestComments on GitHub
func (client *GitHubClient) ListPullRequestComments(ctx context.Context, owner, repository string, pullRequestID int) ([]CommentInfo, error) {
err := validateParametersNotBlank(map[string]string{"owner": owner, "repository": repository})
if err != nil {
return []CommentInfo{}, err
}
var commentsList []*github.IssueComment
err = client.runWithRateLimitRetries(func() (*github.Response, error) {
var ghResponse *github.Response
commentsList, ghResponse, err = client.ghClient.Issues.ListComments(ctx, owner, repository, pullRequestID, &github.IssueListCommentsOptions{})
return ghResponse, err
})
if err != nil {
return []CommentInfo{}, err
}
return mapGitHubIssuesCommentToCommentInfoList(commentsList)
}
// DeletePullRequestReviewComments on GitHub
func (client *GitHubClient) DeletePullRequestReviewComments(ctx context.Context, owner, repository string, _ int, comments ...CommentInfo) error {
for _, comment := range comments {
commentID := comment.ID
err := validateParametersNotBlank(map[string]string{"owner": owner, "repository": repository, "commentID": strconv.FormatInt(commentID, 10)})
if err != nil {
return err
}
err = client.runWithRateLimitRetries(func() (*github.Response, error) {
return client.executeDeletePullRequestReviewComment(ctx, owner, repository, commentID)
})
if err != nil {
return err
}
}
return nil
}
func (client *GitHubClient) executeDeletePullRequestReviewComment(ctx context.Context, owner, repository string, commentID int64) (*github.Response, error) {
ghResponse, err := client.ghClient.PullRequests.DeleteComment(ctx, owner, repository, commentID)
if err != nil {
err = fmt.Errorf("could not delete pull request review comment: %w", err)
}
return ghResponse, err
}
// DeletePullRequestComment on GitHub
func (client *GitHubClient) DeletePullRequestComment(ctx context.Context, owner, repository string, _, commentID int) error {
err := validateParametersNotBlank(map[string]string{"owner": owner, "repository": repository})
if err != nil {
return err
}
return client.runWithRateLimitRetries(func() (*github.Response, error) {
return client.executeDeletePullRequestComment(ctx, owner, repository, commentID)
})
}
func (client *GitHubClient) executeDeletePullRequestComment(ctx context.Context, owner, repository string, commentID int) (*github.Response, error) {
ghResponse, err := client.ghClient.Issues.DeleteComment(ctx, owner, repository, int64(commentID))
if err != nil {
return ghResponse, err
}
var statusCode int
if ghResponse.Response != nil {
statusCode = ghResponse.Response.StatusCode
}
if statusCode != http.StatusNoContent && statusCode != http.StatusOK {
return ghResponse, fmt.Errorf("expected %d status code while received %d status code", http.StatusNoContent, ghResponse.Response.StatusCode)
}
return ghResponse, nil
}
// GetLatestCommit on GitHub
func (client *GitHubClient) GetLatestCommit(ctx context.Context, owner, repository, branch string) (CommitInfo, error) {
commits, err := client.GetCommits(ctx, owner, repository, branch)
if err != nil {
return CommitInfo{}, err
}
latestCommit := CommitInfo{}
if len(commits) > 0 {
latestCommit = commits[0]
}
return latestCommit, nil
}
// GetCommits on GitHub
func (client *GitHubClient) GetCommits(ctx context.Context, owner, repository, branch string) ([]CommitInfo, error) {
err := validateParametersNotBlank(map[string]string{
"owner": owner,
"repository": repository,
"branch": branch,
})
if err != nil {
return nil, err
}
var commitsInfo []CommitInfo
err = client.runWithRateLimitRetries(func() (*github.Response, error) {
var ghResponse *github.Response
commitsInfo, ghResponse, err = client.executeGetCommits(ctx, owner, repository, branch)
return ghResponse, err
})
return commitsInfo, err
}
func (client *GitHubClient) executeGetCommits(ctx context.Context, owner, repository, branch string) ([]CommitInfo, *github.Response, error) {
listOptions := &github.CommitsListOptions{
SHA: branch,
ListOptions: github.ListOptions{
Page: 1,
PerPage: vcsutils.NumberOfCommitsToFetch,
},
}
commits, ghResponse, err := client.ghClient.Repositories.ListCommits(ctx, owner, repository, listOptions)
if err != nil {
return nil, ghResponse, err
}
var commitsInfo []CommitInfo
for _, commit := range commits {
commitInfo := mapGitHubCommitToCommitInfo(commit)
commitsInfo = append(commitsInfo, commitInfo)
}
return commitsInfo, ghResponse, nil
}
// GetRepositoryInfo on GitHub
func (client *GitHubClient) GetRepositoryInfo(ctx context.Context, owner, repository string) (RepositoryInfo, error) {
err := validateParametersNotBlank(map[string]string{"owner": owner, "repository": repository})
if err != nil {
return RepositoryInfo{}, err
}
var repo *github.Repository
err = client.runWithRateLimitRetries(func() (*github.Response, error) {
var ghResponse *github.Response
repo, ghResponse, err = client.ghClient.Repositories.Get(ctx, owner, repository)
return ghResponse, err
})
if err != nil {
return RepositoryInfo{}, err
}
return RepositoryInfo{RepositoryVisibility: getGitHubRepositoryVisibility(repo), CloneInfo: CloneInfo{HTTP: repo.GetCloneURL(), SSH: repo.GetSSHURL()}}, nil
}
// GetCommitBySha on GitHub
func (client *GitHubClient) GetCommitBySha(ctx context.Context, owner, repository, sha string) (CommitInfo, error) {
err := validateParametersNotBlank(map[string]string{
"owner": owner,
"repository": repository,
"sha": sha,
})
if err != nil {
return CommitInfo{}, err
}
var commit *github.RepositoryCommit
err = client.runWithRateLimitRetries(func() (*github.Response, error) {
var ghResponse *github.Response
commit, ghResponse, err = client.ghClient.Repositories.GetCommit(ctx, owner, repository, sha, nil)
return ghResponse, err
})
if err != nil {
return CommitInfo{}, err
}
return mapGitHubCommitToCommitInfo(commit), nil
}
// CreateLabel on GitHub
func (client *GitHubClient) CreateLabel(ctx context.Context, owner, repository string, labelInfo LabelInfo) error {
err := validateParametersNotBlank(map[string]string{"owner": owner, "repository": repository, "LabelInfo.name": labelInfo.Name})
if err != nil {
return err
}
return client.runWithRateLimitRetries(func() (*github.Response, error) {
var ghResponse *github.Response
_, ghResponse, err = client.ghClient.Issues.CreateLabel(ctx, owner, repository, &github.Label{
Name: &labelInfo.Name,
Description: &labelInfo.Description,
Color: &labelInfo.Color,
})
return ghResponse, err
})
}
// GetLabel on GitHub
func (client *GitHubClient) GetLabel(ctx context.Context, owner, repository, name string) (*LabelInfo, error) {
err := validateParametersNotBlank(map[string]string{"owner": owner, "repository": repository, "name": name})
if err != nil {
return nil, err
}
var labelInfo *LabelInfo
err = client.runWithRateLimitRetries(func() (*github.Response, error) {
var ghResponse *github.Response
labelInfo, ghResponse, err = client.executeGetLabel(ctx, owner, repository, name)
return ghResponse, err
})
return labelInfo, err
}
func (client *GitHubClient) executeGetLabel(ctx context.Context, owner, repository, name string) (*LabelInfo, *github.Response, error) {
label, ghResponse, err := client.ghClient.Issues.GetLabel(ctx, owner, repository, name)
if err != nil {
if ghResponse != nil && ghResponse.Response != nil && ghResponse.Response.StatusCode == http.StatusNotFound {
return nil, ghResponse, nil
}
return nil, ghResponse, err
}
labelInfo := &LabelInfo{
Name: *label.Name,
Description: *label.Description,
Color: *label.Color,
}
return labelInfo, ghResponse, nil
}
// ListPullRequestLabels on GitHub
func (client *GitHubClient) ListPullRequestLabels(ctx context.Context, owner, repository string, pullRequestID int) ([]string, error) {
err := validateParametersNotBlank(map[string]string{"owner": owner, "repository": repository})
if err != nil {
return nil, err
}
results := []string{}
for nextPage := 0; ; nextPage++ {
options := &github.ListOptions{Page: nextPage}
var labels []*github.Label
var ghResponse *github.Response
err = client.runWithRateLimitRetries(func() (*github.Response, error) {
labels, ghResponse, err = client.ghClient.Issues.ListLabelsByIssue(ctx, owner, repository, pullRequestID, options)
return ghResponse, err
})
if err != nil {
return nil, err
}
for _, label := range labels {
results = append(results, *label.Name)
}
if nextPage+1 >= ghResponse.LastPage {
break
}
}
return results, nil
}
// UnlabelPullRequest on GitHub
func (client *GitHubClient) UnlabelPullRequest(ctx context.Context, owner, repository, name string, pullRequestID int) error {
err := validateParametersNotBlank(map[string]string{"owner": owner, "repository": repository})
if err != nil {
return err
}
return client.runWithRateLimitRetries(func() (*github.Response, error) {
return client.ghClient.Issues.RemoveLabelForIssue(ctx, owner, repository, pullRequestID, name)
})
}
// UploadCodeScanning to GitHub Security tab
func (client *GitHubClient) UploadCodeScanning(ctx context.Context, owner, repository, branch, sarifContent string) (id string, err error) {
commit, err := client.GetLatestCommit(ctx, owner, repository, branch)
if err != nil {
return
}
commitSHA := commit.Hash
branch = vcsutils.AddBranchPrefix(branch)
client.logger.Debug(vcsutils.UploadingCodeScanning, repository, "/", branch)
err = client.runWithRateLimitRetries(func() (*github.Response, error) {
var ghResponse *github.Response
id, ghResponse, err = client.executeUploadCodeScanning(ctx, owner, repository, branch, commitSHA, sarifContent)
return ghResponse, err
})
return
}
func (client *GitHubClient) executeUploadCodeScanning(ctx context.Context, owner, repository, branch, commitSHA, sarifContent string) (id string, ghResponse *github.Response, err error) {
encodedSarif, err := encodeScanningResult(sarifContent)
if err != nil {
return
}
sarifID, ghResponse, err := client.ghClient.CodeScanning.UploadSarif(ctx, owner, repository, &github.SarifAnalysis{
CommitSHA: &commitSHA,
Ref: &branch,
Sarif: &encodedSarif,
})
// According to go-github API - successful ghResponse will return 202 status code
// The body of the ghResponse will appear in the error, and the Sarif struct will be empty.
if err != nil && ghResponse.Response.StatusCode != http.StatusAccepted {
return
}
id, err = handleGitHubUploadSarifID(sarifID, err)
return
}
func handleGitHubUploadSarifID(sarifID *github.SarifID, uploadSarifErr error) (id string, err error) {
if sarifID != nil && *sarifID.ID != "" {
id = *sarifID.ID
return
}
var result map[string]string
var ghAcceptedError *github.AcceptedError
if errors.As(uploadSarifErr, &ghAcceptedError) {
if err = json.Unmarshal(ghAcceptedError.Raw, &result); err != nil {
return
}
id = result["id"]
}
return
}
// DownloadFileFromRepo on GitHub
func (client *GitHubClient) DownloadFileFromRepo(ctx context.Context, owner, repository, branch, path string) (content []byte, statusCode int, err error) {
err = client.runWithRateLimitRetries(func() (*github.Response, error) {
var ghResponse *github.Response
content, statusCode, ghResponse, err = client.executeDownloadFileFromRepo(ctx, owner, repository, branch, path)
return ghResponse, err
})
return
}
func (client *GitHubClient) executeDownloadFileFromRepo(ctx context.Context, owner, repository, branch, path string) (content []byte, statusCode int, ghResponse *github.Response, err error) {
body, ghResponse, err := client.ghClient.Repositories.DownloadContents(ctx, owner, repository, path, &github.RepositoryContentGetOptions{Ref: branch})
defer func() {
if body != nil {
err = errors.Join(err, body.Close())
}
}()
if ghResponse == nil || ghResponse.Response == nil {
return
}
statusCode = ghResponse.StatusCode
if err != nil && statusCode != http.StatusOK {
err = fmt.Errorf("expected %d status code while received %d status code with error:\n%s", http.StatusOK, ghResponse.StatusCode, err)
return
}
if body != nil {
content, err = io.ReadAll(body)
}
return
}
// GetRepositoryEnvironmentInfo on GitHub
func (client *GitHubClient) GetRepositoryEnvironmentInfo(ctx context.Context, owner, repository, name string) (RepositoryEnvironmentInfo, error) {
err := validateParametersNotBlank(map[string]string{"owner": owner, "repository": repository, "name": name})
if err != nil {
return RepositoryEnvironmentInfo{}, err
}
var repositoryEnvInfo *RepositoryEnvironmentInfo
err = client.runWithRateLimitRetries(func() (*github.Response, error) {
var ghResponse *github.Response
repositoryEnvInfo, ghResponse, err = client.executeGetRepositoryEnvironmentInfo(ctx, owner, repository, name)
return ghResponse, err
})
return *repositoryEnvInfo, err
}
func (client *GitHubClient) executeGetRepositoryEnvironmentInfo(ctx context.Context, owner, repository, name string) (*RepositoryEnvironmentInfo, *github.Response, error) {
environment, ghResponse, err := client.ghClient.Repositories.GetEnvironment(ctx, owner, repository, name)
if err != nil {
return &RepositoryEnvironmentInfo{}, ghResponse, err
}
if err = vcsutils.CheckResponseStatusWithBody(ghResponse.Response, http.StatusOK); err != nil {
return &RepositoryEnvironmentInfo{}, ghResponse, err
}
reviewers, err := extractGitHubEnvironmentReviewers(environment)
if err != nil {
return &RepositoryEnvironmentInfo{}, ghResponse, err
}
return &RepositoryEnvironmentInfo{
Name: environment.GetName(),
Url: environment.GetURL(),
Reviewers: reviewers,
},
ghResponse,
nil
}
func (client *GitHubClient) GetModifiedFiles(ctx context.Context, owner, repository, refBefore, refAfter string) ([]string, error) {
err := validateParametersNotBlank(map[string]string{
"owner": owner,
"repository": repository,
"refBefore": refBefore,
"refAfter": refAfter,
})
if err != nil {
return nil, err
}
var fileNamesList []string
err = client.runWithRateLimitRetries(func() (*github.Response, error) {