Skip to content

Commit

Permalink
make GetPullRequests and GetIssues similar
Browse files Browse the repository at this point in the history
  • Loading branch information
6543 committed Aug 31, 2020
1 parent 53462b9 commit 81f7723
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 23 deletions.
11 changes: 6 additions & 5 deletions modules/migrations/base/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions modules/migrations/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions modules/migrations/gitea_downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions modules/migrations/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion modules/migrations/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
6 changes: 3 additions & 3 deletions modules/migrations/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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 {

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion modules/migrations/gitlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions modules/migrations/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -302,7 +302,7 @@ func migrateRepository(downloader base.Downloader, uploader base.Uploader, opts
}
}

if len(prs) < prBatchSize {
if isEnd {
break
}
}
Expand Down

0 comments on commit 81f7723

Please sign in to comment.