Skip to content
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

bugfix: get pull request info with pagination #10

Merged
merged 1 commit into from
May 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions server/gh/issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
19 changes: 9 additions & 10 deletions server/reporter/weekly.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand Down