Squiddly Inverted

Prevents merging PRs unless it has a label

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği yüklemek için Tampermonkey gibi bir uzantı yüklemeniz gerekir.

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği indirebilmeniz için ayrıca Tampermonkey gibi bir eklenti kurmanız gerekmektedir.

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

Bu stili yüklemek için Stylus gibi bir uzantı yüklemeniz gerekir.

Bu stili yüklemek için Stylus gibi bir uzantı kurmanız gerekir.

Bu stili yükleyebilmek için Stylus gibi bir uzantı yüklemeniz gerekir.

Bu stili yüklemek için bir kullanıcı stili yöneticisi uzantısı yüklemeniz gerekir.

Bu stili yüklemek için bir kullanıcı stili yöneticisi uzantısı kurmanız gerekir.

Bu stili yükleyebilmek için bir kullanıcı stili yöneticisi uzantısı yüklemeniz gerekir.

(Zateb bir user-style yöneticim var, yükleyeyim!)

// ==UserScript==
// @name        Squiddly Inverted
// @namespace   http://tampermonkey.net/
// @match       https://github.com/*
// @grant       GM.getValues
// @grant       GM.setValues
// @grant       GM.listValues
// @version     1.3.0
// @author      kenshoen
// @license     MIT
// @description Prevents merging PRs unless it has a label
// @run-at      document-end
// ==/UserScript==

// Original from https://github.com/joshcartme/squiddly/blob/v1.3.0/content/scripts.ts
// MIT License
// Copyright (c) 2025 Josh Cartmell
// Copyright (c) 2025 kenshoen

"use strict";

const defaultConfig = {
    blockIfFailingChecks: true,
    blockUnlessHasLabel: "マージ可",
};

// warning to show when finding and checking checks isn't working
const FAILING_CHECKS_WARNING = "cannot determine if checks are failing";
const ALL_CHECKS_PASSED_TEXT = "All checks have passed";

const PR_PAGE_REGEX = /\/pull\/\d+$/;

const styles = `button.squiddly-inverted[disabled][aria-label]:hover::after {
    content: attr(aria-label);
    position: absolute;
    left: 100%;
    background: black;
    color: white;
    border-radius: 0.5rem;
    padding: 0.5rem;
    z-index: 100;
    width: 200%;
}`;

class ConfigLoader {
  async prepare() {
    if ((await GM.listValues()).length === 0) {
      await GM.setValues(defaultConfig);
    }
    return GM.getValues(Object.keys(defaultConfig));
  }
}

class SquiddlyInvertedCS {
    observer;
    onPrPage;
    config;
    configLoader;

    constructor(configLoader) {
        this.configLoader = configLoader;
    }

     async listenTo(document) {
        this.config = await this.configLoader.prepare();

        const body = document.body;
        this.observer = new MutationObserver((mutations) => {
            this.onPrPage = PR_PAGE_REGEX.test(document.location.pathname);
            this.blockIfAppropriate();
        });
        if (body) {
            this.observer.observe(body, { childList: true, subtree: true });
        }
        const styleElement = document.createElement("style");
        styleElement.textContent = styles;
        document.head.appendChild(styleElement);
    }

    getChecksDescriptionElement() {
        const checksSection = document.querySelector('[aria-label="Checks"]');
        if (!checksSection) {
            console.warn(`Checks section not found, ${FAILING_CHECKS_WARNING}`);
            return;
        }
        const ariaDescribedBy = checksSection.getAttribute("aria-describedby");
        if (!ariaDescribedBy) {
            console.warn(
                `aria-describedby not found on checks section, ${FAILING_CHECKS_WARNING}`
            );
            return;
        }
        const checksDescription = document.getElementById(ariaDescribedBy);
        if (!checksDescription) {
            console.warn(
                `Element with id ${ariaDescribedBy} not found, ${FAILING_CHECKS_WARNING}`
            );
            return;
        }
        return checksDescription;
    }

    reasonsToBlockMerge() {
        const reasons = [];
        if (this.config.blockIfFailingChecks) {
            const descriptionElement = this.getChecksDescriptionElement();
            if (descriptionElement) {
                if (
                    !descriptionElement.textContent?.includes(
                        ALL_CHECKS_PASSED_TEXT
                    )
                ) {
                    reasons.push(
                        descriptionElement.nextElementSibling?.textContent ||
                            "checks are running or failing"
                    );
                }
            } else {
                reasons.push("unable to determine checks status");
            }
        }
        if (this.config.blockUnlessHasLabel && !this.hasTargetLabel()) {
            reasons.push(`PR has no label "${this.config.blockUnlessHasLabel}"`);
        }
        return reasons;
    }

    blockIfAppropriate() {
        if (!this.onPrPage) {
            return;
        }

        const mergeButtons = this.getMergeButtons();
        for (const mergeButton of mergeButtons) {
            // don't interfere with the confirm merge button
            if (
                !mergeButton ||
                mergeButton.textContent?.toLowerCase().includes("confirm") ||
                mergeButton.dataset.inactive === "true"
            ) {
                continue;
            }
            mergeButton.classList.add("squiddly-inverted");
            const reasons = this.reasonsToBlockMerge();

            if (reasons.length > 0) {
                mergeButton.disabled = true;
                mergeButton.ariaDisabled = "true";
                mergeButton.ariaLabel = `Merge button disabled by Squiddly Inverted: ${reasons.join(
                    ", "
                )}`;
            } else {
                mergeButton.removeAttribute("disabled");
                mergeButton.ariaDisabled = "false";
                mergeButton.removeAttribute("ariaLabel");
            }
        }
    }

    getMergeButtons() {
        return Array.from(
            document.querySelectorAll("button")
        ).filter((button) => {
            const label = button?.textContent?.toLowerCase();
            return label.includes("merge") || label.includes("View status");
        });
    }

    hasTargetLabel() {
        return !!document.querySelector(
            `.discussion-sidebar-item [data-name="${this.config.blockUnlessHasLabel}"]`
        );
    }
}

new SquiddlyInvertedCS(new ConfigLoader()).listenTo(document);