From cbab578093bd34c50b8fcf81fa7b8d48e220d9b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cle=CC=81ment=20Bethuys?= Date: Fri, 6 May 2016 10:19:41 +0200 Subject: [PATCH] Fix: Paginate Gitlab issues requests --- datasource/gitlab/card.go | 21 ++++++++++++--------- modules/gitlab/issue.go | 13 ++++++++----- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/datasource/gitlab/card.go b/datasource/gitlab/card.go index b78f446..a43db20 100644 --- a/datasource/gitlab/card.go +++ b/datasource/gitlab/card.go @@ -21,17 +21,20 @@ func (ds GitLabDataSource) ListCards(board_id string) ([]*models.Card, error) { op := &gitlab.IssueListOptions{ State: "opened", } - op.Page = "1" - op.PerPage = "200" - r, err := ds.client.ListIssues(board_id, op) + op.PerPage = "100" + nextPage := "1" - if err != nil { - return nil, err - } - - for _, d := range r { - b = append(b, mapCardFromGitlab(d)) + for nextPage != "" { + op.Page = nextPage + r, next, err := ds.client.ListIssues(board_id, op) + if err != nil { + return nil, err + } + for _, d := range r { + b = append(b, mapCardFromGitlab(d)) + } + nextPage = next } return b, nil diff --git a/modules/gitlab/issue.go b/modules/gitlab/issue.go index 13990c4..187ab12 100644 --- a/modules/gitlab/issue.go +++ b/modules/gitlab/issue.go @@ -48,21 +48,24 @@ type IssueListOptions struct { // takes pagination parameters page and per_page to restrict the list of issues. // // GitLab API docs: http://doc.gitlab.com/ce/api/issues.html#list-issues -func (g *GitlabContext) ListIssues(project_id string, o *IssueListOptions) ([]*Issue, error) { +func (g *GitlabContext) ListIssues(project_id string, o *IssueListOptions) ([]*Issue, string, error) { path := getUrl([]string{"projects", url.QueryEscape(project_id), "issues"}) u, err := addOptions(path, o) if err != nil { - return nil, err + return nil, "", err } req, _ := http.NewRequest("GET", u, nil) + var ret []*Issue - if _, err := g.Do(req, &ret); err != nil { - return nil, err + resp, err := g.Do(req, &ret) + if err != nil { + return nil, "", err } - return ret, nil + nextPage := resp.Header.Get("X-Next-Page") + return ret, nextPage, nil } // CreateIssue creates a new project issue. -- GitLab