Skip to content

Commit

Permalink
Migrate reactions when migrating repository from github
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny committed Jan 15, 2020
1 parent 4e566df commit 6ac563a
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 48 deletions.
16 changes: 9 additions & 7 deletions models/issue_reaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ import (

// Reaction represents a reactions on issues and comments.
type Reaction struct {
ID int64 `xorm:"pk autoincr"`
Type string `xorm:"INDEX UNIQUE(s) NOT NULL"`
IssueID int64 `xorm:"INDEX UNIQUE(s) NOT NULL"`
CommentID int64 `xorm:"INDEX UNIQUE(s)"`
UserID int64 `xorm:"INDEX UNIQUE(s) NOT NULL"`
User *User `xorm:"-"`
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
ID int64 `xorm:"pk autoincr"`
Type string `xorm:"INDEX UNIQUE(s) NOT NULL"`
IssueID int64 `xorm:"INDEX UNIQUE(s) NOT NULL"`
CommentID int64 `xorm:"INDEX UNIQUE(s)"`
UserID int64 `xorm:"INDEX UNIQUE(s) NOT NULL"`
OriginalAuthorID int64 `xorm:"INDEX UNIQUE(s) NOT NULL"`
OriginalAuthor string
User *User `xorm:"-"`
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
}

// FindReactionsOptions describes the conditions to Find reactions
Expand Down
21 changes: 19 additions & 2 deletions models/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ func insertIssue(sess *xorm.Session, issue *Issue) error {
return err
}

for _, reaction := range issue.Reactions {
reaction.IssueID = issue.ID
}
if _, err := sess.Insert(issue.Reactions); err != nil {
return err
}

cols := make([]string, 0)
if !issue.IsPull {
sess.ID(issue.RepoID).Incr("num_issues")
Expand Down Expand Up @@ -130,9 +137,19 @@ func InsertIssueComments(comments []*Comment) error {
if err := sess.Begin(); err != nil {
return err
}
if _, err := sess.NoAutoTime().Insert(comments); err != nil {
return err
for _, comment := range comments {
if _, err := sess.NoAutoTime().Insert(comment); err != nil {
return err
}

for _, reaction := range comment.Reactions {
reaction.CommentID = comment.ID
}
if _, err := sess.Insert(comment.Reactions); err != nil {
return err
}
}

for issueID := range issueIDs {
if _, err := sess.Exec("UPDATE issue set num_comments = (SELECT count(*) FROM comment WHERE issue_id = ?) WHERE id = ?", issueID, issueID); err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ var migrations = []Migration{
NewMigration("add is_restricted column for users table", addIsRestricted),
// v122 -> v123
NewMigration("Add Require Signed Commits to ProtectedBranch", addRequireSignedCommits),
// v123 -> v124
NewMigration("Add original informations for reactions", addReactionOriginals),
}

// Migrate database to current version
Expand Down
18 changes: 18 additions & 0 deletions models/migrations/v123.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package migrations

import (
"xorm.io/xorm"
)

func addReactionOriginals(x *xorm.Engine) error {
type Reaction struct {
OriginalAuthorID int64 `xorm:"INDEX NOT NULL DEFAULT(0)"`
OriginalAuthor string
}

return x.Sync2(new(Reaction))
}
2 changes: 1 addition & 1 deletion modules/migrations/base/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ type Comment struct {
Created time.Time
Updated time.Time
Content string
Reactions *Reactions
Reactions []*Reaction
}
2 changes: 1 addition & 1 deletion modules/migrations/base/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ type Issue struct {
Updated time.Time
Closed *time.Time
Labels []*Label
Reactions *Reactions
Reactions []*Reaction
}
1 change: 1 addition & 0 deletions modules/migrations/base/pullrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type PullRequest struct {
Assignee string
Assignees []string
IsLocked bool
Reactions []*Reaction
}

// IsForkPullRequest returns true if the pull request from a forked repository but not the same repository
Expand Down
14 changes: 5 additions & 9 deletions modules/migrations/base/reaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@

package base

// Reactions represents a summary of reactions.
type Reactions struct {
TotalCount int
PlusOne int
MinusOne int
Laugh int
Confused int
Heart int
Hooray int
// Reaction represents a reaction to an issue/pr/comment.
type Reaction struct {
UserID int64
UserName string
Content string
}
90 changes: 84 additions & 6 deletions modules/migrations/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,32 @@ func (g *GiteaLocalUploader) CreateIssues(issues ...*base.Issue) error {
if issue.Closed != nil {
is.ClosedUnix = timeutil.TimeStamp(issue.Closed.Unix())
}
// TODO: add reactions
// add reactions
for _, reaction := range issue.Reactions {
userid, ok := g.userMap[reaction.UserID]
if !ok && tp != "" {
var err error
userid, err = models.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", reaction.UserID))
if err != nil {
log.Error("GetUserIDByExternalUserID: %v", err)
}
if userid > 0 {
g.userMap[reaction.UserID] = userid
}
}
var res = models.Reaction{
Type: reaction.Content,
CreatedUnix: timeutil.TimeStampNow(),
}
if userid > 0 {
res.UserID = userid
} else {
res.UserID = g.doer.ID
res.OriginalAuthorID = reaction.UserID
res.OriginalAuthor = reaction.UserName
}
is.Reactions = append(is.Reactions, &res)
}
iss = append(iss, &is)
}

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

cms = append(cms, &cm)
// add reactions
for _, reaction := range comment.Reactions {
userid, ok := g.userMap[reaction.UserID]
if !ok && tp != "" {
var err error
userid, err = models.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", reaction.UserID))
if err != nil {
log.Error("GetUserIDByExternalUserID: %v", err)
}
if userid > 0 {
g.userMap[reaction.UserID] = userid
}
}
var res = models.Reaction{
Type: reaction.Content,
CreatedUnix: timeutil.TimeStampNow(),
}
if userid > 0 {
res.UserID = userid
} else {
res.UserID = g.doer.ID
res.OriginalAuthorID = reaction.UserID
res.OriginalAuthor = reaction.UserName
}
cm.Reactions = append(cm.Reactions, &res)
}

// TODO: Reactions
cms = append(cms, &cm)
}

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

tp := g.gitServiceType.Name()

userid, ok := g.userMap[pr.PosterID]
if !ok {
if !ok && tp != "" {
var err error
userid, err = models.GetUserIDByExternalUserID("github", fmt.Sprintf("%v", pr.PosterID))
userid, err = models.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", pr.PosterID))
if err != nil {
log.Error("GetUserIDByExternalUserID: %v", err)
}
Expand All @@ -601,6 +653,33 @@ func (g *GiteaLocalUploader) newPullRequest(pr *base.PullRequest) (*models.PullR
issue.OriginalAuthorID = pr.PosterID
}

// add reactions
for _, reaction := range pr.Reactions {
userid, ok := g.userMap[reaction.UserID]
if !ok && tp != "" {
var err error
userid, err = models.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", reaction.UserID))
if err != nil {
log.Error("GetUserIDByExternalUserID: %v", err)
}
if userid > 0 {
g.userMap[reaction.UserID] = userid
}
}
var res = models.Reaction{
Type: reaction.Content,
CreatedUnix: timeutil.TimeStampNow(),
}
if userid > 0 {
res.UserID = userid
} else {
res.UserID = g.doer.ID
res.OriginalAuthorID = reaction.UserID
res.OriginalAuthor = reaction.UserName
}
issue.Reactions = append(issue.Reactions, &res)
}

var pullRequest = models.PullRequest{
HeadRepoID: g.repo.ID,
HeadBranch: head,
Expand All @@ -622,7 +701,6 @@ func (g *GiteaLocalUploader) newPullRequest(pr *base.PullRequest) (*models.PullR
pullRequest.MergerID = g.doer.ID
}

// TODO: reactions
// TODO: assignees

return &pullRequest, nil
Expand Down
90 changes: 68 additions & 22 deletions modules/migrations/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,18 +319,6 @@ func (g *GithubDownloaderV3) GetReleases() ([]*base.Release, error) {
return releases, nil
}

func convertGithubReactions(reactions *github.Reactions) *base.Reactions {
return &base.Reactions{
TotalCount: *reactions.TotalCount,
PlusOne: *reactions.PlusOne,
MinusOne: *reactions.MinusOne,
Laugh: *reactions.Laugh,
Confused: *reactions.Confused,
Heart: *reactions.Heart,
Hooray: *reactions.Hooray,
}
}

// GetIssues returns issues according start and limit
func (g *GithubDownloaderV3) GetIssues(page, perPage int) ([]*base.Issue, bool, error) {
opt := &github.IssueListByRepoOptions{
Expand Down Expand Up @@ -366,15 +354,34 @@ func (g *GithubDownloaderV3) GetIssues(page, perPage int) ([]*base.Issue, bool,
for _, l := range issue.Labels {
labels = append(labels, convertGithubLabel(&l))
}
var reactions *base.Reactions
if issue.Reactions != nil {
reactions = convertGithubReactions(issue.Reactions)
}

var email string
if issue.User.Email != nil {
email = *issue.User.Email
}

// get reactions
var reactions []*base.Reaction
for i := 0; ; i++ {
res, _, err := g.client.Reactions.ListIssueReactions(g.ctx, g.repoOwner, g.repoName, issue.GetNumber(), &github.ListOptions{
Page: i,
PerPage: perPage,
})
if err != nil {
return nil, false, err
}
if len(res) == 0 {
break
}
for _, reaction := range res {
reactions = append(reactions, &base.Reaction{
UserID: reaction.User.GetID(),
UserName: reaction.User.GetLogin(),
Content: reaction.GetContent(),
})
}
}

allIssues = append(allIssues, &base.Issue{
Title: *issue.Title,
Number: int64(*issue.Number),
Expand Down Expand Up @@ -418,9 +425,27 @@ func (g *GithubDownloaderV3) GetComments(issueNumber int64) ([]*base.Comment, er
if comment.User.Email != nil {
email = *comment.User.Email
}
var reactions *base.Reactions
if comment.Reactions != nil {
reactions = convertGithubReactions(comment.Reactions)

// get reactions
var reactions []*base.Reaction
for i := 0; ; i++ {
res, _, err := g.client.Reactions.ListIssueCommentReactions(g.ctx, g.repoOwner, g.repoName, comment.GetID(), &github.ListOptions{
Page: i,
PerPage: 100,
})
if err != nil {
return nil, err
}
if len(res) == 0 {
break
}
for _, reaction := range res {
reactions = append(reactions, &base.Reaction{
UserID: reaction.User.GetID(),
UserName: reaction.User.GetLogin(),
Content: reaction.GetContent(),
})
}
}
allComments = append(allComments, &base.Comment{
IssueIndex: issueNumber,
Expand Down Expand Up @@ -473,8 +498,6 @@ func (g *GithubDownloaderV3) GetPullRequests(page, perPage int) ([]*base.PullReq
labels = append(labels, convertGithubLabel(l))
}

// FIXME: This API missing reactions, we may need another extra request to get reactions

var email string
if pr.User.Email != nil {
email = *pr.User.Email
Expand Down Expand Up @@ -515,6 +538,28 @@ func (g *GithubDownloaderV3) GetPullRequests(page, perPage int) ([]*base.PullReq
headUserName = *pr.Head.User.Login
}

// get reactions
var reactions []*base.Reaction
for i := 0; ; i++ {
res, _, err := g.client.Reactions.ListIssueReactions(g.ctx, g.repoOwner, g.repoName, pr.GetNumber(), &github.ListOptions{
Page: i,
PerPage: perPage,
})
if err != nil {
return nil, err
}
if len(res) == 0 {
break
}
for _, reaction := range res {
reactions = append(reactions, &base.Reaction{
UserID: reaction.User.GetID(),
UserName: reaction.User.GetLogin(),
Content: reaction.GetContent(),
})
}
}

allPRs = append(allPRs, &base.PullRequest{
Title: *pr.Title,
Number: int64(*pr.Number),
Expand Down Expand Up @@ -545,7 +590,8 @@ func (g *GithubDownloaderV3) GetPullRequests(page, perPage int) ([]*base.PullReq
RepoName: *pr.Base.Repo.Name,
OwnerName: *pr.Base.User.Login,
},
PatchURL: *pr.PatchURL,
PatchURL: *pr.PatchURL,
Reactions: reactions,
})
}

Expand Down

0 comments on commit 6ac563a

Please sign in to comment.