Skip to content
This repository has been archived by the owner on Mar 10, 2024. It is now read-only.

Remove pagination limit on label copy #42

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 10 additions & 1 deletion migration/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type fakeClient struct {
baseURL error
}
labels []*glab.Label
labelsPage2 []*glab.Label
milestones []*glab.Milestone
users []*glab.User
issues []*glab.Issue
Expand Down Expand Up @@ -91,7 +92,15 @@ func (c *fakeClient) ListLabels(id interface{}, opt *glab.ListLabelsOptions, opt
if err != nil {
return nil, nil, err
}
return c.labels, nil, nil

switch page := opt.Page; page {
case 1:
return c.labels, nil, nil
case 2:
return c.labelsPage2, nil, nil
default:
return make([]*glab.Label, 0), nil, nil
}
}

func (c *fakeClient) ListMilestones(id interface{}, opt *glab.ListMilestonesOptions, options ...glab.OptionFunc) ([]*glab.Milestone, *glab.Response, error) {
Expand Down
18 changes: 15 additions & 3 deletions migration/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var (
const (
// ResultsPerPage is the Number of results per page.
ResultsPerPage = 100
LabelsPerPage = 100
)

// Endpoint refers to the GitLab server endpoints.
Expand Down Expand Up @@ -313,10 +314,21 @@ func (m *Migration) Migrate() error {
s := make([]issueId, 0)

// Copy all source labels on target
labels, _, err := source.ListLabels(srcProjectID, nil)
if err != nil {
return fmt.Errorf("source: can't fetch labels: %s", err.Error())
currentLabelPage := 1
labels := make([]*glab.Label, 0)
for {
paginatedLabels, _, err := source.ListLabels(srcProjectID, &glab.ListLabelsOptions{PerPage: LabelsPerPage, Page: currentLabelPage})
if err != nil {
return fmt.Errorf("source: can't fetch labels: %s", err.Error())
}
if len(paginatedLabels) == 0 {
break
}

labels = append(labels, paginatedLabels...)
currentLabelPage++
}

fmt.Printf("Found %d labels ...\n", len(labels))
for _, label := range labels {
clopts := &glab.CreateLabelOptions{Name: &label.Name, Color: &label.Color, Description: &label.Description}
Expand Down
17 changes: 17 additions & 0 deletions migration/issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,23 @@ func TestMigrate(t *testing.T) {
}
},
},
{
"copy multiple pages labels",
cfg1,
func(src, dst *fakeClient) {
src.labels = makeLabels("bug", "doc")
src.labelsPage2 = makeLabels("foo", "bar")
},
func(err error, src, dst *fakeClient) {
require.NoError(err)
if assert.Equal(4, len(dst.labels)) {
assert.Equal("bug", dst.labels[0].Name)
assert.Equal("doc", dst.labels[1].Name)
assert.Equal("foo", dst.labels[2].Name)
assert.Equal("bar", dst.labels[3].Name)
}
},
},
{
"copy 1 label and 2 issues",
cfg2,
Expand Down
25 changes: 19 additions & 6 deletions stats/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
glab "github.com/xanzy/go-gitlab"
)

const (
LabelsPerPage = 100
)

type ProjectStats struct {
Project *glab.Project
NbIssues, NbClosed, NbOpened, NbNotes int
Expand Down Expand Up @@ -84,13 +88,22 @@ func (p *ProjectStats) ComputeStats(client gitlab.GitLaber) error {
return err
}

labels, _, err := client.ListLabels(p.Project.ID, nil)
if err != nil {
return fmt.Errorf("source: can't fetch labels: %s", err.Error())
}
for _, label := range labels {
p.Labels[label.Name]++
currentLabelPage := 1
for {
paginatedLabels, _, err := client.ListLabels(p.Project.ID, &glab.ListLabelsOptions{PerPage: LabelsPerPage, Page: currentLabelPage})
if err != nil {
return fmt.Errorf("source: can't fetch labels: %s", err.Error())
}
if len(paginatedLabels) == 0 {
break
}

for _, label := range paginatedLabels {
p.Labels[label.Name]++
}
currentLabelPage++
}

return nil
}

Expand Down