-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathclient.go
More file actions
858 lines (754 loc) · 23.7 KB
/
Copy pathclient.go
File metadata and controls
858 lines (754 loc) · 23.7 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
package githubclient
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"os"
"path"
"strings"
"gopkg.in/yaml.v3"
"github.com/ejoffe/spr/config"
"github.com/ejoffe/spr/git"
"github.com/ejoffe/spr/github"
"github.com/ejoffe/spr/github/githubclient/fezzik_types"
"github.com/ejoffe/spr/github/githubclient/gen/genclient"
"github.com/ejoffe/spr/github/template/config_fetcher"
"github.com/rs/zerolog/log"
"github.com/zalando/go-keyring"
"golang.org/x/oauth2"
)
//go:generate go run github.com/inigolabs/fezzik --config fezzik.yaml
// hub cli config (https://hub.github.com)
type hubCLIConfig map[string][]struct {
User string `yaml:"user"`
OauthToken string `yaml:"oauth_token"`
Protocol string `yaml:"protocol"`
}
// readHubCLIConfig finds and deserialized the config file for
// Github's "hub" CLI (https://hub.github.com/).
func readHubCLIConfig() (hubCLIConfig, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("failed to get user home directory: %w", err)
}
f, err := os.Open(path.Join(homeDir, ".config", "hub"))
if err != nil {
return nil, fmt.Errorf("failed to open hub config file: %w", err)
}
var cfg hubCLIConfig
if err := yaml.NewDecoder(f).Decode(&cfg); err != nil {
return nil, fmt.Errorf("failed to parse hub config file: %w", err)
}
return cfg, nil
}
// gh cli config (https://cli.github.com)
type ghCLIConfig map[string]struct {
User string `yaml:"user"`
OauthToken string `yaml:"oauth_token"`
GitProtocol string `yaml:"git_protocol"`
}
func readGhCLIConfig() (*ghCLIConfig, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("failed to get user home directory: %w", err)
}
f, err := os.Open(path.Join(homeDir, ".config", "gh", "hosts.yml"))
if err != nil {
return nil, fmt.Errorf("failed to open gh cli config file: %w", err)
}
var cfg ghCLIConfig
if err := yaml.NewDecoder(f).Decode(&cfg); err != nil {
return nil, fmt.Errorf("failed to parse hub config file: %w", err)
}
return &cfg, nil
}
func findToken(githubHost string) string {
// Try environment variable first
token := os.Getenv("GITHUB_TOKEN")
if token != "" {
return token
}
// Try ~/.config/gh/hosts.yml
cfg, err := readGhCLIConfig()
if err != nil {
log.Warn().Err(err).Msg("failed to read gh cli config file")
} else {
for host, user := range *cfg {
if host == githubHost {
secret, err := keyring.Get("gh:github.com", user.User)
if err != nil {
return user.OauthToken
}
return secret
}
}
}
// Try ~/.config/hub
hubCfg, err := readHubCLIConfig()
if err != nil {
log.Warn().Err(err).Msg("failed to read hub config file")
return ""
}
if c, ok := hubCfg["github.com"]; ok {
if len(c) == 0 {
log.Warn().Msg("no token found in hub config file")
return ""
}
if len(c) > 1 {
log.Warn().Msgf("multiple tokens found in hub config file, using first one: %s", c[0].User)
}
return c[0].OauthToken
}
return ""
}
const tokenHelpText = `
No GitHub OAuth token found! You can either create one
at https://%s/settings/tokens and set the GITHUB_TOKEN environment variable,
or use the official "gh" CLI (https://cli.github.com) config to log in:
$ gh auth login --insecure-storage
Alternatively, configure a token manually in ~/.config/hub:
github.com:
- user: <your username>
oauth_token: <your token>
protocol: https
This configuration file is shared with GitHub's "hub" CLI (https://hub.github.com/),
so if you already use that, spr will automatically pick up your token.
`
func NewGitHubClient(ctx context.Context, config *config.Config) *client {
token := findToken(config.Repo.GitHubHost)
if token == "" {
fmt.Printf(tokenHelpText, config.Repo.GitHubHost)
os.Exit(3)
}
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
var api genclient.Client
var endpoint string
if strings.HasSuffix(config.Repo.GitHubHost, "github.com") {
endpoint = "https://api.github.com/graphql"
} else {
var scheme, host string
gitHubRemoteUrl, err := url.Parse(config.Repo.GitHubHost)
check(err)
if gitHubRemoteUrl.Host == "" {
host = config.Repo.GitHubHost
scheme = "https"
} else {
host = gitHubRemoteUrl.Host
scheme = gitHubRemoteUrl.Scheme
}
endpoint = fmt.Sprintf("%s://%s/api/graphql", scheme, host)
}
api = genclient.NewClient(endpoint, tc)
return &client{
config: config,
api: api,
graphqlEndpoint: endpoint,
httpClient: tc,
}
}
type client struct {
config *config.Config
api genclient.Client
graphqlEndpoint string
httpClient *http.Client
}
func (c *client) GetInfo(ctx context.Context, gitcmd git.GitInterface) *github.GitHubInfo {
if c.config.User.LogGitHubCalls {
fmt.Printf("> github fetch pull requests\n")
}
var pullRequestConnection fezzik_types.PullRequestConnection
var loginName string
var repoID string
if c.config.Repo.MergeQueue {
resp, err := c.api.PullRequestsWithMergeQueue(ctx,
c.config.Repo.GitHubRepoOwner,
c.config.Repo.GitHubRepoName)
check(err)
pullRequestConnection = resp.Viewer.PullRequests
loginName = resp.Viewer.Login
repoID = resp.Repository.Id
} else {
resp, err := c.api.PullRequests(ctx,
c.config.Repo.GitHubRepoOwner,
c.config.Repo.GitHubRepoName)
check(err)
pullRequestConnection = resp.Viewer.PullRequests
loginName = resp.Viewer.Login
repoID = resp.Repository.Id
}
targetBranch := c.config.Repo.GitHubBranch
localCommitStack := git.GetLocalCommitStack(c.config, gitcmd)
pullRequests := matchPullRequestStack(c.config.Repo, c.config.BranchPrefix(), targetBranch, localCommitStack, pullRequestConnection)
// When RequiredChecks is explicitly configured, fetch individual check contexts
// and only evaluate the listed checks. This allows non-required check failures
// to be ignored. When RequiredChecks is not set, the statusCheckRollup.state
// from the fezzik query is used as-is (all checks matter).
if c.config.Repo.RequireChecks && len(c.config.Repo.RequiredChecks) > 0 && len(pullRequests) > 0 {
requiredStatus := c.fetchRequiredChecksStatus(ctx, pullRequests)
if requiredStatus != nil {
for _, pr := range pullRequests {
if status, ok := requiredStatus[pr.Number]; ok {
pr.MergeStatus.ChecksPass = status
}
}
}
}
for _, pr := range pullRequests {
if pr.Ready(c.config) {
pr.MergeStatus.Stacked = true
} else {
break
}
}
info := &github.GitHubInfo{
UserName: loginName,
RepositoryID: repoID,
LocalBranch: git.GetLocalBranchName(gitcmd),
PullRequests: pullRequests,
}
log.Debug().Interface("Info", info).Msg("GetInfo")
return info
}
func matchPullRequestStack(
repoConfig *config.RepoConfig,
branchPrefix string,
targetBranch string,
localCommitStack []git.Commit,
allPullRequests fezzik_types.PullRequestConnection) []*github.PullRequest {
if len(localCommitStack) == 0 || allPullRequests.Nodes == nil {
return []*github.PullRequest{}
}
// pullRequestMap is a map from commit-id to pull request
pullRequestMap := make(map[string]*github.PullRequest)
for _, node := range *allPullRequests.Nodes {
var commits []git.Commit
for _, v := range *node.Commits.Nodes {
for _, line := range strings.Split(v.Commit.MessageBody, "\n") {
if strings.HasPrefix(line, "commit-id:") {
commits = append(commits, git.Commit{
CommitID: strings.Split(line, ":")[1],
CommitHash: v.Commit.Oid,
Subject: v.Commit.MessageHeadline,
Body: v.Commit.MessageBody,
})
}
}
}
pullRequest := &github.PullRequest{
ID: node.Id,
Number: node.Number,
Title: node.Title,
Body: node.Body,
FromBranch: node.HeadRefName,
ToBranch: node.BaseRefName,
Commits: commits,
InQueue: node.MergeQueueEntry != nil,
}
matches := git.BranchNameRegex(branchPrefix).FindStringSubmatch(node.HeadRefName)
if matches != nil {
commit := (*node.Commits.Nodes)[len(*node.Commits.Nodes)-1].Commit
pullRequest.Commit = git.Commit{
CommitID: matches[2],
CommitHash: commit.Oid,
Subject: commit.MessageHeadline,
Body: commit.MessageBody,
}
checkStatus := github.CheckStatusPass
if commit.StatusCheckRollup != nil {
switch commit.StatusCheckRollup.State {
case "SUCCESS":
checkStatus = github.CheckStatusPass
case "PENDING":
checkStatus = github.CheckStatusPending
default:
checkStatus = github.CheckStatusFail
}
}
pullRequest.MergeStatus = github.PullRequestMergeStatus{
ChecksPass: checkStatus,
ReviewApproved: node.ReviewDecision != nil && *node.ReviewDecision == "APPROVED",
NoConflicts: node.Mergeable == "MERGEABLE",
}
pullRequestMap[pullRequest.Commit.CommitID] = pullRequest
}
}
// store local commit hashes on PRs for display purposes
// (the remote CommitHash is preserved for update detection in syncCommitStackToGitHub)
for _, c := range localCommitStack {
if pr, ok := pullRequestMap[c.CommitID]; ok && c.CommitHash != "" {
pr.LocalCommitHash = c.CommitHash
}
}
var pullRequests []*github.PullRequest
// find top pr
var currpr *github.PullRequest
var found bool
for i := len(localCommitStack) - 1; i >= 0; i-- {
currpr, found = pullRequestMap[localCommitStack[i].CommitID]
if found {
break
}
}
// The list of commits from the command line actually starts at the
// most recent commit. In order to reverse the list we use a
// custom prepend function instead of append
prepend := func(l []*github.PullRequest, pr *github.PullRequest) []*github.PullRequest {
l = append(l, &github.PullRequest{})
copy(l[1:], l)
l[0] = pr
return l
}
// build pr stack
for currpr != nil {
pullRequests = prepend(pullRequests, currpr)
if currpr.ToBranch == targetBranch {
break
}
matches := git.BranchNameRegex(branchPrefix).FindStringSubmatch(currpr.ToBranch)
if matches == nil {
panic(fmt.Errorf("invalid base branch for pull request:%s", currpr.ToBranch))
}
nextCommitID := matches[2]
currpr = pullRequestMap[nextCommitID]
}
return pullRequests
}
// GetAssignableUsers is taken from github.com/cli/cli/api and is the approach used by the official gh
// client to resolve user IDs to "ID" values for the update PR API calls. See api.RepoAssignableUsers.
func (c *client) GetAssignableUsers(ctx context.Context) []github.RepoAssignee {
if c.config.User.LogGitHubCalls {
fmt.Printf("> github get assignable users\n")
}
users := []github.RepoAssignee{}
var endCursor *string
for {
resp, err := c.api.AssignableUsers(ctx,
c.config.Repo.GitHubRepoOwner,
c.config.Repo.GitHubRepoName, endCursor)
if err != nil {
log.Fatal().Err(err).Msg("get assignable users failed")
return nil
}
for _, node := range *resp.Repository.AssignableUsers.Nodes {
user := github.RepoAssignee{
ID: node.Id,
Login: node.Login,
}
if node.Name != nil {
user.Name = *node.Name
}
users = append(users, user)
}
if !resp.Repository.AssignableUsers.PageInfo.HasNextPage {
break
}
endCursor = resp.Repository.AssignableUsers.PageInfo.EndCursor
}
return users
}
func (c *client) CreatePullRequest(ctx context.Context, gitcmd git.GitInterface,
info *github.GitHubInfo, commit git.Commit, prevCommit *git.Commit) *github.PullRequest {
baseRefName := c.config.Repo.GitHubBranch
if prevCommit != nil {
baseRefName = git.BranchNameFromCommit(c.config, *prevCommit)
}
headRefName := git.BranchNameFromCommit(c.config, commit)
log.Debug().Interface("Commit", commit).
Str("FromBranch", headRefName).Str("ToBranch", baseRefName).
Msg("CreatePullRequest")
templatizer := config_fetcher.PRTemplatizer(c.config, gitcmd)
body := templatizer.Body(info, commit, nil)
resp, err := c.api.CreatePullRequest(ctx, genclient.CreatePullRequestInput{
RepositoryId: info.RepositoryID,
BaseRefName: baseRefName,
HeadRefName: headRefName,
Title: templatizer.Title(info, commit),
Body: &body,
Draft: &c.config.User.CreateDraftPRs,
})
check(err)
pr := &github.PullRequest{
ID: resp.CreatePullRequest.PullRequest.Id,
Number: resp.CreatePullRequest.PullRequest.Number,
FromBranch: headRefName,
ToBranch: baseRefName,
Commit: commit,
Title: commit.Subject,
Body: resp.CreatePullRequest.PullRequest.Body,
MergeStatus: github.PullRequestMergeStatus{
ChecksPass: github.CheckStatusUnknown,
ReviewApproved: false,
NoConflicts: false,
Stacked: false,
},
}
if c.config.User.LogGitHubCalls {
fmt.Printf("> github create %d : %s\n", pr.Number, pr.Title)
}
return pr
}
func (c *client) UpdatePullRequest(ctx context.Context, gitcmd git.GitInterface,
info *github.GitHubInfo, pullRequests []*github.PullRequest, pr *github.PullRequest,
commit git.Commit, prevCommit *git.Commit) {
baseRefName := c.config.Repo.GitHubBranch
if prevCommit != nil {
baseRefName = git.BranchNameFromCommit(c.config, *prevCommit)
}
log.Debug().Interface("Commit", commit).
Str("FromBranch", pr.FromBranch).Str("ToBranch", baseRefName).
Interface("PR", pr).Msg("UpdatePullRequest")
templatizer := config_fetcher.PRTemplatizer(c.config, gitcmd)
title := templatizer.Title(info, commit)
body := templatizer.Body(info, commit, pr)
// Skip the API call if nothing has actually changed. This avoids
// triggering a spurious pull_request "edited" event on GitHub which
// would cause a second (duplicate) Actions run after the force-push
// already triggered a "synchronize" event.
titleUnchanged := c.config.User.PreserveTitleAndBody || title == pr.Title
bodyUnchanged := c.config.User.PreserveTitleAndBody || body == pr.Body
baseUnchanged := pr.InQueue || baseRefName == pr.ToBranch
if titleUnchanged && bodyUnchanged && baseUnchanged {
log.Debug().Int("number", pr.Number).Msg("UpdatePullRequest: skipping, nothing changed")
if c.config.User.LogGitHubCalls {
fmt.Printf("> github update %d : %s (skipped, no changes)\n", pr.Number, pr.Title)
}
return
}
if c.config.User.LogGitHubCalls {
fmt.Printf("> github update %d : %s\n", pr.Number, pr.Title)
}
input := genclient.UpdatePullRequestInput{
PullRequestId: pr.ID,
Title: &title,
Body: &body,
}
if c.config.User.PreserveTitleAndBody {
input.Title = nil
input.Body = nil
}
if !pr.InQueue {
input.BaseRefName = &baseRefName
}
_, err := c.api.UpdatePullRequest(ctx, input)
if err != nil {
log.Fatal().
Str("id", pr.ID).
Int("number", pr.Number).
Str("title", pr.Title).
Err(err).
Msg("pull request update failed")
}
}
// AddReviewers adds reviewers to the provided pull request using the requestReviews() API call. It
// takes github user IDs (ID type) as its input. These can be found by first querying the AssignableUsers
// for the repo, and then mapping login name to ID.
func (c *client) AddReviewers(ctx context.Context, pr *github.PullRequest, userIDs []string) {
log.Debug().Strs("userIDs", userIDs).Msg("AddReviewers")
if c.config.User.LogGitHubCalls {
fmt.Printf("> github add reviewers %d : %s - %+v\n", pr.Number, pr.Title, userIDs)
}
union := false
_, err := c.api.AddReviewers(ctx, genclient.RequestReviewsInput{
PullRequestId: pr.ID,
Union: &union,
UserIds: &userIDs,
})
if err != nil {
log.Fatal().
Str("id", pr.ID).
Int("number", pr.Number).
Str("title", pr.Title).
Strs("userIDs", userIDs).
Err(err).
Msg("add reviewers failed")
}
}
func (c *client) CommentPullRequest(ctx context.Context, pr *github.PullRequest, comment string) {
_, err := c.api.CommentPullRequest(ctx, genclient.AddCommentInput{
SubjectId: pr.ID,
Body: comment,
})
if err != nil {
log.Fatal().
Str("id", pr.ID).
Int("number", pr.Number).
Str("title", pr.Title).
Err(err).
Msg("pull request update failed")
}
if c.config.User.LogGitHubCalls {
fmt.Printf("> github add comment %d : %s\n", pr.Number, pr.Title)
}
}
func (c *client) MergePullRequest(ctx context.Context,
pr *github.PullRequest, mergeMethod genclient.PullRequestMergeMethod) {
log.Debug().
Interface("PR", pr).
Str("mergeMethod", string(mergeMethod)).
Msg("MergePullRequest")
var err error
if c.config.Repo.MergeQueue {
_, err = c.api.AutoMergePullRequest(ctx, genclient.EnablePullRequestAutoMergeInput{
PullRequestId: pr.ID,
MergeMethod: &mergeMethod,
})
} else {
_, err = c.api.MergePullRequest(ctx, genclient.MergePullRequestInput{
PullRequestId: pr.ID,
MergeMethod: &mergeMethod,
})
}
if err != nil {
log.Fatal().
Str("id", pr.ID).
Int("number", pr.Number).
Str("title", pr.Title).
Err(err).
Msg("pull request merge failed")
}
check(err)
if c.config.User.LogGitHubCalls {
fmt.Printf("> github merge %d : %s\n", pr.Number, pr.Title)
}
}
func (c *client) ClosePullRequest(ctx context.Context, pr *github.PullRequest) {
log.Debug().Interface("PR", pr).Msg("ClosePullRequest")
_, err := c.api.ClosePullRequest(ctx, genclient.ClosePullRequestInput{
PullRequestId: pr.ID,
})
if err != nil {
log.Fatal().
Str("id", pr.ID).
Int("number", pr.Number).
Str("title", pr.Title).
Err(err).
Msg("pull request close failed")
}
if c.config.User.LogGitHubCalls {
fmt.Printf("> github close %d : %s\n", pr.Number, pr.Title)
}
}
// Response types for the raw GraphQL query that fetches individual check contexts.
// These are used instead of fezzik-generated types because fezzik does not support
// inline fragments on union types (StatusCheckRollupContext = CheckRun | StatusContext).
type checkContextNode struct {
TypeName string `json:"__typename"`
Name string `json:"name"` // CheckRun
Conclusion *string `json:"conclusion"` // CheckRun (nil when not completed)
Status string `json:"status"` // CheckRun: COMPLETED, IN_PROGRESS, QUEUED, etc.
Context string `json:"context"` // StatusContext
State string `json:"state"` // StatusContext: SUCCESS, FAILURE, PENDING, etc.
}
type checkContextsResult struct {
Number int `json:"number"`
Commits struct {
Nodes []struct {
Commit struct {
StatusCheckRollup *struct {
Contexts struct {
Nodes []checkContextNode `json:"nodes"`
} `json:"contexts"`
} `json:"statusCheckRollup"`
} `json:"commit"`
} `json:"nodes"`
} `json:"commits"`
}
type graphqlRequest struct {
Query string `json:"query"`
}
type graphqlResponse struct {
Data map[string]json.RawMessage `json:"data"`
Errors []struct {
Message string `json:"message"`
} `json:"errors"`
}
// fetchRequiredChecksStatus makes a single batched GraphQL query to fetch
// individual check contexts for all given pull requests. It evaluates only
// the checks listed in config.Repo.RequiredChecks and returns a map from
// PR number to the computed status.
func (c *client) fetchRequiredChecksStatus(ctx context.Context, pullRequests []*github.PullRequest) map[int]github.CheckStatus {
if len(pullRequests) == 0 {
return nil
}
if c.config.User.LogGitHubCalls {
fmt.Printf("> github fetch required check status\n")
}
// Build a single GraphQL query with one aliased field per PR.
var queryBuilder strings.Builder
queryBuilder.WriteString("query {")
for _, pr := range pullRequests {
fmt.Fprintf(&queryBuilder, `
pr_%d: node(id: %q) {
... on PullRequest {
number
commits(last: 1) {
nodes {
commit {
statusCheckRollup {
contexts(first: 100) {
nodes {
__typename
... on CheckRun {
name
conclusion
status
}
... on StatusContext {
context
state
}
}
}
}
}
}
}
}
}`, pr.Number, pr.ID)
}
queryBuilder.WriteString("\n}")
reqBody, err := json.Marshal(graphqlRequest{
Query: queryBuilder.String(),
})
if err != nil {
log.Warn().Err(err).Msg("failed to marshal required checks query")
return nil
}
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, c.graphqlEndpoint, bytes.NewReader(reqBody))
if err != nil {
log.Warn().Err(err).Msg("failed to create required checks request")
return nil
}
httpReq.Header.Set("Content-Type", "application/json; charset=utf-8")
httpReq.Header.Set("Accept", "application/json; charset=utf-8")
httpResp, err := c.httpClient.Do(httpReq)
if err != nil {
log.Warn().Err(err).Msg("failed to fetch required checks status")
return nil
}
defer httpResp.Body.Close()
var gqlResp graphqlResponse
if err := json.NewDecoder(httpResp.Body).Decode(&gqlResp); err != nil {
log.Warn().Err(err).Msg("failed to decode required checks response")
return nil
}
if len(gqlResp.Errors) > 0 {
log.Warn().Str("error", gqlResp.Errors[0].Message).Msg("graphql error fetching required checks")
return nil
}
// Build the set of required check names from config.
requiredSet := make(map[string]bool, len(c.config.Repo.RequiredChecks))
for _, name := range c.config.Repo.RequiredChecks {
requiredSet[name] = true
}
result := make(map[int]github.CheckStatus)
for _, pr := range pullRequests {
alias := fmt.Sprintf("pr_%d", pr.Number)
raw, ok := gqlResp.Data[alias]
if !ok {
continue
}
var prResult checkContextsResult
if err := json.Unmarshal(raw, &prResult); err != nil {
log.Warn().Err(err).Int("pr", pr.Number).Msg("failed to unmarshal check contexts for PR")
continue
}
if len(prResult.Commits.Nodes) == 0 {
continue
}
commit := prResult.Commits.Nodes[0].Commit
if commit.StatusCheckRollup == nil {
// No checks configured — treat as pass
result[pr.Number] = github.CheckStatusPass
continue
}
result[pr.Number] = computeRequiredCheckStatus(commit.StatusCheckRollup.Contexts.Nodes, requiredSet)
}
return result
}
// contextName returns the display name for a check context node.
// For CheckRun nodes this is the Name field; for StatusContext nodes it's the Context field.
func contextName(ctx checkContextNode) string {
if ctx.TypeName == "StatusContext" {
return ctx.Context
}
return ctx.Name
}
// computeRequiredCheckStatus determines the aggregate check status considering
// only the checks whose name/context appears in requiredChecks.
// If a required check hasn't reported yet (not present in contexts), it is
// treated as pending.
func computeRequiredCheckStatus(contexts []checkContextNode, requiredChecks map[string]bool) github.CheckStatus {
// Track which required checks we've seen
seen := make(map[string]bool, len(requiredChecks))
hasPending := false
hasFail := false
for _, ctx := range contexts {
name := contextName(ctx)
if !requiredChecks[name] {
continue
}
seen[name] = true
switch ctx.TypeName {
case "CheckRun":
switch ctx.Status {
case "COMPLETED":
if ctx.Conclusion == nil {
hasFail = true
} else {
switch *ctx.Conclusion {
case "SUCCESS", "NEUTRAL", "SKIPPED":
// pass
default:
hasFail = true
}
}
default:
// IN_PROGRESS, QUEUED, REQUESTED, WAITING, PENDING
hasPending = true
}
case "StatusContext":
switch ctx.State {
case "SUCCESS":
// pass
case "PENDING", "EXPECTED":
hasPending = true
default:
hasFail = true
}
}
}
// Any required check that hasn't reported yet is pending
for name := range requiredChecks {
if !seen[name] {
hasPending = true
}
}
if hasFail {
return github.CheckStatusFail
}
if hasPending {
return github.CheckStatusPending
}
return github.CheckStatusPass
}
func check(err error) {
if err != nil {
msg := err.Error()
if strings.Contains(msg, "401 Unauthorized") {
errmsg := "error : 401 Unauthorized\n"
errmsg += " make sure GITHUB_TOKEN env variable is set with a valid token\n"
errmsg += " to create a valid token goto: https://github.com/settings/tokens\n"
fmt.Fprint(os.Stderr, errmsg)
os.Exit(-1)
} else {
panic(err)
}
}
}