diff --git a/README.md b/README.md index 436a3da..eed1f2b 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ Run [nixpkgs-review](https://github.com/Mic92/nixpkgs-review) in GitHub Actions 1. Go to https://app.cachix.org/ and set up your binary cache. 2. [Create a new variable](../../settings/variables/actions/new) with the name `CACHIX_CACHE` and set it to the name of your Cachix cache. 3. [Create a new secret](../../settings/secrets/actions/new) with the name `CACHIX_AUTH_TOKEN` and set its value to your auth token. If you are using a self-signed cache, you also need to create a `CACHIX_SIGNING_KEY` secret and set its value to your private signing key. -6. (optional) Add [`shortcut.js`](shortcut.js) as a user script in your browser for `https://github.com/` for example using the [User JavaScript and CSS chrome extension](https://chromewebstore.google.com/detail/user-javascript-and-css/nbhcbdghjpllgmfilhnhkllmkecfmpld). +6. (optional) Add [`shortcut.js`](shortcut.js) as a user script in your browser for `https://github.com/` for example using the [User JavaScript and CSS chrome extension](https://chromewebstore.google.com/detail/user-javascript-and-css/nbhcbdghjpllgmfilhnhkllmkecfmpld) or [Violentmonkey](https://violentmonkey.github.io/). ## Usage 1. Open the [review workflow in the "Actions" tab](../../actions/workflows/review.yml) diff --git a/shortcut.js b/shortcut.js index fc5dec5..c930ebc 100644 --- a/shortcut.js +++ b/shortcut.js @@ -1,7 +1,13 @@ +// ==UserScript== +// @name nixpkgs-review-gha +// @match https://github.com/* +// ==/UserScript== + const repo = "Defelo/nixpkgs-review-gha"; const sleep = duration => new Promise(resolve => setTimeout(resolve, duration)); const query = async (doc, sel) => { + await sleep(0); while (true) { const elem = doc.querySelector(sel); if (elem !== null) return elem; @@ -9,8 +15,19 @@ const query = async (doc, sel) => { } }; -const setup = async () => { - const match = /^https:\/\/github.com\/nixos\/nixpkgs\/pull\/(\d+)/i.exec(location.href); +const setupActionsPage = async () => { + const match = /^https:\/\/github.com\/([^/]+\/[^/]+)\/actions\/workflows\/review.yml#(\d+)$/.exec(location.href); + if (match === null || match[1] !== repo) return; + + const pr = match[2]; + history.replaceState(null, '', location.href.replace(/#\d+$/, "")); + + (await query(document, "details > summary.btn")).click(); + (await query(document, "input.form-control[name='inputs[pr]']")).value = pr; +}; + +const setupPrPage = async () => { + const match = /^https:\/\/github.com\/NixOS\/nixpkgs\/pull\/(\d+)/i.exec(location.href); if (match === null) return; const pr = match[1]; @@ -22,11 +39,7 @@ const setup = async () => { btn.innerText = "Run nixpkgs-review"; actions.prepend(btn); btn.onclick = () => { - const w = window.open(`https://github.com/${repo}/actions/workflows/review.yml`); - w.addEventListener("load", async () => { - (await query(w.document, "details > summary.btn")).click(); - (await query(w.document, "input.form-control[name='inputs[pr]']")).value = pr; - }); + window.open(`https://github.com/${repo}/actions/workflows/review.yml#${pr}`); }; } @@ -42,5 +55,7 @@ const setup = async () => { } } -navigation.addEventListener('navigate', setup); -setup(); +new MutationObserver(setupPrPage).observe(document, { subtree: true, childList: true }); + +setupActionsPage(); +setupPrPage();