-
-
Save lirlia/7173938f84c1a77ca930a76ad3a91043 to your computer and use it in GitHub Desktop.
Add branch protection to a repo using GitHub's Graphql API
This file contains 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
fragment branchProtection on BranchProtectionRule { | |
allowsDeletions | |
allowsForcePushes | |
creator { | |
login | |
} | |
id | |
isAdminEnforced | |
requiredStatusCheckContexts | |
requiredApprovingReviewCount | |
requiresApprovingReviews | |
requiresCodeOwnerReviews | |
requiresStatusChecks | |
restrictsPushes | |
restrictsReviewDismissals | |
dismissesStaleReviews | |
pattern | |
} | |
fragment repositoryPaging on RepositoryConnection { | |
pageInfo { | |
hasNextPage | |
endCursor | |
} | |
totalCount | |
} | |
query listAllReposInOrg($orgLogin: String!, $endCursor: String) { | |
organization(login: $orgLogin) { | |
repositories(first: 100, after: $endCursor) { | |
nodes { | |
name | |
} | |
...repositoryPaging | |
} | |
} | |
} | |
query allOrgRepoDirectCollaborators($orgLogin: String!, $endCursor: String) { | |
organization(login: $orgLogin) { | |
repositories(first: 100, after: $endCursor) { | |
nodes { | |
name | |
isArchived | |
collaborators(affiliation: DIRECT) { | |
edges { | |
node { | |
login | |
} | |
permission | |
permissionSources { | |
permission | |
source { | |
__typename | |
... on Organization { | |
login | |
} | |
... on Repository { | |
name | |
} | |
... on Team { | |
slug | |
} | |
} | |
} | |
} | |
} | |
} | |
...repositoryPaging | |
} | |
} | |
} | |
query showBranchProtection($owner:String!, $repo:String!) { | |
repository(name: $repo, owner: $owner) { | |
id | |
name | |
branchProtectionRules(first: 10) { | |
totalCount | |
nodes { | |
...branchProtection | |
} | |
} | |
} | |
} | |
mutation addBranchProtection($repositoryId:ID!, $branchPattern:String!, $requiredStatusChecks:[String!]) { | |
createBranchProtectionRule(input: { | |
allowsDeletions: false | |
allowsForcePushes:false | |
dismissesStaleReviews:true | |
isAdminEnforced:true | |
pattern: $branchPattern | |
repositoryId: $repositoryId | |
requiresApprovingReviews:true | |
requiredApprovingReviewCount:1 | |
requiresCodeOwnerReviews:true | |
requiredStatusCheckContexts:$requiredStatusChecks | |
requiresStatusChecks:true | |
restrictsReviewDismissals:false | |
}) { | |
branchProtectionRule { | |
...branchProtection | |
} | |
} | |
} | |
mutation deleteBranchProtection($ruleId:ID!) { | |
deleteBranchProtectionRule(input:{branchProtectionRuleId:$ruleId}) { | |
clientMutationId | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
gh api graphql \ -f query="$(curl -s https://gist.githubusercontent.com/lirlia/7173938f84c1a77ca930a76ad3a91043/raw/23c22a0f3922b812581684e2fbd7c6d5bb8f9532/operations.graphql)" \ -f operationName=showBranchProtection \ -F owner=:owner -F repo=:repo