summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
author6543 <6543@obermui.de>2020-04-17 03:00:36 +0200
committerGitHub <noreply@github.com>2020-04-16 22:00:36 -0300
commitc52d48aae46af879fdfcfd94d03b7072878b5441 (patch)
tree8a3c5e7ab477059ceabd96a73ecb702602227c19 /models
parent2cb5878529992c1ffa6de2143d4a1e4673e33b1a (diff)
downloadgitea-c52d48aae46af879fdfcfd94d03b7072878b5441.tar.gz
gitea-c52d48aae46af879fdfcfd94d03b7072878b5441.zip
Prevent merge of outdated PRs on protected branches (#11012)
* Block PR on Outdated Branch * finalize * cleanup * fix typo and sentences thanks @guillep2k Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> Co-authored-by: Lauris BH <lauris@nix.lv>
Diffstat (limited to 'models')
-rw-r--r--models/branches.go6
-rw-r--r--models/migrations/migrations.go6
-rw-r--r--models/migrations/v137.go16
3 files changed, 26 insertions, 2 deletions
diff --git a/models/branches.go b/models/branches.go
index 44cfb41403..e6b8d61a70 100644
--- a/models/branches.go
+++ b/models/branches.go
@@ -47,6 +47,7 @@ type ProtectedBranch struct {
ApprovalsWhitelistTeamIDs []int64 `xorm:"JSON TEXT"`
RequiredApprovals int64 `xorm:"NOT NULL DEFAULT 0"`
BlockOnRejectedReviews bool `xorm:"NOT NULL DEFAULT false"`
+ BlockOnOutdatedBranch bool `xorm:"NOT NULL DEFAULT false"`
DismissStaleApprovals bool `xorm:"NOT NULL DEFAULT false"`
RequireSignedCommits bool `xorm:"NOT NULL DEFAULT false"`
ProtectedFilePatterns string `xorm:"TEXT"`
@@ -194,6 +195,11 @@ func (protectBranch *ProtectedBranch) MergeBlockedByRejectedReview(pr *PullReque
return rejectExist
}
+// MergeBlockedByOutdatedBranch returns true if merge is blocked by an outdated head branch
+func (protectBranch *ProtectedBranch) MergeBlockedByOutdatedBranch(pr *PullRequest) bool {
+ return protectBranch.BlockOnOutdatedBranch && pr.CommitsBehind > 0
+}
+
// GetProtectedFilePatterns parses a semicolon separated list of protected file patterns and returns a glob.Glob slice
func (protectBranch *ProtectedBranch) GetProtectedFilePatterns() []glob.Glob {
extarr := make([]glob.Glob, 0, 10)
diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go
index e1d46236a9..fe72d0f630 100644
--- a/models/migrations/migrations.go
+++ b/models/migrations/migrations.go
@@ -202,10 +202,12 @@ var migrations = []Migration{
NewMigration("Add EmailHash Table", addEmailHashTable),
// v134 -> v135
NewMigration("Refix merge base for merged pull requests", refixMergeBase),
- // v135 -> 136
+ // v135 -> v136
NewMigration("Add OrgID column to Labels table", addOrgIDLabelColumn),
- // v136 -> 137
+ // v136 -> v137
NewMigration("Add CommitsAhead and CommitsBehind Column to PullRequest Table", addCommitDivergenceToPulls),
+ // v137 -> v138
+ NewMigration("Add Branch Protection Block Outdated Branch", addBlockOnOutdatedBranch),
}
// GetCurrentDBVersion returns the current db version
diff --git a/models/migrations/v137.go b/models/migrations/v137.go
new file mode 100644
index 0000000000..f175cf8a80
--- /dev/null
+++ b/models/migrations/v137.go
@@ -0,0 +1,16 @@
+// 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 (
+ "xorm.io/xorm"
+)
+
+func addBlockOnOutdatedBranch(x *xorm.Engine) error {
+ type ProtectedBranch struct {
+ BlockOnOutdatedBranch bool `xorm:"NOT NULL DEFAULT false"`
+ }
+ return x.Sync2(new(ProtectedBranch))
+}