/
commit_info_result.go
160 lines (140 loc) 路 5.89 KB
/
commit_info_result.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
package github
import (
"context"
"github.com/grafeas/voucher/v2/repository"
"github.com/shurcooL/githubv4"
)
// newCommitInfoResult calls the commitInfoQuery and populates the respective variables
func newCommitInfoResult(ctx context.Context, ghc ghGraphQLClient, commitURL string) (repository.Commit, error) {
formattedURI, err := createNewGitHubV4URI(commitURL)
if err != nil {
return repository.Commit{}, err
}
queryResult := new(commitInfoQuery)
associatedPullRequests, err := getAllAssociatedPullRequests(ctx, ghc, queryResult, githubv4.URI(*formattedURI))
if err != nil {
return repository.Commit{}, err
}
checkSuites, err := getAllCheckSuites(ctx, ghc, queryResult, githubv4.URI(*formattedURI))
if err != nil {
return repository.Commit{}, err
}
commit := queryResult.Resource.Commit
status := commit.Status.State
if !status.isValidStatusState() {
return repository.Commit{}, newTypeMismatchError("statusState", status)
}
branchProtections, err := getBranchProtections(ctx, ghc, commit.Repository.URL)
if err != nil {
return repository.Commit{}, err
}
pullRequests, err := convertPullRequests(ctx, ghc, associatedPullRequests, branchProtections)
if err != nil {
return repository.Commit{}, err
}
checks, err := convertCheckSuites(checkSuites)
if err != nil {
return repository.Commit{}, err
}
isSigned := commit.Signature.IsValid
return repository.NewCommit(commit.URL, checks, string(status), isSigned, pullRequests), nil
}
// convertPullRequests creates new PullRequest object for each pullRequest
func convertPullRequests(ctx context.Context, ghc ghGraphQLClient, associatedPullRequests []pullRequest, branchProtections map[string]branchProtection) ([]repository.PullRequest, error) {
pullRequests := make([]repository.PullRequest, 0)
for _, pr := range associatedPullRequests {
commit := repository.NewCommitRef(pr.MergeCommit.URL)
protections, ok := branchProtections[pr.BaseRefName]
if !ok || !protections.RequiresApprovingReviews {
pullRequests = append(pullRequests, repository.NewPullRequest(pr.BaseRefName, pr.HeadRefName, pr.Merged, commit, true))
continue
}
reviews, err := getAllReviews(ctx, ghc, pr.URL)
if err != nil {
return nil, err
}
approvedReviews := 0
for _, review := range reviews {
if !review.State.isValidPullRequestReviewState() {
return nil, newTypeMismatchError("pullRequestReviewState", review.State)
}
if review.State == pullRequestReviewApproved {
approvedReviews++
}
}
hasRequiredApprovals := approvedReviews >= protections.RequiredApprovingReviewCount
pullRequests = append(pullRequests, repository.NewPullRequest(pr.BaseRefName, pr.HeadRefName, pr.Merged, commit, hasRequiredApprovals))
}
return pullRequests, nil
}
// convertCheckSuites creates a new repository.Check object for each checkSuite
func convertCheckSuites(checkSuites []checkSuite) ([]repository.Check, error) {
checks := make([]repository.Check, 0)
for _, checkSuite := range checkSuites {
if !checkSuite.Status.isValidCheckStatusState() {
return nil, newTypeMismatchError("checkStatusState", checkSuite.Status)
}
if !checkSuite.Conclusion.isValidCheckConclusionState() {
return nil, newTypeMismatchError("checkConclusionState", checkSuite.Conclusion)
}
check := repository.NewCheck(string(checkSuite.Status), string(checkSuite.Conclusion))
checks = append(checks, check)
}
return checks, nil
}
// getAllCheckSuites is the GraphQL query for collecting all the Check Suites pertaining to a commit
func getAllCheckSuites(ctx context.Context, ghc ghGraphQLClient, queryResult *commitInfoQuery, uri githubv4.URI) ([]checkSuite, error) {
allCheckSuites := make([]checkSuite, 0)
commitInfoVariables := map[string]interface{}{
"url": uri,
"checkSuitesCursor": (*githubv4.String)(nil),
"associatedPullRequestsCursor": (*githubv4.String)(nil),
}
err := paginationQuery(ctx, ghc, queryResult, commitInfoVariables, queryPageLimit, func(v interface{}) (bool, error) {
ciq, ok := v.(*commitInfoQuery)
if !ok {
return false, newTypeMismatchError("commitInfoQuery", ciq)
}
commit := ciq.Resource.Commit
resourceType := ciq.Resource.Typename
if resourceType != commitType {
return false, repository.NewTypeMismatchError(commitType, resourceType)
}
allCheckSuites = append(allCheckSuites, commit.CheckSuites.Nodes...)
hasMoreResults := commit.CheckSuites.PageInfo.HasNextPage
commitInfoVariables["checkSuitesCursor"] = githubv4.NewString(commit.CheckSuites.PageInfo.EndCursor)
return hasMoreResults, nil
})
if err != nil {
return nil, err
}
return allCheckSuites, nil
}
// getAllAssociatedPullRequests is the GraphQL query for collecting all the pull requests associated with a commit
func getAllAssociatedPullRequests(ctx context.Context, ghc ghGraphQLClient, queryResult *commitInfoQuery, uri githubv4.URI) ([]pullRequest, error) {
allAssociatedPullRequests := make([]pullRequest, 0)
commitInfoVariables := map[string]interface{}{
"url": uri,
"checkSuitesCursor": (*githubv4.String)(nil),
"associatedPullRequestsCursor": (*githubv4.String)(nil),
}
err := paginationQuery(ctx, ghc, queryResult, commitInfoVariables, queryPageLimit, func(v interface{}) (bool, error) {
ciq, ok := v.(*commitInfoQuery)
if !ok {
return false, newTypeMismatchError("commitInfoQuery", ciq)
}
commit := ciq.Resource.Commit
resourceType := ciq.Resource.Typename
if resourceType != commitType {
return false, repository.NewTypeMismatchError(commitType, resourceType)
}
allAssociatedPullRequests = append(allAssociatedPullRequests, commit.AssociatedPullRequests.Nodes...)
hasMoreResults := commit.AssociatedPullRequests.PageInfo.HasNextPage
commitInfoVariables["associatedPullRequestsCursor"] = githubv4.NewString(commit.AssociatedPullRequests.PageInfo.EndCursor)
return hasMoreResults, nil
})
if err != nil {
return nil, err
}
return allAssociatedPullRequests, nil
}