diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2020-01-24 01:28:15 +0800 |
---|---|---|
committer | Lauris BH <lauris@nix.lv> | 2020-01-23 19:28:15 +0200 |
commit | f6067a8465e7762aea1561106cfee291409a0fd6 (patch) | |
tree | 35c97d4117acfe52dce32c9242e4c9a656c4bc6c /models | |
parent | bfdfa9a8b32ae331f98137169693ba1d71c25b09 (diff) | |
download | gitea-f6067a8465e7762aea1561106cfee291409a0fd6.tar.gz gitea-f6067a8465e7762aea1561106cfee291409a0fd6.zip |
Migrate reviews when migrating repository from github (#9463)
* fix typo
* Migrate reviews when migrating repository from github
* fix lint
* Added test and migration when external user login
* fix test
* fix commented state
* Some improvements
* fix bug when get pull request and ref original author on code comments
* Fix migrated line; Added comment for review
* Don't load all pull requests attributes
* Fix typo
* wrong change copy head
* fix tests
* fix reactions
* Fix test
* fix fmt
* fix review comment reactions
Diffstat (limited to 'models')
-rw-r--r-- | models/external_login_user.go | 5 | ||||
-rw-r--r-- | models/migrate.go | 27 | ||||
-rw-r--r-- | models/migrations/migrations.go | 2 | ||||
-rw-r--r-- | models/migrations/v125.go | 23 | ||||
-rw-r--r-- | models/pull.go | 13 | ||||
-rw-r--r-- | models/review.go | 58 |
6 files changed, 119 insertions, 9 deletions
diff --git a/models/external_login_user.go b/models/external_login_user.go index 6585e49fef..ff153dfb86 100644 --- a/models/external_login_user.go +++ b/models/external_login_user.go @@ -181,5 +181,8 @@ func UpdateMigrationsByType(tp structs.GitServiceType, externalUserID string, us return err } - return UpdateReactionsMigrationsByType(tp, externalUserID, userID) + if err := UpdateReactionsMigrationsByType(tp, externalUserID, userID); err != nil { + return err + } + return UpdateReviewsMigrationsByType(tp, externalUserID, userID) } diff --git a/models/migrate.go b/models/migrate.go index fd28fd156f..28661527a6 100644 --- a/models/migrate.go +++ b/models/migrate.go @@ -4,7 +4,12 @@ package models -import "xorm.io/xorm" +import ( + "code.gitea.io/gitea/modules/structs" + + "xorm.io/builder" + "xorm.io/xorm" +) // InsertMilestones creates milestones of repository. func InsertMilestones(ms ...*Milestone) (err error) { @@ -202,3 +207,23 @@ func InsertReleases(rels ...*Release) error { return sess.Commit() } + +// UpdateReviewsMigrationsByType updates reviews' migrations information via given git service type and original id and poster id +func UpdateReviewsMigrationsByType(tp structs.GitServiceType, originalAuthorID string, posterID int64) error { + _, err := x.Table("review"). + Where(builder.In("issue_id", + builder.Select("issue.id"). + From("issue"). + InnerJoin("repository", "issue.repo_id = repository.id"). + Where(builder.Eq{ + "repository.original_service_type": tp, + }), + )). + And("review.original_author_id = ?", originalAuthorID). + Update(map[string]interface{}{ + "poster_id": posterID, + "original_author": "", + "original_author_id": 0, + }) + return err +} diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 226368b7f3..286e809abb 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -304,6 +304,8 @@ var migrations = []Migration{ NewMigration("Add original informations for reactions", addReactionOriginals), // v124 -> v125 NewMigration("Add columns to user and repository", addUserRepoMissingColumns), + // v125 -> v126 + NewMigration("Add some columns on review for migration", addReviewMigrateInfo), } // Migrate database to current version diff --git a/models/migrations/v125.go b/models/migrations/v125.go new file mode 100644 index 0000000000..ac567f66b9 --- /dev/null +++ b/models/migrations/v125.go @@ -0,0 +1,23 @@ +// 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 ( + "fmt" + + "xorm.io/xorm" +) + +func addReviewMigrateInfo(x *xorm.Engine) error { + type Review struct { + OriginalAuthor string + OriginalAuthorID int64 + } + + if err := x.Sync2(new(Review)); err != nil { + return fmt.Errorf("Sync2: %v", err) + } + return nil +} diff --git a/models/pull.go b/models/pull.go index 3ef631852e..42d93bd541 100644 --- a/models/pull.go +++ b/models/pull.go @@ -655,6 +655,19 @@ func GetPullRequestByID(id int64) (*PullRequest, error) { return getPullRequestByID(x, id) } +// GetPullRequestByIssueIDWithNoAttributes returns pull request with no attributes loaded by given issue ID. +func GetPullRequestByIssueIDWithNoAttributes(issueID int64) (*PullRequest, error) { + var pr PullRequest + has, err := x.Where("issue_id = ?", issueID).Get(&pr) + if err != nil { + return nil, err + } + if !has { + return nil, ErrPullRequestNotExist{0, issueID, 0, 0, "", ""} + } + return &pr, nil +} + func getPullRequestByIssueID(e Engine, issueID int64) (*PullRequest, error) { pr := &PullRequest{ IssueID: issueID, diff --git a/models/review.go b/models/review.go index 2838cfa316..ec28b41d35 100644 --- a/models/review.go +++ b/models/review.go @@ -45,13 +45,15 @@ func (rt ReviewType) Icon() string { // Review represents collection of code comments giving feedback for a PR type Review struct { - ID int64 `xorm:"pk autoincr"` - Type ReviewType - Reviewer *User `xorm:"-"` - ReviewerID int64 `xorm:"index"` - Issue *Issue `xorm:"-"` - IssueID int64 `xorm:"index"` - Content string `xorm:"TEXT"` + ID int64 `xorm:"pk autoincr"` + Type ReviewType + Reviewer *User `xorm:"-"` + ReviewerID int64 `xorm:"index"` + OriginalAuthor string + OriginalAuthorID int64 + Issue *Issue `xorm:"-"` + IssueID int64 `xorm:"index"` + Content string `xorm:"TEXT"` // Official is a review made by an assigned approver (counts towards approval) Official bool `xorm:"NOT NULL DEFAULT false"` CommitID string `xorm:"VARCHAR(40)"` @@ -62,6 +64,8 @@ type Review struct { // CodeComments are the initial code comments of the review CodeComments CodeComments `xorm:"-"` + + Comments []*Comment `xorm:"-"` } func (r *Review) loadCodeComments(e Engine) (err error) { @@ -398,3 +402,43 @@ func MarkReviewsAsNotStale(issueID int64, commitID string) (err error) { return } + +// InsertReviews inserts review and review comments +func InsertReviews(reviews []*Review) error { + sess := x.NewSession() + defer sess.Close() + + if err := sess.Begin(); err != nil { + return err + } + + for _, review := range reviews { + if _, err := sess.NoAutoTime().Insert(review); err != nil { + return err + } + + if _, err := sess.NoAutoTime().Insert(&Comment{ + Type: CommentTypeReview, + Content: review.Content, + PosterID: review.ReviewerID, + OriginalAuthor: review.OriginalAuthor, + OriginalAuthorID: review.OriginalAuthorID, + IssueID: review.IssueID, + ReviewID: review.ID, + CreatedUnix: review.CreatedUnix, + UpdatedUnix: review.UpdatedUnix, + }); err != nil { + return err + } + + for _, c := range review.Comments { + c.ReviewID = review.ID + } + + if _, err := sess.NoAutoTime().Insert(review.Comments); err != nil { + return err + } + } + + return sess.Commit() +} |