-
-
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.
This file contains 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 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