Skip to content

Instantly share code, notes, and snippets.

@duboisf
Last active April 26, 2024 12:50
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save duboisf/d48695e36327959c11efe8543508da0e to your computer and use it in GitHub Desktop.
Save duboisf/d48695e36327959c11efe8543508da0e to your computer and use it in GitHub Desktop.
Example GitHub graphql queries using the gh cli

List all the default branches of repositories in a specific organisation

gh api graphql --paginate \
    --jq '.data.organization.repositories.nodes[] | .defaultBranchRef.name + "\t" + .name' \
    -F org=SomeOrg \
    -f query='
query($org:String!, $endCursor:String) { 
  organization(login:$org) {
    repositories(first: 100, after: $endCursor, isFork:false, orderBy: {field:NAME, direction:ASC}) {
      pageInfo {
        hasNextPage
        endCursor
      }
      nodes {
        name
        defaultBranchRef {
          name
        }
      }
    }
  }
}
'

List all pull requests for a given repo inside an organisation

gh api graphql \
    -F org=SOME_ORG -F repo=SOME_REPO \
    -f query='
query allPullRequests($org: String!, $repo: String!, $endCursor: String) {
  organization(login: $org) {
    repository(name: $repo) {
      pullRequests(first: 100, after: $endCursor, states: MERGED,) {
        nodes {
          author {
            login
          }
          number
          createdAt
          mergedAt
          mergedBy {
            login
          }
          approvers: reviews(states: APPROVED, first: 10) {
            nodes {
              author {
                ... on User {
                  name
                  login
                }
              }
              state
            }
          }
          title
          repository {
            owner {
              login
            }
            name
          }
        }
        pageInfo {
          hasNextPage
          endCursor
        }
      }
    }
  }
}
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment