Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate reactions when migrating repository from github #9599

Merged
merged 9 commits into from
Jan 15, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update reactions when external user binding
  • Loading branch information
lunny committed Jan 15, 2020
commit 6e4c2a230e04e47adc73edfe3d6706562c772a2f
6 changes: 5 additions & 1 deletion models/external_login_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,5 +177,9 @@ func UpdateMigrationsByType(tp structs.GitServiceType, externalUserID string, us
return err
}

return UpdateReleasesMigrationsByType(tp, externalUserID, userID)
if err := UpdateReleasesMigrationsByType(tp, externalUserID, userID); err != nil {
return err
}

return UpdateReactionsMigrationsByType(tp, externalUserID, userID)
}
19 changes: 18 additions & 1 deletion models/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,11 @@ func (issue *Issue) loadReactions(e Engine) (err error) {
if err != nil {
return err
}
if err = issue.loadRepo(e); err != nil {
return err
}
// Load reaction user data
if _, err := ReactionList(reactions).loadUsers(e); err != nil {
if _, err := ReactionList(reactions).loadUsers(e, issue.Repo); err != nil {
return err
}

Expand Down Expand Up @@ -1836,3 +1839,17 @@ func UpdateIssuesMigrationsByType(gitServiceType structs.GitServiceType, origina
})
return err
}

// UpdateReactionsMigrationsByType updates all migrated repositories' reactions from gitServiceType to replace originalAuthorID to posterID
func UpdateReactionsMigrationsByType(gitServiceType structs.GitServiceType, originalAuthorID string, userID int64) error {
_, err := x.Table("reaction").
Join("INNER", "issue", "issue.id = reaction.issue_id").
Where("issue.repo_id IN (SELECT id FROM repository WHERE original_service_type = ?)", gitServiceType).
And("reaction.original_author_id = ?", originalAuthorID).
Update(map[string]interface{}{
"user_id": userID,
"original_author": "",
"original_author_id": 0,
})
return err
}
8 changes: 4 additions & 4 deletions models/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ func (c *Comment) LoadDepIssueDetails() (err error) {
return err
}

func (c *Comment) loadReactions(e Engine) (err error) {
func (c *Comment) loadReactions(e Engine, repo *Repository) (err error) {
if c.Reactions != nil {
return nil
}
Expand All @@ -437,15 +437,15 @@ func (c *Comment) loadReactions(e Engine) (err error) {
return err
}
// Load reaction user data
if _, err := c.Reactions.LoadUsers(); err != nil {
if _, err := c.Reactions.loadUsers(e, repo); err != nil {
return err
}
return nil
}

// LoadReactions loads comment reactions
func (c *Comment) LoadReactions() error {
return c.loadReactions(x)
func (c *Comment) LoadReactions(repo *Repository) error {
return c.loadReactions(x, repo)
}

func (c *Comment) loadReview(e Engine) (err error) {
Expand Down
15 changes: 10 additions & 5 deletions models/issue_reaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func (list ReactionList) HasUser(userID int64) bool {
return false
}
for _, reaction := range list {
if reaction.UserID == userID {
if reaction.OriginalAuthor == "" && reaction.UserID == userID {
lunny marked this conversation as resolved.
Show resolved Hide resolved
return true
}
}
Expand All @@ -254,14 +254,17 @@ func (list ReactionList) GroupByType() map[string]ReactionList {
func (list ReactionList) getUserIDs() []int64 {
userIDs := make(map[int64]struct{}, len(list))
for _, reaction := range list {
if reaction.OriginalAuthor == "" {
continue
}
if _, ok := userIDs[reaction.UserID]; !ok {
userIDs[reaction.UserID] = struct{}{}
}
}
return keysInt64(userIDs)
}

func (list ReactionList) loadUsers(e Engine) ([]*User, error) {
func (list ReactionList) loadUsers(e Engine, repo *Repository) ([]*User, error) {
if len(list) == 0 {
return nil, nil
}
Expand All @@ -276,7 +279,9 @@ func (list ReactionList) loadUsers(e Engine) ([]*User, error) {
}

for _, reaction := range list {
if user, ok := userMaps[reaction.UserID]; ok {
if reaction.OriginalAuthor != "" {
reaction.User = NewReplaceUser(fmt.Sprintf("%s(%s)", reaction.OriginalAuthor, repo.OriginalServiceType.Name()))
} else if user, ok := userMaps[reaction.UserID]; ok {
reaction.User = user
} else {
reaction.User = NewGhostUser()
Expand All @@ -286,8 +291,8 @@ func (list ReactionList) loadUsers(e Engine) ([]*User, error) {
}

// LoadUsers loads reactions' all users
func (list ReactionList) LoadUsers() ([]*User, error) {
return list.loadUsers(x)
func (list ReactionList) LoadUsers(repo *Repository) ([]*User, error) {
return list.loadUsers(x, repo)
}

// GetFirstUsers returns first reacted user display names separated by comma
Expand Down
1 change: 1 addition & 0 deletions models/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ func InsertIssueComments(comments []*Comment) error {
}

for _, reaction := range comment.Reactions {
reaction.IssueID = comment.IssueID
reaction.CommentID = comment.ID
}
if _, err := sess.Insert(comment.Reactions); err != nil {
Expand Down
9 changes: 9 additions & 0 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,15 @@ func NewGhostUser() *User {
}
}

// NewReplaceUser creates and returns a fake user for external user
func NewReplaceUser(name string) *User {
return &User{
ID: -1,
Name: name,
LowerName: strings.ToLower(name),
}
}

// IsGhost check if user is fake user for a deleted account
func (u *User) IsGhost() bool {
if u == nil {
Expand Down
4 changes: 2 additions & 2 deletions routers/api/v1/repo/issue_reaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func GetIssueCommentReactions(ctx *context.APIContext) {
ctx.Error(http.StatusInternalServerError, "FindIssueReactions", err)
return
}
_, err = reactions.LoadUsers()
_, err = reactions.LoadUsers(ctx.Repo.Repository)
if err != nil {
ctx.Error(http.StatusInternalServerError, "ReactionList.LoadUsers()", err)
return
Expand Down Expand Up @@ -271,7 +271,7 @@ func GetIssueReactions(ctx *context.APIContext) {
ctx.Error(http.StatusInternalServerError, "FindIssueReactions", err)
return
}
_, err = reactions.LoadUsers()
_, err = reactions.LoadUsers(ctx.Repo.Repository)
if err != nil {
ctx.Error(http.StatusInternalServerError, "ReactionList.LoadUsers()", err)
return
Expand Down
4 changes: 2 additions & 2 deletions routers/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -1608,7 +1608,7 @@ func ChangeCommentReaction(ctx *context.Context, form auth.ReactionForm) {
}
// Reload new reactions
comment.Reactions = nil
if err = comment.LoadReactions(); err != nil {
if err = comment.LoadReactions(ctx.Repo.Repository); err != nil {
log.Info("comment.LoadReactions: %s", err)
break
}
Expand All @@ -1622,7 +1622,7 @@ func ChangeCommentReaction(ctx *context.Context, form auth.ReactionForm) {

// Reload new reactions
comment.Reactions = nil
if err = comment.LoadReactions(); err != nil {
if err = comment.LoadReactions(ctx.Repo.Repository); err != nil {
log.Info("comment.LoadReactions: %s", err)
break
}
Expand Down