-
Notifications
You must be signed in to change notification settings - Fork 7
/
github-branch-protection.bash
46 lines (41 loc) · 1.79 KB
/
github-branch-protection.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function get_current_branch_protection_setting() {
#
# gets branch protections setting, reformats put api body json
#
# environment variables
#
# REPO : github repository name for Owner/RepoName syntax
# BRANCH:
gh api --method GET repos/${REPO}/branches/${BRANCH}/protection | jq '
{
required_status_checks: null,
restrictions: {
users: .restrictions.users | [.[].login],
teams: .restrictions.teams | [.[].slug],
apps: .restrictions.apps | [.[].slug]
},
enforce_admins: .enforce_admins.enabled ,
required_pull_request_reviews: {
dismiss_stale_reviews: .required_pull_request_reviews.dismiss_stale_reviews,
require_code_owner_reviews: .required_pull_request_reviews.require_code_owner_reviews,
required_approving_review_count: .required_pull_request_reviews.required_approving_review_count
},
required_linear_history: .required_linear_history.enabled,
required_signatures: .required_signatures.enabled,
allow_force_pushes: .allow_force_pushes.enabled,
allow_deletions: .allow_deletions.enabled,
block_reations: .block_creations.enabled,
required_conversation_resolution: .required_conversation_resolution.enabled
}'
}
function apply_branch_protection_setting() {
gh api --method PUT -H "Accept: application/vnd.github+json" --input - repos/${REPO}/branches/${BRANCH}/protection
}
function enforce_admins_off() {
get_current_branch_protection_setting | jq '.enforce_admins = false' | apply_branch_protection_setting
}
export -f enforce_admins_off
function enforce_admins_on() {
get_current_branch_protection_setting | jq '.enforce_admins = true' | apply_branch_protection_setting
}
export -f enforce_admins_on