Created
April 9, 2025 09:12
-
-
Save wuhuizuo/aaec4ed3433371798bbb3c899758c93c to your computer and use it in GitHub Desktop.
how to create a branch protection rule with github graphql 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
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 | |
} | |
} | |
} |
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
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