Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ivan-degtiarenko/2943c2b868d3bcd52c575ead19949165 to your computer and use it in GitHub Desktop.
Save ivan-degtiarenko/2943c2b868d3bcd52c575ead19949165 to your computer and use it in GitHub Desktop.
Stackrox version: TamperMonkey script to disable merge options on Github branches
// ==UserScript==
// @name Disable merge options on Github branches
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Avoid accidentally using the wrong merge option for specific branches. This script needs to be tweaked for your personal needs(more precisely "@match" column and "disabledBranchesByMethod").
// @author Ivan Degtiarenko
// @match https://github.com/stackrox/*/pull/*
// @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant none
// @run-at document-start
// ==/UserScript==
const disabledBranchesByMethod = [
// Branches for which "Create a merge commit" should be disabled.
[],
// Branches for which "Squash and merge" should be disabled.
['production'],
// Branches for which "Rebase and merge" should be disabled.
[]
];
(function() {
'use strict';
// Use mutation observer as sometimes merge options widget loads later than the page.
var observer = new MutationObserver(function(mutations, obs) {
// Before this element is loaded function would spam errors to console.
if (!document.getElementsByClassName("merge-message")[0]) {
return;
}
let branchName = document.getElementsByClassName("gh-header-meta")[0].children[1].children[0].querySelector('.commit-ref').textContent;
let mergeMethodsEnabled = [true, true, true];
for (let i =0; i < 3; i++) {
mergeMethodsEnabled[i] = !(document.getElementsByClassName("merge-message")[0].querySelector('.select-menu-merge-method').children[0].children[i].disabled);
}
for (let i =0; i < 3; i++) {
mergeMethodsEnabled[i] = mergeMethodsEnabled[i] && (disabledBranchesByMethod[i].indexOf(branchName) === -1);
}
if (mergeMethodsEnabled.every(v => v === false)) {
// Restrictions are too strict, falling back to using repo restrictions only.
obs.disconnect();
return;
}
// Going in reverse order, so the last click would be the click on the top-most available option.
for (let i = 2; i >= 0; i--) {
if (mergeMethodsEnabled[i]) {
document.getElementsByClassName("merge-message")[0].querySelector('.select-menu-merge-method').children[0].children[i].click();
} else {
document.getElementsByClassName("merge-message")[0].querySelector('.select-menu').children[0].children[i].disabled=true;
document.getElementsByClassName("merge-message")[0].querySelector('.select-menu-merge-method').children[0].children[i].disabled=true;
// If disabled by branch-rule, add explanation note.
if (disabledBranchesByMethod[i].indexOf(branchName) !== -1) {
let child = document.createElement('span');
child.className='unavailable-merge-method';
child.textContent='Not enabled for this branch';
document.getElementsByClassName("merge-message")[0].querySelector('.select-menu-merge-method').children[0].children[i].children[1].appendChild(child);
}
}
}
// Once we successfully disabled merge options, observer can be removed to ease the load.
obs.disconnect();
});
observer.observe(document, {attributes: false, childList: true, characterData: false, subtree: true});
})();
@kovayur
Copy link

kovayur commented Nov 7, 2023

The script didn't work for me until I made a couple of fixes.

  1. Add stage to the restricted branch list
['production', 'stage'],
  1. Remove .children[0] from line 31

@ivan-degtiarenko can you apply those changes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment