Skip to content
This repository was archived by the owner on Apr 17, 2025. It is now read-only.

Commit 31b5ebf

Browse files
authored
Merge pull request #2 from better/PLAT-1242/retry_requests
PLAT-1242 | Add retry mechanism for API requests
2 parents 354a7f0 + b414f6e commit 31b5ebf

2 files changed

Lines changed: 34 additions & 11 deletions

File tree

main.go

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"log"
99
"net/http"
10+
"net/url"
1011
"os"
1112
"strconv"
1213
"strings"
@@ -65,7 +66,7 @@ func NewExecutor(token string, dry bool) *Executor {
6566
return &ex
6667
}
6768

68-
func (ex *Executor) makeRequest(method string, url string) (res *http.Response, err error) {
69+
func (ex *Executor) makeRequest(method string, url string, retries int) (res *http.Response, err error) {
6970
protocol := "https"
7071
if ex.http {
7172
protocol = "http"
@@ -76,12 +77,25 @@ func (ex *Executor) makeRequest(method string, url string) (res *http.Response,
7677
return nil, err
7778
}
7879

80+
req.Header.Add("accept", "application/vnd.github.v3+json")
7981
req.Header.Add("Authorization", "token "+ex.token)
80-
res, _ = ex.client.Do(req)
81-
if res.StatusCode >= 400 {
82-
return res, errors.New(res.Status)
82+
83+
for attempt, retry := 1, true; retry; attempt++ {
84+
85+
res, _ = ex.client.Do(req)
86+
if res.StatusCode >= 400 {
87+
log.Println("Request failed (" + res.Status + ") - retrying...")
88+
89+
if attempt > retries {
90+
return res, errors.New(res.Status)
91+
}
92+
} else {
93+
retry = false
94+
}
8395
}
96+
8497
return res, nil
98+
8599
}
86100

87101
func (ex *Executor) listClosedPullRequests(user string, repo string, days int) ([]pullRequest, error) {
@@ -90,8 +104,8 @@ func (ex *Executor) listClosedPullRequests(user string, repo string, days int) (
90104
maxAgeHours := float64(days*24) + 0.01
91105

92106
for page, keepGoing := 1, true; keepGoing; page++ {
93-
res, err := ex.makeRequest("GET", "repos/"+user+"/"+repo+"/pulls?state=closed&sort=updated&direction=desc&per_page=100&page="+strconv.Itoa(page))
94107

108+
res, err := ex.makeRequest("GET", "repos/"+user+"/"+repo+"/pulls?state=closed&sort=updated&direction=desc&per_page=100&page="+strconv.Itoa(page), 5)
95109
if err != nil {
96110
return pullRequests, errors.New("failed to get closed pull requests (" + err.Error() + ")")
97111
}
@@ -127,7 +141,7 @@ func (ex *Executor) listOpenPullRequests(user string, repo string) ([]pullReques
127141
pullRequests := make([]pullRequest, 0, 1)
128142

129143
for page, keepGoing := 1, true; keepGoing; page++ {
130-
res, err := ex.makeRequest("GET", "repos/"+user+"/"+repo+"/pulls?state=open&sort=updated&direction=desc&per_page=100&page="+strconv.Itoa(page))
144+
res, err := ex.makeRequest("GET", "repos/"+user+"/"+repo+"/pulls?state=open&sort=updated&direction=desc&per_page=100&page="+strconv.Itoa(page), 5)
131145

132146
if err != nil {
133147
return pullRequests, errors.New("failed to get open pull requests (" + err.Error() + ")")
@@ -157,7 +171,7 @@ func (ex *Executor) listUnprotectedBranches(user string, repo string) ([]branch,
157171
branches := make([]branch, 0, 1)
158172

159173
for page := 1; ; page++ {
160-
res, err := ex.makeRequest("GET", "repos/"+user+"/"+repo+"/branches?protected=false&per_page=100&page="+strconv.Itoa(page))
174+
res, err := ex.makeRequest("GET", "repos/"+user+"/"+repo+"/branches?protected=false&per_page=100&page="+strconv.Itoa(page), 5)
161175

162176
if err != nil {
163177
return branches, errors.New("failed to get branches (" + err.Error() + ")")
@@ -192,8 +206,7 @@ func (ex *Executor) deleteBranches(user string, repo string, branches []string)
192206
continue
193207
}
194208

195-
_, err := ex.makeRequest("DELETE", "repos/"+user+"/"+repo+"/git/refs/heads/"+branch)
196-
209+
_, err := ex.makeRequest("DELETE", "repos/"+user+"/"+repo+"/git/refs/heads/"+url.QueryEscape(branch), 5)
197210
if err != nil {
198211
return deletedBranches, errors.New("failed to delete branch " + branch + " (" + err.Error() + ")")
199212
}
@@ -311,27 +324,37 @@ func Run(user string, repo string, days int, ex Executor) error {
311324

312325
var err error
313326

327+
log.Println("Getting closed PRs...")
314328
closedPullRequests, err := ex.listClosedPullRequests(user, repo, days)
315329
if err != nil {
316330
return err
317331
}
332+
log.Println("Collected " + strconv.Itoa(len(closedPullRequests)) + " closed PRs")
318333

334+
log.Println("Getting open PRs...")
319335
openPullRequests, err := ex.listOpenPullRequests(user, repo)
320336
if err != nil {
321337
return err
322338
}
339+
log.Println("Collected " + strconv.Itoa(len(openPullRequests)) + " open PRs")
323340

341+
log.Println("Getting unprotected branches...")
324342
unprotectedBranches, err := ex.listUnprotectedBranches(user, repo)
325343
if err != nil {
326344
return err
327345
}
346+
log.Println("Found " + strconv.Itoa(len(unprotectedBranches)) + " unprotected branches")
328347

348+
log.Println("Finding stale branches to delete")
329349
staleBranches := getStaleBranches(unprotectedBranches, closedPullRequests, openPullRequests)
350+
351+
log.Println("Deleting branches...")
330352
db, err := ex.deleteBranches(user, repo, staleBranches)
331353
if err != nil {
332354
return err
333355
}
334356
log.Println("Deleted " + strconv.Itoa(db) + " branches")
357+
335358
return nil
336359
}
337360

main_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func TestRun(t *testing.T) {
214214
"updated_at": "` + now.Format(time.RFC3339) + `",
215215
"head": {
216216
"ref": "notstalebranch",
217-
"sha": "867530990210abcdefg867530990210abcdefg"
217+
"sha": "8675309902100abcdefg8675309902100abcdefg"
218218
}
219219
}
220220
]`,
@@ -228,7 +228,7 @@ func TestRun(t *testing.T) {
228228
"updated_at": "` + now.Format(time.RFC3339) + `",
229229
"head": {
230230
"ref": "notstalebranch",
231-
"sha": "867530990210abcdefg867530990210abcdefg"
231+
"sha": "8675309902100abcdefg8675309902100abcdefg"
232232
}
233233
}
234234
]`,

0 commit comments

Comments
 (0)