Skip to content

Commit d65ee2f

Browse files
committed
Block PR on Outdated Branch
1 parent 10e2f29 commit d65ee2f

File tree

15 files changed

+88
-6
lines changed

15 files changed

+88
-6
lines changed

models/branches.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type ProtectedBranch struct {
4747
ApprovalsWhitelistTeamIDs []int64 `xorm:"JSON TEXT"`
4848
RequiredApprovals int64 `xorm:"NOT NULL DEFAULT 0"`
4949
BlockOnRejectedReviews bool `xorm:"NOT NULL DEFAULT false"`
50+
BlockOnOutdatedBranch bool `xorm:"NOT NULL DEFAULT false"`
5051
DismissStaleApprovals bool `xorm:"NOT NULL DEFAULT false"`
5152
RequireSignedCommits bool `xorm:"NOT NULL DEFAULT false"`
5253
ProtectedFilePatterns string `xorm:"TEXT"`
@@ -194,6 +195,17 @@ func (protectBranch *ProtectedBranch) MergeBlockedByRejectedReview(pr *PullReque
194195
return rejectExist
195196
}
196197

198+
// MergeBlockedByOutdatedBranch returns true if merge is blocked by an outdated head branch
199+
func (protectBranch *ProtectedBranch) MergeBlockedByOutdatedBranch(pr *PullRequest) bool {
200+
if !protectBranch.BlockOnOutdatedBranch {
201+
return false
202+
}
203+
if pr.CommitsBehind == 0 {
204+
return false
205+
}
206+
return true
207+
}
208+
197209
// GetProtectedFilePatterns parses a semicolon separated list of protected file patterns and returns a glob.Glob slice
198210
func (protectBranch *ProtectedBranch) GetProtectedFilePatterns() []glob.Glob {
199211
extarr := make([]glob.Glob, 0, 10)

models/migrations/migrations.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,12 @@ var migrations = []Migration{
202202
NewMigration("Add EmailHash Table", addEmailHashTable),
203203
// v134 -> v135
204204
NewMigration("Refix merge base for merged pull requests", refixMergeBase),
205-
// v135 -> 136
205+
// v135 -> v136
206206
NewMigration("Add OrgID column to Labels table", addOrgIDLabelColumn),
207-
// v136 -> 137
207+
// v136 -> v137
208208
NewMigration("Add CommitsAhead and CommitsBehind Column to PullRequest Table", addCommitDivergenceToPulls),
209+
// v137 -> v138
210+
NewMigration("Add Branch Protection Block Outdated Branch", addBlockOnOutdatedBranch),
209211
}
210212

211213
// GetCurrentDBVersion returns the current db version

models/migrations/v999.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2020 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package migrations
6+
7+
import (
8+
"xorm.io/xorm"
9+
)
10+
11+
func addBlockOnOutdatedBranch(x *xorm.Engine) error {
12+
type ProtectedBranch struct {
13+
BlockOnOutdatedBranch bool `xorm:"NOT NULL DEFAULT false"`
14+
}
15+
return x.Sync2(new(ProtectedBranch))
16+
}

modules/auth/repo_form.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ type ProtectBranchForm struct {
173173
ApprovalsWhitelistUsers string
174174
ApprovalsWhitelistTeams string
175175
BlockOnRejectedReviews bool
176+
BlockOnOutdatedBranch bool
176177
DismissStaleApprovals bool
177178
RequireSignedCommits bool
178179
ProtectedFilePatterns string

modules/convert/convert.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ func ToBranchProtection(bp *models.ProtectedBranch) *api.BranchProtection {
118118
ApprovalsWhitelistUsernames: approvalsWhitelistUsernames,
119119
ApprovalsWhitelistTeams: approvalsWhitelistTeams,
120120
BlockOnRejectedReviews: bp.BlockOnRejectedReviews,
121+
BlockOnOutdatedBranch: bp.BlockOnOutdatedBranch,
121122
DismissStaleApprovals: bp.DismissStaleApprovals,
122123
RequireSignedCommits: bp.RequireSignedCommits,
123124
ProtectedFilePatterns: bp.ProtectedFilePatterns,

modules/structs/repo_branch.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type BranchProtection struct {
3939
ApprovalsWhitelistUsernames []string `json:"approvals_whitelist_username"`
4040
ApprovalsWhitelistTeams []string `json:"approvals_whitelist_teams"`
4141
BlockOnRejectedReviews bool `json:"block_on_rejected_reviews"`
42+
BlockOnOutdatedBranch bool `json:"block_on_outdated_branch"`
4243
DismissStaleApprovals bool `json:"dismiss_stale_approvals"`
4344
RequireSignedCommits bool `json:"require_signed_commits"`
4445
ProtectedFilePatterns string `json:"protected_file_patterns"`
@@ -66,6 +67,7 @@ type CreateBranchProtectionOption struct {
6667
ApprovalsWhitelistUsernames []string `json:"approvals_whitelist_username"`
6768
ApprovalsWhitelistTeams []string `json:"approvals_whitelist_teams"`
6869
BlockOnRejectedReviews bool `json:"block_on_rejected_reviews"`
70+
BlockOnOutdatedBranch bool `json:"block_on_outdated_branch"`
6971
DismissStaleApprovals bool `json:"dismiss_stale_approvals"`
7072
RequireSignedCommits bool `json:"require_signed_commits"`
7173
ProtectedFilePatterns string `json:"protected_file_patterns"`
@@ -88,6 +90,7 @@ type EditBranchProtectionOption struct {
8890
ApprovalsWhitelistUsernames []string `json:"approvals_whitelist_username"`
8991
ApprovalsWhitelistTeams []string `json:"approvals_whitelist_teams"`
9092
BlockOnRejectedReviews *bool `json:"block_on_rejected_reviews"`
93+
BlockOnOutdatedBranch *bool `json:"block_on_outdated_branch"`
9194
DismissStaleApprovals *bool `json:"dismiss_stale_approvals"`
9295
RequireSignedCommits *bool `json:"require_signed_commits"`
9396
ProtectedFilePatterns *string `json:"protected_file_patterns"`

options/locale/locale_en-US.ini

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,8 @@ pulls.is_checking = "Merge conflict checking is in progress. Try again in few mo
11001100
pulls.required_status_check_failed = Some required checks were not successful.
11011101
pulls.required_status_check_administrator = As an administrator, you may still merge this pull request.
11021102
pulls.blocked_by_approvals = "This Pull Request doesn't have enough approvals yet. %d of %d approvals granted."
1103-
pulls.blocked_by_rejection = "This Pull Request has changes requested by an official reviewer."
1103+
pulls.blocked_by_rejection = "This Pull Request has an outdated head branch."
1104+
pulls.blocked_by_outdated_branch = "This Pull Request is blocked by an outdated head branch."
11041105
pulls.can_auto_merge_desc = This pull request can be merged automatically.
11051106
pulls.cannot_auto_merge_desc = This pull request cannot be merged automatically due to conflicts.
11061107
pulls.cannot_auto_merge_helper = Merge manually to resolve the conflicts.
@@ -1525,6 +1526,8 @@ settings.protected_branch_deletion = Disable Branch Protection
15251526
settings.protected_branch_deletion_desc = Disabling branch protection allows users with write permission to push to the branch. Continue?
15261527
settings.block_rejected_reviews = Block merge on rejected reviews
15271528
settings.block_rejected_reviews_desc = Merging will not be possible when changes are requested by official reviewers, even if there are enough approvals.
1529+
settings.block_outdated_branch = Block merge if branch is outdated
1530+
settings.block_outdated_branch_desc = Merging will not be possible when head branch is behind base branch.
15281531
settings.default_branch_desc = Select a default repository branch for pull requests and code commits:
15291532
settings.choose_branch = Choose a branch…
15301533
settings.no_protected_branch = There are no protected branches.

routers/api/v1/repo/branch.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ func CreateBranchProtection(ctx *context.APIContext, form api.CreateBranchProtec
340340
DismissStaleApprovals: form.DismissStaleApprovals,
341341
RequireSignedCommits: form.RequireSignedCommits,
342342
ProtectedFilePatterns: form.ProtectedFilePatterns,
343+
BlockOnOutdatedBranch: form.BlockOnOutdatedBranch,
343344
}
344345

345346
err = models.UpdateProtectBranch(ctx.Repo.Repository, protectBranch, models.WhitelistOptions{
@@ -475,6 +476,10 @@ func EditBranchProtection(ctx *context.APIContext, form api.EditBranchProtection
475476
protectBranch.ProtectedFilePatterns = *form.ProtectedFilePatterns
476477
}
477478

479+
if form.BlockOnOutdatedBranch != nil {
480+
protectBranch.BlockOnOutdatedBranch = *form.BlockOnOutdatedBranch
481+
}
482+
478483
var whitelistUsers []int64
479484
if form.PushWhitelistUsernames != nil {
480485
whitelistUsers, err = models.GetUserIDsByNames(form.PushWhitelistUsernames, false)

routers/private/hook.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ func HookPreReceive(ctx *macaron.Context, opts private.HookOptions) {
263263
}
264264
}
265265

266+
// Detect Protected file pattern
266267
globs := protectBranch.GetProtectedFilePatterns()
267268
if len(globs) > 0 {
268269
err := checkFileProtection(oldCommitID, newCommitID, globs, gitRepo, env)

routers/repo/issue.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,7 @@ func ViewIssue(ctx *context.Context) {
10651065
cnt := pull.ProtectedBranch.GetGrantedApprovalsCount(pull)
10661066
ctx.Data["IsBlockedByApprovals"] = !pull.ProtectedBranch.HasEnoughApprovals(pull)
10671067
ctx.Data["IsBlockedByRejection"] = pull.ProtectedBranch.MergeBlockedByRejectedReview(pull)
1068+
ctx.Data["IsBlockedByOutdatedBranch"] = pull.ProtectedBranch.MergeBlockedByOutdatedBranch(pull)
10681069
ctx.Data["GrantedApprovals"] = cnt
10691070
ctx.Data["RequireSigned"] = pull.ProtectedBranch.RequireSignedCommits
10701071
}

routers/repo/setting_protected_branch.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ func SettingsProtectedBranchPost(ctx *context.Context, f auth.ProtectBranchForm)
248248
protectBranch.DismissStaleApprovals = f.DismissStaleApprovals
249249
protectBranch.RequireSignedCommits = f.RequireSignedCommits
250250
protectBranch.ProtectedFilePatterns = f.ProtectedFilePatterns
251+
protectBranch.BlockOnOutdatedBranch = f.BlockOnOutdatedBranch
251252

252253
err = models.UpdateProtectBranch(ctx.Repo.Repository, protectBranch, models.WhitelistOptions{
253254
UserIDs: whitelistUsers,

services/pull/merge.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,16 +571,22 @@ func CheckPRReadyToMerge(pr *models.PullRequest) (err error) {
571571
}
572572
}
573573

574-
if enoughApprovals := pr.ProtectedBranch.HasEnoughApprovals(pr); !enoughApprovals {
574+
if !pr.ProtectedBranch.HasEnoughApprovals(pr) {
575575
return models.ErrNotAllowedToMerge{
576576
Reason: "Does not have enough approvals",
577577
}
578578
}
579-
if rejected := pr.ProtectedBranch.MergeBlockedByRejectedReview(pr); rejected {
579+
if pr.ProtectedBranch.MergeBlockedByRejectedReview(pr) {
580580
return models.ErrNotAllowedToMerge{
581581
Reason: "There are requested changes",
582582
}
583583
}
584584

585+
if pr.ProtectedBranch.MergeBlockedByOutdatedBranch(pr) {
586+
return models.ErrNotAllowedToMerge{
587+
Reason: fmt.Sprintf("There head branch %d behind base branch", pr.CommitsBehind),
588+
}
589+
}
590+
585591
return nil
586592
}

templates/repo/issue/view_content/pull.tmpl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
{{- else if .IsPullRequestBroken}}red
6767
{{- else if .IsBlockedByApprovals}}red
6868
{{- else if .IsBlockedByRejection}}red
69+
{{- else if .IsBlockedByOutdatedBranch}}red
6970
{{- else if and .EnableStatusCheck (or .RequiredStatusCheckState.IsFailure .RequiredStatusCheckState.IsError)}}red
7071
{{- else if and .EnableStatusCheck (or .RequiredStatusCheckState.IsPending .RequiredStatusCheckState.IsWarning)}}yellow
7172
{{- else if and .RequireSigned (not .WillSign)}}}red
@@ -138,6 +139,11 @@
138139
<i class="icon icon-octicon">{{svg "octicon-x" 16}}</i>
139140
{{$.i18n.Tr "repo.pulls.blocked_by_rejection"}}
140141
</div>
142+
{{else if .IsBlockedByOutdatedBranch}}
143+
<div class="item text red">
144+
<i class="icon icon-octicon">{{svg "octicon-x" 16}}</i>
145+
{{$.i18n.Tr "repo.pulls.blocked_by_outdated_branch"}}
146+
</div>
141147
{{else if and .EnableStatusCheck (or .RequiredStatusCheckState.IsError .RequiredStatusCheckState.IsFailure)}}
142148
<div class="item text red">
143149
<i class="icon icon-octicon">{{svg "octicon-x" 16}}</i>
@@ -153,7 +159,7 @@
153159
{{$.i18n.Tr (printf "repo.signing.wont_sign.%s" .WontSignReason) }}
154160
</div>
155161
{{end}}
156-
{{$notAllOverridableChecksOk := or .IsBlockedByApprovals .IsBlockedByRejection (and .EnableStatusCheck (not .IsRequiredStatusCheckSuccess))}}
162+
{{$notAllOverridableChecksOk := or .IsBlockedByApprovals .IsBlockedByRejection .IsBlockedByOutdatedBranch (and .EnableStatusCheck (not .IsRequiredStatusCheckSuccess))}}
157163
{{if and (or $.IsRepoAdmin (not $notAllOverridableChecksOk)) (or (not .RequireSigned) .WillSign)}}
158164
{{if $notAllOverridableChecksOk}}
159165
<div class="item text yellow">
@@ -337,6 +343,11 @@
337343
{{svg "octicon-x" 16}}
338344
{{$.i18n.Tr "repo.pulls.blocked_by_rejection"}}
339345
</div>
346+
{{else if .IsBlockedByOutdatedBranch}}
347+
<div class="item text red">
348+
<i class="icon icon-octicon">{{svg "octicon-x" 16}}</i>
349+
{{$.i18n.Tr "repo.pulls.blocked_by_outdated_branch"}}
350+
</div>
340351
{{else if and .EnableStatusCheck (not .IsRequiredStatusCheckSuccess)}}
341352
<div class="item text red">
342353
{{svg "octicon-x" 16}}

templates/repo/settings/protected_branch.tmpl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,13 @@
225225
<p class="help">{{.i18n.Tr "repo.settings.require_signed_commits_desc"}}</p>
226226
</div>
227227
</div>
228+
<div class="field">
229+
<div class="ui checkbox">
230+
<input name="block_on_outdated_branch" type="checkbox" {{if .Branch.BlockOnOutdatedBranch}}checked{{end}}>
231+
<label for="block_on_outdated_branch">{{.i18n.Tr "repo.settings.block_outdated_branch"}}</label>
232+
<p class="help">{{.i18n.Tr "repo.settings.block_outdated_branch_desc"}}</p>
233+
</div>
234+
</div>
228235
<div class="field">
229236
<label for="protected_file_patterns">{{.i18n.Tr "repo.settings.protect_protected_file_patterns"}}</label>
230237
<input name="protected_file_patterns" id="protected_file_patterns" type="text" value="{{.Branch.ProtectedFilePatterns}}">

templates/swagger/v1_json.tmpl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10054,6 +10054,10 @@
1005410054
},
1005510055
"x-go-name": "ApprovalsWhitelistUsernames"
1005610056
},
10057+
"block_on_outdated_branch": {
10058+
"type": "boolean",
10059+
"x-go-name": "BlockOnOutdatedBranch"
10060+
},
1005710061
"block_on_rejected_reviews": {
1005810062
"type": "boolean",
1005910063
"x-go-name": "BlockOnRejectedReviews"
@@ -10374,6 +10378,10 @@
1037410378
},
1037510379
"x-go-name": "ApprovalsWhitelistUsernames"
1037610380
},
10381+
"block_on_outdated_branch": {
10382+
"type": "boolean",
10383+
"x-go-name": "BlockOnOutdatedBranch"
10384+
},
1037710385
"block_on_rejected_reviews": {
1037810386
"type": "boolean",
1037910387
"x-go-name": "BlockOnRejectedReviews"
@@ -11186,6 +11194,10 @@
1118611194
},
1118711195
"x-go-name": "ApprovalsWhitelistUsernames"
1118811196
},
11197+
"block_on_outdated_branch": {
11198+
"type": "boolean",
11199+
"x-go-name": "BlockOnOutdatedBranch"
11200+
},
1118911201
"block_on_rejected_reviews": {
1119011202
"type": "boolean",
1119111203
"x-go-name": "BlockOnRejectedReviews"

0 commit comments

Comments
 (0)