Created
December 18, 2018 21:33
-
-
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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