Skip to content

Instantly share code, notes, and snippets.

@wuhuizuo
Created April 9, 2025 09:12
Show Gist options
  • Save wuhuizuo/aaec4ed3433371798bbb3c899758c93c to your computer and use it in GitHub Desktop.
Save wuhuizuo/aaec4ed3433371798bbb3c899758c93c to your computer and use it in GitHub Desktop.
how to create a branch protection rule with github graphql api
mutation ($repositoryId: ID!) {
createBranchProtectionRule(
input: {
repositoryId: $repositoryId
pattern: "feature/*"
requiresApprovingReviews: false
requiredApprovingReviewCount: null
dismissesStaleReviews: false
requiresStatusChecks: true
requiredStatusCheckContexts: ["build", "test"]
isAdminEnforced: true
restrictsNonAdmins: true
allowsForcePushes: false
allowsDeletions: false
requiresCodeOwnerReviews: false
requiresStrictStatusChecks: false
restrictsPushes: true
restrictsReviewDismissals: false
}
) {
branchProtectionRule {
id
pattern
requiresApprovingReviews
requiredApprovingReviewCount
dismissesStaleReviews
requiresStatusChecks
requiredStatusCheckContexts
isAdminEnforced
}
}
}
owner="<owner>"
repo="<repo>"
# 1. get the repo id
REPO_ID=$(gh api graphql -f query='
query($owner: String!, $repo: String!) {
repository(owner: $owner, name: $repo) {
id
}
}
' -F owner=$owner -F repo=$repo --jq '.data.repository.id')
# 2. create the rule
gh api graphql -f [email protected] -F repositoryId="$REPO_ID"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment