Skip to content

Commit ad4a644

Browse files
6543lafriks
andauthored
Migrations: Gitlab Add Reactions Support for Issues & MergeRequests (#12695)
* fix migrations: gitlab_test * more stable test * Get Reactions to Pulls * Reactions for Issues * Comments are still hard Co-authored-by: Lauris BH <lauris@nix.lv>
1 parent 7af2ccd commit ad4a644

File tree

2 files changed

+120
-23
lines changed

2 files changed

+120
-23
lines changed

modules/migrations/gitlab.go

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,6 @@ func (g *GitlabDownloader) GetAsset(tag string, id int64) (io.ReadCloser, error)
327327

328328
// GetIssues returns issues according start and limit
329329
// Note: issue label description and colors are not supported by the go-gitlab library at this time
330-
// TODO: figure out how to transfer issue reactions
331330
func (g *GitlabDownloader) GetIssues(page, perPage int) ([]*base.Issue, bool, error) {
332331
state := "all"
333332
sort := "asc"
@@ -361,6 +360,22 @@ func (g *GitlabDownloader) GetIssues(page, perPage int) ([]*base.Issue, bool, er
361360
milestone = issue.Milestone.Title
362361
}
363362

363+
var reactions []*base.Reaction
364+
var awardPage = 1
365+
for {
366+
awards, _, err := g.client.AwardEmoji.ListIssueAwardEmoji(g.repoID, issue.IID, &gitlab.ListAwardEmojiOptions{Page: awardPage, PerPage: perPage}, gitlab.WithContext(g.ctx))
367+
if err != nil {
368+
return nil, false, fmt.Errorf("error while listing issue awards: %v", err)
369+
}
370+
if len(awards) < perPage {
371+
break
372+
}
373+
for i := range awards {
374+
reactions = append(reactions, g.awardToReaction(awards[i]))
375+
}
376+
awardPage++
377+
}
378+
364379
allIssues = append(allIssues, &base.Issue{
365380
Title: issue.Title,
366381
Number: int64(issue.IID),
@@ -371,6 +386,7 @@ func (g *GitlabDownloader) GetIssues(page, perPage int) ([]*base.Issue, bool, er
371386
State: issue.State,
372387
Created: *issue.CreatedAt,
373388
Labels: labels,
389+
Reactions: reactions,
374390
Closed: issue.ClosedAt,
375391
IsLocked: issue.DiscussionLocked,
376392
Updated: *issue.UpdatedAt,
@@ -384,6 +400,7 @@ func (g *GitlabDownloader) GetIssues(page, perPage int) ([]*base.Issue, bool, er
384400
}
385401

386402
// GetComments returns comments according issueNumber
403+
// TODO: figure out how to transfer comment reactions
387404
func (g *GitlabDownloader) GetComments(issueNumber int64) ([]*base.Comment, error) {
388405
var allComments = make([]*base.Comment, 0, 100)
389406

@@ -501,6 +518,22 @@ func (g *GitlabDownloader) GetPullRequests(page, perPage int) ([]*base.PullReque
501518
milestone = pr.Milestone.Title
502519
}
503520

521+
var reactions []*base.Reaction
522+
var awardPage = 1
523+
for {
524+
awards, _, err := g.client.AwardEmoji.ListMergeRequestAwardEmoji(g.repoID, pr.IID, &gitlab.ListAwardEmojiOptions{Page: awardPage, PerPage: perPage}, gitlab.WithContext(g.ctx))
525+
if err != nil {
526+
return nil, fmt.Errorf("error while listing merge requests awards: %v", err)
527+
}
528+
if len(awards) < perPage {
529+
break
530+
}
531+
for i := range awards {
532+
reactions = append(reactions, g.awardToReaction(awards[i]))
533+
}
534+
awardPage++
535+
}
536+
504537
// Add the PR ID to the Issue Count because PR and Issues share ID space in Gitea
505538
newPRNumber := g.issueCount + int64(pr.IID)
506539

@@ -520,6 +553,7 @@ func (g *GitlabDownloader) GetPullRequests(page, perPage int) ([]*base.PullReque
520553
MergeCommitSHA: pr.MergeCommitSHA,
521554
MergedTime: mergeTime,
522555
IsLocked: locked,
556+
Reactions: reactions,
523557
Head: base.PullRequestBranch{
524558
Ref: pr.SourceBranch,
525559
SHA: pr.SHA,
@@ -570,3 +604,11 @@ func (g *GitlabDownloader) GetReviews(pullRequestNumber int64) ([]*base.Review,
570604

571605
return reviews, nil
572606
}
607+
608+
func (g *GitlabDownloader) awardToReaction(award *gitlab.AwardEmoji) *base.Reaction {
609+
return &base.Reaction{
610+
UserID: int64(award.User.ID),
611+
UserName: award.User.Username,
612+
Content: award.Name,
613+
}
614+
}

modules/migrations/gitlab_test.go

Lines changed: 77 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func TestGitlabDownloadRepo(t *testing.T) {
130130
PosterName: "lafriks",
131131
State: "closed",
132132
Created: time.Date(2019, 11, 28, 8, 43, 35, 459000000, time.UTC),
133-
Updated: time.Date(2019, 11, 28, 8, 46, 23, 275000000, time.UTC),
133+
Updated: time.Date(2019, 11, 28, 8, 46, 23, 304000000, time.UTC),
134134
Labels: []*base.Label{
135135
{
136136
Name: "bug",
@@ -139,8 +139,18 @@ func TestGitlabDownloadRepo(t *testing.T) {
139139
Name: "discussion",
140140
},
141141
},
142-
Reactions: nil,
143-
Closed: &closed1,
142+
Reactions: []*base.Reaction{
143+
{
144+
UserID: 1241334,
145+
UserName: "lafriks",
146+
Content: "thumbsup",
147+
},
148+
{
149+
UserID: 1241334,
150+
UserName: "lafriks",
151+
Content: "open_mouth",
152+
}},
153+
Closed: &closed1,
144154
},
145155
{
146156
Number: 2,
@@ -157,8 +167,38 @@ func TestGitlabDownloadRepo(t *testing.T) {
157167
Name: "duplicate",
158168
},
159169
},
160-
Reactions: nil,
161-
Closed: &closed2,
170+
Reactions: []*base.Reaction{
171+
{
172+
UserID: 1241334,
173+
UserName: "lafriks",
174+
Content: "thumbsup",
175+
},
176+
{
177+
UserID: 1241334,
178+
UserName: "lafriks",
179+
Content: "thumbsdown",
180+
},
181+
{
182+
UserID: 1241334,
183+
UserName: "lafriks",
184+
Content: "laughing",
185+
},
186+
{
187+
UserID: 1241334,
188+
UserName: "lafriks",
189+
Content: "tada",
190+
},
191+
{
192+
UserID: 1241334,
193+
UserName: "lafriks",
194+
Content: "confused",
195+
},
196+
{
197+
UserID: 1241334,
198+
UserName: "lafriks",
199+
Content: "hearts",
200+
}},
201+
Closed: &closed2,
162202
},
163203
}, issues)
164204

@@ -171,7 +211,6 @@ func TestGitlabDownloadRepo(t *testing.T) {
171211
PosterID: 1241334,
172212
PosterName: "lafriks",
173213
Created: time.Date(2019, 11, 28, 8, 44, 52, 501000000, time.UTC),
174-
Updated: time.Date(2019, 11, 28, 8, 44, 52, 501000000, time.UTC),
175214
Content: "This is a comment",
176215
Reactions: nil,
177216
},
@@ -207,20 +246,29 @@ func TestGitlabDownloadRepo(t *testing.T) {
207246

208247
assert.EqualValues(t, []*base.PullRequest{
209248
{
210-
Number: 4,
211-
Title: "Test branch",
212-
Content: "do not merge this PR",
213-
Milestone: "1.0.0",
214-
PosterID: 1241334,
215-
PosterName: "lafriks",
216-
State: "opened",
217-
Created: time.Date(2019, 11, 28, 15, 56, 54, 104000000, time.UTC),
218-
Updated: time.Date(2019, 11, 28, 15, 56, 54, 104000000, time.UTC),
249+
Number: 4,
250+
OriginalNumber: 2,
251+
Title: "Test branch",
252+
Content: "do not merge this PR",
253+
Milestone: "1.0.0",
254+
PosterID: 1241334,
255+
PosterName: "lafriks",
256+
State: "opened",
257+
Created: time.Date(2019, 11, 28, 15, 56, 54, 104000000, time.UTC),
219258
Labels: []*base.Label{
220259
{
221260
Name: "bug",
222261
},
223262
},
263+
Reactions: []*base.Reaction{{
264+
UserID: 4575606,
265+
UserName: "real6543",
266+
Content: "thumbsup",
267+
}, {
268+
UserID: 4575606,
269+
UserName: "real6543",
270+
Content: "tada",
271+
}},
224272
PatchURL: "https://gitlab.com/gitea/test_repo/-/merge_requests/2.patch",
225273
Head: base.PullRequestBranch{
226274
Ref: "feat/test",
@@ -244,13 +292,20 @@ func TestGitlabDownloadRepo(t *testing.T) {
244292

245293
rvs, err := downloader.GetReviews(1)
246294
assert.NoError(t, err)
247-
if assert.Len(t, prs, 2) {
248-
assert.EqualValues(t, 527793, rvs[0].ReviewerID)
249-
assert.EqualValues(t, "axifive", rvs[0].ReviewerName)
250-
assert.EqualValues(t, "APPROVED", rvs[0].State)
251-
assert.EqualValues(t, 4102996, rvs[1].ReviewerID)
252-
assert.EqualValues(t, "zeripath", rvs[1].ReviewerName)
253-
assert.EqualValues(t, "APPROVED", rvs[1].State)
295+
if assert.Len(t, rvs, 2) {
296+
for i := range rvs {
297+
switch rvs[i].ReviewerID {
298+
case 4102996:
299+
assert.EqualValues(t, "zeripath", rvs[i].ReviewerName)
300+
assert.EqualValues(t, "APPROVED", rvs[i].State)
301+
case 527793:
302+
assert.EqualValues(t, "axifive", rvs[i].ReviewerName)
303+
assert.EqualValues(t, "APPROVED", rvs[i].State)
304+
default:
305+
t.Errorf("Unexpected Reviewer ID: %d", rvs[i].ReviewerID)
306+
307+
}
308+
}
254309
}
255310
rvs, err = downloader.GetReviews(2)
256311
assert.NoError(t, err)

0 commit comments

Comments
 (0)