Last active
April 4, 2020 02:23
-
-
Save the-glima/86e7eec2a597ee7a22121b11cab32a0d to your computer and use it in GitHub Desktop.
Automated checks when creating PRs
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 | |
// @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