Skip to content

Commit 6ac563a

Browse files
committed
Migrate reactions when migrating repository from github
1 parent 4e566df commit 6ac563a

File tree

10 files changed

+208
-48
lines changed

10 files changed

+208
-48
lines changed

models/issue_reaction.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ import (
1717

1818
// Reaction represents a reactions on issues and comments.
1919
type Reaction struct {
20-
ID int64 `xorm:"pk autoincr"`
21-
Type string `xorm:"INDEX UNIQUE(s) NOT NULL"`
22-
IssueID int64 `xorm:"INDEX UNIQUE(s) NOT NULL"`
23-
CommentID int64 `xorm:"INDEX UNIQUE(s)"`
24-
UserID int64 `xorm:"INDEX UNIQUE(s) NOT NULL"`
25-
User *User `xorm:"-"`
26-
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
20+
ID int64 `xorm:"pk autoincr"`
21+
Type string `xorm:"INDEX UNIQUE(s) NOT NULL"`
22+
IssueID int64 `xorm:"INDEX UNIQUE(s) NOT NULL"`
23+
CommentID int64 `xorm:"INDEX UNIQUE(s)"`
24+
UserID int64 `xorm:"INDEX UNIQUE(s) NOT NULL"`
25+
OriginalAuthorID int64 `xorm:"INDEX UNIQUE(s) NOT NULL"`
26+
OriginalAuthor string
27+
User *User `xorm:"-"`
28+
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
2729
}
2830

2931
// FindReactionsOptions describes the conditions to Find reactions

models/migrate.go

+19-2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ func insertIssue(sess *xorm.Session, issue *Issue) error {
6363
return err
6464
}
6565

66+
for _, reaction := range issue.Reactions {
67+
reaction.IssueID = issue.ID
68+
}
69+
if _, err := sess.Insert(issue.Reactions); err != nil {
70+
return err
71+
}
72+
6673
cols := make([]string, 0)
6774
if !issue.IsPull {
6875
sess.ID(issue.RepoID).Incr("num_issues")
@@ -130,9 +137,19 @@ func InsertIssueComments(comments []*Comment) error {
130137
if err := sess.Begin(); err != nil {
131138
return err
132139
}
133-
if _, err := sess.NoAutoTime().Insert(comments); err != nil {
134-
return err
140+
for _, comment := range comments {
141+
if _, err := sess.NoAutoTime().Insert(comment); err != nil {
142+
return err
143+
}
144+
145+
for _, reaction := range comment.Reactions {
146+
reaction.CommentID = comment.ID
147+
}
148+
if _, err := sess.Insert(comment.Reactions); err != nil {
149+
return err
150+
}
135151
}
152+
136153
for issueID := range issueIDs {
137154
if _, err := sess.Exec("UPDATE issue set num_comments = (SELECT count(*) FROM comment WHERE issue_id = ?) WHERE id = ?", issueID, issueID); err != nil {
138155
return err

models/migrations/migrations.go

+2
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,8 @@ var migrations = []Migration{
300300
NewMigration("add is_restricted column for users table", addIsRestricted),
301301
// v122 -> v123
302302
NewMigration("Add Require Signed Commits to ProtectedBranch", addRequireSignedCommits),
303+
// v123 -> v124
304+
NewMigration("Add original informations for reactions", addReactionOriginals),
303305
}
304306

305307
// Migrate database to current version

models/migrations/v123.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2020 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package migrations
6+
7+
import (
8+
"xorm.io/xorm"
9+
)
10+
11+
func addReactionOriginals(x *xorm.Engine) error {
12+
type Reaction struct {
13+
OriginalAuthorID int64 `xorm:"INDEX NOT NULL DEFAULT(0)"`
14+
OriginalAuthor string
15+
}
16+
17+
return x.Sync2(new(Reaction))
18+
}

modules/migrations/base/comment.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ type Comment struct {
1616
Created time.Time
1717
Updated time.Time
1818
Content string
19-
Reactions *Reactions
19+
Reactions []*Reaction
2020
}

modules/migrations/base/issue.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ type Issue struct {
2222
Updated time.Time
2323
Closed *time.Time
2424
Labels []*Label
25-
Reactions *Reactions
25+
Reactions []*Reaction
2626
}

modules/migrations/base/pullrequest.go

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type PullRequest struct {
3333
Assignee string
3434
Assignees []string
3535
IsLocked bool
36+
Reactions []*Reaction
3637
}
3738

3839
// IsForkPullRequest returns true if the pull request from a forked repository but not the same repository

modules/migrations/base/reaction.go

+5-9
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@
55

66
package base
77

8-
// Reactions represents a summary of reactions.
9-
type Reactions struct {
10-
TotalCount int
11-
PlusOne int
12-
MinusOne int
13-
Laugh int
14-
Confused int
15-
Heart int
16-
Hooray int
8+
// Reaction represents a reaction to an issue/pr/comment.
9+
type Reaction struct {
10+
UserID int64
11+
UserName string
12+
Content string
1713
}

modules/migrations/gitea.go

+84-6
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,32 @@ func (g *GiteaLocalUploader) CreateIssues(issues ...*base.Issue) error {
361361
if issue.Closed != nil {
362362
is.ClosedUnix = timeutil.TimeStamp(issue.Closed.Unix())
363363
}
364-
// TODO: add reactions
364+
// add reactions
365+
for _, reaction := range issue.Reactions {
366+
userid, ok := g.userMap[reaction.UserID]
367+
if !ok && tp != "" {
368+
var err error
369+
userid, err = models.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", reaction.UserID))
370+
if err != nil {
371+
log.Error("GetUserIDByExternalUserID: %v", err)
372+
}
373+
if userid > 0 {
374+
g.userMap[reaction.UserID] = userid
375+
}
376+
}
377+
var res = models.Reaction{
378+
Type: reaction.Content,
379+
CreatedUnix: timeutil.TimeStampNow(),
380+
}
381+
if userid > 0 {
382+
res.UserID = userid
383+
} else {
384+
res.UserID = g.doer.ID
385+
res.OriginalAuthorID = reaction.UserID
386+
res.OriginalAuthor = reaction.UserName
387+
}
388+
is.Reactions = append(is.Reactions, &res)
389+
}
365390
iss = append(iss, &is)
366391
}
367392

@@ -420,9 +445,34 @@ func (g *GiteaLocalUploader) CreateComments(comments ...*base.Comment) error {
420445
cm.OriginalAuthorID = comment.PosterID
421446
}
422447

423-
cms = append(cms, &cm)
448+
// add reactions
449+
for _, reaction := range comment.Reactions {
450+
userid, ok := g.userMap[reaction.UserID]
451+
if !ok && tp != "" {
452+
var err error
453+
userid, err = models.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", reaction.UserID))
454+
if err != nil {
455+
log.Error("GetUserIDByExternalUserID: %v", err)
456+
}
457+
if userid > 0 {
458+
g.userMap[reaction.UserID] = userid
459+
}
460+
}
461+
var res = models.Reaction{
462+
Type: reaction.Content,
463+
CreatedUnix: timeutil.TimeStampNow(),
464+
}
465+
if userid > 0 {
466+
res.UserID = userid
467+
} else {
468+
res.UserID = g.doer.ID
469+
res.OriginalAuthorID = reaction.UserID
470+
res.OriginalAuthor = reaction.UserName
471+
}
472+
cm.Reactions = append(cm.Reactions, &res)
473+
}
424474

425-
// TODO: Reactions
475+
cms = append(cms, &cm)
426476
}
427477

428478
return models.InsertIssueComments(cms)
@@ -581,10 +631,12 @@ func (g *GiteaLocalUploader) newPullRequest(pr *base.PullRequest) (*models.PullR
581631
UpdatedUnix: timeutil.TimeStamp(pr.Updated.Unix()),
582632
}
583633

634+
tp := g.gitServiceType.Name()
635+
584636
userid, ok := g.userMap[pr.PosterID]
585-
if !ok {
637+
if !ok && tp != "" {
586638
var err error
587-
userid, err = models.GetUserIDByExternalUserID("github", fmt.Sprintf("%v", pr.PosterID))
639+
userid, err = models.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", pr.PosterID))
588640
if err != nil {
589641
log.Error("GetUserIDByExternalUserID: %v", err)
590642
}
@@ -601,6 +653,33 @@ func (g *GiteaLocalUploader) newPullRequest(pr *base.PullRequest) (*models.PullR
601653
issue.OriginalAuthorID = pr.PosterID
602654
}
603655

656+
// add reactions
657+
for _, reaction := range pr.Reactions {
658+
userid, ok := g.userMap[reaction.UserID]
659+
if !ok && tp != "" {
660+
var err error
661+
userid, err = models.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", reaction.UserID))
662+
if err != nil {
663+
log.Error("GetUserIDByExternalUserID: %v", err)
664+
}
665+
if userid > 0 {
666+
g.userMap[reaction.UserID] = userid
667+
}
668+
}
669+
var res = models.Reaction{
670+
Type: reaction.Content,
671+
CreatedUnix: timeutil.TimeStampNow(),
672+
}
673+
if userid > 0 {
674+
res.UserID = userid
675+
} else {
676+
res.UserID = g.doer.ID
677+
res.OriginalAuthorID = reaction.UserID
678+
res.OriginalAuthor = reaction.UserName
679+
}
680+
issue.Reactions = append(issue.Reactions, &res)
681+
}
682+
604683
var pullRequest = models.PullRequest{
605684
HeadRepoID: g.repo.ID,
606685
HeadBranch: head,
@@ -622,7 +701,6 @@ func (g *GiteaLocalUploader) newPullRequest(pr *base.PullRequest) (*models.PullR
622701
pullRequest.MergerID = g.doer.ID
623702
}
624703

625-
// TODO: reactions
626704
// TODO: assignees
627705

628706
return &pullRequest, nil

modules/migrations/github.go

+68-22
Original file line numberDiff line numberDiff line change
@@ -319,18 +319,6 @@ func (g *GithubDownloaderV3) GetReleases() ([]*base.Release, error) {
319319
return releases, nil
320320
}
321321

322-
func convertGithubReactions(reactions *github.Reactions) *base.Reactions {
323-
return &base.Reactions{
324-
TotalCount: *reactions.TotalCount,
325-
PlusOne: *reactions.PlusOne,
326-
MinusOne: *reactions.MinusOne,
327-
Laugh: *reactions.Laugh,
328-
Confused: *reactions.Confused,
329-
Heart: *reactions.Heart,
330-
Hooray: *reactions.Hooray,
331-
}
332-
}
333-
334322
// GetIssues returns issues according start and limit
335323
func (g *GithubDownloaderV3) GetIssues(page, perPage int) ([]*base.Issue, bool, error) {
336324
opt := &github.IssueListByRepoOptions{
@@ -366,15 +354,34 @@ func (g *GithubDownloaderV3) GetIssues(page, perPage int) ([]*base.Issue, bool,
366354
for _, l := range issue.Labels {
367355
labels = append(labels, convertGithubLabel(&l))
368356
}
369-
var reactions *base.Reactions
370-
if issue.Reactions != nil {
371-
reactions = convertGithubReactions(issue.Reactions)
372-
}
373357

374358
var email string
375359
if issue.User.Email != nil {
376360
email = *issue.User.Email
377361
}
362+
363+
// get reactions
364+
var reactions []*base.Reaction
365+
for i := 0; ; i++ {
366+
res, _, err := g.client.Reactions.ListIssueReactions(g.ctx, g.repoOwner, g.repoName, issue.GetNumber(), &github.ListOptions{
367+
Page: i,
368+
PerPage: perPage,
369+
})
370+
if err != nil {
371+
return nil, false, err
372+
}
373+
if len(res) == 0 {
374+
break
375+
}
376+
for _, reaction := range res {
377+
reactions = append(reactions, &base.Reaction{
378+
UserID: reaction.User.GetID(),
379+
UserName: reaction.User.GetLogin(),
380+
Content: reaction.GetContent(),
381+
})
382+
}
383+
}
384+
378385
allIssues = append(allIssues, &base.Issue{
379386
Title: *issue.Title,
380387
Number: int64(*issue.Number),
@@ -418,9 +425,27 @@ func (g *GithubDownloaderV3) GetComments(issueNumber int64) ([]*base.Comment, er
418425
if comment.User.Email != nil {
419426
email = *comment.User.Email
420427
}
421-
var reactions *base.Reactions
422-
if comment.Reactions != nil {
423-
reactions = convertGithubReactions(comment.Reactions)
428+
429+
// get reactions
430+
var reactions []*base.Reaction
431+
for i := 0; ; i++ {
432+
res, _, err := g.client.Reactions.ListIssueCommentReactions(g.ctx, g.repoOwner, g.repoName, comment.GetID(), &github.ListOptions{
433+
Page: i,
434+
PerPage: 100,
435+
})
436+
if err != nil {
437+
return nil, err
438+
}
439+
if len(res) == 0 {
440+
break
441+
}
442+
for _, reaction := range res {
443+
reactions = append(reactions, &base.Reaction{
444+
UserID: reaction.User.GetID(),
445+
UserName: reaction.User.GetLogin(),
446+
Content: reaction.GetContent(),
447+
})
448+
}
424449
}
425450
allComments = append(allComments, &base.Comment{
426451
IssueIndex: issueNumber,
@@ -473,8 +498,6 @@ func (g *GithubDownloaderV3) GetPullRequests(page, perPage int) ([]*base.PullReq
473498
labels = append(labels, convertGithubLabel(l))
474499
}
475500

476-
// FIXME: This API missing reactions, we may need another extra request to get reactions
477-
478501
var email string
479502
if pr.User.Email != nil {
480503
email = *pr.User.Email
@@ -515,6 +538,28 @@ func (g *GithubDownloaderV3) GetPullRequests(page, perPage int) ([]*base.PullReq
515538
headUserName = *pr.Head.User.Login
516539
}
517540

541+
// get reactions
542+
var reactions []*base.Reaction
543+
for i := 0; ; i++ {
544+
res, _, err := g.client.Reactions.ListIssueReactions(g.ctx, g.repoOwner, g.repoName, pr.GetNumber(), &github.ListOptions{
545+
Page: i,
546+
PerPage: perPage,
547+
})
548+
if err != nil {
549+
return nil, err
550+
}
551+
if len(res) == 0 {
552+
break
553+
}
554+
for _, reaction := range res {
555+
reactions = append(reactions, &base.Reaction{
556+
UserID: reaction.User.GetID(),
557+
UserName: reaction.User.GetLogin(),
558+
Content: reaction.GetContent(),
559+
})
560+
}
561+
}
562+
518563
allPRs = append(allPRs, &base.PullRequest{
519564
Title: *pr.Title,
520565
Number: int64(*pr.Number),
@@ -545,7 +590,8 @@ func (g *GithubDownloaderV3) GetPullRequests(page, perPage int) ([]*base.PullReq
545590
RepoName: *pr.Base.Repo.Name,
546591
OwnerName: *pr.Base.User.Login,
547592
},
548-
PatchURL: *pr.PatchURL,
593+
PatchURL: *pr.PatchURL,
594+
Reactions: reactions,
549595
})
550596
}
551597

0 commit comments

Comments
 (0)