Skip to content

Instantly share code, notes, and snippets.

@sergiosusa
Last active May 17, 2023 08:55
Show Gist options
  • Save sergiosusa/1c3d2f00082dfd58856ff75ff18860be to your computer and use it in GitHub Desktop.
Save sergiosusa/1c3d2f00082dfd58856ff75ff18860be to your computer and use it in GitHub Desktop.
Jenkins Helper
// ==UserScript==
// @name Jenkins Helper
// @namespace ADM (Sergio Susa)
// @version 0.8
// @description Add some features to speed up the team.
// @author Sergio Susa ([email protected])
// @match https://jenkins-sprccsadm.tools.msi.audi.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=audi.com
// @grant GM_setClipboard
// ==/UserScript==
(function() {
'use strict';
try {
let jenkinsHelper = new JenkinsHelper();
jenkinsHelper.render();
} catch (exception) {
alert(exception);
}
})();
function JenkinsHelper() {
this.rendererList = [
new DeployHelper(),
new ReleaseVersionHelper(),
new ReleaseRedirectHelper(),
new AssembleDevelopHelper()
];
this.render = () => {
let renderer = this.findRenderer();
if(renderer){
renderer.render();
}
};
this.findRenderer = () => {
return this.rendererList.find(renderer => renderer.canHandleCurrentPage());
};
}
function Renderer() {
this.handlePage = "";
this.canHandleCurrentPage = () => {
return null !== document.location.href.match(this.handlePage);
};
}
function AssembleDevelopHelper() {
Renderer.call(this);
this.handlePage = /\/job\/assemble-develop\/\d+\/console$/g;
this.render = () => {
let referenceNode = document.querySelector(".console-output");
let style = 'border: 2px solid;border-radius: 4px;padding: 0.375rem 1rem;margin-right: 0.25rem;margin-left: 0.25rem;min-width: 4.5rem;min-height: 2rem;color: var(--btn-text-color);background-color: var(--btn-primary-bg);border-color: var(--btn-primary-bg);cursor:pointer;';
let newNode = document.createElement("div");
newNode.innerHTML = '<div style="margin-top: 10px;margin-bottom: 15px;">' +
'<span style="' + style + '" id="copyVersion">Copy Version</span>' +
'</div>';
referenceNode.parentElement.insertBefore(newNode, referenceNode);
document.querySelector("#copyVersion").onclick = this.copyVersion;
};
this.copyVersion = () => {
let consoleOutput = document.querySelector(".console-output").innerHTML;
let versionMatch = consoleOutput.match(/:(\d+\.\d+\.\d+(-SNAPSHOT)*(-.+)*)\n/)
GM_setClipboard(versionMatch[1]);
alert("Version has been copied in your clipboard");
};
}
AssembleDevelopHelper.prototype = Object.create(Renderer.prototype);
function ReleaseVersionHelper() {
Renderer.call(this);
this.handlePage = /\/job\/build-release\/\d+\/rebuild\/parameterized$/g;
this.render = () => {
let container = document.querySelector(".parameters .jenkins-form-label");
let style = 'border: 2px solid;border-radius: 4px;padding: 0.375rem 1rem;margin-right: 0.25rem;margin-left: 0.25rem;min-width: 4.5rem;min-height: 2rem;color: var(--btn-text-color);background-color: var(--btn-primary-bg);border-color: var(--btn-primary-bg);cursor:pointer;';
container.innerHTML = container.innerHTML + '<div style="text-align: right;width: 100%;" ">'
+'<span style="' + style + '" id="majorBtn">Major</span>'
+ '<span style="' + style + '" id="minorBtn">Minor</span>'
+ '<span style="' + style + '" id="patchBtn">Patch</span></div>';
document.querySelector("#majorBtn").onclick = this.caculateMajor;
document.querySelector("#minorBtn").onclick = this.caculateMinor;
document.querySelector("#patchBtn").onclick = this.caculatePatch;
}
this.caculateMajor = () => {
let inputs = document.querySelectorAll(".parameters input.jenkins-input");
let releaseVersion = inputs[0].value;
let developmentVersion = inputs[1].value;
let releaseVersionParts = releaseVersion.match(/(\d+)\.(\d+)\.(\d+)(-SNAPSHOT)*/);
releaseVersionParts.shift();
releaseVersionParts[1] = '0';
releaseVersionParts[2] = '0';
inputs[0].value = this.calculateSemanticVersion(releaseVersionParts, 0)
let developmentVersionParts = developmentVersion.match(/(\d+)\.(\d+)\.(\d+)(-SNAPSHOT)*/);
developmentVersionParts.shift();
developmentVersionParts[1] = '1';
developmentVersionParts[2] = '0';
inputs[1].value = this.calculateSemanticVersion(developmentVersionParts, 0);
GM_setClipboard(inputs[0].value);
};
this.caculateMinor = () => {
let inputs = document.querySelectorAll(".parameters input.jenkins-input");
let releaseVersion = inputs[0].value;
let developmentVersion = inputs[1].value;
let releaseVersionParts = releaseVersion.match(/(\d+)\.(\d+)\.(\d+)(-SNAPSHOT)*/);
releaseVersionParts.shift();
releaseVersionParts[2] = '0';
inputs[0].value = this.calculateSemanticVersion(releaseVersionParts, 1)
let developmentVersionParts = developmentVersion.match(/(\d+)\.(\d+)\.(\d+)(-SNAPSHOT)*/);
developmentVersionParts.shift();
inputs[1].value = this.calculateSemanticVersion(developmentVersionParts, 1);
GM_setClipboard(inputs[0].value);
};
this.caculatePatch = () => {
let inputs = document.querySelectorAll(".parameters input.jenkins-input");
let releaseVersion = inputs[0].value;
let releaseVersionParts = releaseVersion.match(/(\d+)\.(\d+)\.(\d+)(-SNAPSHOT)*/);
releaseVersionParts.shift();
inputs[0].value = this.calculateSemanticVersion(releaseVersionParts, 2)
GM_setClipboard(inputs[0].value);
};
this.calculateSemanticVersion = (versionParts, index) => {
versionParts[index] = parseInt(versionParts[index])+1;
return versionParts[0] + '.' + versionParts[1] + '.' + versionParts[2] + (versionParts[3] != undefined ? versionParts[3]: '');
};
}
ReleaseVersionHelper.prototype = Object.create(Renderer.prototype);
function DeployHelper() {
Renderer.call(this);
this.handlePage = /https:\/\/jenkins-sprccsadm\.tools\.msi\.audi\.com\/job\/Deployments\/job\/deploy-team-helmfile\/$/g;
this.render = () => {
var referenceDiv = document.querySelector("#description");
let newElement = document.createElement("div");
newElement.innerHTML = this.template();
this.insertBefore(newElement, referenceDiv);
this.setUpInnerLinks();
};
this.template = () => {
let template = '<div class="tabBarFrame">' +
'<h2>ADM Deploy</h2>'+
'<div style="justify-content: center;" class="tabBar">';
let url = "test";
let service = "service";
template +='<div class="tab active"><a id="deployQA" style="cursor:pointer;" href="javascript:void(0);" class="">QA</a></div>' +
'<div class="tab active"><a id="deployLIVE" style="cursor:pointer;" href="javascript:void(0);" class="">LIVE</a></div>';
template += '</div>' +
'</div>';
return template;
};
this.insertBefore = (newNode, referenceNode) => {
referenceNode.parentNode.insertBefore(newNode, referenceNode);
};
this.setUpInnerLinks = () => {
let deployQA = document.querySelector("#deployQA");
let DeployLIVE = document.querySelector("#deployLIVE");
deployQA.onclick = this.deployQA;
DeployLIVE.onclick = this.deployLIVE;
};
this.deployQA = () => {
this.deploy("qa");
};
this.deployLIVE = () => {
this.deploy("live");
};
this.deploy = (stage) => {
var node = document.querySelector(".jenkins-search input.jenkins-search__input");
node.setValue(stage);
var event = new Event("input", {bubbles: true, canceleable: true});
node.dispatchEvent(event);
};
}
DeployHelper.prototype = Object.create(Renderer.prototype);
function ReleaseRedirectHelper() {
Renderer.call(this);
this.services = {
admb: "https://jenkins-sprccsadm.tools.msi.audi.com/job/Services/job/AWSIADM/job/adm-backend/",
admccaf: "https://jenkins-sprccsadm.tools.msi.audi.com/job/Frontends/job/AWSIADM/job/adm-frontend/",
admf: "https://jenkins-sprccsadm.tools.msi.audi.com/job/Frontends/job/AWSIADM/job/adm-dealer-frontend/",
admdamref: "https://jenkins-sprccsadm.tools.msi.audi.com/job/Services/job/AWSIADM/job/adm-damage-refinement-service/",
admis: "https://jenkins-sprccsadm.tools.msi.audi.com/job/Services/job/AWSIADM/job/adm-insurance-service",
admlibrary: "https://jenkins-sprccsadm.tools.msi.audi.com/job/Libraries/job/AWSIADM/job/adm-library/",
admm: "https://jenkins-sprccsadm.tools.msi.audi.com/job/Services/job/AWSIADM/job/adm-mail-service/",
admts: "https://jenkins-sprccsadm.tools.msi.audi.com/job/Services/job/AWSIADM/job/adm-translation-service/",
admdis: "https://jenkins-sprccsadm.tools.msi.audi.com/job/Services/job/AWSIADM/job/adm-data-inquiry-service/",
admdbs: "https://jenkins-sprccsadm.tools.msi.audi.com/job/Services/job/AWSIADM/job/adm-dat-connection-service/",
admdatd: "https://jenkins-sprccsadm.tools.msi.audi.com/job/Services/job/AWSIADM/job/adm-dat-dealer-mapping-service/",
admcns: "https://jenkins-sprccsadm.tools.msi.audi.com/job/Services/job/AWSIADM/job/adm-creation-notification-service/",
admrsafeproxy: "https://jenkins-sprccsadm.tools.msi.audi.com/job/Services/job/AWSIADM/job/adm-rsafe-proxy/",
admds: "https://jenkins-sprccsadm.tools.msi.audi.com/job/Services/job/AWSIADM/job/document-store-service/"
};
this.handlePage = /https:\/\/jenkins-sprccsadm\.tools\.msi\.audi\.com\/$/g;
this.render = () => {
var referenceDiv = document.querySelector("#projectstatus-tabBar");
if (referenceDiv === null){
return;
}
let newElement = document.createElement("div");
newElement.innerHTML = this.tempalte();
this.insertBefore(newElement, referenceDiv);
this.setUpInnerLinks();
};
this.tempalte = () => {
let template = '<div class="tabBarFrame">' +
'<h2>ADM Projects</h2>'+
'<div style="justify-content: center;" class="tabBar">';
for (const [service, url] of Object.entries(this.services)){
template +='<div style="display: flex;margin-right: 10px;" class="tab active"><a style="cursor:pointer;" href="' + url + '" class="">' + service +'</a> <span style="cursor:pointer;margin-top: 7px;" id="service' + service +'">&nbsp;|&nbsp;&nbsp;🏁</span></div>';
}
template += '</div>' +
'</div>';
template += '<div class="tabBarFrame">' +
'<h2>Actions</h2>'+
'<div style="justify-content: center;" class="tabBar">' +
'<div class="tab active"><a style="cursor:pointer;" href="https://jenkins-sprccsadm.tools.msi.audi.com/job/Deployments/job/deploy-team-helmfile/" class="">Deploy QA/LIVE</a></div>';
'</div>' +
'</div>';
return template;
};
this.insertBefore = (newNode, referenceNode) => {
referenceNode.parentNode.insertBefore(newNode, referenceNode);
};
this.setUpInnerLinks = () => {
for (const [service, url] of Object.entries(this.services)){
let flag = document.querySelector("#service" + service);
flag.setAttribute("redirecturl", url + "job/build-release/");
flag.addEventListener('click', function(event){
event.preventDefault();
window.open(this.getAttribute("redirecturl"));
});
}
};
}
ReleaseRedirectHelper.prototype = Object.create(Renderer.prototype);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment