Last active
August 26, 2024 20:07
-
-
Save ericboehs/106cdb082bd7ab5ca34ab8c976aabe1d to your computer and use it in GitHub Desktop.
GitHub PR Review Helpers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name GitHub PR Review Actions | |
// @namespace http://tampermonkey.net/ | |
// @version 1.0 | |
// @description Adds buttons to help review | |
// @author Eric Boehs | |
// @match https://github.com/*/*/pull/* | |
// @run-at document-ready | |
// @inject-into content | |
// ==/UserScript== | |
(function() { | |
document.addEventListener('keydown', function(event) { | |
if (event.ctrlKey && event.key === 'a') { | |
if (window.location.pathname.match(/\/pull\/\d+$/)) | |
window.location.pathname += '/files'; // Redirect to Files Changed if we're on Conversation tab | |
const modal = document.querySelector('anchored-position#review-changes-modal'); | |
if (modal) { | |
if (window.getComputedStyle(modal).display === 'none') { | |
document.querySelector('#overlay-show-review-changes-modal').click(); // Review changes | |
document.querySelector('label[for="pull_request_review[event]_approve"]').click(); // Select Approve | |
document.querySelector('#pull_request_review_body').focus(); // Focus comment box | |
} else { | |
modal.querySelector('#pull_requests_submit_review').submit(); // Submit review if modal already open | |
} | |
} | |
} | |
if (event.ctrlKey && event.key === 'v') { | |
// Get all "Viewed" labels | |
const viewedLabels = document.querySelectorAll('label.js-reviewed-toggle'); | |
if (viewedLabels.length > 0) { | |
// Find the label closest to the top of the viewport with an unchecked checkbox | |
let closestLabel = null; | |
let minDistance = Infinity; | |
viewedLabels.forEach(label => { | |
const checkbox = label.querySelector('input.js-reviewed-checkbox'); | |
if (checkbox && !checkbox.checked) { | |
const rect = label.getBoundingClientRect(); | |
if (rect.top >= 0 && rect.top < minDistance) { | |
minDistance = rect.top; | |
closestLabel = label; | |
} | |
} | |
}); | |
// Click the closest label if found | |
if (closestLabel) closestLabel.click(); | |
} | |
} | |
if (event.ctrlKey && event.key === 'x') { | |
if (document.querySelector('.js-auto-merge-box').offsetParent !== null) { | |
if (window.getComputedStyle(document.querySelector('.merge-branch-form')).display === "none") { | |
const mergeButton = Array.from(document.querySelectorAll('.js-auto-merge-box button')).find(button => button.offsetParent !== null); | |
mergeButton.scrollIntoView(); // Scroll the button into view | |
mergeButton.click(); // Open merge form | |
} else { | |
form = Array.from(document.querySelectorAll('.merge-branch-form')).find(form => form.offsetParent !== 'null') | |
form.querySelector('button').click() | |
} | |
} else { | |
if (window.getComputedStyle(document.querySelector('.merge-branch-form')).display === "none") { | |
const mergeButton = document.querySelector('.merge-box-button'); | |
mergeButton.scrollIntoView(); // Scroll the button into view | |
mergeButton.click(); // Open merge form | |
} else { | |
form = Array.from(document.querySelectorAll('.merge-branch-form')).find(form => form.offsetParent !== 'null') | |
form.querySelector('button').click() | |
} | |
} | |
} | |
}); | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This adds three shortcuts:
Ctrl-A
which helps with adding a review to a PRCtrl-V
which checks the "Viewed" checkbox for the file you're looking atCtrl-X
to merge a PR (used on the "Conversation" tab)Ctrl-A
andCtrl-X
have a couple features.Ctrl-A
will take you to the "Files Changed" tab to review if you're on the "Conversation" tab.Ctrl-A
again will open the review dialog, select approve, and focus the comment field (triple tab to change your choice to comment or reject).Ctrl-A
again and it will submit your review (left-hand alternative toCmd-Enter
).Ctrl-X
will open the merge dialog box, scrolling int into view, and hitting it again will merge the PR using the shown options (e.g. Squash & Merge). It also works for enabling "Auto-merge".