forked from ceph/ceph-csi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
275 lines (241 loc) · 8.13 KB
/
main.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
/*
Copyright 2021 The Ceph-CSI 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 main
import (
"context"
"errors"
"fmt"
"log"
"os"
"slices"
"strconv"
"strings"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
)
var (
totalRequiredReviews int
retry int
)
type retestConfig struct {
retryLimit string
requiredReviewCount string
exemptlabel string
requiredlabel string
githubToken string
owner string
repo string
client *github.Client
}
// standard input env variables in action more details at
// https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#inputs
func getConfig() *retestConfig {
c := &retestConfig{}
c.retryLimit = os.Getenv("INPUT_MAX-RETRY")
c.requiredReviewCount = os.Getenv("INPUT_REQUIRED-APPROVE-COUNT")
c.exemptlabel = os.Getenv("INPUT_EXEMPT-LABEL")
c.requiredlabel = os.Getenv("INPUT_REQUIRED-LABEL")
c.githubToken = os.Getenv("GITHUB_TOKEN")
c.owner, c.repo = func() (string, string) {
if os.Getenv("GITHUB_REPOSITORY") != "" {
if len(strings.Split(os.Getenv("GITHUB_REPOSITORY"), "/")) == 2 {
return strings.Split(os.Getenv("GITHUB_REPOSITORY"), "/")[0], strings.Split(os.Getenv("GITHUB_REPOSITORY"), "/")[1]
}
}
return "", ""
}()
return c
}
// validate validates the input parameters.
func (c retestConfig) validate() error {
if c.requiredlabel == "" {
return errors.New("required-label is not set")
}
if c.githubToken == "" {
return errors.New("GITHUB_TOKEN is not set")
}
if c.owner == "" || c.repo == "" {
return errors.New("GITHUB_REPOSITORY is not set")
}
return nil
}
// createClient creates a new secure client.
func (c *retestConfig) createClient() {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: c.githubToken},
)
tc := oauth2.NewClient(context.TODO(), ts)
c.client = github.NewClient(tc)
}
func main() {
var err error
c := getConfig()
if err = c.validate(); err != nil {
log.Fatalf("%v", err)
}
retry, err = strconv.Atoi(c.retryLimit)
if err != nil {
log.Fatalf("max-retry %q is not valid", c.retryLimit)
}
totalRequiredReviews, err = strconv.Atoi(c.requiredReviewCount)
if err != nil {
log.Fatalf("required-review-count %q is not valid", c.requiredReviewCount)
}
c.createClient()
opt := &github.PullRequestListOptions{}
req, _, err := c.client.PullRequests.List(context.TODO(), c.owner, c.repo, opt)
if err != nil {
log.Fatalf("failed to list pull requests %v\n", err)
}
for _, re := range req {
if (re.State != nil) && (*re.State == "open") {
prNumber := re.GetNumber()
log.Printf("PR with ID %d with Title %q is open\n", prNumber, re.GetTitle())
for _, l := range re.Labels {
// check if label is exempt
if strings.EqualFold(c.exemptlabel, l.GetName()) {
continue
}
// check if label is matching
if !strings.EqualFold(c.requiredlabel, l.GetName()) {
continue
}
// check if PR has required approvals
if !c.checkPRRequiredApproval(prNumber) {
continue
}
log.Printf("checking status for PR %d with label %s", prNumber, l.GetName())
rs, _, err := c.client.Repositories.ListStatuses(context.TODO(), c.owner, c.repo, re.GetHead().GetSHA(), &github.ListOptions{})
if err != nil {
log.Printf("failed to list status %v\n", err)
continue
}
statusList := filterStatusList(rs)
failedTestFound := false
for _, r := range statusList {
log.Printf("found context %s with status %s\n", r.GetContext(), r.GetState())
if slices.Contains([]string{"failed", "failure"}, r.GetState()) {
log.Printf("found failed test %s\n", r.GetContext())
failedTestFound = true
// rebase the pr if it is behind the devel branch.
if (re.MergeableState != nil) && (*re.MergeableState == "BEHIND") {
comment := &github.IssueComment{
Body: github.String("@mergifyio rebase"),
}
_, _, err := c.client.Issues.CreateComment(context.TODO(), c.owner, c.repo, prNumber, comment)
if err != nil {
log.Printf("failed to create comment %v\n", err)
}
break
}
// check if retest limit is reached
msg := fmt.Sprintf("/retest %s", r.GetContext())
ok, err := c.checkRetestLimitReached(prNumber, msg)
if err != nil {
log.Printf("failed to check retest limit %v\n", err)
continue
}
if ok {
log.Printf("Pull Request %d: %q reached maximum attempt. skipping retest %v\n", prNumber, r.GetContext(), retry)
continue
}
comment := &github.IssueComment{
Body: github.String(msg),
}
_, _, err = c.client.Issues.CreateComment(context.TODO(), c.owner, c.repo, prNumber, comment)
if err != nil {
log.Printf("failed to create comment %v\n", err)
continue
}
// Post comment with target URL for retesting
msg = fmt.Sprintf("@%s %q test failed. Logs are available at [location](%s) for debugging", re.GetUser().GetLogin(), r.GetContext(), r.GetTargetURL())
comment.Body = github.String(msg)
_, _, err = c.client.Issues.CreateComment(context.TODO(), c.owner, c.repo, prNumber, comment)
if err != nil {
log.Printf("failed to create comment %v\n", err)
continue
}
}
}
if failedTestFound {
// comment `@Mergifyio requeue` so mergifyio adds the pr back into the queue.
msg := "@Mergifyio requeue"
comment := &github.IssueComment{
Body: github.String(msg),
}
_, _, err = c.client.Issues.CreateComment(context.TODO(), c.owner, c.repo, prNumber, comment)
if err != nil {
log.Printf("failed to create comment %q: %v\n", msg, err)
}
// exit after adding retests to a pr once to avoid retesting multiple prs
// at the same time.
break
}
}
}
}
}
// checkPRRequiredApproval check PullRequest has required approvals.
func (c *retestConfig) checkPRRequiredApproval(prNumber int) bool {
opts := github.ListOptions{PerPage: 100} // defaults to 30 reviews, too few sometimes
rev, _, err := c.client.PullRequests.ListReviews(context.TODO(), c.owner, c.repo, prNumber, &opts)
if err != nil {
log.Printf("failed to list reviews %v\n", err)
return false
}
approvedReviews := 0
for _, rv := range rev {
if rv.GetState() == "APPROVED" {
approvedReviews += 1
}
}
if !(approvedReviews >= totalRequiredReviews) {
log.Printf("total approved reviews for PR %d are %d but required %d", prNumber, approvedReviews, totalRequiredReviews)
return false
}
return true
}
// checkRetestLimitReached check if retest limit is reached.
func (c *retestConfig) checkRetestLimitReached(prNumber int, msg string) (bool, error) {
creq, _, err := c.client.Issues.ListComments(context.TODO(), c.owner, c.repo, prNumber, &github.IssueListCommentsOptions{})
if err != nil {
return false, err
}
retestCount := 0
for _, pc := range creq {
if pc.GetBody() == msg {
retestCount += 1
}
}
log.Printf("found %d retries and remaining %d retries\n", retestCount, retry-retestCount)
if retestCount >= int(retry) {
return true, nil
}
return false, nil
}
// filterStatusesList returns list of unique and recently updated github RepoStatuses.
// Raw github RepoStatus list may contain duplicate and older statuses.
func filterStatusList(rawStatusList []*github.RepoStatus) []*github.RepoStatus {
testStatus := make(map[string]*github.RepoStatus)
for _, r := range rawStatusList {
status, ok := testStatus[r.GetContext()]
if !ok || r.GetUpdatedAt().After(status.GetUpdatedAt()) {
testStatus[r.GetContext()] = r
}
}
statusList := make([]*github.RepoStatus, 0)
for _, rs := range testStatus {
statusList = append(statusList, rs)
}
return statusList
}