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
{{ message }}
Repository navigation
Create and edit branch protection rules for a repository #3528
Describe the feature or problem you’d like to solve
Could the cli tool be extended to update properties on repos for an organisation? Such as setting branch protection on all repos belonging to an organisation.
Proposed solution
Could be an extension to the gh repo action to admin or update and pass in some flags/params to update certain things.
Repository administration is a wide area and it's unlikely that the CLI will have dedicated commands to cover every possible aspect of it. Therefore, I will rename this issue to focus on just editing branch protection rules, since I believe it's a good idea!
In the meantime, you may be able to use gh api to interact with GitHub API for branch protection rules. For example, to create a rule that requires at least 3 approving reviews for a PR:
# get the repo ID of the "cli/cli" repository
repositoryId="$(gh api graphql -f query='{repository(owner:"cli",name:"cli"){id}}' -q .data.repository.id)"# create a branch protection rule (untested!)
gh api graphql -f query='mutation($repositoryId:ID!,$branch:String!,$requiredReviews:Int!) { createBranchProtectionRule(input: { repositoryId: $repositoryId pattern: $branch requiresApprovingReviews: true requiredApprovingReviewCount: $requiredReviews }) { clientMutationId }}' -f repositoryId="$repositoryId" -f branch=main -F requiredReviews=3
Reacted by Marti B, Jianfeng Hu, Björn Kautler, 4TT1L4 and Jonathan Mohrbacher
In OP's post it sounds like there is an interest in org-level operations, which I wanted to point out. We've discussed adding something for managing orgs and this is at least something we can consider incorporating (operations on all of an org's repositories).
As for directly managing a single repo's branch protection rules (which is what @mislav is addressing), I agree that it's a nice idea. I can imagine a nice multi-select prompt for specifying the rules; we should do a spike on what that UI might look like.
Reacted by John SlayMoore, Marcelo Assis and Ian Brown
Repository administration is a wide area and it's unlikely that the CLI will have dedicated commands to cover every possible aspect of it. Therefore, I will rename this issue to focus on just editing branch protection rules, since I believe it's a good idea!
In the meantime, you may be able to use gh api to interact with GitHub API for branch protection rules. For example, to create a rule that requires at least 3 approving reviews for a PR:
# get the repo ID of the "cli/cli" repository
repositoryId="$(gh api graphql -f query='{repository(owner:"cli",name:"cli"){id}}' -q .data.repository.id)"# create a branch protection rule (untested!)
gh api graphql -f query='mutation(repositoryId:ID!,$branch:String!,$requiredReviews:Int!) { createBranchProtectionRule(input: { repositoryId: $repositoryId pattern: $branch requiresApprovingReviews: true requiredApprovingReviewCount: $requiredReviews }) { clientMutationId }}' -f repositoryId="$repositoryId" -f branch=main -F requiredReviews=3
You had a typo, you were missing a $ on repositoryId:ID!
I'm not familiar with graphql so it took me a hot second to figure out
Here is a tested working version of your snippet (I ran it inside a .sh bash file)
Here a little one-liner magic (mainly that I find it in the future again :-D) to set the required checks to all checks that were produced on a specific commit or reference:
Here a little one-liner magic (mainly that I find it in the future again :-D) to set the required checks to all checks that were produced on a specific commit or reference:
This would be amazing. It was a lot of custom code to get this to work in a script, in addition to having to create a github app with administration permissions. I can share the script if anyone is interested.
I think that rulesets are managed by the CLI and can replace features from branch protection. That being said, it would be helpful to have a migration feature between the two.
Activity
mislav commentedon Apr 28, 2021
Thanks for writing in!
Repository administration is a wide area and it's unlikely that the CLI will have dedicated commands to cover every possible aspect of it. Therefore, I will rename this issue to focus on just editing branch protection rules, since I believe it's a good idea!
In the meantime, you may be able to use
gh apito interact with GitHub API for branch protection rules. For example, to create a rule that requires at least 3 approving reviews for a PR:[-]Administering repos?[/-][+]Create and edit branch protection rules for a repository[/+]vilmibm commentedon May 18, 2021
In OP's post it sounds like there is an interest in org-level operations, which I wanted to point out. We've discussed adding something for managing orgs and this is at least something we can consider incorporating (operations on all of an org's repositories).
As for directly managing a single repo's branch protection rules (which is what @mislav is addressing), I agree that it's a nice idea. I can imagine a nice multi-select prompt for specifying the rules; we should do a spike on what that UI might look like.
james-perfectserve commentedon Sep 24, 2021
james-perfectserve commentedon Sep 24, 2021
You had a typo, you were missing a $ on repositoryId:ID!
I'm not familiar with graphql so it took me a hot second to figure out
Here is a tested working version of your snippet (I ran it inside a .sh bash file)
twelvelabs commentedon Oct 26, 2022
I’ve created an extension that allows you to manage repo and branch protection settings declaratively: https://github.com/twelvelabs/gh-repo-config
It basically lets you generate and store the REST API payloads in the .GitHub die of the repo.
@vilmibm The origin of my extension was wanting to do exactly that. I have a workflow that runs regularly to ensure that everything in one of my orgs is configured correctly: https://github.com/gostamp/.github/blob/main/.github/config/configure.sh (still need to rework it to use the extension 😬 ).
Vampire commentedon Nov 4, 2022
Here a little one-liner magic (mainly that I find it in the future again :-D) to set the required checks to all checks that were produced on a specific commit or reference:
Shion1305 commentedon Jun 29, 2023
I really want this feature!!
janeklb commentedon Jul 13, 2023
I adjusted this a little by manually crafting the list of required checks writing them to a file
status-checks.json{ "checks": [ { "context": "lint", "app_id": 100000000 }, { "context": "detect-cyclic-imports", "app_id": 100000000 }, { "context": "prettier", "app_id": 100000000 }, { "context": "tsc", "app_id": 100000000 }, { "context": "test", "app_id": 100000000 }, { "context": "test-prod", "app_id": 100000000 }, { "context": "build", "app_id": 100000000 }, { "context": "UI Review", "app_id": null }, { "context": "UI Tests", "app_id": null } ] }(replace list of checks as well as
app_idvalues as you see fit)and then calling
gh api repos/{owner}/{repo}/branches/master/protection/required_status_checks \ --method PATCH \ --input status-checks.jsonBenLiyanage commentedon Dec 7, 2023
This would be amazing. It was a lot of custom code to get this to work in a script, in addition to having to create a github app with administration permissions. I can share the script if anyone is interested.
thejimbirch commentedon Jan 28, 2024
https://github.com/twelvelabs/gh-repo-config Looks great. Thanks for making it @twelvelabs!
fbuetler commentedon Mar 25, 2024
If you just want to protect a branch, this worked for me:
gh api "repos/<org>/<repo>/branches/<branch>/protection" --method PUT --input branchProtection.jsonbranchProtection.json:If you want to further configure the branch protection rules, have a look at the docs.
Applying the branch protection rules for all repos in an org for the default branch can be accomplished like this (pay attention to the limit):
remyleone commentedon Jul 7, 2025
I think that rulesets are managed by the CLI and can replace features from branch protection. That being said, it would be helpful to have a migration feature between the two.
feat: add gh ruleset create, update, and delete commands (#3)
Upstream sync 2026-05-29 (#16)
Upstream sync 2026-05-29 (#19)
feat: add gh ruleset create, update, and delete commands (#3)
feat: add gh ruleset create, update, and delete commands (#3)
feat: add gh ruleset create, update, and delete commands (#3)
feat: add gh ruleset create, update, and delete commands (#3)