Github Bors Merge

Adds a button to easily start/stop PR merge when using bors

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Github Bors Merge
// @namespace    https://sanketmishra.me
// @version      0.1
// @description  Adds a button to easily start/stop PR merge when using bors
// @author       Sanket Mishra
// @match        https://github.com/*
// @match        https://*.github.com/*
// @icon         https://github.githubassets.com/favicons/favicon-dark.png
// @grant        none
// ==/UserScript==

(function () {
  "use strict";

  const BORS_MERGE_COMMENT = "bors r+";
  const BORS_CANCEL_COMMENT = "bors r-";
  const BORS_BUTTON_ID = "bors-button-submit";
  const BORS_MERGE_BUTTON_LABEL = "Bors Merge";
  const BORS_CANCEL_BUTTON_LABEL = "Bors Cancel";
  const BORS_PENDING_PREFIX = "borspending";
  const INTERVAL_DURATION = 3 * 1000; // 3 sec
  const PULL_REQUEST_COMMENTS_PAGE_REGEX = /\/pull\/\d+$/;

  const BORS_BUTTON_TYPE = "submit"; // change to "button" for testing

  let intervalId = null;

  function isPrCommentsPage() {
    return PULL_REQUEST_COMMENTS_PAGE_REGEX.test(window.location.pathname);
  }

  function isPrOpen() {
    return document.getElementsByClassName("State--open").length > 0;
  }

  function isBorsRunning() {
    const mergeStatusItems = [
      ...document.getElementsByClassName("merge-status-item"),
    ];
    return mergeStatusItems.some((item) => {
      const textContent = item.innerText
        .replaceAll(/[\W\n]+/g, "")
        .trim()
        .toLowerCase();
      return textContent.startsWith(BORS_PENDING_PREFIX);
    });
  }

  function getExistingBorsButton() {
    return document.getElementById(BORS_BUTTON_ID);
  }

  function isBorsButtonPresent() {
    return getExistingBorsButton() !== null;
  }

  function getExistingBorsButtonText() {
    return getExistingBorsButton()?.innerText ?? "";
  }

  function getBorsBaseButton() {
    const borsButton = document.createElement("button");
    borsButton.className = "btn-primary btn mr-1";
    borsButton.setAttribute("type", BORS_BUTTON_TYPE);
    borsButton.setAttribute("id", BORS_BUTTON_ID);
    return borsButton;
  }

  function getBorsButtonLabel(isBorsAlreadyRunning) {
    return isBorsAlreadyRunning
      ? BORS_CANCEL_BUTTON_LABEL
      : BORS_MERGE_BUTTON_LABEL;
  }

  function getBorsButtonCommentText(isBorsAlreadyRunning) {
    return isBorsAlreadyRunning ? BORS_CANCEL_COMMENT : BORS_MERGE_COMMENT;
  }

  function addOrReplaceBorsButton(borsButton, borsButtonParent) {
    if (isBorsButtonPresent()) {
      borsButtonParent.replaceChild(
        borsButton,
        borsButtonParent.firstElementChild
      );
    } else {
      borsButtonParent.insertBefore(
        borsButton,
        borsButtonParent.firstElementChild
      );
    }
  }

  function handleBorsButton() {
    const commentTextArea = document.getElementById("new_comment_field");
    const commentBoxButtonsParent = document.getElementById(
      "partial-new-comment-form-actions"
    ).firstElementChild;
    const commentButton = [
      ...commentBoxButtonsParent.getElementsByTagName("button"),
    ].filter((btn) => btn.innerText.toLowerCase() === "comment")[0];

    if (!commentButton) {
      return;
    }

    const isBorsAlreadyRunning = isBorsRunning();
    const borsButton = getBorsBaseButton();

    borsButton.innerText = getBorsButtonLabel(isBorsAlreadyRunning);
    borsButton.addEventListener("click", () => {
      commentTextArea.value = getBorsButtonCommentText(isBorsAlreadyRunning);
    });

    addOrReplaceBorsButton(borsButton, commentBoxButtonsParent);
  }

  function shouldHandleBorsButton() {
    if (!isPrCommentsPage()) {
      return false;
    }

    if (!isPrOpen()) {
      return false;
    }

    if (!isBorsButtonPresent()) {
      return true;
    }

    const borsButtonActualText = getExistingBorsButtonText();
    const borsButtonExpectedText = isBorsRunning()
      ? BORS_CANCEL_BUTTON_LABEL
      : BORS_MERGE_BUTTON_LABEL;

    return borsButtonActualText !== borsButtonExpectedText;
  }

  function execute() {
    if (shouldHandleBorsButton()) {
      handleBorsButton();
    }
  }

  function runOnInterval() {
    if (intervalId !== null) {
      clearInterval(intervalId);
      intervalId = null;
    }

    // Run it immediately for the first time
    execute();

    // Setup an interval to check whether the bors button is in DOM
    // Add it to the DOM if not present. Do nothing if present.
    intervalId = setInterval(() => {
      execute();
    }, INTERVAL_DURATION);
  }

  // Main function starts here
  runOnInterval();
})();