Skip to content

Add support for migrating GitLab comment reactions #29413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
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
79 changes: 44 additions & 35 deletions services/migrations/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,21 +434,11 @@ func (g *GitlabDownloader) GetIssues(page, perPage int) ([]*base.Issue, bool, er
milestone = issue.Milestone.Title
}

var reactions []*gitlab.AwardEmoji
awardPage := 1
for {
awards, _, err := g.client.AwardEmoji.ListIssueAwardEmoji(g.repoID, issue.IID, &gitlab.ListAwardEmojiOptions{Page: awardPage, PerPage: perPage}, gitlab.WithContext(g.ctx))
if err != nil {
return nil, false, fmt.Errorf("error while listing issue awards: %w", err)
}

reactions = append(reactions, awards...)

if len(awards) < perPage {
break
}

awardPage++
reactions, err := g.loadAwards(func(awardPage int) ([]*gitlab.AwardEmoji, *gitlab.Response, error) {
return g.client.AwardEmoji.ListIssueAwardEmoji(g.repoID, issue.IID, &gitlab.ListAwardEmojiOptions{Page: awardPage, PerPage: perPage}, gitlab.WithContext(g.ctx))
})
if err != nil {
return nil, false, err
}

allIssues = append(allIssues, &base.Issue{
Expand All @@ -461,7 +451,7 @@ func (g *GitlabDownloader) GetIssues(page, perPage int) ([]*base.Issue, bool, er
State: issue.State,
Created: *issue.CreatedAt,
Labels: labels,
Reactions: g.awardsToReactions(reactions),
Reactions: reactions,
Closed: issue.ClosedAt,
IsLocked: issue.DiscussionLocked,
Updated: *issue.UpdatedAt,
Expand All @@ -477,7 +467,6 @@ func (g *GitlabDownloader) GetIssues(page, perPage int) ([]*base.Issue, bool, er
}

// GetComments returns comments according issueNumber
// TODO: figure out how to transfer comment reactions
func (g *GitlabDownloader) GetComments(commentable base.Commentable) ([]*base.Comment, bool, error) {
context, ok := commentable.GetContext().(gitlabIssueContext)
if !ok {
Expand Down Expand Up @@ -509,7 +498,17 @@ func (g *GitlabDownloader) GetComments(commentable base.Commentable) ([]*base.Co
}
for _, comment := range comments {
for _, note := range comment.Notes {
allComments = append(allComments, g.convertNoteToComment(commentable.GetLocalIndex(), note))
reactions, err := g.loadAwards(func(awardPage int) ([]*gitlab.AwardEmoji, *gitlab.Response, error) {
if context.IsMergeRequest {
return g.client.AwardEmoji.ListMergeRequestAwardEmojiOnNote(g.repoID, note.NoteableIID, note.ID, &gitlab.ListAwardEmojiOptions{Page: awardPage, PerPage: g.maxPerPage}, gitlab.WithContext(g.ctx))
}
return g.client.AwardEmoji.ListIssuesAwardEmojiOnNote(g.repoID, note.NoteableIID, note.ID, &gitlab.ListAwardEmojiOptions{Page: awardPage, PerPage: g.maxPerPage}, gitlab.WithContext(g.ctx))
})
if err != nil {
return nil, false, err
}

allComments = append(allComments, g.convertNoteToComment(commentable.GetLocalIndex(), note, reactions))
}
}
if resp.NextPage == 0 {
Expand Down Expand Up @@ -576,7 +575,7 @@ func (g *GitlabDownloader) GetComments(commentable base.Commentable) ([]*base.Co

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

func (g *GitlabDownloader) convertNoteToComment(localIndex int64, note *gitlab.Note) *base.Comment {
func (g *GitlabDownloader) convertNoteToComment(localIndex int64, note *gitlab.Note, reactions []*base.Reaction) *base.Comment {
comment := &base.Comment{
IssueIndex: localIndex,
Index: int64(note.ID),
Expand All @@ -586,6 +585,7 @@ func (g *GitlabDownloader) convertNoteToComment(localIndex int64, note *gitlab.N
Content: note.Body,
Created: *note.CreatedAt,
Meta: map[string]any{},
Reactions: reactions,
}

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

var reactions []*gitlab.AwardEmoji
awardPage := 1
for {
awards, _, err := g.client.AwardEmoji.ListMergeRequestAwardEmoji(g.repoID, pr.IID, &gitlab.ListAwardEmojiOptions{Page: awardPage, PerPage: perPage}, gitlab.WithContext(g.ctx))
if err != nil {
return nil, false, fmt.Errorf("error while listing merge requests awards: %w", err)
}

reactions = append(reactions, awards...)

if len(awards) < perPage {
break
}

awardPage++
reactions, err := g.loadAwards(func(awardPage int) ([]*gitlab.AwardEmoji, *gitlab.Response, error) {
return g.client.AwardEmoji.ListMergeRequestAwardEmoji(g.repoID, pr.IID, &gitlab.ListAwardEmojiOptions{Page: awardPage, PerPage: perPage}, gitlab.WithContext(g.ctx))
})
if err != nil {
return nil, false, err
}

// Generate new PR Numbers by the known Issue Numbers, because they share the same number space in Gitea, but they are independent in Gitlab
Expand All @@ -706,7 +696,7 @@ func (g *GitlabDownloader) GetPullRequests(page, perPage int) ([]*base.PullReque
MergeCommitSHA: mergeCommitSHA,
MergedTime: mergeTime,
IsLocked: locked,
Reactions: g.awardsToReactions(reactions),
Reactions: reactions,
Head: base.PullRequestBranch{
Ref: pr.SourceBranch,
SHA: pr.SHA,
Expand Down Expand Up @@ -767,6 +757,25 @@ func (g *GitlabDownloader) GetReviews(reviewable base.Reviewable) ([]*base.Revie
return reviews, nil
}

func (g *GitlabDownloader) loadAwards(load func(page int) ([]*gitlab.AwardEmoji, *gitlab.Response, error)) ([]*base.Reaction, error) {
var allAwards []*gitlab.AwardEmoji
page := 1
for {
awards, resp, err := load(page)
if err != nil {
return nil, fmt.Errorf("error while listing awards: %w", err)
}

allAwards = append(allAwards, awards...)

if resp.NextPage == 0 {
break
}
page = resp.NextPage
}
return g.awardsToReactions(allAwards), nil
}

func (g *GitlabDownloader) awardsToReactions(awards []*gitlab.AwardEmoji) []*base.Reaction {
result := make([]*base.Reaction, 0, len(awards))
uniqCheck := make(container.Set[string])
Expand Down
2 changes: 1 addition & 1 deletion services/migrations/gitlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ func TestNoteToComment(t *testing.T) {
}}

for i, note := range notes {
actualComment := *downloader.convertNoteToComment(17, &note)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have some tests for reactions mirgrations?

actualComment := *downloader.convertNoteToComment(17, &note, nil)
assert.EqualValues(t, actualComment, comments[i])
}
}
Expand Down