psuvjd / jenkins select branch to deploy quickly

// ==UserScript==
// @name            jenkins select branch to deploy quickly
// @namespace       https://openuserjs.org/users/psuvjd
// @description     快速选择部署分支&&自动选择部署环境
// @copyright       2020, psuvjd (https://openuserjs.org/users/psuvjd)
// @license         MIT
// @version         1.0.3
// @grant           none
// @author          psuvjd
//
// @match           https://jenkins.int.ybm100.com/view/YKQ/job/YKQ-*/job/*/build*
// @match           https://ci.test.ybm100.com/job/INVT/job/INVT-BE-*/job/*/build*
// @match           https://ci.int.ybm100.com/job/INVT/job/INVT-BE-*/job/*/build*
// @exclude         https://jenkins.int.ybm100.com/view/YKQ/job/YKQ-prod/job/*/build*
//
// @updateURL       https://openuserjs.org/meta/psuvjd/jenkins_select_branch_to_deploy_quickly.meta.js
// @downloadURL     https://openuserjs.org/install/psuvjd/jenkins_select_branch_to_deploy_quickly.user.js
// ==/UserScript==

// ==OpenUserJS==
// @author          psuvjd
// ==/OpenUserJS==

(function () {
  'use strict';

  setInterval(doReal, 200);

  let opDone = false;

  let branch2EnvMap = {
    "develop": "dev",
    "test": "test",
    "stage": "stage",
    "master": "prod"
  }

  function doReal() {

    if (opDone) {
      return;
    }

    let branchList = ["develop", "test", "stage", "master"];

    if (document.querySelector(".behavior-loading") && document.querySelector(".behavior-loading").style.display != "none") {
      // console.log("载入中...");
      return;
    }

    let branchSelector = document.querySelector("div[name='parameter']");

    /* 添加选择分支提示 */
    let lab = document.createElement("label");
    lab.style.display = 'block';
    lab.innerHTML = '选择分支: ';

    let spanRoot = document.createElement("span");
    spanRoot.className += 'yui-button primary';
    spanRoot.style.display = 'block';

    let spanChild = document.createElement("span");
    spanChild.className += 'first-child';

    branchList.forEach(branchName => {
      let branchBtn = document.createElement("button");
      branchBtn.style.display = 'inline';
      branchBtn.style.width = '87px';
      branchBtn.style.marginLeft = '0.25em';
      branchBtn.type = 'button';
      branchBtn.innerHTML = branchName;

      branchBtn.onclick = function () {
        fastSelectBranch(branchName);
      }

      spanChild.appendChild(branchBtn);
    });

    spanRoot.appendChild(spanChild);

    branchSelector.appendChild(lab);
    branchSelector.appendChild(spanRoot);

    opDone = true;
  };

  function fastSelectBranch(branchName) {
    /* 选择分支 */
    let options = document.querySelector("#select").options;
    Array.from(options).forEach((item, idx) => {
      let branch = item.value.split('/').slice(-1)[0];
      if (branch == branchName) {
        document.querySelector("#select").selectedIndex = idx;
        return;
      }
    });

    /* 选择部署环境 */
    /* 部署环境识别 */
    let env = branch2EnvMap[branchName];
    if (env && env != "") {
      /* 部署环境选择器 */
      let envSelector = document.querySelector("input[value='SRV_ENV']").nextElementSibling;
      /* 设置部署环境 */
      envSelector.value = env;

      /* 更新所选环境对应的部署机器列表 */
      let changeEvent = document.createEvent("HTMLEvents");
      changeEvent.initEvent("change", false, true);
      envSelector.dispatchEvent(changeEvent);
    }
  }

})();