Skip to content

Commit f6067a8

Browse files
lunnylafriks
authored andcommitted
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
1 parent bfdfa9a commit f6067a8

File tree

18 files changed

+567
-32
lines changed

18 files changed

+567
-32
lines changed

models/external_login_user.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -181,5 +181,8 @@ func UpdateMigrationsByType(tp structs.GitServiceType, externalUserID string, us
181181
return err
182182
}
183183

184-
return UpdateReactionsMigrationsByType(tp, externalUserID, userID)
184+
if err := UpdateReactionsMigrationsByType(tp, externalUserID, userID); err != nil {
185+
return err
186+
}
187+
return UpdateReviewsMigrationsByType(tp, externalUserID, userID)
185188
}

models/migrate.go

+26-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44

55
package models
66

7-
import "xorm.io/xorm"
7+
import (
8+
"code.gitea.io/gitea/modules/structs"
9+
10+
"xorm.io/builder"
11+
"xorm.io/xorm"
12+
)
813

914
// InsertMilestones creates milestones of repository.
1015
func InsertMilestones(ms ...*Milestone) (err error) {
@@ -202,3 +207,23 @@ func InsertReleases(rels ...*Release) error {
202207

203208
return sess.Commit()
204209
}
210+
211+
// UpdateReviewsMigrationsByType updates reviews' migrations information via given git service type and original id and poster id
212+
func UpdateReviewsMigrationsByType(tp structs.GitServiceType, originalAuthorID string, posterID int64) error {
213+
_, err := x.Table("review").
214+
Where(builder.In("issue_id",
215+
builder.Select("issue.id").
216+
From("issue").
217+
InnerJoin("repository", "issue.repo_id = repository.id").
218+
Where(builder.Eq{
219+
"repository.original_service_type": tp,
220+
}),
221+
)).
222+
And("review.original_author_id = ?", originalAuthorID).
223+
Update(map[string]interface{}{
224+
"poster_id": posterID,
225+
"original_author": "",
226+
"original_author_id": 0,
227+
})
228+
return err
229+
}

models/migrations/migrations.go

+2
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,8 @@ var migrations = []Migration{
304304
NewMigration("Add original informations for reactions", addReactionOriginals),
305305
// v124 -> v125
306306
NewMigration("Add columns to user and repository", addUserRepoMissingColumns),
307+
// v125 -> v126
308+
NewMigration("Add some columns on review for migration", addReviewMigrateInfo),
307309
}
308310

309311
// Migrate database to current version

models/migrations/v125.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
"fmt"
9+
10+
"xorm.io/xorm"
11+
)
12+
13+
func addReviewMigrateInfo(x *xorm.Engine) error {
14+
type Review struct {
15+
OriginalAuthor string
16+
OriginalAuthorID int64
17+
}
18+
19+
if err := x.Sync2(new(Review)); err != nil {
20+
return fmt.Errorf("Sync2: %v", err)
21+
}
22+
return nil
23+
}

models/pull.go

+13
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,19 @@ func GetPullRequestByID(id int64) (*PullRequest, error) {
655655
return getPullRequestByID(x, id)
656656
}
657657

658+
// GetPullRequestByIssueIDWithNoAttributes returns pull request with no attributes loaded by given issue ID.
659+
func GetPullRequestByIssueIDWithNoAttributes(issueID int64) (*PullRequest, error) {
660+
var pr PullRequest
661+
has, err := x.Where("issue_id = ?", issueID).Get(&pr)
662+
if err != nil {
663+
return nil, err
664+
}
665+
if !has {
666+
return nil, ErrPullRequestNotExist{0, issueID, 0, 0, "", ""}
667+
}
668+
return &pr, nil
669+
}
670+
658671
func getPullRequestByIssueID(e Engine, issueID int64) (*PullRequest, error) {
659672
pr := &PullRequest{
660673
IssueID: issueID,

models/review.go

+51-7
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,15 @@ func (rt ReviewType) Icon() string {
4545

4646
// Review represents collection of code comments giving feedback for a PR
4747
type Review struct {
48-
ID int64 `xorm:"pk autoincr"`
49-
Type ReviewType
50-
Reviewer *User `xorm:"-"`
51-
ReviewerID int64 `xorm:"index"`
52-
Issue *Issue `xorm:"-"`
53-
IssueID int64 `xorm:"index"`
54-
Content string `xorm:"TEXT"`
48+
ID int64 `xorm:"pk autoincr"`
49+
Type ReviewType
50+
Reviewer *User `xorm:"-"`
51+
ReviewerID int64 `xorm:"index"`
52+
OriginalAuthor string
53+
OriginalAuthorID int64
54+
Issue *Issue `xorm:"-"`
55+
IssueID int64 `xorm:"index"`
56+
Content string `xorm:"TEXT"`
5557
// Official is a review made by an assigned approver (counts towards approval)
5658
Official bool `xorm:"NOT NULL DEFAULT false"`
5759
CommitID string `xorm:"VARCHAR(40)"`
@@ -62,6 +64,8 @@ type Review struct {
6264

6365
// CodeComments are the initial code comments of the review
6466
CodeComments CodeComments `xorm:"-"`
67+
68+
Comments []*Comment `xorm:"-"`
6569
}
6670

6771
func (r *Review) loadCodeComments(e Engine) (err error) {
@@ -398,3 +402,43 @@ func MarkReviewsAsNotStale(issueID int64, commitID string) (err error) {
398402

399403
return
400404
}
405+
406+
// InsertReviews inserts review and review comments
407+
func InsertReviews(reviews []*Review) error {
408+
sess := x.NewSession()
409+
defer sess.Close()
410+
411+
if err := sess.Begin(); err != nil {
412+
return err
413+
}
414+
415+
for _, review := range reviews {
416+
if _, err := sess.NoAutoTime().Insert(review); err != nil {
417+
return err
418+
}
419+
420+
if _, err := sess.NoAutoTime().Insert(&Comment{
421+
Type: CommentTypeReview,
422+
Content: review.Content,
423+
PosterID: review.ReviewerID,
424+
OriginalAuthor: review.OriginalAuthor,
425+
OriginalAuthorID: review.OriginalAuthorID,
426+
IssueID: review.IssueID,
427+
ReviewID: review.ID,
428+
CreatedUnix: review.CreatedUnix,
429+
UpdatedUnix: review.UpdatedUnix,
430+
}); err != nil {
431+
return err
432+
}
433+
434+
for _, c := range review.Comments {
435+
c.ReviewID = review.ID
436+
}
437+
438+
if _, err := sess.NoAutoTime().Insert(review.Comments); err != nil {
439+
return err
440+
}
441+
}
442+
443+
return sess.Commit()
444+
}

modules/migrations/base/downloader.go

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type Downloader interface {
2323
GetIssues(page, perPage int) ([]*Issue, bool, error)
2424
GetComments(issueNumber int64) ([]*Comment, error)
2525
GetPullRequests(page, perPage int) ([]*PullRequest, error)
26+
GetReviews(pullRequestNumber int64) ([]*Review, error)
2627
}
2728

2829
// DownloaderFactory defines an interface to match a downloader implementation and create a downloader

modules/migrations/base/review.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2019 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 base
6+
7+
import "time"
8+
9+
// enumerate all review states
10+
const (
11+
ReviewStatePending = "PENDING"
12+
ReviewStateApproved = "APPROVED"
13+
ReviewStateChangesRequested = "CHANGES_REQUESTED"
14+
ReviewStateCommented = "COMMENTED"
15+
)
16+
17+
// Review is a standard review information
18+
type Review struct {
19+
ID int64
20+
IssueIndex int64
21+
ReviewerID int64
22+
ReviewerName string
23+
Official bool
24+
CommitID string
25+
Content string
26+
CreatedAt time.Time
27+
State string // PENDING, APPROVED, REQUEST_CHANGES, or COMMENT
28+
Comments []*ReviewComment
29+
}
30+
31+
// ReviewComment represents a review comment
32+
type ReviewComment struct {
33+
ID int64
34+
InReplyTo int64
35+
Content string
36+
TreePath string
37+
DiffHunk string
38+
Position int
39+
CommitID string
40+
PosterID int64
41+
Reactions []*Reaction
42+
CreatedAt time.Time
43+
UpdatedAt time.Time
44+
}

modules/migrations/base/uploader.go

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type Uploader interface {
1717
CreateIssues(issues ...*Issue) error
1818
CreateComments(comments ...*Comment) error
1919
CreatePullRequests(prs ...*PullRequest) error
20+
CreateReviews(reviews ...*Review) error
2021
Rollback() error
2122
Close()
2223
}

modules/migrations/git.go

+5
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,8 @@ func (g *PlainGitDownloader) GetComments(issueNumber int64) ([]*base.Comment, er
7878
func (g *PlainGitDownloader) GetPullRequests(start, limit int) ([]*base.PullRequest, error) {
7979
return nil, ErrNotSupported
8080
}
81+
82+
// GetReviews returns reviews according issue number
83+
func (g *PlainGitDownloader) GetReviews(issueNumber int64) ([]*base.Review, error) {
84+
return nil, ErrNotSupported
85+
}

0 commit comments

Comments
 (0)