diff options
author | 6543 <6543@obermui.de> | 2020-04-14 15:53:34 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-14 09:53:34 -0400 |
commit | 10e2f291442fdc7efc31c02f5ffcba79a36db9ac (patch) | |
tree | 5dbda60a983caf8d83ad7202755587acd692c212 /models | |
parent | c571c5bb286b925297f4eb32d130a5496126c3cb (diff) | |
download | gitea-10e2f291442fdc7efc31c02f5ffcba79a36db9ac.tar.gz gitea-10e2f291442fdc7efc31c02f5ffcba79a36db9ac.zip |
Cache PullRequest Divergence (#10914)
* Cache PullRequest Divergence
* only re-calc divergence if AddTestPullRequestTask() is exec
* migrate already open pulls
* finalize
* take care of closed¬-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
Diffstat (limited to 'models')
-rw-r--r-- | models/migrations/migrations.go | 2 | ||||
-rw-r--r-- | models/migrations/v136.go | 69 | ||||
-rw-r--r-- | models/pull.go | 17 |
3 files changed, 88 insertions, 0 deletions
diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index cad7f05f15..e1d46236a9 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 0000000000..115861f344 --- /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 055f9bbc6e..9f1f485266 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 |