forked from imsky/github-fresh
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
386 lines (310 loc) · 9.08 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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"log"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
)
// todo: docs
// todo: GitHub action
// todo: fix blank build vars when using go build
// todo: add minimum age of pull request
// todo: close old pull requests
// variables used to embed build information in the binary
var (
BuildTime string
BuildSHA string
Version string
)
var crash = log.Fatalf
type pullRequest struct {
Number uint32 `json:"number"`
UpdatedAt time.Time `json:"updated_at"`
Head struct {
Ref string `json:"ref"`
SHA string `json:"sha"`
} `json:"head"`
}
type branch struct {
Name string `json:"name"`
Commit struct {
SHA string `json:"sha"`
} `json:"commit"`
}
// Executor provides runtime configuration and facilities
type Executor struct {
client *http.Client
token string
http bool
dry bool
}
// NewExecutor returns a new executor of GitHub operations
func NewExecutor(token string, dry bool) *Executor {
ex := Executor{
client: &http.Client{},
token: token,
dry: dry,
}
return &ex
}
func (ex *Executor) makeRequest(method string, url string, retries int) (res *http.Response, err error) {
protocol := "https"
if ex.http {
protocol = "http"
}
req, err := http.NewRequest(method, protocol+"://api.github.com/"+url, nil)
if err != nil {
return nil, err
}
req.Header.Add("accept", "application/vnd.github.v3+json")
req.Header.Add("Authorization", "token "+ex.token)
for attempt, retry := 1, true; retry; attempt++ {
res, _ = ex.client.Do(req)
if res.StatusCode >= 400 {
log.Println("Request failed (" + res.Status + ") - retrying...")
if attempt > retries {
return res, errors.New(res.Status)
}
} else {
retry = false
}
}
return res, nil
}
func (ex *Executor) listClosedPullRequests(user string, repo string, days int) ([]pullRequest, error) {
pullRequests := make([]pullRequest, 0, 1)
now := time.Now()
maxAgeHours := float64(days*24) + 0.01
for page, keepGoing := 1, true; keepGoing; page++ {
res, err := ex.makeRequest("GET", "repos/"+user+"/"+repo+"/pulls?state=closed&sort=updated&direction=desc&per_page=100&page="+strconv.Itoa(page), 5)
if err != nil {
return pullRequests, errors.New("failed to get closed pull requests (" + err.Error() + ")")
}
d := json.NewDecoder(res.Body)
var prs struct {
PullRequests []pullRequest
}
err = d.Decode(&prs.PullRequests)
if err != nil {
return pullRequests, errors.New("failed to parse closed pull requests (" + err.Error() + ")")
}
for _, pr := range prs.PullRequests {
prAge := now.Sub(pr.UpdatedAt).Hours()
if prAge > maxAgeHours {
keepGoing = false
break
}
pullRequests = append(pullRequests, pr)
}
if len(prs.PullRequests) == 0 || len(prs.PullRequests) < 100 {
break
}
}
return pullRequests, nil
}
func (ex *Executor) listOpenPullRequests(user string, repo string) ([]pullRequest, error) {
pullRequests := make([]pullRequest, 0, 1)
for page, keepGoing := 1, true; keepGoing; page++ {
res, err := ex.makeRequest("GET", "repos/"+user+"/"+repo+"/pulls?state=open&sort=updated&direction=desc&per_page=100&page="+strconv.Itoa(page), 5)
if err != nil {
return pullRequests, errors.New("failed to get open pull requests (" + err.Error() + ")")
}
d := json.NewDecoder(res.Body)
var prs struct {
PullRequests []pullRequest
}
err = d.Decode(&prs.PullRequests)
if err != nil {
return pullRequests, errors.New("failed to parse open pull requests (" + err.Error() + ")")
}
pullRequests = append(pullRequests, prs.PullRequests...)
if len(prs.PullRequests) == 0 || len(prs.PullRequests) < 100 {
break
}
}
return pullRequests, nil
}
func (ex *Executor) listUnprotectedBranches(user string, repo string) ([]branch, error) {
branches := make([]branch, 0, 1)
for page := 1; ; page++ {
res, err := ex.makeRequest("GET", "repos/"+user+"/"+repo+"/branches?protected=false&per_page=100&page="+strconv.Itoa(page), 5)
if err != nil {
return branches, errors.New("failed to get branches (" + err.Error() + ")")
}
d := json.NewDecoder(res.Body)
var bs struct {
Branches []branch
}
err = d.Decode(&bs.Branches)
if err != nil {
return branches, errors.New("failed to parse branches (" + err.Error() + ")")
}
branches = append(branches, bs.Branches...)
if len(bs.Branches) == 0 || len(bs.Branches) < 100 {
break
}
}
return branches, nil
}
func (ex *Executor) deleteBranches(user string, repo string, branches []string) (int, error) {
deletedBranches := 0
for _, branch := range branches {
if ex.dry {
log.Println("Will delete branch", branch)
continue
}
_, err := ex.makeRequest("DELETE", "repos/"+user+"/"+repo+"/git/refs/heads/"+url.QueryEscape(branch), 5)
if err != nil {
return deletedBranches, errors.New("failed to delete branch " + branch + " (" + err.Error() + ")")
}
log.Println("Deleted branch", branch)
deletedBranches++
}
return deletedBranches, nil
}
func getStaleBranches(closedBranches []branch, closedPullRequests []pullRequest, openPullRequests []pullRequest) []string {
branchShaMap := make(map[string]string) // Map[branch_name]branch_SHA
staleBranchMap := make(map[string]bool) // Map[branch_name]is_stale
for _, b := range closedBranches {
branchShaMap[b.Name] = b.Commit.SHA
}
for _, pr := range closedPullRequests {
staleBranchSHA, branchExists := branchShaMap[pr.Head.Ref]
if branchExists && staleBranchSHA == pr.Head.SHA {
staleBranchMap[pr.Head.Ref] = true
}
}
// If we've marked this branch as stale, but there is another open PR tied to the branch, unmark as stale
for _, pr := range openPullRequests {
_, branchExists := staleBranchMap[pr.Head.Ref]
if branchExists {
staleBranchMap[pr.Head.Ref] = false
}
}
staleBranches := make([]string, 0, 1)
for branch, isStale := range staleBranchMap {
if isStale {
staleBranches = append(staleBranches, branch)
}
}
return staleBranches
}
func getConfig() struct {
token, user, repo string
days int
dry bool
} {
var config struct {
token string
user string
repo string
days int
dry bool
}
for _, s := range []string{"GITHUB_FRESH_TOKEN", "GITHUB_TOKEN"} {
if os.Getenv(s) != "" {
config.token = os.Getenv(s)
}
}
githubRepository := os.Getenv("GITHUB_REPOSITORY")
if githubRepository != "" && strings.Contains(githubRepository, "/") {
split := strings.Split(githubRepository, "/")
config.user = split[0]
config.repo = split[1]
}
if config.user == "" {
config.user = os.Getenv("GITHUB_FRESH_USER")
}
if config.repo == "" {
config.repo = os.Getenv("GITHUB_FRESH_REPO")
}
envDays := os.Getenv("GITHUB_FRESH_DAYS")
if envDays != "" {
d, err := strconv.Atoi(envDays)
if err == nil {
config.days = d
}
}
if config.days == 0 {
config.days = 1
}
envDry := os.Getenv("GITHUB_FRESH_DRY")
if envDry != "" {
r, err := strconv.ParseBool(envDry)
if err == nil {
config.dry = r
}
}
return config
}
// Run finds branches of recently closed pull requests and deletes them
func Run(user string, repo string, days int, ex Executor) error {
switch {
case user == "":
return errors.New("missing user")
case repo == "":
return errors.New("missing repo")
case days < 1:
return errors.New("invalid value for days (" + strconv.Itoa(days) + ")")
}
var err error
log.Println("Getting closed PRs...")
closedPullRequests, err := ex.listClosedPullRequests(user, repo, days)
if err != nil {
return err
}
log.Println("Collected " + strconv.Itoa(len(closedPullRequests)) + " closed PRs")
log.Println("Getting open PRs...")
openPullRequests, err := ex.listOpenPullRequests(user, repo)
if err != nil {
return err
}
log.Println("Collected " + strconv.Itoa(len(openPullRequests)) + " open PRs")
log.Println("Getting unprotected branches...")
unprotectedBranches, err := ex.listUnprotectedBranches(user, repo)
if err != nil {
return err
}
log.Println("Found " + strconv.Itoa(len(unprotectedBranches)) + " unprotected branches")
log.Println("Finding stale branches to delete")
staleBranches := getStaleBranches(unprotectedBranches, closedPullRequests, openPullRequests)
log.Println("Deleting branches...")
db, err := ex.deleteBranches(user, repo, staleBranches)
if err != nil {
return err
}
log.Println("Deleted " + strconv.Itoa(db) + " branches")
return nil
}
func setupUsage() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "github-fresh v"+Version+" "+BuildTime+" "+BuildSHA+"\n\n")
flag.PrintDefaults()
}
}
func main() {
config := getConfig()
var token = flag.String("token", config.token, "GitHub API token (GITHUB_FRESH_TOKEN)")
var user = flag.String("user", config.user, "GitHub user (GITHUB_FRESH_USER)")
var repo = flag.String("repo", config.repo, "GitHub repo (GITHUB_FRESH_REPO)")
var days = flag.Int("days", config.days, "Max age in days of checked pull requests (GITHUB_FRESH_DAYS)")
var dry = flag.Bool("dry", config.dry, "Dry run (GITHUB_FRESH_DRY)")
setupUsage()
flag.Parse()
if flag.NFlag() == 0 {
flag.Usage()
os.Exit(1)
}
ex := NewExecutor(*token, *dry)
err := Run(*user, *repo, *days, *ex)
if err != nil {
crash(err.Error())
}
}