From 0640e3e9a6357db7f55770e49d5dffeb42deab2f Mon Sep 17 00:00:00 2001 From: Ivan Malopinsky Date: Thu, 25 Apr 2019 00:11:07 -0400 Subject: [PATCH] stop iterating PRs when max age reached; use updated_at instead of closed_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 --- Makefile | 2 +- main.go | 14 ++++++++------ main_test.go | 5 +++-- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 405ca00..f8ab960 100644 --- a/Makefile +++ b/Makefile @@ -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') diff --git a/main.go b/main.go index 9bef6d9..af16ce9 100644 --- a/main.go +++ b/main.go @@ -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"` @@ -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 { @@ -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 { diff --git a/main_test.go b/main_test.go index f3065b3..99f9e8d 100644 --- a/main_test.go +++ b/main_test.go @@ -62,6 +62,7 @@ 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() @@ -69,7 +70,7 @@ func TestListClosedPullRequests(t *testing.T) { for i := range prs { prs[i].Number = uint32(i) + 1 - prs[i].ClosedAt = now + prs[i].UpdatedAt = now } firstPageJSON, _ := json.Marshal(prs) @@ -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)