ECOD-GitLab2Jira

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

目前為 2024-02-29 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         ECOD-GitLab2Jira
// @namespace    ecod.gitlab.utils
// @version      2.1.0
// @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+\): \w+/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>`;

	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;
            if(!checkTitle(node)) divContainer.appendChild(titleWarning);
			divContainer.appendChild(linkGitLab);
			divContainer.setAttribute("style", "margin-top:10px; display: inline");

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