Skip to content

Instantly share code, notes, and snippets.

@maxmarkus
Last active February 19, 2018 15:02
Show Gist options
  • Save maxmarkus/046a104971e25d488ec87e3641f72470 to your computer and use it in GitHub Desktop.
Save maxmarkus/046a104971e25d488ec87e3641f72470 to your computer and use it in GitHub Desktop.
Adds links to Queue and Jobs for killing all of them. Be aware - as soon as you click the links, all currently shown jobs or queues will be cancelled without asking.
// ==UserScript==
// @name Jenkins kill BuildQueue + Jobs
// @namespace https://jenkins
// @version 0.32
// @description Adds a link to Queue and Jobs for killing all of them.
// @author Markus Edenhauser
// @include /^https://jenkins*/
// @exclude *configure
// @grant none
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// ==/UserScript==
(function() {
var cancelWaitTime = 150;
var currentTime = 0;
jQuery('body').prepend('<style>body { background-color: #eee; }; </style>');
var jenkinsPath = location.protocol + '//' + location.host;
var makeid = function() {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < 5; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
};
var settings = {
"async": true,
"crossDomain": true,
"url": null,
"method": "POST",
"headers": {
"accept": "text/javascript, text/html, application/xml, text/xml, */*",
"x-prototype-version": "1.7",
"x-requested-with": "XMLHttpRequest",
"content-type": "application/x-www-form-urlencoded; charset=UTF-8",
"cache-control": "no-cache"
}
};
var killQueue = function(baseLink, selector){
console.log('tampermonkey - killing ' + jQuery(selector).length + ' jobs');
jQuery(selector).each(function(elem, index){
var onClickContent = jQuery(this).attr('onclick');
if(onClickContent.indexOf('null') !== -1) {
console.log('skipped already canceled item.');
return;
}
var targetHref = jQuery(this).attr('href');
console.log('queued', targetHref);
settings.url = baseLink + targetHref;
currentTime = currentTime + cancelWaitTime;
setTimeout(function(sett) {
console.log('killing', targetHref);
jQuery.ajax(sett).done(function(response) { console.log('done'); });
}(settings), currentTime);
});
};
var addKillLink = function(selector, prependSelector, linkText, baseLink) {
var newSelect = makeid();
jQuery(prependSelector).prepend('<a class="collapse ' + newSelect + '" style="cursor:pointer">' + linkText + '</a>');
jQuery('.' + newSelect).on('click', function() { killQueue(baseLink, selector); });
};
var buildQueueSelector = '#buildQueue .pane-header';
if(jQuery(buildQueueSelector).length) {
addKillLink('#buildQueue .stop-button-link', buildQueueSelector, 'Kill Queue!', jenkinsPath);
}
var executorsSelector = '#executors .pane-header';
if(jQuery(executorsSelector).length) {
addKillLink('#executors .stop-button-link', executorsSelector, 'Kill Jobs!', jenkinsPath);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment