Last active
November 9, 2022 19:09
-
-
Save smileart/653afb8e402ab5950a697430b5958bf4 to your computer and use it in GitHub Desktop.
π A user-script to auto-choose "Auto-merge Squash" in the PR unless it's a release PR
This file contains hidden or 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 Butternut Squash | |
// @namespace http://tampermonkey.net/ | |
// @version 0.3 | |
// @description Choose Auto-merge Squash unless it's a release PR | |
// @author Serge Bedzhyk | |
// @icon https://www.google.com/s2/favicons?sz=64&domain=github.io | |
// @match https://github.com/* | |
// ==/UserScript== | |
if (navigator.userAgent.indexOf('Safari') > -1) { HTMLElement.prototype.checkVisibility = function() { return this.getBoundingClientRect().x > 0 } } | |
function wfe(selector){return new Promise(resolve=>{if(document.querySelector(selector)){return resolve(document.querySelector(selector))};const observer=new MutationObserver(mutations=>{if(document.querySelector(selector)){resolve(document.querySelector(selector));observer.disconnect()}});observer.observe(document.body,{childList:true,subtree:true})})} | |
function last(arr) {return arr[arr.length - 1]} | |
async function main() { | |
const releaseBranches = ['production', 'qa'] | |
const targetBranch = "span.css-truncate-target" | |
const menuArrows = "summary[aria-label='Select merge method']" | |
const squashOption = "button[value='squash']" | |
const menuList = "details-menu.js-merge-method-menu" | |
if(!window.location.pathname.includes('/pull/')) return | |
await wfe(targetBranch) | |
if (releaseBranches.includes(document.querySelector(targetBranch).textContent)) return | |
await wfe(menuArrows) | |
last(document.querySelectorAll(menuArrows)).click() | |
await wfe(squashOption) | |
document.querySelector(squashOption).click() | |
let visible = false | |
document.querySelectorAll(menuList).forEach((e) => { visible = (visible || e.checkVisibility()) }) | |
if (visible) { | |
last(document.querySelectorAll(menuArrows)).click() | |
document.querySelector(squashOption).click() | |
} | |
console.log(">>>> Butternut Squaaaaash!!! π") | |
window.scrollTo(0, 0) | |
} | |
(async function() { | |
let lastUrl = location.href | |
new MutationObserver(() => { | |
const url = location.href | |
if (url !== lastUrl) { | |
lastUrl = url | |
onUrlChange() | |
} | |
}).observe(document, {subtree: true, childList: true}) | |
async function onUrlChange() { | |
await main() | |
} | |
await main() | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment