Skip to content

Instantly share code, notes, and snippets.

@sammcj
Forked from duboisf/operations.graphql
Created November 15, 2022 23:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sammcj/d7fed42315ddbcf4d8880031e42fb366 to your computer and use it in GitHub Desktop.
Save sammcj/d7fed42315ddbcf4d8880031e42fb366 to your computer and use it in GitHub Desktop.
Add branch protection to a repo using GitHub's Graphql API
fragment branchProtection on BranchProtectionRule {
allowsDeletions
allowsForcePushes
creator {
login
}
id
isAdminEnforced
requiredStatusCheckContexts
requiredApprovingReviewCount
requiresApprovingReviews
requiresCodeOwnerReviews
requiresConversationResolution
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:false
pattern: $branchPattern
repositoryId: $repositoryId
requiresApprovingReviews:true
requiredApprovingReviewCount:1
requiresCodeOwnerReviews:true
requiresConversationResolution: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