Skip to content

Commit c834fc1

Browse files
committed
Add support for migrating GitLab comment reactions
1 parent 17f170e commit c834fc1

File tree

1 file changed

+45
-35
lines changed

1 file changed

+45
-35
lines changed

services/migrations/gitlab.go

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -434,21 +434,11 @@ func (g *GitlabDownloader) GetIssues(page, perPage int) ([]*base.Issue, bool, er
434434
milestone = issue.Milestone.Title
435435
}
436436

437-
var reactions []*gitlab.AwardEmoji
438-
awardPage := 1
439-
for {
440-
awards, _, err := g.client.AwardEmoji.ListIssueAwardEmoji(g.repoID, issue.IID, &gitlab.ListAwardEmojiOptions{Page: awardPage, PerPage: perPage}, gitlab.WithContext(g.ctx))
441-
if err != nil {
442-
return nil, false, fmt.Errorf("error while listing issue awards: %w", err)
443-
}
444-
445-
reactions = append(reactions, awards...)
446-
447-
if len(awards) < perPage {
448-
break
449-
}
450-
451-
awardPage++
437+
reactions, err := g.loadAwards(func(awardPage int) ([]*gitlab.AwardEmoji, *gitlab.Response, error) {
438+
return g.client.AwardEmoji.ListIssueAwardEmoji(g.repoID, issue.IID, &gitlab.ListAwardEmojiOptions{Page: awardPage, PerPage: perPage}, gitlab.WithContext(g.ctx))
439+
})
440+
if err != nil {
441+
return nil, false, err
452442
}
453443

454444
allIssues = append(allIssues, &base.Issue{
@@ -461,7 +451,7 @@ func (g *GitlabDownloader) GetIssues(page, perPage int) ([]*base.Issue, bool, er
461451
State: issue.State,
462452
Created: *issue.CreatedAt,
463453
Labels: labels,
464-
Reactions: g.awardsToReactions(reactions),
454+
Reactions: reactions,
465455
Closed: issue.ClosedAt,
466456
IsLocked: issue.DiscussionLocked,
467457
Updated: *issue.UpdatedAt,
@@ -477,7 +467,6 @@ func (g *GitlabDownloader) GetIssues(page, perPage int) ([]*base.Issue, bool, er
477467
}
478468

479469
// GetComments returns comments according issueNumber
480-
// TODO: figure out how to transfer comment reactions
481470
func (g *GitlabDownloader) GetComments(commentable base.Commentable) ([]*base.Comment, bool, error) {
482471
context, ok := commentable.GetContext().(gitlabIssueContext)
483472
if !ok {
@@ -509,7 +498,18 @@ func (g *GitlabDownloader) GetComments(commentable base.Commentable) ([]*base.Co
509498
}
510499
for _, comment := range comments {
511500
for _, note := range comment.Notes {
512-
allComments = append(allComments, g.convertNoteToComment(commentable.GetLocalIndex(), note))
501+
reactions, err := g.loadAwards(func(awardPage int) ([]*gitlab.AwardEmoji, *gitlab.Response, error) {
502+
if context.IsMergeRequest {
503+
return g.client.AwardEmoji.ListMergeRequestAwardEmojiOnNote(g.repoID, note.NoteableIID, note.ID, &gitlab.ListAwardEmojiOptions{Page: awardPage, PerPage: g.maxPerPage}, gitlab.WithContext(g.ctx))
504+
} else {
505+
return g.client.AwardEmoji.ListIssuesAwardEmojiOnNote(g.repoID, note.NoteableIID, note.ID, &gitlab.ListAwardEmojiOptions{Page: awardPage, PerPage: g.maxPerPage}, gitlab.WithContext(g.ctx))
506+
}
507+
})
508+
if err != nil {
509+
return nil, false, err
510+
}
511+
512+
allComments = append(allComments, g.convertNoteToComment(commentable.GetLocalIndex(), note, reactions))
513513
}
514514
}
515515
if resp.NextPage == 0 {
@@ -576,7 +576,7 @@ func (g *GitlabDownloader) GetComments(commentable base.Commentable) ([]*base.Co
576576

577577
var targetBranchChangeRegexp = regexp.MustCompile("^changed target branch from `(.*?)` to `(.*?)`$")
578578

579-
func (g *GitlabDownloader) convertNoteToComment(localIndex int64, note *gitlab.Note) *base.Comment {
579+
func (g *GitlabDownloader) convertNoteToComment(localIndex int64, note *gitlab.Note, reactions []*base.Reaction) *base.Comment {
580580
comment := &base.Comment{
581581
IssueIndex: localIndex,
582582
Index: int64(note.ID),
@@ -586,6 +586,7 @@ func (g *GitlabDownloader) convertNoteToComment(localIndex int64, note *gitlab.N
586586
Content: note.Body,
587587
Created: *note.CreatedAt,
588588
Meta: map[string]any{},
589+
Reactions: reactions,
589590
}
590591

591592
// Try to find the underlying event of system notes.
@@ -671,21 +672,11 @@ func (g *GitlabDownloader) GetPullRequests(page, perPage int) ([]*base.PullReque
671672
milestone = pr.Milestone.Title
672673
}
673674

674-
var reactions []*gitlab.AwardEmoji
675-
awardPage := 1
676-
for {
677-
awards, _, err := g.client.AwardEmoji.ListMergeRequestAwardEmoji(g.repoID, pr.IID, &gitlab.ListAwardEmojiOptions{Page: awardPage, PerPage: perPage}, gitlab.WithContext(g.ctx))
678-
if err != nil {
679-
return nil, false, fmt.Errorf("error while listing merge requests awards: %w", err)
680-
}
681-
682-
reactions = append(reactions, awards...)
683-
684-
if len(awards) < perPage {
685-
break
686-
}
687-
688-
awardPage++
675+
reactions, err := g.loadAwards(func(awardPage int) ([]*gitlab.AwardEmoji, *gitlab.Response, error) {
676+
return g.client.AwardEmoji.ListMergeRequestAwardEmoji(g.repoID, pr.IID, &gitlab.ListAwardEmojiOptions{Page: awardPage, PerPage: perPage}, gitlab.WithContext(g.ctx))
677+
})
678+
if err != nil {
679+
return nil, false, err
689680
}
690681

691682
// Generate new PR Numbers by the known Issue Numbers, because they share the same number space in Gitea, but they are independent in Gitlab
@@ -706,7 +697,7 @@ func (g *GitlabDownloader) GetPullRequests(page, perPage int) ([]*base.PullReque
706697
MergeCommitSHA: mergeCommitSHA,
707698
MergedTime: mergeTime,
708699
IsLocked: locked,
709-
Reactions: g.awardsToReactions(reactions),
700+
Reactions: reactions,
710701
Head: base.PullRequestBranch{
711702
Ref: pr.SourceBranch,
712703
SHA: pr.SHA,
@@ -767,6 +758,25 @@ func (g *GitlabDownloader) GetReviews(reviewable base.Reviewable) ([]*base.Revie
767758
return reviews, nil
768759
}
769760

761+
func (g *GitlabDownloader) loadAwards(load func(page int) ([]*gitlab.AwardEmoji, *gitlab.Response, error)) ([]*base.Reaction, error) {
762+
var allAwards []*gitlab.AwardEmoji
763+
page := 1
764+
for {
765+
awards, resp, err := load(page)
766+
if err != nil {
767+
return nil, fmt.Errorf("error while listing awards: %w", err)
768+
}
769+
770+
allAwards = append(allAwards, awards...)
771+
772+
if resp.NextPage == 0 {
773+
break
774+
}
775+
page = resp.NextPage
776+
}
777+
return g.awardsToReactions(allAwards), nil
778+
}
779+
770780
func (g *GitlabDownloader) awardsToReactions(awards []*gitlab.AwardEmoji) []*base.Reaction {
771781
result := make([]*base.Reaction, 0, len(awards))
772782
uniqCheck := make(container.Set[string])

0 commit comments

Comments
 (0)