Skip to content

Commit af3deb0

Browse files
authored
GitLab migration: Sanitize response for reaction list (#25054)
1 parent b6d8d69 commit af3deb0

File tree

2 files changed

+67
-15
lines changed

2 files changed

+67
-15
lines changed

services/migrations/gitlab.go

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -413,17 +413,15 @@ func (g *GitlabDownloader) GetIssues(page, perPage int) ([]*base.Issue, bool, er
413413
milestone = issue.Milestone.Title
414414
}
415415

416-
var reactions []*base.Reaction
416+
var reactions []*gitlab.AwardEmoji
417417
awardPage := 1
418418
for {
419419
awards, _, err := g.client.AwardEmoji.ListIssueAwardEmoji(g.repoID, issue.IID, &gitlab.ListAwardEmojiOptions{Page: awardPage, PerPage: perPage}, gitlab.WithContext(g.ctx))
420420
if err != nil {
421421
return nil, false, fmt.Errorf("error while listing issue awards: %w", err)
422422
}
423423

424-
for i := range awards {
425-
reactions = append(reactions, g.awardToReaction(awards[i]))
426-
}
424+
reactions = append(reactions, awards...)
427425

428426
if len(awards) < perPage {
429427
break
@@ -442,7 +440,7 @@ func (g *GitlabDownloader) GetIssues(page, perPage int) ([]*base.Issue, bool, er
442440
State: issue.State,
443441
Created: *issue.CreatedAt,
444442
Labels: labels,
445-
Reactions: reactions,
443+
Reactions: g.awardsToReactions(reactions),
446444
Closed: issue.ClosedAt,
447445
IsLocked: issue.DiscussionLocked,
448446
Updated: *issue.UpdatedAt,
@@ -577,17 +575,15 @@ func (g *GitlabDownloader) GetPullRequests(page, perPage int) ([]*base.PullReque
577575
milestone = pr.Milestone.Title
578576
}
579577

580-
var reactions []*base.Reaction
578+
var reactions []*gitlab.AwardEmoji
581579
awardPage := 1
582580
for {
583581
awards, _, err := g.client.AwardEmoji.ListMergeRequestAwardEmoji(g.repoID, pr.IID, &gitlab.ListAwardEmojiOptions{Page: awardPage, PerPage: perPage}, gitlab.WithContext(g.ctx))
584582
if err != nil {
585583
return nil, false, fmt.Errorf("error while listing merge requests awards: %w", err)
586584
}
587585

588-
for i := range awards {
589-
reactions = append(reactions, g.awardToReaction(awards[i]))
590-
}
586+
reactions = append(reactions, awards...)
591587

592588
if len(awards) < perPage {
593589
break
@@ -614,7 +610,7 @@ func (g *GitlabDownloader) GetPullRequests(page, perPage int) ([]*base.PullReque
614610
MergeCommitSHA: pr.MergeCommitSHA,
615611
MergedTime: mergeTime,
616612
IsLocked: locked,
617-
Reactions: reactions,
613+
Reactions: g.awardsToReactions(reactions),
618614
Head: base.PullRequestBranch{
619615
Ref: pr.SourceBranch,
620616
SHA: pr.SHA,
@@ -675,10 +671,19 @@ func (g *GitlabDownloader) GetReviews(reviewable base.Reviewable) ([]*base.Revie
675671
return reviews, nil
676672
}
677673

678-
func (g *GitlabDownloader) awardToReaction(award *gitlab.AwardEmoji) *base.Reaction {
679-
return &base.Reaction{
680-
UserID: int64(award.User.ID),
681-
UserName: award.User.Username,
682-
Content: award.Name,
674+
func (g *GitlabDownloader) awardsToReactions(awards []*gitlab.AwardEmoji) []*base.Reaction {
675+
result := make([]*base.Reaction, 0, len(awards))
676+
uniqCheck := make(map[string]struct{})
677+
for _, award := range awards {
678+
uid := fmt.Sprintf("%s%d", award.Name, award.User.ID)
679+
if _, ok := uniqCheck[uid]; !ok {
680+
result = append(result, &base.Reaction{
681+
UserID: int64(award.User.ID),
682+
UserName: award.User.Username,
683+
Content: award.Name,
684+
})
685+
uniqCheck[uid] = struct{}{}
686+
}
683687
}
688+
return result
684689
}

services/migrations/gitlab_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"testing"
1414
"time"
1515

16+
"code.gitea.io/gitea/modules/json"
1617
base "code.gitea.io/gitea/modules/migration"
1718

1819
"github.com/stretchr/testify/assert"
@@ -469,3 +470,49 @@ func TestGitlabGetReviews(t *testing.T) {
469470
assertReviewsEqual(t, []*base.Review{&review}, rvs)
470471
}
471472
}
473+
474+
func TestAwardsToReactions(t *testing.T) {
475+
downloader := &GitlabDownloader{}
476+
// yes gitlab can have duplicated reactions (https://gitlab.com/jaywink/socialhome/-/issues/24)
477+
testResponse := `
478+
[
479+
{
480+
"name": "thumbsup",
481+
"user": {
482+
"id": 1241334,
483+
"username": "lafriks"
484+
}
485+
},
486+
{
487+
"name": "thumbsup",
488+
"user": {
489+
"id": 1241334,
490+
"username": "lafriks"
491+
}
492+
},
493+
{
494+
"name": "thumbsup",
495+
"user": {
496+
"id": 4575606,
497+
"username": "real6543"
498+
}
499+
}
500+
]
501+
`
502+
var awards []*gitlab.AwardEmoji
503+
assert.NoError(t, json.Unmarshal([]byte(testResponse), &awards))
504+
505+
reactions := downloader.awardsToReactions(awards)
506+
assert.EqualValues(t, []*base.Reaction{
507+
{
508+
UserName: "lafriks",
509+
UserID: 1241334,
510+
Content: "thumbsup",
511+
},
512+
{
513+
UserName: "real6543",
514+
UserID: 4575606,
515+
Content: "thumbsup",
516+
},
517+
}, reactions)
518+
}

0 commit comments

Comments
 (0)