forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
269 lines (247 loc) · 8.19 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
/*
Copyright 2017 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.
*/
// Commenter provides a way to --query for issues and append a --comment to matches.
//
// The --token determines who interacts with github.
// By default commenter runs in dry mode, add --confirm to make it leave comments.
// The --updated, --include-closed, --ceiling options provide minor safeguards
// around leaving excessive comments.
package main
import (
"bytes"
"errors"
"flag"
"fmt"
"log"
"math/rand"
"net/url"
"regexp"
"strconv"
"strings"
"text/template"
"time"
"k8s.io/test-infra/prow/config/secret"
"k8s.io/test-infra/prow/flagutil"
"k8s.io/test-infra/prow/github"
)
const (
templateHelp = `--comment is a golang text/template if set.
Valid placeholders:
.Org - github org
.Repo - github repo
.Number - issue number
Advanced (see kubernetes/test-infra/prow/github/types.go):
.Issue.User.Login - github account
.Issue.Title
.Issue.State
.Issue.HTMLURL
.Issue.Assignees - list of assigned .Users
.Issue.Labels - list of applied labels (.Name)
`
)
func flagOptions() options {
o := options{
endpoint: flagutil.NewStrings(github.DefaultAPIEndpoint),
}
flag.StringVar(&o.query, "query", "", "See https://help.github.com/articles/searching-issues-and-pull-requests/")
flag.DurationVar(&o.updated, "updated", 2*time.Hour, "Filter to issues unmodified for at least this long if set")
flag.BoolVar(&o.includeArchived, "include-archived", false, "Match archived issues if set")
flag.BoolVar(&o.includeClosed, "include-closed", false, "Match closed issues if set")
flag.BoolVar(&o.includeLocked, "include-locked", false, "Match locked issues if set")
flag.BoolVar(&o.confirm, "confirm", false, "Mutate github if set")
flag.StringVar(&o.comment, "comment", "", "Append the following comment to matching issues")
flag.BoolVar(&o.useTemplate, "template", false, templateHelp)
flag.IntVar(&o.ceiling, "ceiling", 3, "Maximum number of issues to modify, 0 for infinite")
flag.Var(&o.endpoint, "endpoint", "GitHub's API endpoint")
flag.StringVar(&o.graphqlEndpoint, "graphql-endpoint", github.DefaultGraphQLEndpoint, "GitHub's GraphQL API Endpoint")
flag.StringVar(&o.token, "token", "", "Path to github token")
flag.BoolVar(&o.random, "random", false, "Choose random issues to comment on from the query")
flag.Parse()
return o
}
type meta struct {
Number int
Org string
Repo string
Issue github.Issue
}
type options struct {
asc bool
ceiling int
comment string
includeArchived bool
includeClosed bool
includeLocked bool
useTemplate bool
query string
sort string
endpoint flagutil.Strings
graphqlEndpoint string
token string
updated time.Duration
confirm bool
random bool
}
func parseHTMLURL(url string) (string, string, int, error) {
// Example: https://github.com/batterseapower/pinyin-toolkit/issues/132
re := regexp.MustCompile(`.+/(.+)/(.+)/(issues|pull)/(\d+)$`)
mat := re.FindStringSubmatch(url)
if mat == nil {
return "", "", 0, fmt.Errorf("failed to parse: %s", url)
}
n, err := strconv.Atoi(mat[4])
if err != nil {
return "", "", 0, err
}
return mat[1], mat[2], n, nil
}
func makeQuery(query string, includeArchived, includeClosed, includeLocked bool, minUpdated time.Duration) (string, error) {
// GitHub used to allow \n but changed it at some point to result in no results at all
query = strings.ReplaceAll(query, "\n", " ")
parts := []string{query}
if !includeArchived {
if strings.Contains(query, "archived:true") {
return "", errors.New("archived:true requires --include-archived")
}
parts = append(parts, "archived:false")
} else if strings.Contains(query, "archived:false") {
return "", errors.New("archived:false conflicts with --include-archived")
}
if !includeClosed {
if strings.Contains(query, "is:closed") {
return "", errors.New("is:closed requires --include-closed")
}
parts = append(parts, "is:open")
} else if strings.Contains(query, "is:open") {
return "", errors.New("is:open conflicts with --include-closed")
}
if !includeLocked {
if strings.Contains(query, "is:locked") {
return "", errors.New("is:locked requires --include-locked")
}
parts = append(parts, "is:unlocked")
} else if strings.Contains(query, "is:unlocked") {
return "", errors.New("is:unlocked conflicts with --include-locked")
}
if minUpdated != 0 {
latest := time.Now().Add(-minUpdated)
parts = append(parts, "updated:<="+latest.Format(time.RFC3339))
}
return strings.Join(parts, " "), nil
}
type client interface {
CreateComment(owner, repo string, number int, comment string) error
FindIssues(query, sort string, asc bool) ([]github.Issue, error)
}
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
o := flagOptions()
if o.query == "" {
log.Fatal("empty --query")
}
if o.token == "" {
log.Fatal("empty --token")
}
if o.comment == "" {
log.Fatal("empty --comment")
}
if err := secret.Add(o.token); err != nil {
log.Fatalf("Error starting secrets agent: %v", err)
}
var err error
for _, ep := range o.endpoint.Strings() {
_, err = url.ParseRequestURI(ep)
if err != nil {
log.Fatalf("Invalid --endpoint URL %q: %v.", ep, err)
}
}
var c client
if o.confirm {
c = github.NewClient(secret.GetTokenGenerator(o.token), secret.Censor, o.graphqlEndpoint, o.endpoint.Strings()...)
} else {
c = github.NewDryRunClient(secret.GetTokenGenerator(o.token), secret.Censor, o.graphqlEndpoint, o.endpoint.Strings()...)
}
query, err := makeQuery(o.query, o.includeArchived, o.includeClosed, o.includeLocked, o.updated)
if err != nil {
log.Fatalf("Bad query %q: %v", o.query, err)
}
sort := ""
asc := false
if o.updated > 0 {
sort = "updated"
asc = true
}
commenter := makeCommenter(o.comment, o.useTemplate)
if err := run(c, query, sort, asc, o.random, commenter, o.ceiling); err != nil {
log.Fatalf("Failed run: %v", err)
}
}
func makeCommenter(comment string, useTemplate bool) func(meta) (string, error) {
if !useTemplate {
return func(_ meta) (string, error) {
return comment, nil
}
}
t := template.Must(template.New("comment").Parse(comment))
return func(m meta) (string, error) {
out := bytes.Buffer{}
err := t.Execute(&out, m)
return out.String(), err
}
}
func run(c client, query, sort string, asc, random bool, commenter func(meta) (string, error), ceiling int) error {
log.Printf("Searching: %s", query)
issues, err := c.FindIssues(query, sort, asc)
if err != nil {
return fmt.Errorf("search failed: %w", err)
}
problems := []string{}
log.Printf("Found %d matches", len(issues))
if random {
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(issues), func(i, j int) {
issues[i], issues[j] = issues[j], issues[i]
})
}
for n, i := range issues {
if ceiling > 0 && n == ceiling {
log.Printf("Stopping at --ceiling=%d of %d results", n, len(issues))
break
}
log.Printf("Matched %s (%s)", i.HTMLURL, i.Title)
org, repo, number, err := parseHTMLURL(i.HTMLURL)
if err != nil {
msg := fmt.Sprintf("Failed to parse %s: %v", i.HTMLURL, err)
log.Print(msg)
problems = append(problems, msg)
}
comment, err := commenter(meta{Number: number, Org: org, Repo: repo, Issue: i})
if err != nil {
msg := fmt.Sprintf("Failed to create comment for %s/%s#%d: %v", org, repo, number, err)
log.Print(msg)
problems = append(problems, msg)
continue
}
if err := c.CreateComment(org, repo, number, comment); err != nil {
msg := fmt.Sprintf("Failed to apply comment to %s/%s#%d: %v", org, repo, number, err)
log.Print(msg)
problems = append(problems, msg)
continue
}
log.Printf("Commented on %s", i.HTMLURL)
}
if len(problems) > 0 {
return fmt.Errorf("encoutered %d failures: %v", len(problems), problems)
}
return nil
}