Last active
September 5, 2023 08:06
-
-
Save tiesmaster/ffd4f4a8b4dd2c127516c93af6cc7b46 to your computer and use it in GitHub Desktop.
Enable/disable branch protection on GitHub through their API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 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 thePATCH
operation of curl into thejson
command selecting theprotection
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
This is not working, the rule continues enabled any idea why?
what is
-s | json protection