diff --git a/modules/migrations/base/downloader.go b/modules/migrations/base/downloader.go index ae12e6083c863..49eb5370e166f 100644 --- a/modules/migrations/base/downloader.go +++ b/modules/migrations/base/downloader.go @@ -29,7 +29,7 @@ type Downloader interface { GetLabels() ([]*Label, error) GetIssues(page, perPage int) ([]*Issue, bool, error) GetComments(issueNumber int64) ([]*Comment, error) - GetPullRequests(page, perPage int) ([]*PullRequest, error) + GetPullRequests(page, perPage int) ([]*PullRequest, bool, error) GetReviews(pullRequestNumber int64) ([]*Review, error) } @@ -178,19 +178,20 @@ func (d *RetryDownloader) GetComments(issueNumber int64) ([]*Comment, error) { } // GetPullRequests returns a repository's pull requests with retry -func (d *RetryDownloader) GetPullRequests(page, perPage int) ([]*PullRequest, error) { +func (d *RetryDownloader) GetPullRequests(page, perPage int) ([]*PullRequest, bool, error) { var ( times = d.RetryTimes prs []*PullRequest err error + isEnd bool ) for ; times > 0; times-- { - if prs, err = d.Downloader.GetPullRequests(page, perPage); err == nil { - return prs, nil + if prs, isEnd, err = d.Downloader.GetPullRequests(page, perPage); err == nil { + return prs, isEnd, nil } time.Sleep(time.Second * time.Duration(d.RetryDelay)) } - return nil, err + return nil, false, err } // GetReviews returns pull requests reviews diff --git a/modules/migrations/git.go b/modules/migrations/git.go index 7128ee60c2af5..0aad8dbef5bb7 100644 --- a/modules/migrations/git.go +++ b/modules/migrations/git.go @@ -81,8 +81,8 @@ func (g *PlainGitDownloader) GetComments(issueNumber int64) ([]*base.Comment, er } // GetPullRequests returns pull requests according page and perPage -func (g *PlainGitDownloader) GetPullRequests(start, limit int) ([]*base.PullRequest, error) { - return nil, ErrNotSupported +func (g *PlainGitDownloader) GetPullRequests(start, limit int) ([]*base.PullRequest, bool, error) { + return nil, false, ErrNotSupported } // GetReviews returns reviews according issue number diff --git a/modules/migrations/gitea_downloader.go b/modules/migrations/gitea_downloader.go index b8da0f7c2bd6f..82c590ace8180 100644 --- a/modules/migrations/gitea_downloader.go +++ b/modules/migrations/gitea_downloader.go @@ -473,9 +473,9 @@ func (g *GiteaDownloader) GetComments(index int64) ([]*base.Comment, error) { } // GetPullRequests returns pull requests according page and perPage -func (g *GiteaDownloader) GetPullRequests(page, perPage int) ([]*base.PullRequest, error) { +func (g *GiteaDownloader) GetPullRequests(page, perPage int) ([]*base.PullRequest, bool, error) { if g == nil { - return nil, errors.New("error: GiteaDownloader is nil") + return nil, false, errors.New("error: GiteaDownloader is nil") } if perPage > g.maxPerPage { @@ -491,7 +491,7 @@ func (g *GiteaDownloader) GetPullRequests(page, perPage int) ([]*base.PullReques State: gitea_sdk.StateAll, }) if err != nil { - return nil, fmt.Errorf("error while listing repos: %v", err) + return nil, false, fmt.Errorf("error while listing repos: %v", err) } for _, pr := range prs { var milestone string @@ -527,7 +527,7 @@ func (g *GiteaDownloader) GetPullRequests(page, perPage int) ([]*base.PullReques reactions, err := g.getIssueReactions(pr.Index) if err != nil { - return nil, fmt.Errorf("error while loading reactions: %v", err) + return nil, false, fmt.Errorf("error while loading reactions: %v", err) } var assignees []string @@ -580,7 +580,7 @@ func (g *GiteaDownloader) GetPullRequests(page, perPage int) ([]*base.PullReques }) } - return allPRs, nil + return allPRs, len(prs) < perPage, nil } // GetReviews returns pull requests review diff --git a/modules/migrations/github.go b/modules/migrations/github.go index e322171614eeb..2fcbda3107748 100644 --- a/modules/migrations/github.go +++ b/modules/migrations/github.go @@ -486,7 +486,7 @@ func (g *GithubDownloaderV3) GetComments(issueNumber int64) ([]*base.Comment, er } // GetPullRequests returns pull requests according page and perPage -func (g *GithubDownloaderV3) GetPullRequests(page, perPage int) ([]*base.PullRequest, error) { +func (g *GithubDownloaderV3) GetPullRequests(page, perPage int) ([]*base.PullRequest, bool, error) { opt := &github.PullRequestListOptions{ Sort: "created", Direction: "asc", @@ -500,7 +500,7 @@ func (g *GithubDownloaderV3) GetPullRequests(page, perPage int) ([]*base.PullReq g.sleep() prs, resp, err := g.client.PullRequests.List(g.ctx, g.repoOwner, g.repoName, opt) if err != nil { - return nil, fmt.Errorf("error while listing repos: %v", err) + return nil, false, fmt.Errorf("error while listing repos: %v", err) } g.rate = &resp.Rate for _, pr := range prs { @@ -566,7 +566,7 @@ func (g *GithubDownloaderV3) GetPullRequests(page, perPage int) ([]*base.PullReq PerPage: perPage, }) if err != nil { - return nil, err + return nil, false, err } g.rate = &resp.Rate if len(res) == 0 { @@ -616,7 +616,7 @@ func (g *GithubDownloaderV3) GetPullRequests(page, perPage int) ([]*base.PullReq }) } - return allPRs, nil + return allPRs, len(prs) < perPage, nil } func convertGithubReview(r *github.PullRequestReview) *base.Review { diff --git a/modules/migrations/github_test.go b/modules/migrations/github_test.go index 0b8c559d305f4..617a57455027b 100644 --- a/modules/migrations/github_test.go +++ b/modules/migrations/github_test.go @@ -269,7 +269,7 @@ func TestGitHubDownloadRepo(t *testing.T) { }, comments[:2]) // downloader.GetPullRequests() - prs, err := downloader.GetPullRequests(1, 2) + prs, _, err := downloader.GetPullRequests(1, 2) assert.NoError(t, err) assert.EqualValues(t, 2, len(prs)) diff --git a/modules/migrations/gitlab.go b/modules/migrations/gitlab.go index 7fc3fab0ec0ee..d504ed5f95d2c 100644 --- a/modules/migrations/gitlab.go +++ b/modules/migrations/gitlab.go @@ -441,7 +441,7 @@ func (g *GitlabDownloader) GetComments(issueNumber int64) ([]*base.Comment, erro } // GetPullRequests returns pull requests according page and perPage -func (g *GitlabDownloader) GetPullRequests(page, perPage int) ([]*base.PullRequest, error) { +func (g *GitlabDownloader) GetPullRequests(page, perPage int) ([]*base.PullRequest, bool, error) { opt := &gitlab.ListProjectMergeRequestsOptions{ ListOptions: gitlab.ListOptions{ @@ -457,7 +457,7 @@ func (g *GitlabDownloader) GetPullRequests(page, perPage int) ([]*base.PullReque prs, _, err := g.client.MergeRequests.ListProjectMergeRequests(g.repoID, opt, nil) if err != nil { - return nil, fmt.Errorf("error while listing merge requests: %v", err) + return nil, false, fmt.Errorf("error while listing merge requests: %v", err) } for _, pr := range prs { @@ -530,7 +530,7 @@ func (g *GitlabDownloader) GetPullRequests(page, perPage int) ([]*base.PullReque }) } - return allPRs, nil + return allPRs, len(prs) < perPage, nil } // GetReviews returns pull requests review diff --git a/modules/migrations/gitlab_test.go b/modules/migrations/gitlab_test.go index daf05f8e3a6fa..cf445e171bf47 100644 --- a/modules/migrations/gitlab_test.go +++ b/modules/migrations/gitlab_test.go @@ -200,7 +200,7 @@ func TestGitlabDownloadRepo(t *testing.T) { }, }, comments[:4]) - prs, err := downloader.GetPullRequests(1, 1) + prs, _, err := downloader.GetPullRequests(1, 1) assert.NoError(t, err) assert.Len(t, prs, 1) diff --git a/modules/migrations/migrate.go b/modules/migrations/migrate.go index 7858dfc6850c6..b27e7125b57b7 100644 --- a/modules/migrations/migrate.go +++ b/modules/migrations/migrate.go @@ -231,7 +231,7 @@ func migrateRepository(downloader base.Downloader, uploader base.Uploader, opts log.Trace("migrating pull requests and comments") var prBatchSize = uploader.MaxBatchInsertSize("pullrequest") for i := 1; ; i++ { - prs, err := downloader.GetPullRequests(i, prBatchSize) + prs, isEnd, err := downloader.GetPullRequests(i, prBatchSize) if err != nil { return err } @@ -302,7 +302,7 @@ func migrateRepository(downloader base.Downloader, uploader base.Uploader, opts } } - if len(prs) < prBatchSize { + if isEnd { break } }