Skip to content

Instantly share code, notes, and snippets.

@simonewebdesign
Forked from JamesWP/Get the first commit url.js
Last active October 26, 2016 12:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonewebdesign/a70f6c89ffd71e6ba4f7dcf7cc74ccf8 to your computer and use it in GitHub Desktop.
Save simonewebdesign/a70f6c89ffd71e6ba4f7dcf7cc74ccf8 to your computer and use it in GitHub Desktop.
A snippet using GitHub's public API to get the URL and SHA of the first commit for a given repo. Works also for private repos, but you need a token.
// A snippet using GitHub's public API to get
// the URL and SHA of the first commit for a given repo
function openFirstCommit(userorg, repo, authToken) {
var opts = authToken ?
{ headers: new Headers({
'Authorization': 'token ' + authToken
})
} : {}
return fetch('https://api.github.com/repos/'+userorg+'/'+repo+'/commits', opts)
.then(obj=>obj.headers.get('link'))
// the link header has additional urls for paging
.then(link=>link.split(',')[1].split(';')[0].slice(2,-1))
// 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
.then(pageurl=>fetch(pageurl, opts).then(x=>x.json()))
// open the url to the last page
.then(commits=>commits[commits.length-1].html_url)
// navigate to the last commit and extract the userpage url
.then(x=>window.location = x);
// navigate there
}
// Example usage
openFirstCommit('facebook', 'react');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment