Comgate GitLab MR Rules

Softove hlida ze splnujeme pravidla pro MR

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==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");
})();