Forked from JamesWP/Get the first commit url.js
Last active
November 21, 2016 04:08
-
-
Save pitaj/e52862409dd5726711214a55189f332d to your computer and use it in GitHub Desktop.
A snippet using the github public api to get the url and sha of the first commit for a given repo
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
// A snippet using the github public api to get | |
// the url and sha of the first commit for a given repo | |
function openFirstCommit(userorg, repo) { | |
return fetch('https://api.github.com/repos/'+userorg+'/'+repo+'/commits') | |
// the link header has additional urls for paging | |
// parse the original JSON for the case where no other pages exist | |
.then(res => Promise.all([res.headers.get('link'), res.json()])) | |
.then(([link, commits]) => { | |
if (link) { | |
// the link contains two urls in the form | |
// <https://github.com/...>; rel=blah, <https://github.com/...>; rel=thelastpage | |
// split the url out of the string | |
const pageurl = link.split(',')[1].split(';')[0].slice(2,-1); | |
// fetch the last page | |
return fetch(pageurl).then(res => res.json()); | |
} | |
// if no link, we know we're on the only page | |
return commits; | |
}) | |
// get the last commit and extract the userpage url | |
.then(commits => commits[commits.length-1].html_url) | |
// navigate there | |
.then(url => window.location.href = url); | |
} | |
function go() { | |
const [, userorg, repo] = window.location.pathname.split('/'); | |
return openFirstCommit(userorg, repo); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment