Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save TomerFi/1c78e1a5d5a1a70b549b7a2d24086c23 to your computer and use it in GitHub Desktop.
Save TomerFi/1c78e1a5d5a1a70b549b7a2d24086c23 to your computer and use it in GitHub Desktop.
Retrieving forks (including branches and commits) per repository using Node JS and GitHub's GraphQL API
const { graphql } = require('@octokit/graphql');
// UPDATE the OWNER and the REPOSITORY constants
const OWNER = "Tomerfi";
const REPOSITORY = "aioswitcher";
// max 100 for all, for more - use pagination leveraging edges, pageInfo, and cursor.
const TOTAL_FORKS_TO_RETRIEVE = 100;
const TOTAL_BRANCHES_PER_FORK = 100;
const TOTAL_COMMITS_PER_BRANCH = 10; // be careful with this, you might exceed the maximum limit of 500,000 possible nodes.
async function run() {
// token requirements: https://docs.github.com/en/graphql/guides/forming-calls-with-graphql#authenticating-with-graphql
const requestParams = {
headers: {
authorization: `bearer ${process.env.GITHUB_TOKEN}`,
}
};
const query = `
{
repository (owner:"${OWNER}", name:"${REPOSITORY}") {
forkCount
forkingAllowed
forks (first:${TOTAL_FORKS_TO_RETRIEVE}) {
totalCount
nodes {
owner {
login
}
pushedAt
refs (first:${TOTAL_BRANCHES_PER_FORK}, refPrefix:"refs/heads/") {
totalCount
nodes {
name
target {
... on Commit {
history (first:${TOTAL_COMMITS_PER_BRANCH}) {
totalCount
nodes {
abbreviatedOid
}
}
}
}
}
}
}
}
}
}
`;
const result = await graphql(query, requestParams);
console.log(JSON.stringify(result, null, 2));
}
run();
/*
#############################
## Content of package.json ##
#############################
{
"scripts": {
"start": "node get-forks-branches-commits-per-repository.js"
},
"dependencies": {
"@octokit/graphql": "^5.0.1"
}
}
###############
## Execution ##
###############
set GITHUB_TOKEN environment variable
update the above OWNER and the REPOSITORY constants
run `npm i` to install the package
run `npm start` to run the script
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment