diff --git a/integrations/migration-test/migration_test.go b/integrations/migration-test/migration_test.go index a4d01efef3c..4ee045db383 100644 --- a/integrations/migration-test/migration_test.go +++ b/integrations/migration-test/migration_test.go @@ -12,6 +12,7 @@ import ( "io/ioutil" "os" "path" + "path/filepath" "regexp" "sort" "strings" @@ -25,6 +26,7 @@ import ( "code.gitea.io/gitea/modules/setting" "github.com/stretchr/testify/assert" + "github.com/unknwon/com" "xorm.io/xorm" ) @@ -54,6 +56,11 @@ func initMigrationTest(t *testing.T) func() { } setting.NewContext() + + assert.True(t, len(setting.RepoRootPath) != 0) + assert.NoError(t, os.RemoveAll(setting.RepoRootPath)) + assert.NoError(t, com.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"), setting.RepoRootPath)) + setting.CheckLFSVersion() setting.InitDBConfig() setting.NewLogServices(true) diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index cad7f05f159..e1d46236a9b 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -204,6 +204,8 @@ var migrations = []Migration{ NewMigration("Refix merge base for merged pull requests", refixMergeBase), // v135 -> 136 NewMigration("Add OrgID column to Labels table", addOrgIDLabelColumn), + // v136 -> 137 + NewMigration("Add CommitsAhead and CommitsBehind Column to PullRequest Table", addCommitDivergenceToPulls), } // GetCurrentDBVersion returns the current db version diff --git a/models/migrations/v136.go b/models/migrations/v136.go new file mode 100644 index 00000000000..115861f344c --- /dev/null +++ b/models/migrations/v136.go @@ -0,0 +1,69 @@ +// 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" + + "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/setting" + pull_service "code.gitea.io/gitea/services/pull" + + "xorm.io/xorm" +) + +func addCommitDivergenceToPulls(x *xorm.Engine) error { + + if err := x.Sync2(new(models.PullRequest)); err != nil { + return fmt.Errorf("Sync2: %v", err) + } + + var last int + batchSize := setting.Database.IterateBufferSize + sess := x.NewSession() + defer sess.Close() + for { + if err := sess.Begin(); err != nil { + return err + } + var results = make([]*models.PullRequest, 0, batchSize) + err := sess.Where("has_merged = ?", false).OrderBy("id").Limit(batchSize, last).Find(&results) + if err != nil { + return err + } + if len(results) == 0 { + break + } + last += len(results) + + for _, pr := range results { + divergence, err := pull_service.GetDiverging(pr) + if err != nil { + if err = pr.LoadIssue(); err != nil { + return fmt.Errorf("pr.LoadIssue()[%d]: %v", pr.ID, err) + } + if !pr.Issue.IsClosed { + return fmt.Errorf("GetDiverging: %v", err) + } + log.Warn("Could not recalculate Divergence for pull: %d", pr.ID) + pr.CommitsAhead = 0 + pr.CommitsBehind = 0 + } + if divergence != nil { + pr.CommitsAhead = divergence.Ahead + pr.CommitsBehind = divergence.Behind + } + if _, err = sess.ID(pr.ID).Cols("commits_ahead", "commits_behind").Update(pr); err != nil { + return fmt.Errorf("Update Cols: %v", err) + } + } + + if err := sess.Commit(); err != nil { + return err + } + } + return nil +} diff --git a/models/pull.go b/models/pull.go index 055f9bbc6e5..9f1f485266a 100644 --- a/models/pull.go +++ b/models/pull.go @@ -42,6 +42,8 @@ type PullRequest struct { Type PullRequestType Status PullRequestStatus ConflictedFiles []string `xorm:"TEXT JSON"` + CommitsAhead int + CommitsBehind int IssueID int64 `xorm:"INDEX"` Issue *Issue `xorm:"-"` @@ -615,6 +617,21 @@ func (pr *PullRequest) GetWorkInProgressPrefix() string { return "" } +// UpdateCommitDivergence update Divergence of a pull request +func (pr *PullRequest) UpdateCommitDivergence(ahead, behind int) error { + return pr.updateCommitDivergence(x, ahead, behind) +} + +func (pr *PullRequest) updateCommitDivergence(e Engine, ahead, behind int) error { + if pr.ID == 0 { + return fmt.Errorf("pull ID is 0") + } + pr.CommitsAhead = ahead + pr.CommitsBehind = behind + _, err := e.ID(pr.ID).Cols("commits_ahead", "commits_behind").Update(pr) + return err +} + // IsSameRepo returns true if base repo and head repo is the same func (pr *PullRequest) IsSameRepo() bool { return pr.BaseRepoID == pr.HeadRepoID diff --git a/routers/repo/pull.go b/routers/repo/pull.go index ef000208f44..00195fd02a1 100644 --- a/routers/repo/pull.go +++ b/routers/repo/pull.go @@ -399,8 +399,6 @@ func PrepareViewPullInfo(ctx *context.Context, issue *models.Issue) *git.Compare var headBranchSha string // HeadRepo may be missing if pull.HeadRepo != nil { - var err error - headGitRepo, err := git.OpenRepository(pull.HeadRepo.RepoPath()) if err != nil { ctx.ServerError("OpenRepository", err) @@ -420,19 +418,11 @@ func PrepareViewPullInfo(ctx *context.Context, issue *models.Issue) *git.Compare } if headBranchExist { - allowUpdate, err := pull_service.IsUserAllowedToUpdate(pull, ctx.User) + ctx.Data["UpdateAllowed"], err = pull_service.IsUserAllowedToUpdate(pull, ctx.User) if err != nil { ctx.ServerError("IsUserAllowedToUpdate", err) return nil } - ctx.Data["UpdateAllowed"] = allowUpdate - - divergence, err := pull_service.GetDiverging(pull) - if err != nil { - ctx.ServerError("GetDiverging", err) - return nil - } - ctx.Data["Divergence"] = divergence ctx.Data["GetCommitMessages"] = pull_service.GetCommitMessages(pull) } diff --git a/services/pull/pull.go b/services/pull/pull.go index 2797294980e..fb4af063728 100644 --- a/services/pull/pull.go +++ b/services/pull/pull.go @@ -31,6 +31,13 @@ func NewPullRequest(repo *models.Repository, pull *models.Issue, labelIDs []int6 return err } + divergence, err := GetDiverging(pr) + if err != nil { + return err + } + pr.CommitsAhead = divergence.Ahead + pr.CommitsBehind = divergence.Behind + if err := models.NewPullRequest(repo, pull, labelIDs, uuids, pr); err != nil { return err } @@ -212,6 +219,15 @@ func AddTestPullRequestTask(doer *models.User, repoID int64, branch string, isSy if err := models.MarkReviewsAsNotStale(pr.IssueID, newCommitID); err != nil { log.Error("MarkReviewsAsNotStale: %v", err) } + divergence, err := GetDiverging(pr) + if err != nil { + log.Error("GetDiverging: %v", err) + } else { + err = pr.UpdateCommitDivergence(divergence.Ahead, divergence.Behind) + if err != nil { + log.Error("UpdateCommitDivergence: %v", err) + } + } } pr.Issue.PullRequest = pr @@ -229,6 +245,15 @@ func AddTestPullRequestTask(doer *models.User, repoID int64, branch string, isSy return } for _, pr := range prs { + divergence, err := GetDiverging(pr) + if err != nil { + log.Error("GetDiverging: %v", err) + } else { + err = pr.UpdateCommitDivergence(divergence.Ahead, divergence.Behind) + if err != nil { + log.Error("UpdateCommitDivergence: %v", err) + } + } AddToTaskQueue(pr) } }) diff --git a/templates/repo/issue/view_content/pull.tmpl b/templates/repo/issue/view_content/pull.tmpl index e580d955865..03ac1b6cd70 100644 --- a/templates/repo/issue/view_content/pull.tmpl +++ b/templates/repo/issue/view_content/pull.tmpl @@ -305,7 +305,7 @@ {{end}} {{end}} - {{if and .Divergence (gt .Divergence.Behind 0)}} + {{if gt .Issue.PullRequest.CommitsBehind 0}}