Skip to content

Commit 4e07e64

Browse files
authored
adding new parameter BRANCH_PROTECTION_ENABLED (#9)
* support for branch protection enabled * switching to string check * cleanup
1 parent 2d63b91 commit 4e07e64

File tree

4 files changed

+45
-66
lines changed

4 files changed

+45
-66
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ jobs:
4343
REBASE_MERGE: 'true'
4444
AUTO_MERGE: 'false'
4545
DELETE_HEAD: 'false'
46-
BRANCH_PROTECTION_NAME: 'master'
46+
BRANCH_PROTECTION_ENABLED: true
47+
BRANCH_PROTECTION_NAME: 'main'
4748
BRANCH_PROTECTION_REQUIRED_REVIEWERS: '1'
4849
BRANCH_PROTECTION_DISMISS: 'true'
4950
BRANCH_PROTECTION_CODE_OWNERS: 'true'
@@ -63,6 +64,7 @@ jobs:
6364
| REBASE_MERGE | false | true | Whether or not to allow rebase merges on the repo |
6465
| AUTO_MERGE | false | false | Whether or not to allow auto-merge on the repo |
6566
| DELETE_HEAD | false | false | Whether or not to delete head branch after merges |
67+
| BRANCH_PROTECTION_ENABLED | false | false | Whether or not to enable branch protection |
6668
| BRANCH_PROTECTION_NAME | false | 'master' | Branch name pattern for branch protection rule |
6769
| BRANCH_PROTECTION_REQUIRED_REVIEWERS | false | 1 | Number of required reviewers for branch protection rule |
6870
| BRANCH_PROTECTION_DISMISS | false | true | Dismiss stale pull request approvals when new commits are pushed |

action.yml

+5-1
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,14 @@ inputs:
4141
description: 'Whether or not to delete head branch after merges'
4242
required: false
4343
default: 'false'
44+
BRANCH_PROTECTION_ENABLED:
45+
description: 'Whether or not to enable branch protection'
46+
required: false
47+
default: 'false'
4448
BRANCH_PROTECTION_NAME:
4549
description: 'Branch name pattern for branch protection rule'
4650
required: false
47-
default: 'master'
51+
default: 'main'
4852
BRANCH_PROTECTION_REQUIRED_REVIEWERS:
4953
description: 'Number of required reviewers for branch protection rule'
5054
required: false

azure-pipelines.yml

-39
This file was deleted.

entrypoint.sh

+37-25
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ AUTO_MERGE=$INPUT_AUTO_MERGE
3333
echo "Auto-Merge : $AUTO_MERGE"
3434
DELETE_HEAD=$INPUT_DELETE_HEAD
3535
echo "Delete Head : $DELETE_HEAD"
36+
BRANCH_PROTECTION_ENABLED=$INPUT_BRANCH_PROTECTION_ENABLED
37+
echo "Branch Protection : $BRANCH_PROTECTION_ENABLED"
3638
BRANCH_PROTECTION_NAME=$INPUT_BRANCH_PROTECTION_NAME
3739
echo "Branch Protection Name : $BRANCH_PROTECTION_NAME"
3840
BRANCH_PROTECTION_REQUIRED_REVIEWERS=$INPUT_BRANCH_PROTECTION_REQUIRED_REVIEWERS
@@ -114,31 +116,41 @@ for repository in "${REPOSITORIES[@]}"; do
114116

115117
echo " "
116118

117-
echo "Setting [${BRANCH_PROTECTION_NAME}] branch protection rules"
118-
119-
# the argjson instead of just arg lets us pass the values not as strings
120-
jq -n \
121-
--argjson enforceAdmins $BRANCH_PROTECTION_ENFORCE_ADMINS \
122-
--argjson dismissStaleReviews $BRANCH_PROTECTION_DISMISS \
123-
--argjson codeOwnerReviews $BRANCH_PROTECTION_CODE_OWNERS \
124-
--argjson reviewCount $BRANCH_PROTECTION_REQUIRED_REVIEWERS \
125-
'{
126-
required_status_checks:null,
127-
enforce_admins:$enforceAdmins,
128-
required_pull_request_reviews:{
129-
dismiss_stale_reviews:$dismissStaleReviews,
130-
require_code_owner_reviews:$codeOwnerReviews,
131-
required_approving_review_count:$reviewCount
132-
},
133-
restrictions:null
134-
}' \
135-
| curl -d @- \
136-
-X PUT \
137-
-H "Accept: application/vnd.github.luke-cage-preview+json" \
138-
-H "Content-Type: application/json" \
139-
-u ${USERNAME}:${GITHUB_TOKEN} \
140-
--silent \
141-
${GITHUB_API_URL}/repos/${repository}/branches/${BRANCH_PROTECTION_NAME}/protection
119+
if [ "$BRANCH_PROTECTION_ENABLED" == "true" ]; then
120+
echo "Setting [${BRANCH_PROTECTION_NAME}] branch protection rules"
121+
122+
# the argjson instead of just arg lets us pass the values not as strings
123+
jq -n \
124+
--argjson enforceAdmins $BRANCH_PROTECTION_ENFORCE_ADMINS \
125+
--argjson dismissStaleReviews $BRANCH_PROTECTION_DISMISS \
126+
--argjson codeOwnerReviews $BRANCH_PROTECTION_CODE_OWNERS \
127+
--argjson reviewCount $BRANCH_PROTECTION_REQUIRED_REVIEWERS \
128+
'{
129+
required_status_checks:null,
130+
enforce_admins:$enforceAdmins,
131+
required_pull_request_reviews:{
132+
dismiss_stale_reviews:$dismissStaleReviews,
133+
require_code_owner_reviews:$codeOwnerReviews,
134+
required_approving_review_count:$reviewCount
135+
},
136+
restrictions:null
137+
}' \
138+
| curl -d @- \
139+
-X PUT \
140+
-H "Accept: application/vnd.github.luke-cage-preview+json" \
141+
-H "Content-Type: application/json" \
142+
-u ${USERNAME}:${GITHUB_TOKEN} \
143+
--silent \
144+
${GITHUB_API_URL}/repos/${repository}/branches/${BRANCH_PROTECTION_NAME}/protection
145+
else
146+
curl \
147+
-X DELETE \
148+
-H "Accept: application/vnd.github.luke-cage-preview+json" \
149+
-H "Content-Type: application/json" \
150+
-u ${USERNAME}:${GITHUB_TOKEN} \
151+
--silent \
152+
${GITHUB_API_URL}/repos/${repository}/branches/${BRANCH_PROTECTION_NAME}/protection
153+
fi
142154

143155
echo "Completed [${repository}]"
144156
echo "::endgroup::"

0 commit comments

Comments
 (0)