Skip to content

Commit 8a9f762

Browse files
committed
Add block on pending codeowner reviews branch protection
1 parent c55a017 commit 8a9f762

File tree

16 files changed

+191
-1
lines changed

16 files changed

+191
-1
lines changed

models/git/protected_branch.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ type ProtectedBranch struct {
5757
RequiredApprovals int64 `xorm:"NOT NULL DEFAULT 0"`
5858
BlockOnRejectedReviews bool `xorm:"NOT NULL DEFAULT false"`
5959
BlockOnOfficialReviewRequests bool `xorm:"NOT NULL DEFAULT false"`
60+
BlockOnCodeownerReviews bool `xorm:"NOT NULL DEFAULT false"`
6061
BlockOnOutdatedBranch bool `xorm:"NOT NULL DEFAULT false"`
6162
DismissStaleApprovals bool `xorm:"NOT NULL DEFAULT false"`
6263
IgnoreStaleApprovals bool `xorm:"NOT NULL DEFAULT false"`

models/migrations/migrations.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"code.gitea.io/gitea/models/migrations/v1_23"
2626
"code.gitea.io/gitea/models/migrations/v1_24"
2727
"code.gitea.io/gitea/models/migrations/v1_25"
28+
"code.gitea.io/gitea/models/migrations/v1_26"
2829
"code.gitea.io/gitea/models/migrations/v1_6"
2930
"code.gitea.io/gitea/models/migrations/v1_7"
3031
"code.gitea.io/gitea/models/migrations/v1_8"
@@ -395,6 +396,9 @@ func prepareMigrationTasks() []*migration {
395396
newMigration(321, "Use LONGTEXT for some columns and fix review_state.updated_files column", v1_25.UseLongTextInSomeColumnsAndFixBugs),
396397
newMigration(322, "Extend comment tree_path length limit", v1_25.ExtendCommentTreePathLength),
397398
newMigration(323, "Add support for actions concurrency", v1_25.AddActionsConcurrency),
399+
400+
// Gitea 1.25.0 ends at database version 323
401+
newMigration(324, "Add block on codeowner reviews branch protection", v1_26.AddBlockOnCodeownerReviews),
398402
}
399403
return preparedMigrations
400404
}

models/migrations/v1_26/v324.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package v1_26
5+
6+
import (
7+
"xorm.io/xorm"
8+
)
9+
10+
func AddBlockOnCodeownerReviews(x *xorm.Engine) error {
11+
type ProtectedBranch struct {
12+
BlockOnCodeownerReviews bool `xorm:"NOT NULL DEFAULT false"`
13+
}
14+
15+
return x.Sync(new(ProtectedBranch))
16+
}

modules/structs/repo_branch.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ type BranchProtection struct {
5858
ApprovalsWhitelistTeams []string `json:"approvals_whitelist_teams"`
5959
BlockOnRejectedReviews bool `json:"block_on_rejected_reviews"`
6060
BlockOnOfficialReviewRequests bool `json:"block_on_official_review_requests"`
61+
BlockOnCodeownerReviews bool `json:"block_on_codeowner_reviews"`
6162
BlockOnOutdatedBranch bool `json:"block_on_outdated_branch"`
6263
DismissStaleApprovals bool `json:"dismiss_stale_approvals"`
6364
IgnoreStaleApprovals bool `json:"ignore_stale_approvals"`
@@ -98,6 +99,7 @@ type CreateBranchProtectionOption struct {
9899
ApprovalsWhitelistTeams []string `json:"approvals_whitelist_teams"`
99100
BlockOnRejectedReviews bool `json:"block_on_rejected_reviews"`
100101
BlockOnOfficialReviewRequests bool `json:"block_on_official_review_requests"`
102+
BlockOnCodeownerReviews bool `json:"block_on_codeowner_reviews"`
101103
BlockOnOutdatedBranch bool `json:"block_on_outdated_branch"`
102104
DismissStaleApprovals bool `json:"dismiss_stale_approvals"`
103105
IgnoreStaleApprovals bool `json:"ignore_stale_approvals"`
@@ -131,6 +133,7 @@ type EditBranchProtectionOption struct {
131133
ApprovalsWhitelistTeams []string `json:"approvals_whitelist_teams"`
132134
BlockOnRejectedReviews *bool `json:"block_on_rejected_reviews"`
133135
BlockOnOfficialReviewRequests *bool `json:"block_on_official_review_requests"`
136+
BlockOnCodeownerReviews *bool `json:"block_on_codeowner_reviews"`
134137
BlockOnOutdatedBranch *bool `json:"block_on_outdated_branch"`
135138
DismissStaleApprovals *bool `json:"dismiss_stale_approvals"`
136139
IgnoreStaleApprovals *bool `json:"ignore_stale_approvals"`

options/locale/locale_en-US.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1916,6 +1916,7 @@ pulls.required_status_check_administrator = As an administrator, you may still m
19161916
pulls.blocked_by_approvals = "This pull request doesn't have enough required approvals yet. %d of %d official approvals granted."
19171917
pulls.blocked_by_approvals_whitelisted = "This pull request doesn't have enough required approvals yet. %d of %d approvals granted from users or teams on the allowlist."
19181918
pulls.blocked_by_rejection = "This pull request has changes requested by an official reviewer."
1919+
pulls.blocked_by_codeowners = "This pull request is missing approval from one or more code owners."
19191920
pulls.blocked_by_official_review_requests = "This pull request has official review requests."
19201921
pulls.blocked_by_outdated_branch = "This pull request is blocked because it's outdated."
19211922
pulls.blocked_by_changed_protected_files_1= "This pull request is blocked because it changes a protected file:"
@@ -2553,6 +2554,8 @@ settings.block_rejected_reviews = Block merge on rejected reviews
25532554
settings.block_rejected_reviews_desc = Merging will not be possible when changes are requested by official reviewers, even if there are enough approvals.
25542555
settings.block_on_official_review_requests = Block merge on official review requests
25552556
settings.block_on_official_review_requests_desc = Merging will not be possible when it has official review requests, even if there are enough approvals.
2557+
settings.block_on_codeowner_reviews = Require approval from code owners
2558+
settings.block_on_codeowner_reviews_desc = Merging will only be possible if at least one code owner per code owner rule has given an approving review.
25562559
settings.block_outdated_branch = Block merge if pull request is outdated
25572560
settings.block_outdated_branch_desc = Merging will not be possible when head branch is behind base branch.
25582561
settings.block_admin_merge_override = Administrators must follow branch protection rules

result

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/nix/store/fggjagh3w5ba30jc86d0c68jrxm2pzmb-gitea-1.25.0

routers/api/v1/repo/branch.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,10 @@ func EditBranchProtection(ctx *context.APIContext) {
855855
protectBranch.BlockOnOfficialReviewRequests = *form.BlockOnOfficialReviewRequests
856856
}
857857

858+
if form.BlockOnCodeownerReviews != nil {
859+
protectBranch.BlockOnCodeownerReviews = *form.BlockOnCodeownerReviews
860+
}
861+
858862
if form.DismissStaleApprovals != nil {
859863
protectBranch.DismissStaleApprovals = *form.DismissStaleApprovals
860864
}

routers/web/repo/issue_view.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,7 @@ func preparePullViewReviewAndMerge(ctx *context.Context, issue *issues_model.Iss
948948
ctx.Data["ProtectedBranch"] = pb
949949
ctx.Data["IsBlockedByApprovals"] = !issues_model.HasEnoughApprovals(ctx, pb, pull)
950950
ctx.Data["IsBlockedByRejection"] = issues_model.MergeBlockedByRejectedReview(ctx, pb, pull)
951+
ctx.Data["IsBlockedByCodeowners"] = !issue_service.HasAllRequiredCodeownerReviews(ctx, pb, pull)
951952
ctx.Data["IsBlockedByOfficialReviewRequests"] = issues_model.MergeBlockedByOfficialReviewRequests(ctx, pb, pull)
952953
ctx.Data["IsBlockedByOutdatedBranch"] = issues_model.MergeBlockedByOutdatedBranch(pb, pull)
953954
ctx.Data["GrantedApprovals"] = issues_model.GetGrantedApprovalsCount(ctx, pb, pull)

routers/web/repo/setting/protected_branch.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ func SettingsProtectedBranchPost(ctx *context.Context) {
254254
}
255255
protectBranch.BlockOnRejectedReviews = f.BlockOnRejectedReviews
256256
protectBranch.BlockOnOfficialReviewRequests = f.BlockOnOfficialReviewRequests
257+
protectBranch.BlockOnCodeownerReviews = f.BlockOnCodeownerReviews
257258
protectBranch.DismissStaleApprovals = f.DismissStaleApprovals
258259
protectBranch.IgnoreStaleApprovals = f.IgnoreStaleApprovals
259260
protectBranch.RequireSignedCommits = f.RequireSignedCommits

services/convert/convert.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ func ToBranchProtection(ctx context.Context, bp *git_model.ProtectedBranch, repo
189189
ApprovalsWhitelistTeams: approvalsWhitelistTeams,
190190
BlockOnRejectedReviews: bp.BlockOnRejectedReviews,
191191
BlockOnOfficialReviewRequests: bp.BlockOnOfficialReviewRequests,
192+
BlockOnCodeownerReviews: bp.BlockOnCodeownerReviews,
192193
BlockOnOutdatedBranch: bp.BlockOnOutdatedBranch,
193194
DismissStaleApprovals: bp.DismissStaleApprovals,
194195
IgnoreStaleApprovals: bp.IgnoreStaleApprovals,

0 commit comments

Comments
 (0)