Skip to content

Instantly share code, notes, and snippets.

@lucasrangit
Last active July 5, 2024 10:06
Show Gist options
  • Save lucasrangit/792324f4695babc701cdd64e18a63f4b to your computer and use it in GitHub Desktop.
Save lucasrangit/792324f4695babc701cdd64e18a63f4b to your computer and use it in GitHub Desktop.
Tampermonkey/Greasemonkey UserScript to Allow Approving Closed GitHub PR
// ==UserScript==
// @name Approve Closed GitHub PR
// @icon https://raw.githubusercontent.com/xthexder/wide-github/master/icons/icon.png
// @namespace https://gist.github.com/lucasrangit/792324f4695babc701cdd64e18a63f4b
// @version 1.2
// @description A closed PR does not allow approval via the Web GUI. This adds the approval event back when submitting a retroactive review.
// @author Lucas Magasweran
// @match https://*.github.*/*/pull/*
// @grant none
// @require http://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// ==/UserScript==
(function() {
'use strict';
function add_approve_to_submit_review_button() {
// expand the Review changes button so that the the approve can be added
const review_buttons = document.getElementsByClassName("details-reset details-overlay js-dropdown-details");
console.log(review_buttons);
for (const a of review_buttons) {
a.setAttribute("open", "");
}
const elms = document.getElementsByName("pull_request_review[event]");
// length is 1 when PR is closed
if (elms.length == 1) {
elms[0].insertAdjacentHTML("afterend", '<input type="hidden" name="pull_request_review[event]" value="approve">');
// Update button text to signal that review will also approve
for (const a of document.querySelectorAll("button")) {
if (a.textContent.includes("Submit review")) {
a.textContent = "Submit review + Approve";
}
}
}
// collapse the Review changes button
for (const a of review_buttons) {
a.removeAttribute("open");
}
}
// run if PR is closed
if (document.getElementsByClassName("State State--red")) {
// wait for Review button on "Files changed" tab
waitForKeyElements (
".details-reset.details-overlay.js-dropdown-details",
add_approve_to_submit_review_button
);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment