-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Restrict fetching PRs and issues only to time range needed #103
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -238,14 +238,18 @@ func keepPRsWithCommits(prs []ghPullRequest, commits []string, filters ...prFilt | |
} | ||
|
||
//nolint:funlen | ||
func fetchMergedPRs(user, repo string) ([]ghPullRequest, error) { | ||
func fetchMergedPRs(user, repo string, since *time.Time) ([]ghPullRequest, error) { | ||
src := oauth2.StaticTokenSource( | ||
// TODO: DI this | ||
&oauth2.Token{AccessToken: os.Getenv("GITHUB_TOKEN")}, | ||
) | ||
httpClient := oauth2.NewClient(context.Background(), src) | ||
client := githubv4.NewClient(httpClient) | ||
var allPRs []ghPullRequest | ||
var ( | ||
pages = 0 | ||
saw = 0 | ||
allPRs []ghPullRequest | ||
) | ||
|
||
{ | ||
// TODO: act on hitting a rate limit | ||
|
@@ -276,8 +280,9 @@ func fetchMergedPRs(user, repo string) ([]ghPullRequest, error) { | |
MergeCommit struct { | ||
OID githubv4.String | ||
} | ||
MergedAt githubv4.DateTime | ||
Labels struct { | ||
UpdatedAt githubv4.DateTime | ||
MergedAt githubv4.DateTime | ||
Labels struct { | ||
Edges []struct { | ||
Node struct { | ||
Name githubv4.String | ||
|
@@ -305,7 +310,7 @@ func fetchMergedPRs(user, repo string) ([]ghPullRequest, error) { | |
} `graphql:"closingIssuesReferences(last:10)"` | ||
} | ||
} | ||
} `graphql:"pullRequests(first:100, states:MERGED, after:$prCursor)"` | ||
} `graphql:"pullRequests(first:100, states:MERGED, after:$prCursor, orderBy:{field: UPDATED_AT, direction: DESC})"` | ||
} `graphql:"repository(owner:$repositoryOwner, name:$repositoryName)"` | ||
|
||
RateLimit rateLimit | ||
|
@@ -317,14 +322,28 @@ func fetchMergedPRs(user, repo string) ([]ghPullRequest, error) { | |
} | ||
|
||
// var limit rateLimit | ||
for { | ||
var ( | ||
process bool | ||
terminate = false | ||
) | ||
|
||
for !terminate { | ||
log.WithFields("user", user, "repo", repo, "page", pages).Trace("fetching merged PRs from github.com") | ||
|
||
err := client.Query(context.Background(), &query, variables) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// limit = query.RateLimit | ||
|
||
for _, prEdge := range query.Repository.PullRequests.Edges { | ||
saw++ | ||
process, terminate = checkSearchTermination(since, &prEdge.Node.UpdatedAt, &prEdge.Node.MergedAt) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm trying to understand the logic here. What I think is happening:
Is that right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yup yup -- right on. The go case is that a PR isn't updated after it's merged, so |
||
if !process || terminate { | ||
continue | ||
} | ||
|
||
var labels []string | ||
for _, lEdge := range prEdge.Node.Labels.Edges { | ||
labels = append(labels, string(lEdge.Node.Name)) | ||
|
@@ -359,8 +378,28 @@ func fetchMergedPRs(user, repo string) ([]ghPullRequest, error) { | |
break | ||
} | ||
variables["prCursor"] = githubv4.NewString(query.Repository.PullRequests.PageInfo.EndCursor) | ||
pages++ | ||
} | ||
} | ||
|
||
log.WithFields("kept", len(allPRs), "saw", saw, "pages", pages, "since", since).Trace("merged PRs fetched from github.com") | ||
|
||
return allPRs, nil | ||
} | ||
|
||
func checkSearchTermination(since *time.Time, updatedAt, closedAt *githubv4.DateTime) (process bool, terminate bool) { | ||
process = true | ||
if since == nil { | ||
return | ||
} | ||
|
||
if closedAt.Before(*since) { | ||
process = false | ||
} | ||
|
||
if updatedAt.Before(*since) { | ||
terminate = true | ||
} | ||
|
||
return | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this meant to be commented out? (I see it's not part of this diff, so not a blocking comment; just surprised.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, this is a partial implementation of #9