Skip to content

Instantly share code, notes, and snippets.

@dnicolson
Created December 18, 2018 21:33
Show Gist options
  • Save dnicolson/eb8248ec98a04722dfff7039dbcccf5e to your computer and use it in GitHub Desktop.
Save dnicolson/eb8248ec98a04722dfff7039dbcccf5e to your computer and use it in GitHub Desktop.
This reads the Link HTTP header and counts the pages of commits (the contributors and search API's do not give accurate commit counts).
const fetch = require('node-fetch');
const parse = require('parse-link-header');
async function githubCommits(owner, repository) {
const response = await fetch(`https://api.github.com/repos/${owner}/${repository}/commits`);
let link;
if (response.headers) {
for (let pair of response.headers.entries()) {
if (pair[0] === 'link') {
link = parse(pair[1]);
}
}
}
if (!link) {
const commits = await response.json();
return commits.length;
}
let commits = (link.last.page - 1) * 30;
const lastPageResponse = await fetch(link.last.url);
const lastPageCommits = await lastPageResponse.json();
return commits + lastPageCommits.length;
}
console.log(githubCommits('rails', 'rails').then(commits => console.log(commits)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment