Skip to content

Commit

Permalink
stop iterating PRs when max age reached; use updated_at instead of cl…
Browse files Browse the repository at this point in the history
…osed_at

there's no way to sort by closed_at and iteration can stop early if
a PR closed before it's updated is encountered
  • Loading branch information
imsky committed Apr 25, 2019
1 parent cb87db7 commit 0640e3e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NAME=github-fresh
VERSION=0.6.0
VERSION=0.7.0
COMMIT=$(shell git rev-parse --short=7 HEAD)
TIMESTAMP:=$(shell date -u '+%Y-%m-%dT%I:%M:%SZ')

Expand Down
14 changes: 8 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ var (
var crash = log.Fatalf

type pullRequest struct {
Number uint32 `json:"number"`
ClosedAt time.Time `json:"closed_at"`
Number uint32 `json:"number"`
UpdatedAt time.Time `json:"updated_at"`

Head struct {
Ref string `json:"ref"`
Expand Down Expand Up @@ -84,7 +84,7 @@ func (ex *Executor) listClosedPullRequests(user string, repo string, days int) (
now := time.Now()
maxAgeHours := float64(days*24) + 0.01

for page := 1; ; page++ {
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))

if err != nil {
Expand All @@ -102,10 +102,12 @@ func (ex *Executor) listClosedPullRequests(user string, repo string, days int) (
}

for _, pr := range prs.PullRequests {
prAge := now.Sub(pr.ClosedAt).Hours()
if prAge <= maxAgeHours {
pullRequests = append(pullRequests, pr)
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 {
Expand Down
5 changes: 3 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,15 @@ func TestDryRun(t *testing.T) {
}
}

//todo: test a full third page to make sure the function doesn't keep iterating through pages of old pull requests
func TestListClosedPullRequests(t *testing.T) {
now := time.Now()

prs := make([]pullRequest, 100)

for i := range prs {
prs[i].Number = uint32(i) + 1
prs[i].ClosedAt = now
prs[i].UpdatedAt = now
}

firstPageJSON, _ := json.Marshal(prs)
Expand All @@ -81,7 +82,7 @@ func TestListClosedPullRequests(t *testing.T) {

for i := range prs {
prs[i].Number += +100
prs[i].ClosedAt = now.AddDate(0, 0, 0-(i+1))
prs[i].UpdatedAt = now.AddDate(0, 0, 0-(i+1))
}

secondPageJSON, _ := json.Marshal(prs)
Expand Down

0 comments on commit 0640e3e

Please sign in to comment.