Skip to content

Instantly share code, notes, and snippets.

@sheeeng
Forked from duboisf/README.md
Last active April 29, 2024 09:19
Show Gist options
  • Save sheeeng/2174426d0cee3594d41bbf395c36df66 to your computer and use it in GitHub Desktop.
Save sheeeng/2174426d0cee3594d41bbf395c36df66 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
        }
      }
    }
  }
}
'
# https://github.com/cli/cli/discussions/6852#discussioncomment-4709231
gh api graphql \
--field owner="${GITHUB_ORGANIZATION}" \
--field query='
query ($perPage: Int = 30, $endCursor: String, $owner: String!) {
repositoryOwner(login: $owner) {
repositories(first: $perPage, after: $endCursor, ownerAffiliations: OWNER) {
nodes {
nameWithOwner
collaborators {
edges {
permission
node {
login
}
}
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment