Add support for migrating GitLab comment reactions

This commit is contained in:
Sebastian Brückner 2023-11-06 21:20:54 -08:00
parent 17f170ee37
commit da75245a46
2 changed files with 46 additions and 36 deletions

View File

@ -434,21 +434,11 @@ func (g *GitlabDownloader) GetIssues(page, perPage int) ([]*base.Issue, bool, er
milestone = issue.Milestone.Title milestone = issue.Milestone.Title
} }
var reactions []*gitlab.AwardEmoji reactions, err := g.loadAwards(func(awardPage int) ([]*gitlab.AwardEmoji, *gitlab.Response, error) {
awardPage := 1 return g.client.AwardEmoji.ListIssueAwardEmoji(g.repoID, issue.IID, &gitlab.ListAwardEmojiOptions{Page: awardPage, PerPage: perPage}, gitlab.WithContext(g.ctx))
for { })
awards, _, err := g.client.AwardEmoji.ListIssueAwardEmoji(g.repoID, issue.IID, &gitlab.ListAwardEmojiOptions{Page: awardPage, PerPage: perPage}, gitlab.WithContext(g.ctx))
if err != nil { if err != nil {
return nil, false, fmt.Errorf("error while listing issue awards: %w", err) return nil, false, err
}
reactions = append(reactions, awards...)
if len(awards) < perPage {
break
}
awardPage++
} }
allIssues = append(allIssues, &base.Issue{ allIssues = append(allIssues, &base.Issue{
@ -461,7 +451,7 @@ func (g *GitlabDownloader) GetIssues(page, perPage int) ([]*base.Issue, bool, er
State: issue.State, State: issue.State,
Created: *issue.CreatedAt, Created: *issue.CreatedAt,
Labels: labels, Labels: labels,
Reactions: g.awardsToReactions(reactions), Reactions: reactions,
Closed: issue.ClosedAt, Closed: issue.ClosedAt,
IsLocked: issue.DiscussionLocked, IsLocked: issue.DiscussionLocked,
Updated: *issue.UpdatedAt, Updated: *issue.UpdatedAt,
@ -477,7 +467,6 @@ func (g *GitlabDownloader) GetIssues(page, perPage int) ([]*base.Issue, bool, er
} }
// GetComments returns comments according issueNumber // GetComments returns comments according issueNumber
// TODO: figure out how to transfer comment reactions
func (g *GitlabDownloader) GetComments(commentable base.Commentable) ([]*base.Comment, bool, error) { func (g *GitlabDownloader) GetComments(commentable base.Commentable) ([]*base.Comment, bool, error) {
context, ok := commentable.GetContext().(gitlabIssueContext) context, ok := commentable.GetContext().(gitlabIssueContext)
if !ok { if !ok {
@ -509,7 +498,18 @@ func (g *GitlabDownloader) GetComments(commentable base.Commentable) ([]*base.Co
} }
for _, comment := range comments { for _, comment := range comments {
for _, note := range comment.Notes { 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))
} else {
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 { if resp.NextPage == 0 {
@ -576,7 +576,7 @@ func (g *GitlabDownloader) GetComments(commentable base.Commentable) ([]*base.Co
var targetBranchChangeRegexp = regexp.MustCompile("^changed target branch from `(.*?)` to `(.*?)`$") 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{ comment := &base.Comment{
IssueIndex: localIndex, IssueIndex: localIndex,
Index: int64(note.ID), Index: int64(note.ID),
@ -586,6 +586,7 @@ func (g *GitlabDownloader) convertNoteToComment(localIndex int64, note *gitlab.N
Content: note.Body, Content: note.Body,
Created: *note.CreatedAt, Created: *note.CreatedAt,
Meta: map[string]any{}, Meta: map[string]any{},
Reactions: reactions,
} }
// Try to find the underlying event of system notes. // Try to find the underlying event of system notes.
@ -671,21 +672,11 @@ func (g *GitlabDownloader) GetPullRequests(page, perPage int) ([]*base.PullReque
milestone = pr.Milestone.Title milestone = pr.Milestone.Title
} }
var reactions []*gitlab.AwardEmoji reactions, err := g.loadAwards(func(awardPage int) ([]*gitlab.AwardEmoji, *gitlab.Response, error) {
awardPage := 1 return g.client.AwardEmoji.ListMergeRequestAwardEmoji(g.repoID, pr.IID, &gitlab.ListAwardEmojiOptions{Page: awardPage, PerPage: perPage}, gitlab.WithContext(g.ctx))
for { })
awards, _, err := g.client.AwardEmoji.ListMergeRequestAwardEmoji(g.repoID, pr.IID, &gitlab.ListAwardEmojiOptions{Page: awardPage, PerPage: perPage}, gitlab.WithContext(g.ctx))
if err != nil { if err != nil {
return nil, false, fmt.Errorf("error while listing merge requests awards: %w", err) return nil, false, err
}
reactions = append(reactions, awards...)
if len(awards) < perPage {
break
}
awardPage++
} }
// Generate new PR Numbers by the known Issue Numbers, because they share the same number space in Gitea, but they are independent in Gitlab // 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
MergeCommitSHA: mergeCommitSHA, MergeCommitSHA: mergeCommitSHA,
MergedTime: mergeTime, MergedTime: mergeTime,
IsLocked: locked, IsLocked: locked,
Reactions: g.awardsToReactions(reactions), Reactions: reactions,
Head: base.PullRequestBranch{ Head: base.PullRequestBranch{
Ref: pr.SourceBranch, Ref: pr.SourceBranch,
SHA: pr.SHA, SHA: pr.SHA,
@ -767,6 +758,25 @@ func (g *GitlabDownloader) GetReviews(reviewable base.Reviewable) ([]*base.Revie
return reviews, nil 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 { func (g *GitlabDownloader) awardsToReactions(awards []*gitlab.AwardEmoji) []*base.Reaction {
result := make([]*base.Reaction, 0, len(awards)) result := make([]*base.Reaction, 0, len(awards))
uniqCheck := make(container.Set[string]) uniqCheck := make(container.Set[string])

View File

@ -594,7 +594,7 @@ func TestNoteToComment(t *testing.T) {
}} }}
for i, note := range notes { for i, note := range notes {
actualComment := *downloader.convertNoteToComment(17, &note) actualComment := *downloader.convertNoteToComment(17, &note, nil)
assert.EqualValues(t, actualComment, comments[i]) assert.EqualValues(t, actualComment, comments[i])
} }
} }