forked from kubernetes-sigs/kubebuilder-release-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.go
366 lines (309 loc) · 11.2 KB
/
plugin.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
/*
Copyright 2020 The Kubernetes 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 action
import (
"context"
"errors"
"fmt"
"time"
"github.com/google/go-github/v32/github"
"sigs.k8s.io/kubebuilder-release-tools/verify/pkg/log"
)
const (
actionOpen = "opened"
actionReopen = "reopened"
actionEdit = "edited"
actionSync = "synchronize"
)
// ValidateFunc is the type of the callback that a Plugin will use to validate the PR contents
type ValidateFunc func(*github.PullRequest) (string, string, error)
// plugin performs the wrapped validate and uploads the results using GitHub Check API
type plugin struct {
checkRunName string
checkRunOutputTitle string
validate ValidateFunc
log.Logger
}
// New creates a new Plugin that validates a PR event uploading the results
// using GitHub Check API with the provided name and output title.
func NewPlugin(name, title string, validate ValidateFunc) Plugin {
return plugin{
checkRunName: name,
checkRunOutputTitle: title,
validate: validate,
Logger: log.NewFor(name),
}
}
// Name implements Plugin interface.
func (p plugin) Name() string {
return p.checkRunName
}
// Entrypoint implements Plugin interface.
func (p plugin) Entrypoint(env *PREnv) error {
switch env.Event.GetAction() {
case actionOpen:
return p.onOpen(env)
case actionReopen:
return p.onReopen(env)
case actionEdit:
return p.onEdit(env)
case actionSync:
return p.onSync(env)
default:
p.Warningf("action %q received with no defined procedure, skipping", env.Event.GetAction())
}
return nil
}
// onOpen handles "opened" actions
func (p plugin) onOpen(env *PREnv) error {
p.Debugf("%q handler", actionOpen)
// Create the check run
checkRun, err := p.createCheckRun(env.Client, env.Owner, env.Repo, env.Event.GetPullRequest().GetHead().GetSHA())
if err != nil {
return err
}
// Process the PR and submit the results
_, err = p.validateAndSubmit(env, checkRun)
return err
}
// onReopen handles "reopened" actions
func (p plugin) onReopen(env *PREnv) error {
p.Debugf("%q handler", actionReopen)
// Get the check run
checkRun, err := p.getCheckRun(env.Client, env.Owner, env.Repo, env.Event.GetPullRequest().GetHead().GetSHA())
if err != nil {
return err
}
// Rerun the tests if they weren't finished
if !Finished.Equal(checkRun.GetStatus()) {
// Process the PR and submit the results
_, err = p.validateAndSubmit(env, checkRun)
return err
}
// Return failure here too so that the whole suite fails (since the actions
// suite seems to ignore failing check runs when calculating general failure)
if *checkRun.Conclusion == "failure" {
return fmt.Errorf("failed: %v", *checkRun.Output.Summary)
}
return nil
}
// onEdit handles "edited" actions
func (p plugin) onEdit(env *PREnv) error {
p.Debugf("%q handler", actionEdit)
// Reset the check run
checkRun, err := p.resetCheckRun(env.Client, env.Owner, env.Repo, env.Event.GetPullRequest().GetHead().GetSHA())
if err != nil {
return err
}
// Process the PR and submit the results
_, err = p.validateAndSubmit(env, checkRun)
return err
}
// onSync handles "synchronize" actions
func (p plugin) onSync(env *PREnv) error {
p.Debugf("%q handler", actionSync)
// Get the check run
checkRun, err := p.getCheckRun(env.Client, env.Owner, env.Repo, env.Event.GetBefore())
if err != nil {
return err
}
// Rerun the tests if they weren't finished
if !Finished.Equal(checkRun.GetStatus()) {
// Process the PR and submit the results
checkRun, err = p.validateAndSubmit(env, checkRun)
if err != nil {
return err
}
}
// Create a duplicate for the new commit
checkRun, err = p.duplicateCheckRun(env.Client, env.Owner, env.Repo, env.Event.GetAfter(), checkRun)
if err != nil {
return err
}
// Return failure here too so that the whole suite fails (since the actions
// suite seems to ignore failing check runs when calculating general failure)
if *checkRun.Conclusion == "failure" {
return fmt.Errorf("failed: %v", *checkRun.Output.Summary)
}
return nil
}
// validatePR executes the provided validating function and parses the result
func (p plugin) validatePR(pr *github.PullRequest) (conclusion, summary, text string, err error) {
p.Debug("execute the plugin checks")
summary, text, err = p.validate(pr)
if err == nil {
conclusion = "success"
} else {
conclusion = "failure"
summary = err.Error()
var detailedErr ErrorWithDetails
if errors.As(err, &detailedErr) {
text = detailedErr.Details()
}
}
// Log in case we can't submit the result for some reason
p.Debugf("plugin conclusion: %q", conclusion)
p.Debugf("plugin result summary: %q", summary)
p.Debugf("plugin result details: %q", text)
return conclusion, summary, text, err
}
// validateAndSubmit performs the validation and submits the result
func (p plugin) validateAndSubmit(env *PREnv, checkRun *github.CheckRun) (*github.CheckRun, error) {
// Validate the PR
conclusion, summary, text, validateErr := p.validatePR(env.Event.PullRequest)
// Update the check run
checkRun, err := p.finishCheckRun(env.Client, env.Owner, env.Repo, checkRun.GetID(), conclusion, summary, text)
if err != nil {
return checkRun, err
}
// Return failure here too so that the whole suite fails (since the actions
// suite seems to ignore failing check runs when calculating general failure)
if validateErr != nil {
return checkRun, fmt.Errorf("failed: %v", validateErr)
}
return checkRun, nil
}
////////////////////////////////////////////////////////////////////////////////
// Check API calls //
////////////////////////////////////////////////////////////////////////////////
// createCheckRun creates a new Check-Run.
// It returns an error in case it couldn't be created.
func (p plugin) createCheckRun(client *github.Client, owner, repo, headSHA string) (*github.CheckRun, error) {
p.Debugf("creating check run %q on %s/%s @ %s...", p.checkRunName, owner, repo, headSHA)
checkRun, res, err := client.Checks.CreateCheckRun(
context.TODO(),
owner,
repo,
github.CreateCheckRunOptions{
Name: p.checkRunName,
HeadSHA: headSHA,
Status: Started.StringP(),
},
)
p.Debugf("create check API response: %+v", res)
p.Debugf("created run: %+v", checkRun)
if err != nil {
return nil, fmt.Errorf("unable to create check run: %w", err)
}
return checkRun, nil
}
// getCheckRun returns the Check-Run, creating it if it doesn't exist.
// It returns an error in case it didn't exist and couldn't be created, or if there are multiple matches.
func (p plugin) getCheckRun(client *github.Client, owner, repo, headSHA string) (*github.CheckRun, error) {
p.Debugf("getting check run %q on %s/%s @ %s...", p.checkRunName, owner, repo, headSHA)
checkRunList, res, err := client.Checks.ListCheckRunsForRef(
context.TODO(),
owner,
repo,
headSHA,
&github.ListCheckRunsOptions{
CheckName: github.String(p.checkRunName),
},
)
p.Debugf("list check API response: %+v", res)
p.Debugf("listed runs: %+v", checkRunList)
if err != nil {
return nil, fmt.Errorf("unable to get check run: %w", err)
}
switch n := *checkRunList.Total; {
case n == 0:
return p.createCheckRun(client, owner, repo, headSHA)
case n == 1:
return checkRunList.CheckRuns[0], nil
case n > 1:
return nil, fmt.Errorf("multiple instances of `%s` check run found on %s/%s @ %s",
p.checkRunName, owner, repo, headSHA)
default: // Should never happen
return nil, fmt.Errorf("negative number of instances (%d) of `%s` check run found on %s/%s @ %s",
n, p.checkRunName, owner, repo, headSHA)
}
}
// resetCheckRun returns the Check-Run with executing status, creating it if it doesn't exist.
// It returns an error in case it didn't exist and couldn't be created, if there are multiple matches,
// or if it exists but couldn't be updated.
func (p plugin) resetCheckRun(client *github.Client, owner, repo string, headSHA string) (*github.CheckRun, error) {
checkRun, err := p.getCheckRun(client, owner, repo, headSHA)
// If it errored, or it was created but not finished, we don't need to update it
if err != nil || Started.Equal(checkRun.GetStatus()) {
return checkRun, err
}
p.Debugf("resetting check run %q on %s/%s...", p.checkRunName, owner, repo)
checkRun, updateResp, err := client.Checks.UpdateCheckRun(
context.TODO(),
owner,
repo,
checkRun.GetID(),
github.UpdateCheckRunOptions{
Name: p.checkRunName,
Status: Started.StringP(),
},
)
p.Debugf("update check API response: %+v", updateResp)
p.Debugf("updated run: %+v", checkRun)
if err != nil {
return checkRun, fmt.Errorf("unable to reset check run: %w", err)
}
return checkRun, nil
}
// finishCheckRun updates the Check-Run with id checkRunID setting its output.
// It returns an error in case it couldn't be updated.
func (p plugin) finishCheckRun(client *github.Client, owner, repo string, checkRunID int64, conclusion, summary, text string) (*github.CheckRun, error) {
p.Debugf("adding results to check run %q on %s/%s...", p.checkRunName, owner, repo)
// CheckRun.Output.Text is optional, so empty text strings should actually be nil pointers
var testPointer *string
if text != "" {
testPointer = github.String(text)
}
checkRun, updateResp, err := client.Checks.UpdateCheckRun(context.TODO(), owner, repo, checkRunID, github.UpdateCheckRunOptions{
Name: p.checkRunName,
Conclusion: github.String(conclusion),
CompletedAt: &github.Timestamp{Time: time.Now()},
Output: &github.CheckRunOutput{
Title: github.String(p.checkRunOutputTitle),
Summary: github.String(summary),
Text: testPointer,
},
})
p.Debugf("update check API response: %+v", updateResp)
p.Debugf("updated run: %+v", checkRun)
if err != nil {
return checkRun, fmt.Errorf("unable to update check run with results: %w", err)
}
return checkRun, nil
}
// duplicateCheckRun creates a new Check-Run with the same info as the provided one but for a new headSHA
func (p plugin) duplicateCheckRun(client *github.Client, owner, repo, headSHA string, checkRun *github.CheckRun) (*github.CheckRun, error) {
p.Debugf("duplicating check run %q on %s/%s @ %s...", p.checkRunName, owner, repo, headSHA)
checkRun, res, err := client.Checks.CreateCheckRun(
context.TODO(),
owner,
repo,
github.CreateCheckRunOptions{
Name: p.checkRunName,
HeadSHA: headSHA,
DetailsURL: checkRun.DetailsURL,
ExternalID: checkRun.ExternalID,
Status: checkRun.Status,
Conclusion: checkRun.Conclusion,
StartedAt: checkRun.StartedAt,
CompletedAt: checkRun.CompletedAt,
Output: checkRun.Output,
},
)
p.Debugf("create check API response: %+v", res)
p.Debugf("created run: %+v", checkRun)
if err != nil {
return checkRun, fmt.Errorf("unable to duplicate check run: %w", err)
}
return checkRun, nil
}