Comgate GitLab MR Rules

Softove hlida ze splnujeme pravidla pro MR

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

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

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

// ==UserScript==
// @name         Comgate GitLab MR Rules
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Softove hlida ze splnujeme pravidla pro MR
// @author       You
// @match        https://gitlab.comgate.cz/*/merge_requests/*
// @grant        none
// @run-at       document-idle
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';


    function checkRules() {
        const btn = document.querySelector('.accept-merge-request');
        if (!btn) return;

        const currentUser = window.gon?.current_username;

        const authorLink = document.querySelector('.issuable-meta .author-link');
        const authorUsername = authorLink ? authorLink.getAttribute('href').replace('/', '') : '';

        const title = document.querySelector('h1.title')?.innerText.trim() || '';


        let validApprovals = 0;

        const approverLinks = document.querySelectorAll('a.gl-link.gl-avatar-link.user-avatar-link.js-user-link');
        validApprovals = Array.from(approverLinks).filter(link => {
            const username = link.getAttribute('href').split('/').pop();
            return username !== authorUsername;
        }).length;

        const errors = [];

        if (currentUser === authorUsername) {
            errors.push("⛔ Jsi autor tohoto MR.");
        }

        const titleRegex = /^INFSYS-[0-9]{4,5}/;
        if (!titleRegex.test(title)) {
            errors.push("⛔ Špatný název (chybí INFSYS-XXXX). Nalezeno: '" + title + "'");
        }

        if (validApprovals < 3) {
            errors.push(`⛔ Málo schválení (${validApprovals}/3).`);
        }

        const errorContainerId = 'mr-rules-blocker-msg';
        let errorMsg = document.getElementById(errorContainerId);

        if (errors.length > 0) {
            btn.disabled = true;
            btn.style.opacity = '0.5';
            btn.style.cursor = 'not-allowed';

            if (!errorMsg) {
                errorMsg = document.createElement('div');
                errorMsg.id = errorContainerId;
                errorMsg.style.color = '#d9534f';
                errorMsg.style.marginTop = '10px';
                errorMsg.style.fontWeight = 'bold';
                errorMsg.style.whiteSpace = 'pre-line';
                errorMsg.style.marginLeft = '10px'
                btn.parentElement.appendChild(errorMsg);
            }
            errorMsg.innerText = "BLOKOVÁNO:\n" + errors.join('\n');
        } else {
            btn.disabled = false;
            btn.style.opacity = '1';
            btn.style.cursor = 'pointer';
            if (errorMsg) errorMsg.remove();
        }
    }

    if (window.mrRulesInterval) clearInterval(window.mrRulesInterval);
    window.mrRulesInterval = setInterval(checkRules, 1000);

    // Initial check after a short delay to ensure page is fully loaded
    setTimeout(checkRules, 500);

    console.log("🛡️ Comgate MR Rules Loaded");
})();