GHA workflow_dispatch

Add missing info in workflow_dispatch actions

As of 2021-11-20. See the latest version.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         GHA workflow_dispatch
// @namespace    https://gist.github.com/rehangit/58409fc3cca4ec7630487ac13a055b27
// @version      0.1.4
// @description  Add missing info in workflow_dispatch actions
// @author       Rehan Ahmad
// @match        https://github.com/*/*/actions
// @match        https://github.com/*/*/actions/*
// @grant        none
// @icon         data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="165" height="238" stroke="#007fff" stroke-linecap="round" stroke-linejoin="round" fill="none" fill-rule="evenodd"><circle cx="57.5" cy="57.5" stroke-width="15" r="50"/><path d="M42.5 89.2647L90 57.1324 42.5 25z" stroke-width="8"/><g stroke-width="15"><path d="M57.5 110c0 22.5 0 22.5 50 22.006" stroke-linecap="butt"/><path d="M57.5 112.5c0 92.7005 0 92.7005 35 95"/></g><circle cx="135" cy="207.5" stroke-width="10" r="25"/><path d="M122.5 207.5h7m11 0h7" stroke-width="8" stroke-dasharray="40 48"/><circle cx="132.5" cy="132.5" stroke-width="10" r="25"/><path d="M121.5 135.8333l6.6667 6.6667 13.3333-20" stroke-width="8"/></svg>
// ==/UserScript==

// Use with extension: Tampermonkey or ViolentMonkey

let GITHUB_TOKEN = localStorage.getItem('GITHUB_TOKEN');
if (!GITHUB_TOKEN) {
  GITHUB_TOKEN = prompt(
    "Please enter your github token with 'repo' access",
    'Github Token'
  );
  localStorage.setItem('GITHUB_TOKEN', GITHUB_TOKEN);
}

const headers = { headers: { Authorization: `token ${GITHUB_TOKEN}` } };

const ghCacheStored = localStorage.getItem('GITHUB_API_CACHE');
const ghCache = (ghCacheStored && JSON.parse(ghCacheStored)) || {};
const getGH = async url => {
  if (!ghCache[url]) {
    ghCache[url] = await fetch(url, headers).then(res => res.json());
    localStorage.setItem('GITHUB_API_CACHE', JSON.stringify(ghCache));
  }
  return ghCache[url];
};

const render = event => {
  console.log('render fired', event);
  setTimeout(() => {
    document
      .querySelectorAll('.Box-row.js-socket-channel.js-updatable-content')
      .forEach(async node => {
        const middle = node.querySelector('.d-table-cell + .d-none');
        if (middle.innerText.trim() === '') {
          const link = node.querySelector('.Link--primary');
          const url = link.href.replace(
            '//github.com',
            '//api.github.com/repos'
          );
          const res = await getGH(url, headers);

          const branch = res.head_branch;
          const href = [res.repository.html_url, 'tree', branch].join('/');

          const sha = res.head_sha.slice(0, 7);
          const href_sha = [
            res.repository.html_url,
            'commit',
            res.head_sha,
          ].join('/');

          middle.innerHTML = `
            <div class="d-inline-block branch-name css-truncate css-truncate-target" style="max-width: 200px;">
              <a href="${href}" target="_blank">${branch}</a>
            </div> 
            <div style="padding: 2px 6px">
              <a class="d-block text-small color-fg-muted" href="${href_sha}" target="_blank">#${sha}</a>
            </div>
          `;
        }
      });
  }, 2000);
};

(() => {
  render();
  document.addEventListener('load', render);
  document.addEventListener('visibilitychange', render);
  document.addEventListener('readystatechange', render);
  window.addEventListener('hashchange', render);

  let lastUrl = location.href;
  new MutationObserver(() => {
    const url = location.href;
    if (url !== lastUrl) {
      lastUrl = url;
      render('mutation-observer');
    }
  }).observe(document, { subtree: true, childList: true });
})();