Skip to content

Instantly share code, notes, and snippets.

@nachoesmite
Last active April 7, 2020 10:37
Show Gist options
  • Save nachoesmite/5c1740531c454f7d170364007393743a to your computer and use it in GitHub Desktop.
Save nachoesmite/5c1740531c454f7d170364007393743a to your computer and use it in GitHub Desktop.
Github Queries for V4

Get branch protection rule for a repository

query { 
  rateLimit {
  limit
  cost
  remaining
  resetAt
}
  organization(login: "YOUR_USERNAME_OR_ORGANIZATION") {
        repository(name:"YOUR_REPO") {
              owner {
                login
              }
              name
              defaultBranchRef {
                name
              }
              branchProtectionRules(first: 100) {
                nodes {
                  pattern
                  requiresApprovingReviews
                  dismissesStaleReviews
                  requiresCodeOwnerReviews
                  requiredStatusCheckContexts
                  requiresStatusChecks
                  restrictsReviewDismissals
                  requiredApprovingReviewCount
                  isAdminEnforced
                  restrictsPushes,
                  pushAllowances(last:100) {
                    nodes {
                      actor {
                        ... on App{
                          appName: name
                          id
                        }
                        ... on User{
                          userName: name
                          id
                        }
                        ... on Team{
                          teamName: name
                          id
                        }
                      }
                    }
                  }
									
                }
              }
            }
      }
}

Get branch protection rules for all repos of an organization

This is a paged query to get all the branch protection rules for all non forked repos for your organization. On each call you need to update after: x where x is the content of pageInfo.endCursor if hasNextPage == true

query { 
  rateLimit {
  limit
  cost
  remaining
  resetAt
}
 organization(login: "YOUR_USERNAME_OR_ORGANIZATION") {
        repositories(first: 100,isFork: false after: null) {
          pageInfo {
            endCursor
            hasNextPage
          }
          nodes {
              owner {
                login
              }
              name
              defaultBranchRef {
                name
              }
              branchProtectionRules(first: 100) {
                nodes {
                  pattern
                  requiresApprovingReviews
                  dismissesStaleReviews
                  requiresCodeOwnerReviews
                  requiredStatusCheckContexts
                  requiresStatusChecks
                  isAdminEnforced
                  restrictsPushes
                }
              }
            }
        }
      }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment