Skip to content

Instantly share code, notes, and snippets.

@keriati
Created May 24, 2018 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keriati/6b73452847ffb0a3fcec62c151f22cd1 to your computer and use it in GitHub Desktop.
Save keriati/6b73452847ffb0a3fcec62c151f22cd1 to your computer and use it in GitHub Desktop.
Keybindings / shortcuts for Github Enterprise for faster code reviews in pull requests
// ==UserScript==
// @name Github Enterprise Keyboard Shortcuts for Pull Requests
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author Attila Kerekes
// @match https://github.url/*
// @grant none
// ==/UserScript==
(function () {
'use strict';
addKeyBindings();
function addKeyBindings() {
var actions = {
g: enterCommandMode
};
document.addEventListener('keydown', function (event) {
try {
actions[event.key]();
} catch (e) {
}
});
}
function enterCommandMode() {
document.addEventListener('keydown', onCommandKey);
setTimeout(function () {
try {
exitCommandMode()
} catch (e) {
}
}, 2000);
}
function onCommandKey(event) {
var actions = {
f: showPullRequestFiles,
r: openReviewWindow,
m: focusMergePullRequestButton,
b: copyBranchName
};
try {
actions[event.key]();
event.preventDefault();
} catch (e) {
}
exitCommandMode();
}
function exitCommandMode() {
document.removeEventListener('keydown', onCommandKey);
}
function openReviewWindow() {
var pathName = document.location.pathname;
if (isPullRequestFilesPath(pathName)) {
clickReviewButton();
focusReviewMessageBox();
}
}
function isPullRequestFilesPath(pathName) {
return /pull\/\d*\/files$/.test(pathName);
}
function clickReviewButton() {
document.querySelectorAll('.js-reviews-container .btn-primary')[0].click();
}
function focusReviewMessageBox() {
document.querySelectorAll('#pull_request_review_body')[0].focus();
}
function showPullRequestFiles() {
var pathName = document.location.pathname;
if (isPullRequestPath(pathName)) {
document.location.pathname = getPullRequestRootPath(pathName) + '/files';
}
}
function isPullRequestPath(pathName) {
return /pull/.test(pathName);
}
function getPullRequestRootPath(pathName) {
return pathName
.split('/')
.slice(0,5)
.join('/');
}
function focusMergePullRequestButton() {
var pathName = document.location.pathname;
if (isPullRequestPath(pathName)) {
document.querySelectorAll('button[data-details-container=".js-merge-pr"]')[2].focus();
}
}
function copyBranchName() {
navigator.clipboard.writeText(getBranchName());
}
function getBranchName() {
return document.querySelectorAll('.commit-ref')[1].title.split(':')[1];
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment