Skip to content

Instantly share code, notes, and snippets.

@the-glima
Last active April 4, 2020 02:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save the-glima/86e7eec2a597ee7a22121b11cab32a0d to your computer and use it in GitHub Desktop.
Save the-glima/86e7eec2a597ee7a22121b11cab32a0d to your computer and use it in GitHub Desktop.
Automated checks when creating PRs
// ==UserScript==
// @name Github PR
// @namespace https://github.com/gabrihellmateus/
// @version 0.1
// @description Automate tasks for helping creating PRs
// @author Gabriel Lima (inspired by Doug Bacelar)
// @match https://github.com/gabrihellmateus/mercearia/pull/*
// ==/UserScript==
(function() {
'use strict';
const PR_BASE_URI = 'https://github.com/gabrihellmateus/mercearia/pull/';
const userName = document.querySelector('meta[name="user-login"]').content;
const header = document.getElementById('partial-discussion-header');
const sidebar = document.getElementsByClassName('discussion-sidebar')[0];
const author = header.getElementsByClassName('author')[0].innerText;
const title = header.getElementsByClassName('js-issue-title')[0].innerText;
const PRCheck = {
init: () => {
if (!PRCheck.isInPrPage()) return;
PRCheck.log(`🎯`, `PR Page Detected!`);
PRCheck.addAssignYourself();
PRCheck.addLabel();
},
isInPrPage: () => !!window.location.href.match(PR_BASE_URI),
addAssignYourself: () => {
const assignSelfButton = sidebar.getElementsByClassName('js-issue-assign-self')[0];
if (!assignSelfButton) return;
if (author === userName) {
assignSelfButton.click();
PRCheck.log(`✅`, `Completed: assigned PR to yourself!`);
}
},
mutationOberserver: (callback) => {
const MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.type === 'childList') {
callback(mutation);
}
if (mutation.type === 'subtree') {
callback();
}
if (mutation.type === 'attributes') {
callback();
}
if (mutation.type === 'characterData') {
callback();
}
});
});
return observer;
},
addLabel: () => {
const config = {
childList: true,
subtree: true,
attributes: false,
characterData: false
};
const target = sidebar.getElementsByClassName('js-issue-labels-menu-content')[0];
const sidebarLabels = sidebar.getElementsByClassName('sidebar-labels')[0];
const labelToggle = sidebarLabels.getElementsByClassName('discussion-sidebar-toggle')[0];
labelToggle.click();
const observer = PRCheck.mutationOberserver((mutation) => {
mutation.addedNodes.forEach(function(node) {
if (node.className === 'select-menu-list') {
let labels = sidebarLabels
.getElementsByClassName('select-menu-list')[0]
.getElementsByClassName('select-menu-item');
PRCheck.checkLabel(Array.from(labels), labelToggle);
}
});
});
observer.observe(target, config);
},
checkLabel: (labels, labelToggle) => {
labels.forEach((label, i, arr) => {
let labelInput = label.querySelector('input[data-label-name]');
let labelName = labelInput.getAttribute('data-label-name');
let regex = new RegExp(labelName, 'ig');
if (title.match(regex) && !labelInput.checked) {
label.click();
PRCheck.log(`🎯`, `Label ${labelName} applied!`);
}
if (Object.is(arr.length - 1, i)) {
PRCheck.dismissLabelDialog(labelToggle);
PRCheck.log(`✅`, `Completed: all labels applied!`);
}
});
},
dismissLabelDialog: (labelToggle) => {
labelToggle.click();
},
log: (emoji, message) => {
console.log(`----> ${emoji} ${message}`);
}
};
PRCheck.log(`🤖`, `GitHub PR script is running!`);
PRCheck.init();
window.onpopstate = function(event) {
PRCheck.init();
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment