Skip to content

Commit

Permalink
Cache PullRequest Divergence (#10914)
Browse files Browse the repository at this point in the history
* Cache PullRequest Divergence

* only re-calc divergence if AddTestPullRequestTask() is exec

* migrate already open pulls

* finalize

* take care of closed&not-merged+deleted-branch pull requests

* fix nil pointer exeption

Signed-off-by: 6543 <[email protected]>

* try this

* no error its a warn

* init gitea-repositories-meta

* dont use gitDivergence type

* CI.restart()

* CI.restart()

* CI.restart()

* CI.restart()

* check IsUserAllowedToUpdate independend from CommitsBehind
  • Loading branch information
6543 authored Apr 14, 2020
1 parent c571c5b commit 10e2f29
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 12 deletions.
7 changes: 7 additions & 0 deletions integrations/migration-test/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"io/ioutil"
"os"
"path"
"path/filepath"
"regexp"
"sort"
"strings"
Expand All @@ -25,6 +26,7 @@ import (
"code.gitea.io/gitea/modules/setting"

"github.com/stretchr/testify/assert"
"github.com/unknwon/com"
"xorm.io/xorm"
)

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
69 changes: 69 additions & 0 deletions models/migrations/v136.go
Original file line number Diff line number Diff line change
@@ -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
}
17 changes: 17 additions & 0 deletions models/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:"-"`
Expand Down Expand Up @@ -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
Expand Down
12 changes: 1 addition & 11 deletions routers/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}

Expand Down
25 changes: 25 additions & 0 deletions services/pull/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
})
Expand Down
2 changes: 1 addition & 1 deletion templates/repo/issue/view_content/pull.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@
</div>
{{end}}
{{end}}
{{if and .Divergence (gt .Divergence.Behind 0)}}
{{if gt .Issue.PullRequest.CommitsBehind 0}}
<div class="ui very compact branch-update grid">
<div class="row">
<div class="item text gray eleven wide left floated column">
Expand Down

0 comments on commit 10e2f29

Please sign in to comment.