* 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 <6543@obermui.de>

* 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
This commit is contained in:
6543 2020-04-14 15:53:34 +02:00 коммит произвёл GitHub
Родитель c571c5bb28
Коммит 10e2f29144
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
7 изменённых файлов: 122 добавлений и 12 удалений

Просмотреть файл

@ -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)

Просмотреть файл

@ -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

69
models/migrations/v136.go Normal file
Просмотреть файл

@ -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
}

Просмотреть файл

@ -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

Просмотреть файл

@ -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)
}

Просмотреть файл

@ -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)
}
})

Просмотреть файл

@ -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">