ECOD-GitLab2Jira

Gitlab Merge request title check + Links back to Jira based on ECOD-XXXX formats

Verze ze dne 22. 04. 2024. Zobrazit nejnovější verzi.

// ==UserScript==
// @name         ECOD-GitLab2Jira
// @namespace    ecod.gitlab.utils
// @version      2.1.1
// @description  Gitlab Merge request title check + Links back to Jira based on ECOD-XXXX formats
// @author       CRK
// @match        https://gitlab.ecodesigncloud.com/ecodesign*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';
    function hookTheMonkeyForGitLab() {
	function goToGitLabUrl(node){
		let baseUrl = 'https://jira.fsc.atos-services.net/browse/';
		let ecodRegex = /(ecod[ -_]?\d+)|(\/\d+)/i;
		let result = ecodRegex.test(node.innerHTML);
		if(result){
			let matches = ecodRegex.exec(node.innerHTML);
            let id = matches[0].trim();
			return baseUrl + (id.charAt(0) === '/' ? "ECOD-" + id.replace('/', '') : id.replace(' ', '-').replace('_', '-'));
		} 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 = ecodRegex.test(branchName);
                if(result){
                    let matches = ecodRegex.exec(branchName);
                    let id = matches[0].trim();
                    return baseUrl + (id.charAt(0) === '/' ? "ECOD-" + id.replace('/', '') : id.replace(' ', '-').replace('_', '-'));
                }
            }
        }
		return false;
		//window.open(baseUrl + result, '_blank');
	}

    function checkTitle(node){
		let ecodRegex = /(fix|feat)\(ecod[ -_]?\d+\): .+/i;
		let result = ecodRegex.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: #5707a6; color:white;'
								   id='linkgit'
                                   title='Go to JIRA issue'
								   name='linkgit'>Goto JIRA</a>`;

    var titleWarning = document.createElement('span');
	titleWarning.setAttribute("style" ,"cursor:help;");
	titleWarning.innerHTML = `<span
								   style='color:red; display: inline; padding-left: 15px; padding-right: 15px; 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:green; display: inline; padding-left: 15px; padding-right: 15px; font-size: 22px;'
								   id='titleWarning'
                                   name='titleWarning'
                                   title='Merge Request title is Standard-OK'
								   >&#x1F5F9;</span>`;

	let node= document.querySelector('h1[data-testid="title-content"]')
	if(!node) node = document.querySelector('h3[class="commit-title"]')
	if(node) {
		let url = goToGitLabUrl(node);

		if(url){
			let divContainer = document.createElement('div');
			linkGitLab.firstChild.href = url;
            divContainer.appendChild(checkTitle(node) ? titleOk : titleWarning);
			divContainer.appendChild(linkGitLab);
			divContainer.setAttribute("style", "margin-top:10px; display: inline");

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