Skip to content

Commit 8e94b71

Browse files
GiteaBot6543
andauthored
GitLab migration: Sanitize response for reaction list (#25054) (#25059)
Backport #25054 by @6543 Co-authored-by: 6543 <[email protected]>
1 parent b2ec116 commit 8e94b71

File tree

2 files changed

+67
-15
lines changed

2 files changed

+67
-15
lines changed

services/migrations/gitlab.go

+20-15
Original file line numberDiff line numberDiff line change
@@ -415,17 +415,15 @@ func (g *GitlabDownloader) GetIssues(page, perPage int) ([]*base.Issue, bool, er
415415
milestone = issue.Milestone.Title
416416
}
417417

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

426-
for i := range awards {
427-
reactions = append(reactions, g.awardToReaction(awards[i]))
428-
}
426+
reactions = append(reactions, awards...)
429427

430428
if len(awards) < perPage {
431429
break
@@ -444,7 +442,7 @@ func (g *GitlabDownloader) GetIssues(page, perPage int) ([]*base.Issue, bool, er
444442
State: issue.State,
445443
Created: *issue.CreatedAt,
446444
Labels: labels,
447-
Reactions: reactions,
445+
Reactions: g.awardsToReactions(reactions),
448446
Closed: issue.ClosedAt,
449447
IsLocked: issue.DiscussionLocked,
450448
Updated: *issue.UpdatedAt,
@@ -579,17 +577,15 @@ func (g *GitlabDownloader) GetPullRequests(page, perPage int) ([]*base.PullReque
579577
milestone = pr.Milestone.Title
580578
}
581579

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

590-
for i := range awards {
591-
reactions = append(reactions, g.awardToReaction(awards[i]))
592-
}
588+
reactions = append(reactions, awards...)
593589

594590
if len(awards) < perPage {
595591
break
@@ -616,7 +612,7 @@ func (g *GitlabDownloader) GetPullRequests(page, perPage int) ([]*base.PullReque
616612
MergeCommitSHA: pr.MergeCommitSHA,
617613
MergedTime: mergeTime,
618614
IsLocked: locked,
619-
Reactions: reactions,
615+
Reactions: g.awardsToReactions(reactions),
620616
Head: base.PullRequestBranch{
621617
Ref: pr.SourceBranch,
622618
SHA: pr.SHA,
@@ -677,10 +673,19 @@ func (g *GitlabDownloader) GetReviews(reviewable base.Reviewable) ([]*base.Revie
677673
return reviews, nil
678674
}
679675

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

services/migrations/gitlab_test.go

+47
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)