GitHub First Commit

Add a link to a GitHub repo's first commit

As of 2022-07-06. See the latest version.

  1. "use strict";
  2.  
  3. // ==UserScript==
  4. // @name GitHub First Commit
  5. // @description Add a link to a GitHub repo's first commit
  6. // @author chocolateboy
  7. // @copyright chocolateboy
  8. // @version 2.8.0
  9. // @namespace https://github.com/chocolateboy/userscripts
  10. // @license GPL
  11. // @include https://github.com/
  12. // @include https://github.com/*
  13. // @require https://cdn.jsdelivr.net/npm/cash-dom@8.1.1/dist/cash.min.js
  14. // @grant GM_log
  15. // ==/UserScript==
  16.  
  17. // NOTE This file is generated from src/github-first-commit.user.ts and should not be edited directly.
  18.  
  19. (() => {
  20. // src/github-first-commit.user.ts
  21. // @license GPL
  22. var COMMIT_BAR = "div.js-details-container[data-issue-and-pr-hovercards-enabled] > *:last-child ul";
  23. var FIRST_COMMIT_LABEL = '<span aria-label="First commit"><strong>1st</strong> commit</span>';
  24. function openFirstCommit(user, repo) {
  25. return fetch(`https://api.github.com/repos/${user}/${repo}/commits`).then((res) => Promise.all([res.headers.get("link"), res.json()])).then(([link, commits]) => {
  26. if (!link) {
  27. return commits;
  28. }
  29. const lastPage = link.match(/^.+?<([^>]+)>;/)[1];
  30. return fetch(lastPage).then((res) => res.json());
  31. }).then((commits) => {
  32. if (Array.isArray(commits)) {
  33. location.href = commits[commits.length - 1].html_url;
  34. } else {
  35. console.error(commits);
  36. }
  37. });
  38. }
  39. function run() {
  40. const $commitBar = $(COMMIT_BAR);
  41. if (!$commitBar.length) {
  42. return;
  43. }
  44. $commitBar.find("#first-commit").remove();
  45. const $firstCommit = $commitBar.find("li").eq(0).clone().attr("id", "first-commit");
  46. const $link = $firstCommit.find("a").removeAttr("href").css("cursor", "pointer");
  47. const $label = $(FIRST_COMMIT_LABEL);
  48. $link.find(":scope > span").empty().append($label);
  49. const [user, repo] = $('meta[name="octolytics-dimension-repository_network_root_nwo"]').attr("content").split("/");
  50. $link.on("click", () => {
  51. $label.text("Loading...");
  52. openFirstCommit(user, repo);
  53. return false;
  54. });
  55. $commitBar.append($firstCommit);
  56. }
  57. $(document).on("turbo:load", run);
  58. })();