From cb62be4973b888bc34ed31938b33a32a85a1f7b5 Mon Sep 17 00:00:00 2001 From: zhuangqh Date: Sat, 28 Apr 2018 00:54:07 +0800 Subject: [PATCH] bugfix: get pull request info with pagination --- server/gh/issues.go | 39 ++++++++++++++++++++++++++++++++++----- server/reporter/weekly.go | 19 +++++++++---------- 2 files changed, 43 insertions(+), 15 deletions(-) diff --git a/server/gh/issues.go b/server/gh/issues.go index 0474f20..e1ecb7c 100644 --- a/server/gh/issues.go +++ b/server/gh/issues.go @@ -170,13 +170,42 @@ func (c *Client) IssueHasLabel(num int, inputLabel string) bool { } // SearchIssues searches issues. -func (c *Client) SearchIssues(query string, opt *github.SearchOptions) (*github.IssuesSearchResult, error) { +// search result's wrapper is never be nil. +func (c *Client) SearchIssues(query string, opt *github.SearchOptions, all bool) (*github.IssuesSearchResult, error) { c.Mutex.Lock() defer c.Mutex.Unlock() - issueSearchResult, _, err := c.Search.Issues(context.Background(), query, opt) - if err != nil { - logrus.Errorf("failed to search issues by query %s", query) - return nil, err + + if all && opt == nil { + opt = new(github.SearchOptions) + opt.Page = 1 // first page. + opt.PerPage = 30 } + + total := 0 + issueSearchResult := &github.IssuesSearchResult{Total: &total} + + for { + result, resp, err := c.Search.Issues(context.Background(), query, opt) + if err != nil { + logrus.Errorf("failed to search issues by query %s", query) + return nil, err + } + if result.Total == nil { + break + } + total += *result.Total + issueSearchResult.Issues = append(issueSearchResult.Issues, result.Issues...) + + // just retrieve a page. + if !all { + break + } + + if resp.NextPage == 0 { + break + } + opt.Page = resp.NextPage + } + return issueSearchResult, nil } diff --git a/server/reporter/weekly.go b/server/reporter/weekly.go index 638f963..258c681 100644 --- a/server/reporter/weekly.go +++ b/server/reporter/weekly.go @@ -32,7 +32,7 @@ type SimplePR struct { } func (r *Reporter) weeklyReport() error { - wr, err := r.construcWeekReport() + wr, err := r.constructWeekReport() if err != nil { return err } @@ -43,7 +43,7 @@ func (r *Reporter) weeklyReport() error { return r.client.CreateIssue(issueTitle, issueBody) } -func (r *Reporter) construcWeekReport() (WeekReport, error) { +func (r *Reporter) constructWeekReport() (WeekReport, error) { var wr WeekReport now := time.Now() @@ -71,23 +71,22 @@ func (r *Reporter) construcWeekReport() (WeekReport, error) { logrus.Infof("Start: %s, End: %s", wr.StartDate, wr.EndDate) query := fmt.Sprintf("is:merged type:pr repo:%s/%s merged:>=%s", r.client.Owner(), r.client.Repo(), wr.StartDate) - issueSearchResult, err := r.client.SearchIssues(query, nil) + issueSearchResult, err := r.client.SearchIssues(query, nil, true) if err != nil { return wr, err } - // SearchIssues returns a list of issue, and we can treat them as pull request as well. - prs := issueSearchResult.Issues - - r.setContributorAndCommits(&wr, prs) + r.setContributorAndPRSummary(&wr, issueSearchResult) return wr, nil } -func (r *Reporter) setContributorAndCommits(wr *WeekReport, prs []github.Issue) { - wr.CountOfPR = len(prs) +func (r *Reporter) setContributorAndPRSummary(wr *WeekReport, issueSearchResult *github.IssuesSearchResult) { + wr.CountOfPR = issueSearchResult.GetTotal() wr.MergedPR = map[string][]*SimplePR{} - for _, pr := range prs { + + // SearchIssues returns a list of issue, and we can treat them as pull request as well. + for _, pr := range issueSearchResult.Issues { comments, err := r.client.ListComments(*pr.Number) if err != nil { continue