GitHub First Commit

Add a link to a GitHub repo's first commit

2020-07-01 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

  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. // @version 2.6.1
  7. // @namespace https://github.com/chocolateboy/userscripts
  8. // @license GPL: https://www.gnu.org/copyleft/gpl.html
  9. // @include https://github.com/
  10. // @include https://github.com/*
  11. // @require https://code.jquery.com/jquery-3.5.1.slim.min.js
  12. // @require https://cdn.jsdelivr.net/gh/eclecto/jQuery-onMutate@79bbb2b8caccabfc9b9ade046fe63f15f593fef6/src/jquery.onmutate.min.js
  13. // @grant GM_log
  14. // @inject-into auto
  15. // ==/UserScript==
  16.  
  17. // XXX note: the unused grant is a workaround for a Greasemonkey bug:
  18. // https://github.com/greasemonkey/greasemonkey/issues/1614
  19.  
  20. const COMMIT_BAR = 'div.js-details-container[data-issue-and-pr-hovercards-enabled] > *:last ul'
  21. const FIRST_COMMIT_LABEL = '<span aria-label="First commit"><strong>1st</strong> commit</span>'
  22.  
  23. // this function extracts the URL of the repo's first commit and navigates to it.
  24. // it is based on code by several developers, a list of whom can be found here:
  25. // https://github.com/FarhadG/init#contributors
  26. //
  27. // XXX it doesn't work on private repos. a way to do that can be found here,
  28. // but it requires an authentication token:
  29. // https://gist.github.com/simonewebdesign/a70f6c89ffd71e6ba4f7dcf7cc74ccf8
  30. function openFirstCommit (user, repo) {
  31. return fetch(`https://api.github.com/repos/${user}/${repo}/commits`)
  32. // the `Link` header has additional URLs for paging.
  33. // parse the original JSON for the case where no other pages exist
  34. .then(res => Promise.all([res.headers.get('link'), res.json()]))
  35.  
  36. .then(([link, commits]) => {
  37. if (link) {
  38. // the link header contains two URLs and has the following
  39. // format (wrapped for readability):
  40. //
  41. // <https://api.github.com/repositories/1234/commits?page=2>;
  42. // rel="next",
  43. // <https://api.github.com/repositories/1234/commits?page=9>;
  44. // rel="last"
  45.  
  46. // extract the URL of the last page (commits are ordered in
  47. // reverse chronological order, like the git CLI, so the oldest
  48. // commit is on the last page)
  49. const lastPage = link.match(/^.+?<([^>]+)>;/)[1]
  50.  
  51. // fetch the last page of results
  52. return fetch(lastPage).then(res => res.json())
  53. }
  54.  
  55. // if there's no link, we know we're on the only page
  56. return commits
  57. })
  58.  
  59. // get the last commit and navigate to its target URL
  60. .then(commits => {
  61. location.href = commits[commits.length - 1].html_url
  62. })
  63. }
  64.  
  65. // add the "First commit" link as the last child of the commit bar
  66. //
  67. // XXX on most sites, hitting the back button takes you back to a snapshot of
  68. // the previous DOM tree, e.g. navigating away from a page on Hacker News
  69. // highlighted via Hacker News Highlighter [1], then back to the highlighted
  70. // page, retains the DOM changes (the highlighting). that isn't the case on
  71. // GitHub, which triggers network requests and fragment/page reloads when
  72. // navigating (back) to any (SPA) page, even when not logged in. this means, for
  73. // example, we don't need to check if the first-commit widget already exists (it
  74. // never does), and don't need to restore its old label when navigating away
  75. // from a page (since the widget will always be created from scratch rather than
  76. // reused)
  77. //
  78. // this has not always been the case, and may not be the case in the future (and
  79. // may not be the case in some scenarios I haven't tested, e.g. on mobile), so
  80. // rather than taking it for granted, we assume the site behaves like every
  81. // other site in this respect and code to that (defensive coding). this costs
  82. // nothing (apart from this explanation) and saves us having to scramble for a
  83. // fix if the implementation changes.
  84. //
  85. // [1] https://greatest.deepsurf.us/en/scripts/39311-hacker-news-highlighter
  86. function addLink ($commitBar) {
  87. // bail if it already exists
  88. let $firstCommit = $commitBar.find('#first-commit')
  89.  
  90. if ($firstCommit.length) {
  91. return
  92. }
  93.  
  94. /*
  95. * This is the first LI in the commit bar (UL), which we clone to create the
  96. * "First commit" widget.
  97. *
  98. * <li class="ml-3">
  99. * <a data-pjax="" href="/foo/bar/commits/master" class="link-gray-dark no-underline">
  100. * <svg height="16">...</svg>
  101. *
  102. * <span class="d-none d-sm-inline">
  103. * <strong>42</strong>
  104. * <span aria-label="Commits on master">commits</span>
  105. * </span>
  106. * </a>
  107. * </li>
  108. */
  109.  
  110. // create it
  111. $firstCommit = $commitBar
  112. .find('li')
  113. .eq(0)
  114. .clone()
  115. .attr('id', 'first-commit')
  116.  
  117. const $link = $firstCommit
  118. .find('a')
  119. .removeAttr('href')
  120. .css('cursor', 'pointer')
  121.  
  122. const $label = $(FIRST_COMMIT_LABEL)
  123.  
  124. $link.find('> span').empty().append($label)
  125.  
  126. const [user, repo] = $('meta[name="octolytics-dimension-repository_network_root_nwo"]')
  127. .attr('content')
  128. .split('/')
  129.  
  130. // restore the original label so it has the correct value if we navigate
  131. // back to the repo page without making a new request (e.g. via the back
  132. // button)
  133. const oldLabelHtml = $label.html()
  134.  
  135. $(window).on('unload', () => {
  136. $label.html(oldLabelHtml)
  137. })
  138.  
  139. $link.on('click', () => {
  140. $label.text('Loading...')
  141. openFirstCommit(user, repo)
  142. return false // stop processing the click
  143. })
  144.  
  145. $commitBar.append($firstCommit)
  146. }
  147.  
  148. $.onCreate(COMMIT_BAR, addLink, true /* multi */)