GitHub First Commit

Add a link to a GitHub repo's first commit

As of 2018-03-25. See the latest version.

  1. // ==UserScript==
  2. // @name GitHub First Commit
  3. // @description Add a link to a GitHub repo's first commit
  4. // @author chocolateboy
  5. // @copyright chocolateboy
  6. // @namespace https://github.com/chocolateboy/userscripts
  7. // @version 2.2.2
  8. // @license GPL: http://www.gnu.org/copyleft/gpl.html
  9. // @include https://github.com/*/*
  10. // @require https://code.jquery.com/jquery-3.3.1.min.js
  11. // @require https://cdn.rawgit.com/eclecto/jQuery-onMutate/79bbb2b8caccabfc9b9ade046fe63f15f593fef6/src/jquery.onmutate.min.js
  12. // @grant GM_log
  13. // ==/UserScript==
  14.  
  15. // XXX note: the unused grant is a workaround for a Greasemonkey bug:
  16. // https://github.com/greasemonkey/greasemonkey/issues/1614
  17.  
  18. const COMMIT_BAR = 'div.commit-tease.js-details-container > span.float-right'
  19.  
  20. const FIRST_COMMIT =
  21. `<span id="first-commit">
  22. |&nbsp;
  23. <a id="first-commit-link" style="cursor: pointer" class="message">First commit</a>
  24. </span>`
  25.  
  26. // this function extracts the URL of the repo's first commit and navigates to it.
  27. // it is based on code by several developers, a list of whom can be found here:
  28. // https://github.com/FarhadG/init#contributors
  29. //
  30. // XXX it doesn't work on private repos. a way to do that can be found here,
  31. // but it requires an authentication token:
  32. // https://gist.github.com/simonewebdesign/a70f6c89ffd71e6ba4f7dcf7cc74ccf8
  33. function openFirstCommit (user, repo) {
  34. return fetch(`https://api.github.com/repos/${user}/${repo}/commits`)
  35. // the `Link` header has additional URLs for paging.
  36. // parse the original JSON for the case where no other pages exist
  37. .then(res => Promise.all([res.headers.get('link'), res.json()]))
  38.  
  39. .then(([link, commits]) => {
  40. if (link) {
  41. // the link header contains two URLs and has the following
  42. // format (wrapped for readability):
  43. //
  44. // <https://api.github.com/repositories/1234/commits?page=2>;
  45. // rel="next",
  46. // <https://api.github.com/repositories/1234/commits?page=9>;
  47. // rel="last"
  48.  
  49. // extract the URL of the last page (commits are ordered in
  50. // reverse chronological order, like the CLI, so the oldest
  51. // commit is on the last page)
  52. const lastPage = link.match(/^.+?<([^>]+)>;/)[1]
  53.  
  54. // fetch the last page of results
  55. return fetch(lastPage).then(res => res.json())
  56. }
  57.  
  58. // if there's no link, we know we're on the only page
  59. return commits
  60. })
  61.  
  62. // get the last commit and extract the target URL
  63. .then(commits => commits[commits.length - 1].html_url)
  64.  
  65. // navigate there
  66. .then(url => location.href = url)
  67. }
  68.  
  69. // add the "First commit" link as the last child of the commit bar
  70. function addLink ($commitBar) {
  71. const [user, repo] = $('meta[name="octolytics-dimension-repository_network_root_nwo"]')
  72. .attr('content')
  73. .split('/')
  74.  
  75. // the "First commit" link already exists when navigating to a repo's
  76. // homepage via the back button. however, resurrecting the link in this way
  77. // causes its onclick event handler to be unregistered (XXX why?), so we
  78. // still need to re-attach it
  79. let $firstCommit = $commitBar.find('#first-commit')
  80.  
  81. if (!$firstCommit.length) {
  82. $firstCommit = $(FIRST_COMMIT)
  83. $commitBar.append($firstCommit)
  84. }
  85.  
  86. const $link = $firstCommit.find('#first-commit-link')
  87.  
  88. $link.on('click', event => {
  89. $link.text('Loading...')
  90. openFirstCommit(user, repo)
  91. return false
  92. })
  93. }
  94.  
  95. // the commit bar (div.commit-tease) is statically defined in the HTML
  96. // for users who aren't logged in. for logged-in users, it's loaded dynamically
  97. // via an <include-fragment> custom element:
  98. //
  99. // https://github.com/github/include-fragment-element
  100. //
  101. // jQuery-onMutate fires the callback immediately if the element already exists,
  102. // so it handles both cases
  103.  
  104. // #js-repo-pjax-container is only created on repo homepages
  105. // see here for more details: https://github.com/Mottie/GitHub-userscripts/wiki/How-to
  106. $('#js-repo-pjax-container').onCreate(COMMIT_BAR, addLink, true /* multi */)