Skip to content

Instantly share code, notes, and snippets.

@MichaelCurrin
Last active January 30, 2024 18:04
Show Gist options
  • Save MichaelCurrin/72dea64bad7e285323865788aa5871f6 to your computer and use it in GitHub Desktop.
Save MichaelCurrin/72dea64bad7e285323865788aa5871f6 to your computer and use it in GitHub Desktop.
GitHub GraphQL - Get owned repos

Public repos owned by the current user

Get repos from the GitHub GraphQL API which are owned by the authenticated user (owner of the token).

Customize the query

Instead of OWNER, you can also change the ownerAffiliations filter in the query to be COLLABORATOR or ORGANIZATION_MEMBER. Or omit the filter.

Instead of setting privacy filter to PUBLIC, you can set to PRIVATE. Or omit the filter.

The API allows getting up to 100 repos on a page, as set here. For faster page loads when setting, a small number is useful.

To lookup repos for a target user by GH login, see this gist.

Getting more than 100 repos

To get more than 100 repos in the results, you normally need paging, but there is a workaround here to get 100 original repos, 100 forks and 100 archived repos, effectively giving more than 100 repos.

This works great in the GH GraphQL explorer. If you run the request part of a script, the query might timeout every time because of the amount of data requested.

Then do each section in a separate script and query, or request fewer fields for each repo, or use paging to do more requests with fewer repos on each page (which gets complicated to page on three lists at the same time).

Resources

# Public repos for the current user
fragment Repos on RepositoryConnection {
nodes {
# Metadata
name
url
description
# Dates
createdAt
updatedAt
pushedAt
# Counts
stargazers {
totalCount
}
forkCount
defaultBranchRef {
commits: target {
... on Commit {
history(first: 1) {
totalCount
}
}
}
}
repositoryTopics(first: 10) {
nodes {
topic {
name
stargazers {
totalCount
}
viewerHasStarred
}
url
}
}
}
}
query ownedRepos {
viewer {
original: repositories(
first: 100
ownerAffiliations: OWNER
privacy: PUBLIC
isFork: false
isLocked: false
orderBy: { field: UPDATED_AT, direction: DESC }
) {
...Repos
}
forked: repositories(
first: 100
ownerAffiliations: OWNER
privacy: PUBLIC
isFork: true
isLocked: false
orderBy: { field: UPDATED_AT, direction: DESC }
) {
...Repos
}
archived: repositories(
first: 100
ownerAffiliations: OWNER
privacy: PUBLIC
isLocked: true
orderBy: { field: UPDATED_AT, direction: DESC }
) {
...Repos
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment