You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: .github/actions/github/branch-protection/lock/action.yml
+39-19Lines changed: 39 additions & 19 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
name: 'Lock branch'
2
2
author: 'Pete Sramek'
3
-
description: 'Apply branch protection to prevent direct pushes. Requires PRs with at least one approval.'
3
+
description: 'Apply branch protection to prevent direct pushes. Requires PRs with configurable approval count; admins can optionally bypass all restrictions.'
4
4
inputs:
5
5
branch:
6
6
description: 'Branch name to lock.'
@@ -9,7 +9,19 @@ inputs:
9
9
description: 'GitHub token with administration:write (repo admin) permission. Use a PAT; GITHUB_TOKEN cannot call the branch protection API.'
10
10
required: true
11
11
lock-branch:
12
-
description: 'When true, sets lock_branch to prevent even PR merges (use during automated operations). When false (default), only direct pushes are blocked; PRs with required reviews can still be merged.'
12
+
description: 'When true, sets lock_branch to prevent even PR merges (use during automated operations). When false (default), only direct pushes are blocked; PRs can still be merged.'
13
+
required: false
14
+
default: 'false'
15
+
required-approving-review-count:
16
+
description: 'Number of approving reviews required before a PR can be merged. Set to 0 to require PRs without requiring approvals.'
17
+
required: false
18
+
default: '1'
19
+
dismiss-stale-reviews:
20
+
description: 'When true, approved reviews are dismissed when new commits are pushed to the branch.'
21
+
required: false
22
+
default: 'true'
23
+
bypass-admins:
24
+
description: 'When true, repository admins are exempt from all branch protection rules (enforce_admins is disabled). When false (default), admins are also subject to the rules.'
13
25
required: false
14
26
default: 'false'
15
27
@@ -21,23 +33,31 @@ runs:
21
33
env:
22
34
GH_TOKEN: ${{ inputs.token }}
23
35
run: |
24
-
if ! gh api --method PUT /repos/${{ github.repository }}/branches/${{ inputs.branch }}/protection \
25
-
--input - << 'EOF'
26
-
{
27
-
"required_status_checks": null,
28
-
"enforce_admins": false,
29
-
"required_pull_request_reviews": {
30
-
"dismiss_stale_reviews": true,
31
-
"require_code_owner_reviews": false,
32
-
"required_approving_review_count": 1
33
-
},
34
-
"restrictions": null,
35
-
"allow_force_pushes": false,
36
-
"allow_deletions": false,
37
-
"lock_branch": ${{ inputs.lock-branch }}
38
-
}
39
-
EOF
40
-
then
36
+
ENFORCE_ADMINS=true
37
+
if [ '${{ inputs.bypass-admins }}' = 'true' ]; then
if ! echo "$PAYLOAD" | gh api --method PUT /repos/${{ github.repository }}/branches/${{ inputs.branch }}/protection --input -; then
41
61
echo "::error::Failed to apply branch protection to '${{ inputs.branch }}'. Ensure the token has 'administration: write' permission and the branch exists."
0 commit comments