Skip to content

Instantly share code, notes, and snippets.

@tiesmaster
Last active September 5, 2023 08:06
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tiesmaster/ffd4f4a8b4dd2c127516c93af6cc7b46 to your computer and use it in GitHub Desktop.
Save tiesmaster/ffd4f4a8b4dd2c127516c93af6cc7b46 to your computer and use it in GitHub Desktop.
Enable/disable branch protection on GitHub through their API
#!/bin/bash
OAUTH2_TOKEN=<fill in your own OAUTH2 token>
OWNER=tiesmaster
REPO=Coolkit.Converters # retrieve this with: basename $(git config --get remote.origin.url) .git
curl https://api.github.com/repos/${OWNER}/${REPO}/branches/master \
-H "Authorization: token $OAUTH2_TOKEN" \
-H "Accept: application/vnd.github.loki-preview+json" \
-X PATCH \
-d '{
"protection": {
"enabled": false
}
}' \
-s | json protection
#!/bin/bash
OAUTH2_TOKEN=<fill in your own OAUTH2 token>
OWNER=tiesmaster
REPO=Coolkit.Converters # retrieve this with: basename $(git config --get remote.origin.url) .git
curl https://api.github.com/repos/${OWNER}/${REPO}/branches/master \
-H "Authorization: token $OAUTH2_TOKEN" \
-H "Accept: application/vnd.github.loki-preview+json" \
-X PATCH \
-d '{
"protection": {
"enabled": true,
"required_status_checks": {
"enforcement_level": "everyone",
"contexts": [
"default"
]
}
}
}' \
-s | json protection
@jajabu33
Copy link

jajabu33 commented Sep 5, 2023

This is not working, the rule continues enabled any idea why?
what is -s | json protection

@tiesmaster
Copy link
Author

@jajabu33 Oh boy, this is a really, really old script. I would be surprised if it still worked 😅 This hits the GitHub API, and that might have changed (and probably has). Checkout the GitHub API docs to see how to update the branch protection rules with the API for details.

-s | json protection: This is composed of the following parts:

  • -s: This is the shorthand for the --silent option. I usually use this on Windows to prevent the "progress" bar from appearing
  • | json protection: This "pipes" the output of the PATCH operation of curl into the json command selecting the protection key in the output of the GitHub API call, which appearently outputs the resulting status of the branch protection.

Good luck!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment