Skip to content

Instantly share code, notes, and snippets.

@JamesWP
Last active November 21, 2016 03:58
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save JamesWP/bc3622dd3d94bf6cbeb5c83b7f183b55 to your computer and use it in GitHub Desktop.
Save JamesWP/bc3622dd3d94bf6cbeb5c83b7f183b55 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
// 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')
.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).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
}
@simonewebdesign
Copy link

Thanks for this! I've improved your snippet to be able to work also on private repos: https://gist.github.com/simonewebdesign/a70f6c89ffd71e6ba4f7dcf7cc74ccf8

@FarhadG
Copy link

FarhadG commented Nov 9, 2016

Thanks for the issue (FarhadG/init#6), James. I'll incorporate this and update the Init bookmarklet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment