forked from gitea/gitea
1
0
Fork 0

GitLab migration: Sanitize response for reaction list (#25054) (#25059)

Backport #25054 by @6543

Co-authored-by: 6543 <6543@obermui.de>
This commit is contained in:
Giteabot 2023-06-02 17:13:33 -04:00 committed by GitHub
parent b2ec116c60
commit 8e94b715cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 67 additions and 15 deletions

View File

@ -415,7 +415,7 @@ func (g *GitlabDownloader) GetIssues(page, perPage int) ([]*base.Issue, bool, er
milestone = issue.Milestone.Title milestone = issue.Milestone.Title
} }
var reactions []*base.Reaction var reactions []*gitlab.AwardEmoji
awardPage := 1 awardPage := 1
for { for {
awards, _, err := g.client.AwardEmoji.ListIssueAwardEmoji(g.repoID, issue.IID, &gitlab.ListAwardEmojiOptions{Page: awardPage, PerPage: perPage}, gitlab.WithContext(g.ctx)) awards, _, err := g.client.AwardEmoji.ListIssueAwardEmoji(g.repoID, issue.IID, &gitlab.ListAwardEmojiOptions{Page: awardPage, PerPage: perPage}, gitlab.WithContext(g.ctx))
@ -423,9 +423,7 @@ func (g *GitlabDownloader) GetIssues(page, perPage int) ([]*base.Issue, bool, er
return nil, false, fmt.Errorf("error while listing issue awards: %w", err) return nil, false, fmt.Errorf("error while listing issue awards: %w", err)
} }
for i := range awards { reactions = append(reactions, awards...)
reactions = append(reactions, g.awardToReaction(awards[i]))
}
if len(awards) < perPage { if len(awards) < perPage {
break break
@ -444,7 +442,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: reactions, Reactions: g.awardsToReactions(reactions),
Closed: issue.ClosedAt, Closed: issue.ClosedAt,
IsLocked: issue.DiscussionLocked, IsLocked: issue.DiscussionLocked,
Updated: *issue.UpdatedAt, Updated: *issue.UpdatedAt,
@ -579,7 +577,7 @@ func (g *GitlabDownloader) GetPullRequests(page, perPage int) ([]*base.PullReque
milestone = pr.Milestone.Title milestone = pr.Milestone.Title
} }
var reactions []*base.Reaction var reactions []*gitlab.AwardEmoji
awardPage := 1 awardPage := 1
for { for {
awards, _, err := g.client.AwardEmoji.ListMergeRequestAwardEmoji(g.repoID, pr.IID, &gitlab.ListAwardEmojiOptions{Page: awardPage, PerPage: perPage}, gitlab.WithContext(g.ctx)) awards, _, err := g.client.AwardEmoji.ListMergeRequestAwardEmoji(g.repoID, pr.IID, &gitlab.ListAwardEmojiOptions{Page: awardPage, PerPage: perPage}, gitlab.WithContext(g.ctx))
@ -587,9 +585,7 @@ func (g *GitlabDownloader) GetPullRequests(page, perPage int) ([]*base.PullReque
return nil, false, fmt.Errorf("error while listing merge requests awards: %w", err) return nil, false, fmt.Errorf("error while listing merge requests awards: %w", err)
} }
for i := range awards { reactions = append(reactions, awards...)
reactions = append(reactions, g.awardToReaction(awards[i]))
}
if len(awards) < perPage { if len(awards) < perPage {
break break
@ -616,7 +612,7 @@ func (g *GitlabDownloader) GetPullRequests(page, perPage int) ([]*base.PullReque
MergeCommitSHA: pr.MergeCommitSHA, MergeCommitSHA: pr.MergeCommitSHA,
MergedTime: mergeTime, MergedTime: mergeTime,
IsLocked: locked, IsLocked: locked,
Reactions: reactions, Reactions: g.awardsToReactions(reactions),
Head: base.PullRequestBranch{ Head: base.PullRequestBranch{
Ref: pr.SourceBranch, Ref: pr.SourceBranch,
SHA: pr.SHA, SHA: pr.SHA,
@ -677,10 +673,19 @@ func (g *GitlabDownloader) GetReviews(reviewable base.Reviewable) ([]*base.Revie
return reviews, nil return reviews, nil
} }
func (g *GitlabDownloader) awardToReaction(award *gitlab.AwardEmoji) *base.Reaction { func (g *GitlabDownloader) awardsToReactions(awards []*gitlab.AwardEmoji) []*base.Reaction {
return &base.Reaction{ result := make([]*base.Reaction, 0, len(awards))
UserID: int64(award.User.ID), uniqCheck := make(map[string]struct{})
UserName: award.User.Username, for _, award := range awards {
Content: award.Name, uid := fmt.Sprintf("%s%d", award.Name, award.User.ID)
if _, ok := uniqCheck[uid]; !ok {
result = append(result, &base.Reaction{
UserID: int64(award.User.ID),
UserName: award.User.Username,
Content: award.Name,
})
uniqCheck[uid] = struct{}{}
}
} }
return result
} }

View File

@ -13,6 +13,7 @@ import (
"testing" "testing"
"time" "time"
"code.gitea.io/gitea/modules/json"
base "code.gitea.io/gitea/modules/migration" base "code.gitea.io/gitea/modules/migration"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -469,3 +470,49 @@ func TestGitlabGetReviews(t *testing.T) {
assertReviewsEqual(t, []*base.Review{&review}, rvs) assertReviewsEqual(t, []*base.Review{&review}, rvs)
} }
} }
func TestAwardsToReactions(t *testing.T) {
downloader := &GitlabDownloader{}
// yes gitlab can have duplicated reactions (https://gitlab.com/jaywink/socialhome/-/issues/24)
testResponse := `
[
{
"name": "thumbsup",
"user": {
"id": 1241334,
"username": "lafriks"
}
},
{
"name": "thumbsup",
"user": {
"id": 1241334,
"username": "lafriks"
}
},
{
"name": "thumbsup",
"user": {
"id": 4575606,
"username": "real6543"
}
}
]
`
var awards []*gitlab.AwardEmoji
assert.NoError(t, json.Unmarshal([]byte(testResponse), &awards))
reactions := downloader.awardsToReactions(awards)
assert.EqualValues(t, []*base.Reaction{
{
UserName: "lafriks",
UserID: 1241334,
Content: "thumbsup",
},
{
UserName: "real6543",
UserID: 4575606,
Content: "thumbsup",
},
}, reactions)
}