Skip to content

Commit bf98373

Browse files
GiteaBotwxiaoguang
andauthored
Use known issue IID to generate new PR index number when migrating from GitLab (go-gitea#28616) (go-gitea#28618)
Backport go-gitea#28616 by wxiaoguang Fix go-gitea#13884 Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
1 parent 7a2786c commit bf98373

File tree

2 files changed

+45
-11
lines changed

2 files changed

+45
-11
lines changed

services/migrations/gitlab.go

+28-11
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,36 @@ func (f *GitlabDownloaderFactory) GitServiceType() structs.GitServiceType {
5555
return structs.GitlabService
5656
}
5757

58+
type gitlabIIDResolver struct {
59+
maxIssueIID int64
60+
frozen bool
61+
}
62+
63+
func (r *gitlabIIDResolver) recordIssueIID(issueIID int) {
64+
if r.frozen {
65+
panic("cannot record issue IID after pull request IID generation has started")
66+
}
67+
r.maxIssueIID = max(r.maxIssueIID, int64(issueIID))
68+
}
69+
70+
func (r *gitlabIIDResolver) generatePullRequestNumber(mrIID int) int64 {
71+
r.frozen = true
72+
return r.maxIssueIID + int64(mrIID)
73+
}
74+
5875
// GitlabDownloader implements a Downloader interface to get repository information
5976
// from gitlab via go-gitlab
6077
// - issueCount is incremented in GetIssues() to ensure PR and Issue numbers do not overlap,
6178
// because Gitlab has individual Issue and Pull Request numbers.
6279
type GitlabDownloader struct {
6380
base.NullDownloader
64-
ctx context.Context
65-
client *gitlab.Client
66-
baseURL string
67-
repoID int
68-
repoName string
69-
issueCount int64
70-
maxPerPage int
81+
ctx context.Context
82+
client *gitlab.Client
83+
baseURL string
84+
repoID int
85+
repoName string
86+
iidResolver gitlabIIDResolver
87+
maxPerPage int
7188
}
7289

7390
// NewGitlabDownloader creates a gitlab Downloader via gitlab API
@@ -450,8 +467,8 @@ func (g *GitlabDownloader) GetIssues(page, perPage int) ([]*base.Issue, bool, er
450467
Context: gitlabIssueContext{IsMergeRequest: false},
451468
})
452469

453-
// increment issueCount, to be used in GetPullRequests()
454-
g.issueCount++
470+
// record the issue IID, to be used in GetPullRequests()
471+
g.iidResolver.recordIssueIID(issue.IID)
455472
}
456473

457474
return allIssues, len(issues) < perPage, nil
@@ -594,8 +611,8 @@ func (g *GitlabDownloader) GetPullRequests(page, perPage int) ([]*base.PullReque
594611
awardPage++
595612
}
596613

597-
// Add the PR ID to the Issue Count because PR and Issues share ID space in Gitea
598-
newPRNumber := g.issueCount + int64(pr.IID)
614+
// Generate new PR Numbers by the known Issue Numbers, because they share the same number space in Gitea, but they are independent in Gitlab
615+
newPRNumber := g.iidResolver.generatePullRequestNumber(pr.IID)
599616

600617
allPRs = append(allPRs, &base.PullRequest{
601618
Title: pr.Title,

services/migrations/gitlab_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -516,3 +516,20 @@ func TestAwardsToReactions(t *testing.T) {
516516
},
517517
}, reactions)
518518
}
519+
520+
func TestGitlabIIDResolver(t *testing.T) {
521+
r := gitlabIIDResolver{}
522+
r.recordIssueIID(1)
523+
r.recordIssueIID(2)
524+
r.recordIssueIID(3)
525+
r.recordIssueIID(2)
526+
assert.EqualValues(t, 4, r.generatePullRequestNumber(1))
527+
assert.EqualValues(t, 13, r.generatePullRequestNumber(10))
528+
529+
assert.Panics(t, func() {
530+
r := gitlabIIDResolver{}
531+
r.recordIssueIID(1)
532+
assert.EqualValues(t, 2, r.generatePullRequestNumber(1))
533+
r.recordIssueIID(3) // the generation procedure has been started, it shouldn't accept any new issue IID, so it panics
534+
})
535+
}

0 commit comments

Comments
 (0)