LBD-Github2Jira

Github Merge request title check + Links back to Jira based on LBD-XXX formats

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         LBD-Github2Jira
// @namespace    lbd.github.utils
// @version      2.3.1
// @description  Github Merge request title check + Links back to Jira based on LBD-XXX formats
// @author       CRK
// @match        https://github.gsissc.myatos.net/NL-EHV-NWC/LBDash4/pull/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';
    function hookTheMonkeyForGitLab() {
	function getGitHubUrl(node){
		let baseUrl = 'https://msdevopsjira.fsc.atos-services.net/browse/';
		let lbdRegex = /(LBD[ -_]?\d+)\|(LBD[ -_]?\d+)|(LBD[ -_]?\d+)|(\/\d+)/i;
		let result = lbdRegex.test(node.innerHTML);
		if(result){
			let matches = lbdRegex.exec(node.innerHTML);
            let id = undefined;
            let parentId = undefined;
            //simple regex, no parentId
            if(matches[0]?.trim() === matches[3]?.trim())
            {
                id = matches[0]?.trim();
            } else {
                id = matches[2]?.trim();
                parentId = matches[1]?.trim();
            }
            //console.log(matches);
			return {
                issueUrl: id ? baseUrl + (id.charAt(0) === '/' ? "LBD-" + id.replace('/', '') : id.replace(' ', '-').replace('_', '-')) : undefined,
                issueButton: id ? id.toUpperCase() : undefined,
                parentUrl: parentId ? baseUrl + (id.charAt(0) === '/' ? "LBD-" + parentId.replace('/', '') : parentId.replace(' ', '-').replace('_', '-')) : undefined,
                parentButton: parentId ? parentId.toUpperCase() : undefined,
            };
		} else {
            //try as alternative to check the branch name for ID
            let branchNameElement = node.parentNode.parentNode.parentNode.querySelector("button[data-clipboard-text]");
            if(branchNameElement){
                let branchName = branchNameElement.attributes['data-clipboard-text'].value ?? '';

                let result = lbdRegex.test(branchName);
                if(result){
                    let matches = lbdRegex.exec(branchName);
                    let id = matches[0].trim();
                    return {
                        issueUrl : baseUrl + (id.charAt(0) === '/' ? "LBD-" + id.replace('/', '') : id.replace(' ', '-').replace('_', '-')),
                        issueButton: id.toUpperCase(),
                    };
                }
            }
        }
		return false;
		//window.open(baseUrl + result, '_blank');
	}

    function checkTitle(node){
		let lbdRegex = /^LBD[-]?\d+.+/i;
		let result = lbdRegex.test(node.innerHTML);
		return result;
	}

	var linkGitLab = document.createElement('span');
	linkGitLab.setAttribute("style" ,"cursor:pointer;");
	linkGitLab.innerHTML = `<a href='#' class='gl-button btn btn-md btn-default js-issuable-edit'
								   target='_blank'
								   style='background-color: #be06e4; color:white;'
								   id='linkgit'
                                   title='Go to JIRA issue'
								   name='linkgit'>JIRA</a>`;

    var linkGitLabParent = document.createElement('span');
	linkGitLabParent.setAttribute("style" ,"cursor:pointer;");
	linkGitLabParent.innerHTML = `<a href='#' class='gl-button btn btn-md btn-default js-issuable-edit'
								   target='_blank'
								   style='background-color: black; color:white;'
								   id='linkgit'
                                   title='Go to JIRA parent issue'
								   name='linkgit'>Parent</a>`;

    var titleWarning = document.createElement('span');
	titleWarning.setAttribute("style" ,"cursor:help;");
	titleWarning.innerHTML = `<span
								   style='color:red; display: inline; padding-left: 10px; padding-right: 10px; font-size: 22px;'
								   id='titleWarning'
                                   name='titleWarning'
                                   title='Merge Request title is Non-Standard. Make sure you use the Jira "Merge" button to copy the standard MR name convention. Ex: fix(ecod-1234): exact Jira title'
								   >&#9888;</span>`;

    var titleOk = document.createElement('span');
	titleOk.setAttribute("style" ,"cursor:help;");
	titleOk.innerHTML = `<span
								   style='color: #00ff00; display: inline; padding-left: 10px; padding-right: 10px; font-size: 22px;'
								   id='titleWarning'
                                   name='titleWarning'
                                   title='Merge Request title is Standard-OK'
								   >&#x1F5F9;</span>`;

     var shareLinkButton = document.createElement('span');
     shareLinkButton.innerHTML = `<span
                                   class='gl-button btn btn-md btn-default js-issuable-edit'
                                   style='cursor:pointer; background-color: #0f7700; margin-left: 10px;'
                                   name='shareLink'
                                   id='shareLink'
                                   onclick='navigator.clipboard.writeText(window.location.href);'
                                   title='Sharable link'>🔗</span>`;

	let node = document.querySelector('bdi[class="js-issue-title markdown-title"]')
    console.info(node);
	if(node) {
		let url = getGitHubUrl(node);

		if(url && url.issueUrl){
			let divContainer = document.createElement('div');

            linkGitLab.firstChild.href = url.issueUrl;
            if(url.issueButton) linkGitLab.firstChild.innerHTML = url.issueButton;

            linkGitLabParent.firstChild.href = url.parentUrl
            if(url?.parentButton) linkGitLabParent.firstChild.innerHTML = url.parentButton;

            divContainer.appendChild(checkTitle(node) ? titleOk : titleWarning);
			divContainer.appendChild(linkGitLab);
            if(url.parentUrl) divContainer.appendChild(linkGitLabParent);
            divContainer.appendChild(shareLinkButton);
			divContainer.setAttribute("style", "margin-top:10px; display: inline");

			node.appendChild(divContainer);
		}
	}
}
    hookTheMonkeyForGitLab()
})();