From c52d48aae46af879fdfcfd94d03b7072878b5441 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Fri, 17 Apr 2020 03:00:36 +0200 Subject: 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 --- models/branches.go | 6 ++++++ models/migrations/migrations.go | 6 ++++-- models/migrations/v137.go | 16 ++++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 models/migrations/v137.go (limited to 'models') 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)) +} -- cgit v1.2.3