forked from ossf/scorecard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckruns.go
183 lines (170 loc) · 5.9 KB
/
checkruns.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
// Copyright 2021 OpenSSF Scorecard Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package githubrepo
import (
"context"
"fmt"
"strings"
"sync"
"github.com/google/go-github/v38/github"
"github.com/shurcooL/githubv4"
"github.com/ossf/scorecard/v4/clients"
sce "github.com/ossf/scorecard/v4/errors"
"github.com/ossf/scorecard/v4/log"
)
//nolint:govet
type checkRunsGraphqlData struct {
Repository struct {
Object struct {
Commit struct {
History struct {
Nodes []struct {
AssociatedPullRequests struct {
Nodes []struct {
HeadRefOid githubv4.String
Commits struct {
Nodes []struct {
Commit struct {
CheckSuites struct {
Nodes []struct {
App struct {
Slug githubv4.String
}
Conclusion githubv4.CheckConclusionState
Status githubv4.CheckStatusState
}
} `graphql:"checkSuites(first: $checksToAnalyze)"`
}
}
} `graphql:"commits(last:1)"`
}
} `graphql:"associatedPullRequests(first: $pullRequestsToAnalyze)"`
}
} `graphql:"history(first: $commitsToAnalyze)"`
} `graphql:"... on Commit"`
} `graphql:"object(expression: $commitExpression)"`
} `graphql:"repository(owner: $owner, name: $name)"`
RateLimit struct {
Cost *int
}
}
type checkRunsByRef = map[string][]clients.CheckRun
// nolint: govet
type checkrunsHandler struct {
client *github.Client
graphClient *githubv4.Client
repourl *repoURL
logger *log.Logger
checkData *checkRunsGraphqlData
setupOnce *sync.Once
ctx context.Context
commitDepth int
checkRunsByRef checkRunsByRef
errSetup error
}
func (handler *checkrunsHandler) init(ctx context.Context, repourl *repoURL, commitDepth int) {
handler.ctx = ctx
handler.repourl = repourl
handler.commitDepth = commitDepth
handler.logger = log.NewLogger(log.DefaultLevel)
handler.checkData = new(checkRunsGraphqlData)
handler.setupOnce = new(sync.Once)
handler.checkRunsByRef = checkRunsByRef{}
handler.errSetup = nil
}
func (handler *checkrunsHandler) setup() error {
handler.setupOnce.Do(func() {
handler.errSetup = nil
commitExpression := handler.repourl.commitExpression()
vars := map[string]interface{}{
"owner": githubv4.String(handler.repourl.owner),
"name": githubv4.String(handler.repourl.repo),
"pullRequestsToAnalyze": githubv4.Int(pullRequestsToAnalyze),
"commitsToAnalyze": githubv4.Int(handler.commitDepth),
"commitExpression": githubv4.String(commitExpression),
"checksToAnalyze": githubv4.Int(checksToAnalyze),
}
// TODO(#2224):
// sast and ci checks causes cache miss if commits dont match number of check runs.
// paging for this needs to be implemented if using higher than 100 --number-of-commits
if handler.commitDepth > 99 {
vars["commitsToAnalyze"] = githubv4.Int(99)
}
if err := handler.graphClient.Query(handler.ctx, handler.checkData, vars); err != nil {
// quit early without setting crsErrSetup for "Resource not accessible by integration" error
// for whatever reason, this check doesn't work with a GITHUB_TOKEN, only a PAT
if strings.Contains(err.Error(), "Resource not accessible by integration") {
return
}
handler.errSetup = err
return
}
handler.checkRunsByRef = parseCheckRuns(handler.checkData)
})
return handler.errSetup
}
func (handler *checkrunsHandler) listCheckRunsForRef(ref string) ([]clients.CheckRun, error) {
if err := handler.setup(); err != nil {
return nil, fmt.Errorf("error during graphqlHandler.setupCheckRuns: %w", err)
}
if crs, ok := handler.checkRunsByRef[ref]; ok {
return crs, nil
}
msg := fmt.Sprintf("listCheckRunsForRef cache miss: %s/%s:%s", handler.repourl.owner, handler.repourl.repo, ref)
handler.logger.Info(msg)
checkRuns, _, err := handler.client.Checks.ListCheckRunsForRef(
handler.ctx, handler.repourl.owner, handler.repourl.repo, ref, &github.ListCheckRunsOptions{})
if err != nil {
return nil, sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("ListCheckRunsForRef: %v", err))
}
handler.checkRunsByRef[ref] = checkRunsFrom(checkRuns)
return handler.checkRunsByRef[ref], nil
}
func parseCheckRuns(data *checkRunsGraphqlData) checkRunsByRef {
checkCache := checkRunsByRef{}
for _, commit := range data.Repository.Object.Commit.History.Nodes {
for _, pr := range commit.AssociatedPullRequests.Nodes {
var crs []clients.CheckRun
for _, c := range pr.Commits.Nodes {
for _, checkRun := range c.Commit.CheckSuites.Nodes {
crs = append(crs, clients.CheckRun{
// the REST API returns lowercase. the graphQL API returns upper
Status: strings.ToLower(string(checkRun.Status)),
Conclusion: strings.ToLower(string(checkRun.Conclusion)),
App: clients.CheckRunApp{
Slug: string(checkRun.App.Slug),
},
})
}
}
headRef := string(pr.HeadRefOid)
checkCache[headRef] = crs
}
}
return checkCache
}
func checkRunsFrom(data *github.ListCheckRunsResults) []clients.CheckRun {
var checkRuns []clients.CheckRun
for _, checkRun := range data.CheckRuns {
checkRuns = append(checkRuns, clients.CheckRun{
Status: checkRun.GetStatus(),
Conclusion: checkRun.GetConclusion(),
URL: checkRun.GetURL(),
App: clients.CheckRunApp{
Slug: checkRun.GetApp().GetSlug(),
},
})
}
return checkRuns
}