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 reviews when migrating repository from github #9463

Merged
merged 17 commits into from
Jan 23, 2020
5 changes: 4 additions & 1 deletion models/external_login_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
27 changes: 26 additions & 1 deletion models/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
2 changes: 2 additions & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions models/migrations/v125.go
Original file line number Diff line number Diff line change
@@ -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
}
13 changes: 13 additions & 0 deletions models/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
58 changes: 51 additions & 7 deletions models/review.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)"`
Expand All @@ -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) {
Expand Down Expand Up @@ -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()
}
1 change: 1 addition & 0 deletions modules/migrations/base/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Downloader interface {
GetIssues(page, perPage int) ([]*Issue, bool, error)
GetComments(issueNumber int64) ([]*Comment, error)
GetPullRequests(page, perPage int) ([]*PullRequest, error)
GetReviews(pullRequestNumber int64) ([]*Review, error)
}

// DownloaderFactory defines an interface to match a downloader implementation and create a downloader
Expand Down
44 changes: 44 additions & 0 deletions modules/migrations/base/review.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2019 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 base

import "time"

// enumerate all review states
const (
ReviewStatePending = "PENDING"
ReviewStateApproved = "APPROVED"
ReviewStateChangesRequested = "CHANGES_REQUESTED"
ReviewStateCommented = "COMMENTED"
)

// Review is a standard review information
type Review struct {
ID int64
IssueIndex int64
ReviewerID int64
ReviewerName string
Official bool
CommitID string
Content string
CreatedAt time.Time
State string // PENDING, APPROVED, REQUEST_CHANGES, or COMMENT
Comments []*ReviewComment
}

// ReviewComment represents a review comment
type ReviewComment struct {
ID int64
InReplyTo int64
Content string
TreePath string
DiffHunk string
Position int
CommitID string
PosterID int64
Reactions []*Reaction
CreatedAt time.Time
UpdatedAt time.Time
}
1 change: 1 addition & 0 deletions modules/migrations/base/uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Uploader interface {
CreateIssues(issues ...*Issue) error
CreateComments(comments ...*Comment) error
CreatePullRequests(prs ...*PullRequest) error
CreateReviews(reviews ...*Review) error
Rollback() error
Close()
}
5 changes: 5 additions & 0 deletions modules/migrations/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,8 @@ func (g *PlainGitDownloader) GetComments(issueNumber int64) ([]*base.Comment, er
func (g *PlainGitDownloader) GetPullRequests(start, limit int) ([]*base.PullRequest, error) {
return nil, ErrNotSupported
}

// GetReviews returns reviews according issue number
func (g *PlainGitDownloader) GetReviews(issueNumber int64) ([]*base.Review, error) {
return nil, ErrNotSupported
}
Loading