-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathgithub.go
More file actions
1182 lines (1048 loc) · 37.5 KB
/
github.go
File metadata and controls
1182 lines (1048 loc) · 37.5 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
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 github
import (
"context"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"fmt"
"math/rand"
"net/http"
"net/url"
"regexp"
"strings"
"sync"
"time"
"github.com/gobwas/glob"
"github.com/golang-jwt/jwt/v4"
"github.com/google/go-github/v84/github"
"github.com/jonboulle/clockwork"
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/keys"
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/v1alpha1"
"github.com/openshift-pipelines/pipelines-as-code/pkg/changedfiles"
"github.com/openshift-pipelines/pipelines-as-code/pkg/events"
"github.com/openshift-pipelines/pipelines-as-code/pkg/params"
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/triggertype"
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider"
"go.uber.org/zap"
"golang.org/x/oauth2"
"k8s.io/client-go/kubernetes"
)
const (
apiPublicURL = "https://api.github.com/"
// TODO: makes this configurable for GHE in the ConfigMap.
// on our GHE instance, it looks like this :
// https://raw.ghe.openshiftpipelines.com/pac/chmouel-test/main/README.md
// we can perhaps do some autodetection with event.Provider.GHEURL and adding
// a raw into it.
publicRawURLHost = "raw.githubusercontent.com"
defaultPaginedNumber = 100
// maxCommentPages caps the number of pages fetched when scanning PR
// comments (e.g. for /ok-to-test). With defaultPaginedNumber=100 this
// allows up to 1000 comments, which is generous for legitimate use while
// preventing rate-limit exhaustion from comment flooding.
maxCommentPages = 10
)
var _ provider.Interface = (*Provider)(nil)
type Provider struct {
ghClient *github.Client
Logger *zap.SugaredLogger
Run *params.Run
pacInfo *info.PacOpts
Token, APIURL *string
ApplicationID *int64
providerName string
provenance string
RepositoryIDs []int64
repo *v1alpha1.Repository
eventEmitter *events.EventEmitter
PaginedNumber int
userType string // The type of user i.e bot or not
skippedRun
triggerEvent string
cachedChangedFiles *changedfiles.ChangedFiles
commitInfo *github.Commit
cachedPullRequest *github.PullRequest
pacUserLogin string // user/bot login used by PAC
clock clockwork.Clock
graphQLClient *graphQLClient
checkRunsCache checkRunsCache
}
type skippedRun struct {
mutex *sync.Mutex
checkRunID int64
}
type checkRunsCache struct {
mu sync.Mutex
entries map[string]*checkRunsCacheEntry
}
type checkRunsCacheEntry struct {
runs []*github.CheckRun
loading bool
done chan struct{}
}
func New() *Provider {
return &Provider{
APIURL: github.Ptr(keys.PublicGithubAPIURL),
PaginedNumber: defaultPaginedNumber,
skippedRun: skippedRun{
mutex: &sync.Mutex{},
},
clock: clockwork.NewRealClock(),
checkRunsCache: checkRunsCache{
entries: map[string]*checkRunsCacheEntry{},
},
}
}
func (v *Provider) getClock() clockwork.Clock {
if v.clock == nil {
return clockwork.NewRealClock()
}
return v.clock
}
func (v *Provider) Client() *github.Client {
return v.ghClient
}
func (v *Provider) SetGithubClient(client *github.Client) {
v.ghClient = client
}
func (v *Provider) SetPacInfo(pacInfo *info.PacOpts) {
v.pacInfo = pacInfo
}
// detectGHERawURL Detect if we have a raw URL in GHE.
func detectGHERawURL(event *info.Event, taskHost string) bool {
gheURL, err := url.Parse(event.GHEURL)
if err != nil {
// should not happen but may as well make sure
return false
}
return taskHost == fmt.Sprintf("raw.%s", gheURL.Host)
}
// splitGithubURL Take a Github url and split it with org/repo path ref, supports rawURL.
func splitGithubURL(event *info.Event, uri string) (string, string, string, string, error) {
pURL, err := url.Parse(uri)
if err != nil {
return "", "", "", "", fmt.Errorf("URL %s is not a valid provider URL: %w", uri, err)
}
path := pURL.Path
if pURL.RawPath != "" {
path = pURL.RawPath
}
split := strings.Split(path, "/")
if len(split) <= 3 {
return "", "", "", "", fmt.Errorf("URL %s does not seem to be a proper provider url: %w", uri, err)
}
var spOrg, spRepo, spRef, spPath string
switch {
case (pURL.Host == publicRawURLHost || detectGHERawURL(event, pURL.Host)) && len(split) >= 5:
spOrg = split[1]
spRepo = split[2]
spRef = split[3]
spPath = strings.Join(split[4:], "/")
case split[3] == "blob" && len(split) >= 5:
spOrg = split[1]
spRepo = split[2]
spRef = split[4]
spPath = strings.Join(split[5:], "/")
default:
return "", "", "", "", fmt.Errorf("cannot recognize task as a GitHub URL to fetch: %s", uri)
}
// url decode the org, repo, ref and path
if spRef, err = url.QueryUnescape(spRef); err != nil {
return "", "", "", "", fmt.Errorf("cannot decode ref: %w", err)
}
if spPath, err = url.QueryUnescape(spPath); err != nil {
return "", "", "", "", fmt.Errorf("cannot decode path: %w", err)
}
if spOrg, err = url.QueryUnescape(spOrg); err != nil {
return "", "", "", "", fmt.Errorf("cannot decode org: %w", err)
}
if spRepo, err = url.QueryUnescape(spRepo); err != nil {
return "", "", "", "", fmt.Errorf("cannot decode repo: %w", err)
}
return spOrg, spRepo, spPath, spRef, nil
}
func (v *Provider) GetTaskURI(ctx context.Context, event *info.Event, uri string) (bool, string, error) {
if ret := provider.CompareHostOfURLS(uri, event.URL); !ret {
return false, "", nil
}
spOrg, spRepo, spPath, spRef, err := splitGithubURL(event, uri)
if err != nil {
return false, "", err
}
nEvent := info.NewEvent()
nEvent.Organization = spOrg
nEvent.Repository = spRepo
nEvent.BaseBranch = spRef
ret, err := v.GetFileInsideRepo(ctx, nEvent, spPath, spRef)
if err != nil {
return false, "", err
}
return true, ret, nil
}
func (v *Provider) InitAppClient(ctx context.Context, kube kubernetes.Interface, event *info.Event) error {
var err error
// TODO: move this out of here when we move al config inside context
ns := info.GetNS(ctx)
event.Provider.Token, err = v.GetAppToken(ctx, kube, event.GHEURL, event.InstallationID, ns)
if err != nil {
return err
}
return nil
}
func (v *Provider) SetLogger(logger *zap.SugaredLogger) {
v.Logger = logger
}
func (v *Provider) Validate(_ context.Context, _ *params.Run, event *info.Event) error {
signature := event.Request.Header.Get(github.SHA256SignatureHeader)
if signature == "" {
signature = event.Request.Header.Get(github.SHA1SignatureHeader)
}
if signature == "" || signature == "sha1=" {
// if no signature is present then don't validate, because user hasn't set one
return fmt.Errorf("no signature has been detected, for security reason we are not allowing webhooks that has no secret")
}
if event.Provider.WebhookSecret == "" {
return fmt.Errorf("no webhook secret has been set, in repository CR or secret")
}
return github.ValidateSignature(signature, event.Request.Payload, []byte(event.Provider.WebhookSecret))
}
func (v *Provider) GetConfig() *info.ProviderConfig {
return &info.ProviderConfig{
TaskStatusTMPL: taskStatusTemplate,
APIURL: apiPublicURL,
Name: v.providerName,
}
}
func MakeClient(ctx context.Context, apiURL, token string) (*github.Client, string, *string) {
var client *github.Client
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
if apiURL != "" {
if !strings.HasPrefix(apiURL, "https") && !strings.HasPrefix(apiURL, "http") {
apiURL = "https://" + apiURL
}
}
providerName := "github"
if apiURL != "" && apiURL != apiPublicURL {
providerName = "github-enterprise"
uploadURL := apiURL + "/api/uploads"
client, _ = github.NewClient(tc).WithEnterpriseURLs(apiURL, uploadURL)
} else {
client = github.NewClient(tc)
apiURL = client.BaseURL.String()
}
return client, providerName, github.Ptr(apiURL)
}
func parseTS(headerTS string) (time.Time, error) {
ts := time.Time{}
// Normal UTC: 2023-01-31 23:00:00 UTC
if t, err := time.Parse("2006-01-02 15:04:05 MST", headerTS); err == nil {
ts = t
}
// With TZ(???), ie: a token from Christoph 2023-04-26 23:23:26 +2000
if t, err := time.Parse("2006-01-02 15:04:05 -0700", headerTS); err == nil {
ts = t
}
if ts.Year() == 1 {
return ts, fmt.Errorf("cannot parse token expiration date: %s", headerTS)
}
return ts, nil
}
// checkWebhookSecretValidity check the webhook secret is valid and not
// ratelimited. we try to check first the header is set (unlimited life token would
// not have an expiration) we would anyway get a 401 error when trying to use it
// but this gives a nice hint to the user into their namespace event of where
// the issue was.
func (v *Provider) checkWebhookSecretValidity(ctx context.Context, cw clockwork.Clock) error {
rl, resp, err := wrapAPI(v, "check_rate_limit", func() (*github.RateLimits, *github.Response, error) {
return v.Client().RateLimit.Get(ctx)
})
if resp != nil && resp.StatusCode == http.StatusNotFound {
v.Logger.Info("skipping checking if token has expired, rate_limit api is not enabled on token")
return nil
}
if err != nil {
return fmt.Errorf("error making request to the GitHub API checking rate limit: %w", err)
}
if resp != nil && resp.Header.Get("GitHub-Authentication-Token-Expiration") != "" {
ts, err := parseTS(resp.Header.Get("GitHub-Authentication-Token-Expiration"))
if err != nil {
return fmt.Errorf("error parsing token expiration date: %w", err)
}
if cw.Now().After(ts) {
errMsg := fmt.Sprintf("token has expired at %s", resp.TokenExpiration.Format(time.RFC1123))
return fmt.Errorf("%s", errMsg)
}
}
// Guard against nil rl or rl.SCIM which could lead to a panic.
if rl == nil || rl.SCIM == nil {
v.Logger.Info("skipping token expiration check, SCIM rate limit API is not available for this token")
return nil
}
if rl.SCIM.Remaining == 0 {
return fmt.Errorf("api rate limit exceeded. Access will be restored at %s", rl.SCIM.Reset.Format(time.RFC1123))
}
return nil
}
func (v *Provider) SetClient(ctx context.Context, run *params.Run, event *info.Event, repo *v1alpha1.Repository, eventsEmitter *events.EventEmitter) error {
client, providerName, apiURL := MakeClient(ctx, event.Provider.URL, event.Provider.Token)
v.providerName = providerName
v.Run = run
v.repo = repo
v.eventEmitter = eventsEmitter
v.triggerEvent = event.EventType
// check that the Client is not already set, so we don't override our fakeclient
// from unittesting.
if v.ghClient == nil {
v.ghClient = client
}
if v.ghClient == nil {
return fmt.Errorf("no github client has been initialized")
}
// Added log for security audit purposes to log client access when a token is used
integration := "github-webhook"
if event.InstallationID != 0 {
integration = "github-app"
}
run.Clients.Log.Infof(integration+": initialized OAuth2 client for providerName=%s providerURL=%s", v.providerName, event.Provider.URL)
v.APIURL = apiURL
if event.Provider.WebhookSecretFromRepo {
// check the webhook secret is valid and not ratelimited
if err := v.checkWebhookSecretValidity(ctx, clockwork.NewRealClock()); err != nil {
return fmt.Errorf("the webhook secret is not valid: %w", err)
}
}
return nil
}
func (v *Provider) GetCommitStatuses(_ context.Context, _ *info.Event) ([]provider.CommitStatusInfo, error) {
return nil, nil
}
// GetTektonDir retrieves all YAML files from the .tekton directory and returns them as a single concatenated multi-document YAML file.
func (v *Provider) GetTektonDir(ctx context.Context, runevent *info.Event, path, provenance string) (string, error) {
tektonDirSha := ""
v.provenance = provenance
// default set provenance from the SHA
revision := runevent.SHA
if provenance == "default_branch" {
v.Logger.Infof("Using PipelineRun definition from default_branch: %s", runevent.DefaultBranch)
branch, _, err := wrapAPI(v, "get_default_branch", func() (*github.Branch, *github.Response, error) {
return v.Client().Repositories.GetBranch(ctx, runevent.Organization, runevent.Repository, runevent.DefaultBranch, 1)
})
if err != nil {
return "", err
}
revision = branch.GetCommit().GetSHA()
if revision == "" {
return "", fmt.Errorf("default_branch %s did not resolve to a commit SHA", runevent.DefaultBranch)
}
} else {
prInfo := ""
if runevent.TriggerTarget == triggertype.PullRequest {
prInfo = fmt.Sprintf("%s/%s#%d", runevent.Organization, runevent.Repository, runevent.PullRequestNumber)
}
v.Logger.Infof("Using PipelineRun definition from source %s %s on commit SHA %s", runevent.TriggerTarget.String(), prInfo, runevent.SHA)
}
rootobjects, _, err := wrapAPI(v, "get_root_tree", func() (*github.Tree, *github.Response, error) {
return v.Client().Git.GetTree(ctx, runevent.Organization, runevent.Repository, revision, false)
})
if err != nil {
return "", err
}
for _, object := range rootobjects.Entries {
if object.GetPath() == path {
if object.GetType() != "tree" {
return "", fmt.Errorf("%s has been found but is not a directory", path)
}
tektonDirSha = object.GetSHA()
}
}
// If we didn't find a .tekton directory then just silently ignore the error.
if tektonDirSha == "" {
return "", nil
}
// Get all files in the .tekton directory recursively
// there is a limit on this recursive calls to 500 entries, as documented here:
// https://docs.github.com/en/rest/reference/git#get-a-tree
// so we may need to address it in the future.
tektonDirObjects, _, err := wrapAPI(v, "get_tekton_tree", func() (*github.Tree, *github.Response, error) {
return v.Client().Git.GetTree(ctx, runevent.Organization, runevent.Repository, tektonDirSha,
true)
})
if err != nil {
return "", err
}
return v.concatAllYamlFiles(ctx, tektonDirObjects.Entries, runevent, path, revision)
}
// GetCommitInfo get info (url and title) on a commit in runevent, this needs to
// be run after sewebhook while we already matched a token.
func (v *Provider) GetCommitInfo(ctx context.Context, runevent *info.Event) error {
if v.ghClient == nil {
return fmt.Errorf("no github client has been initialized, " +
"exiting... (hint: did you forget setting a secret on your repo?)")
}
// if we don't have a sha we may have a branch (ie: incoming webhook) then
// use the branch as sha since github supports it
var commit *github.Commit
sha := runevent.SHA
if runevent.SHA == "" && runevent.HeadBranch != "" {
branchinfo, _, err := wrapAPI(v, "get_branch_info", func() (*github.Branch, *github.Response, error) {
return v.Client().Repositories.GetBranch(ctx, runevent.Organization, runevent.Repository, runevent.HeadBranch, 1)
})
if err != nil {
return err
}
sha = branchinfo.Commit.GetSHA()
}
var err error
// check if the commit info is already cached in provider
if v.commitInfo == nil {
commit, _, err = wrapAPI(v, "get_commit", func() (*github.Commit, *github.Response, error) {
return v.Client().Git.GetCommit(ctx, runevent.Organization, runevent.Repository, sha)
})
if err != nil {
return err
}
} else {
commit = v.commitInfo
}
runevent.SHAURL = commit.GetHTMLURL()
runevent.SHATitle = strings.Split(commit.GetMessage(), "\n\n")[0]
runevent.SHA = commit.GetSHA()
runevent.HasSkipCommand = provider.SkipCI(commit.GetMessage())
// Populate full commit information for LLM context
runevent.SHAMessage = commit.GetMessage()
if commit.Author != nil {
runevent.SHAAuthorName = commit.Author.GetName()
runevent.SHAAuthorEmail = commit.Author.GetEmail()
if commit.Author.Date != nil {
runevent.SHAAuthorDate = commit.Author.Date.Time
}
}
if commit.Committer != nil {
runevent.SHACommitterName = commit.Committer.GetName()
runevent.SHACommitterEmail = commit.Committer.GetEmail()
if commit.Committer.Date != nil {
runevent.SHACommitterDate = commit.Committer.Date.Time
}
}
// For incoming webhooks, DefaultBranch is not populated from the event
// payload (since there is no webhook payload to parse). Fetch it from the
// GitHub API so that pipelinerun_provenance: default_branch works correctly.
// For other event types (push, pull_request), DefaultBranch is already set
// by ParsePayload from the webhook payload's repository.default_branch field.
if runevent.DefaultBranch == "" && runevent.EventType == "incoming" {
ghRepo, _, err := wrapAPI(v, "get_repo", func() (*github.Repository, *github.Response, error) {
return v.Client().Repositories.Get(ctx, runevent.Organization, runevent.Repository)
})
if err != nil {
return err
}
runevent.DefaultBranch = ghRepo.GetDefaultBranch()
}
return nil
}
// GetFileInsideRepo Get a file via Github API using the runinfo information, we
// branch is true, the user the branch as ref instead of the SHA
// TODO: merge GetFileInsideRepo amd GetTektonDir.
func (v *Provider) GetFileInsideRepo(ctx context.Context, runevent *info.Event, path, target string) (string, error) {
ref := runevent.SHA
if target != "" {
ref = runevent.BaseBranch
} else if v.provenance == "default_branch" {
ref = runevent.DefaultBranch
}
fp, objects, _, err := wrapAPIGetContents(v, "get_file_contents", func() (*github.RepositoryContent, []*github.RepositoryContent, *github.Response, error) {
return v.Client().Repositories.GetContents(ctx, runevent.Organization,
runevent.Repository, path, &github.RepositoryContentGetOptions{Ref: ref})
})
if err != nil {
return "", err
}
if objects != nil {
return "", fmt.Errorf("referenced file inside the Github Repository %s is a directory", path)
}
getobj, err := v.getObject(ctx, fp.GetSHA(), runevent)
if err != nil {
return "", err
}
return string(getobj), nil
}
// concatAllYamlFiles concat all yaml files from a directory as one big multi document yaml string.
func (v *Provider) concatAllYamlFiles(ctx context.Context, objects []*github.TreeEntry, runevent *info.Event, tektonDirPath, ref string) (string, error) {
var yamlFiles []string
for _, value := range objects {
if strings.HasSuffix(value.GetPath(), ".yaml") ||
strings.HasSuffix(value.GetPath(), ".yml") {
fullPath := tektonDirPath + "/" + value.GetPath()
yamlFiles = append(yamlFiles, fullPath)
}
}
if len(yamlFiles) == 0 {
return "", nil
}
if v.graphQLClient == nil {
var err error
v.graphQLClient, err = newGraphQLClient(v)
if err != nil {
return "", fmt.Errorf("failed to create GraphQL client: %w", err)
}
}
graphQLResults, err := v.graphQLClient.fetchFiles(ctx, runevent.Organization, runevent.Repository, ref, yamlFiles)
if err != nil {
return "", fmt.Errorf("failed to fetch .tekton files via GraphQL: %w", err)
}
var buf strings.Builder
for _, path := range yamlFiles {
content, ok := graphQLResults[path]
if !ok {
return "", fmt.Errorf("file %s not found in GraphQL response", path)
}
// it used to be like that (stripped prefix) before we moved to GraphQL so
// let's keep it that way.
relativePath := strings.TrimPrefix(path, tektonDirPath+"/")
if err := provider.ValidateYaml(content, relativePath); err != nil {
return "", err
}
if buf.Len() > 0 && !strings.HasPrefix(string(content), "---") {
buf.WriteString("---")
}
buf.WriteString("\n")
buf.Write(content)
buf.WriteString("\n")
}
return buf.String(), nil
}
// getPullRequest get a pull request details, caching the result for the lifetime of the event.
func (v *Provider) getPullRequest(ctx context.Context, runevent *info.Event) (*info.Event, error) {
if v.cachedPullRequest != nil {
return runevent, nil
}
pr, _, err := wrapAPI(v, "get_pull_request", func() (*github.PullRequest, *github.Response, error) {
return v.Client().PullRequests.Get(ctx, runevent.Organization, runevent.Repository, runevent.PullRequestNumber)
})
if err != nil {
return runevent, err
}
v.cachedPullRequest = pr
return v.populateRunEventFromPullRequest(runevent, pr), nil
}
func (v *Provider) populateRunEventFromPullRequest(runevent *info.Event, pr *github.PullRequest) *info.Event {
// Make sure to use the Base for Default BaseBranch or there would be a potential hijack
runevent.DefaultBranch = pr.GetBase().GetRepo().GetDefaultBranch()
runevent.URL = pr.GetBase().GetRepo().GetHTMLURL()
runevent.SHA = pr.GetHead().GetSHA()
runevent.SHAURL = fmt.Sprintf("%s/commit/%s", pr.GetHTMLURL(), pr.GetHead().GetSHA())
runevent.PullRequestTitle = pr.GetTitle()
// TODO: check if we really need this
if runevent.Sender == "" {
runevent.Sender = pr.GetUser().GetLogin()
}
runevent.HeadBranch = pr.GetHead().GetRef()
runevent.BaseBranch = pr.GetBase().GetRef()
runevent.HeadURL = pr.GetHead().GetRepo().GetHTMLURL()
runevent.BaseURL = pr.GetBase().GetRepo().GetHTMLURL()
if runevent.EventType == "" {
runevent.EventType = triggertype.PullRequest.String()
}
for _, label := range pr.Labels {
runevent.PullRequestLabel = append(runevent.PullRequestLabel, label.GetName())
}
v.RepositoryIDs = []int64{
pr.GetBase().GetRepo().GetID(),
}
return runevent
}
// GetFiles gets and caches the list of files changed by a given event.
func (v *Provider) GetFiles(ctx context.Context, runevent *info.Event) (changedfiles.ChangedFiles, error) {
if v.cachedChangedFiles == nil {
changes, err := v.fetchChangedFiles(ctx, runevent)
if err != nil {
return changedfiles.ChangedFiles{}, err
}
v.cachedChangedFiles = &changes
}
return *v.cachedChangedFiles, nil
}
func (v *Provider) fetchChangedFiles(ctx context.Context, runevent *info.Event) (changedfiles.ChangedFiles, error) {
changedFiles := changedfiles.ChangedFiles{}
switch runevent.TriggerTarget {
case triggertype.PullRequest:
opt := &github.ListOptions{PerPage: v.PaginedNumber}
for {
repoCommit, resp, err := wrapAPI(v, "list_pull_request_files", func() ([]*github.CommitFile, *github.Response, error) {
return v.Client().PullRequests.ListFiles(ctx, runevent.Organization, runevent.Repository, runevent.PullRequestNumber, opt)
})
if err != nil {
return changedfiles.ChangedFiles{}, err
}
for j := range repoCommit {
changedFiles.All = append(changedFiles.All, *repoCommit[j].Filename)
if *repoCommit[j].Status == "added" {
changedFiles.Added = append(changedFiles.Added, *repoCommit[j].Filename)
}
if *repoCommit[j].Status == "removed" {
changedFiles.Deleted = append(changedFiles.Deleted, *repoCommit[j].Filename)
}
if *repoCommit[j].Status == "modified" {
changedFiles.Modified = append(changedFiles.Modified, *repoCommit[j].Filename)
}
if *repoCommit[j].Status == "renamed" {
changedFiles.Renamed = append(changedFiles.Renamed, *repoCommit[j].Filename)
}
}
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
case triggertype.Push:
rC, _, err := wrapAPI(v, "get_commit_files", func() (*github.RepositoryCommit, *github.Response, error) {
return v.Client().Repositories.GetCommit(ctx, runevent.Organization, runevent.Repository, runevent.SHA, &github.ListOptions{})
})
if err != nil {
return changedfiles.ChangedFiles{}, err
}
for i := range rC.Files {
changedFiles.All = append(changedFiles.All, *rC.Files[i].Filename)
if *rC.Files[i].Status == "added" {
changedFiles.Added = append(changedFiles.Added, *rC.Files[i].Filename)
}
if *rC.Files[i].Status == "removed" {
changedFiles.Deleted = append(changedFiles.Deleted, *rC.Files[i].Filename)
}
if *rC.Files[i].Status == "modified" {
changedFiles.Modified = append(changedFiles.Modified, *rC.Files[i].Filename)
}
if *rC.Files[i].Status == "renamed" {
changedFiles.Renamed = append(changedFiles.Renamed, *rC.Files[i].Filename)
}
}
default:
// No action necessary
}
return changedFiles, nil
}
// getObject Get an object from a repository.
func (v *Provider) getObject(ctx context.Context, sha string, runevent *info.Event) ([]byte, error) {
blob, _, err := wrapAPI(v, "get_blob", func() (*github.Blob, *github.Response, error) {
return v.Client().Git.GetBlob(ctx, runevent.Organization, runevent.Repository, sha)
})
if err != nil {
return nil, err
}
decoded, err := base64.StdEncoding.DecodeString(blob.GetContent())
if err != nil {
return nil, err
}
return decoded, err
}
// ListRepos lists all the repos for a particular token.
func ListRepos(ctx context.Context, v *Provider) ([]string, error) {
if v.ghClient == nil {
return []string{}, fmt.Errorf("no github client has been initialized, " +
"exiting... (hint: did you forget setting a secret on your repo?)")
}
opt := &github.ListOptions{PerPage: v.PaginedNumber}
repoURLs := []string{}
for {
repoList, resp, err := wrapAPI(v, "list_app_repos", func() (*github.ListRepositories, *github.Response, error) {
return v.Client().Apps.ListRepos(ctx, opt)
})
if err != nil {
return []string{}, err
}
for i := range repoList.Repositories {
repoURLs = append(repoURLs, *repoList.Repositories[i].HTMLURL)
}
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
return repoURLs, nil
}
func (v *Provider) CreateToken(ctx context.Context, repository []string, event *info.Event) (string, error) {
var appReposCache []*github.Repository
for _, r := range repository {
// Check if this is a glob pattern
if strings.ContainsAny(r, "*?[") {
if err := v.expandGlobAndAddRepoIDs(ctx, r, &appReposCache); err != nil {
v.Logger.Warn("failed to expand glob pattern %q: %v", r, err)
}
continue
}
split := strings.Split(r, "/")
// Validate the URLs do not include additional path segments (like https://github.com/org/repo/extra).
// This validation is not required for glob as a pattern like "org/*/*" would not be matched.
if len(split) > 2 {
return "", fmt.Errorf("github repository URL must follow org/repo format without subgroups (found %d path segments, expected 2): %s", len(split), r)
}
infoData, _, err := wrapAPI(v, "get_repository", func() (*github.Repository, *github.Response, error) {
return v.Client().Repositories.Get(ctx, split[0], split[1])
})
if err != nil {
v.Logger.Warn("we have an invalid repository: `%s` or no access to it: %v", r, err)
continue
}
v.RepositoryIDs = uniqueRepositoryID(v.RepositoryIDs, infoData.GetID())
}
ns := info.GetNS(ctx)
token, err := v.GetAppToken(ctx, v.Run.Clients.Kube, event.Provider.URL, event.InstallationID, ns)
if err != nil {
return "", err
}
return token, nil
}
func (v *Provider) expandGlobAndAddRepoIDs(ctx context.Context, repoPattern string, cache *[]*github.Repository) error {
// We can skip error check here as all the glob compilation has been checked
// before this method is called.
reposToScope, _ := glob.Compile(repoPattern)
if *cache == nil {
repos, err := v.listAppRepos(ctx)
if err != nil {
return err
}
*cache = repos
}
for _, repo := range *cache {
repoFullName := repo.GetFullName()
if reposToScope.Match(repoFullName) {
v.RepositoryIDs = uniqueRepositoryID(v.RepositoryIDs, repo.GetID())
}
}
return nil
}
func (v *Provider) listAppRepos(ctx context.Context) ([]*github.Repository, error) {
var allRepos []*github.Repository
opt := &github.ListOptions{PerPage: v.PaginedNumber}
for {
repoList, resp, err := wrapAPI(v, "list_app_repos", func() (*github.ListRepositories, *github.Response, error) {
return v.Client().Apps.ListRepos(ctx, opt)
})
if err != nil {
return nil, fmt.Errorf("failed to list app repos: %w", err)
}
allRepos = append(allRepos, repoList.Repositories...)
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
return allRepos, nil
}
func uniqueRepositoryID(repoIDs []int64, id int64) []int64 {
r := repoIDs
m := make(map[int64]bool)
for _, val := range repoIDs {
if _, ok := m[val]; !ok {
m[val] = true
}
}
if _, ok := m[id]; !ok {
r = append(r, id)
}
return r
}
// isHeadCommitOfBranch checks whether provided branch is valid or not and SHA is HEAD commit of the branch.
func (v *Provider) isHeadCommitOfBranch(ctx context.Context, runevent *info.Event, branchName string) error {
if v.ghClient == nil {
return fmt.Errorf("no github client has been initialized, " +
"exiting... (hint: did you forget setting a secret on your repo?)")
}
branchInfo, _, err := wrapAPI(v, "get_branch", func() (*github.Branch, *github.Response, error) {
return v.Client().Repositories.GetBranch(ctx, runevent.Organization, runevent.Repository, branchName, 1)
})
if err != nil {
return err
}
if branchInfo.Commit.GetSHA() == runevent.SHA {
return nil
}
return fmt.Errorf("provided SHA %s is not the HEAD commit of the branch %s", runevent.SHA, branchName)
}
func (v *Provider) GetTemplate(commentType provider.CommentType) string {
return provider.GetHTMLTemplate(commentType)
}
type commentTraceLogContext struct {
dedupTrace string
eventID string
markerHash string
markerLen int
controllerLabel string
}
func newDedupTraceID() string {
//nolint:gosec // best-effort correlation ID for debug logs only
return fmt.Sprintf("%x-%04x", time.Now().UnixNano(), rand.Intn(1<<16))
}
func markerHash(marker string) string {
if marker == "" {
return "none"
}
sum := sha256.Sum256([]byte(marker))
digest := hex.EncodeToString(sum[:])
if len(digest) > 12 {
return digest[:12]
}
return digest
}
func formatCommentTime(ts github.Timestamp) string {
if ts.IsZero() {
return "unknown"
}
return ts.UTC().Format(time.RFC3339)
}
func compactCommentIDs(comments []*github.IssueComment) []string {
out := make([]string, 0, len(comments))
for _, comment := range comments {
out = append(out, fmt.Sprintf("%d@%s", comment.GetID(), formatCommentTime(comment.GetCreatedAt())))
}
return out
}
func responseStatusCode(resp *github.Response) int {
if resp == nil {
return 0
}
return resp.StatusCode
}
func githubRequestID(resp *github.Response) string {
if resp == nil || resp.Response == nil {
return ""
}
return resp.Header.Get("X-GitHub-Request-Id")
}
func bodyHash(body string) string {
sum := sha256.Sum256([]byte(body))
return hex.EncodeToString(sum[:4])
}
func eventID(event *info.Event) string {
if event == nil || event.Request == nil {
return "unknown"
}
if id := event.Request.Header.Get("X-GitHub-Delivery"); id != "" {
return id
}
return "unknown"
}
func (v *Provider) controllerLabel(ctx context.Context) string {
if name := info.GetCurrentControllerName(ctx); name != "" {
return name
}
if v.Run != nil && v.Run.Info.Controller != nil && v.Run.Info.Controller.Name != "" {
return v.Run.Info.Controller.Name
}
return "unknown"
}
func (v *Provider) newCommentTraceLogContext(ctx context.Context, event *info.Event, marker string) commentTraceLogContext {
return commentTraceLogContext{
dedupTrace: newDedupTraceID(),
eventID: eventID(event),
markerHash: markerHash(marker),
markerLen: len(marker),
controllerLabel: v.controllerLabel(ctx),
}
}
func (v *Provider) debugCommentPhase(event *info.Event, trace commentTraceLogContext, phase string, kv ...any) {
if v.Logger == nil {
return
}
org := "unknown"
repo := "unknown"
pr := 0
if event != nil {
org = event.Organization
repo = event.Repository
pr = event.PullRequestNumber
}
baseFields := make([]any, 0, 18+len(kv))
baseFields = append(baseFields,
"phase", phase,
"organization", org,
"repository", repo,
"pr", pr,
"event_id", trace.eventID,
"dedup_trace", trace.dedupTrace,
"marker_hash", trace.markerHash,
"marker_len", trace.markerLen,
"controller_label", trace.controllerLabel,
)
v.Logger.Debugw("github comment dedup flow", append(baseFields, kv...)...)
}
func (v *Provider) listCommentsByMarker(
ctx context.Context,
event *info.Event,
marker, phase string,
trace commentTraceLogContext,
) ([]*github.IssueComment, error) {
comments, _, err := wrapAPI(v, "list_comments", func() ([]*github.IssueComment, *github.Response, error) {
return v.Client().Issues.ListComments(ctx, event.Organization, event.Repository, event.PullRequestNumber, &github.IssueListCommentsOptions{
ListOptions: github.ListOptions{
Page: 1,
PerPage: v.PaginedNumber,
},
})
})
if err != nil {
return nil, err