NOTICE: By continued use of this site you understand and agree to the binding Terms of Service and Privacy Policy.
// ==UserScript== // @name Enhance some features of the Self-managed Gitlab // @description Enhance some features of the Self-managed Gitlab, such as the CI/CD settings page, the merge request create/edit page etc. // @description:zh-CN 强化自托管 Gitlab 的一些功能,如 CI/CD 设置页面、合并请求创建/编辑页面等。 // @version 7 // @author Arylo Yeung <arylo.open@gmail.com> // @include /^https:\/\/(git(lab)?|code)\.[^/]+\/.*\/-\/settings\/ci_cd$/ // @include /^https:\/\/(git(lab)?|code)\.[^/]+\/.*\/-\/merge_requests\/new\b/ // @include /^https:\/\/(git(lab)?|code)\.[^/]+\/.*\/-\/merge_requests\/\d+/edit\b/ // @include /^https:\/\/(git(lab)?|code)\.[^/]+\/dashboard\/merge_requests\b/ // @license MIT // @name:zh-CN 强化自托管 Gitlab 能力 // @homepage https://github.com/Arylo/scripts#readme // @supportURL https://github.com/Arylo/scripts/issues // @downloadURL https://raw.githubusercontent.com/Arylo/scripts/monkey/gitlab-enhance.user.js // @updateURL https://raw.githubusercontent.com/Arylo/scripts/monkey/gitlab-enhance.meta.js // @run-at document-end // @grant GM_addStyle // @grant GM_setClipboard // ==/UserScript== "use strict"; (() => { // src/monkey/polyfill/GM.ts var thisGlobal = window; if (typeof thisGlobal.GM === "undefined") { thisGlobal.GM = {}; } function getGMWindow() { return thisGlobal; } // src/monkey/polyfill/GM_addStyle.ts var w = getGMWindow(); if (typeof w.GM_addStyle === "undefined") { w.GM_addStyle = function GM_addStyle2(cssContent) { const head = document.getElementsByTagName("head")[0]; if (head) { const styleElement = document.createElement("style"); styleElement.setAttribute("type", "text/css"); styleElement.textContent = cssContent; head.appendChild(styleElement); return styleElement; } return null; }; } if (typeof w.GM.addStyle === "undefined") { w.GM.addStyle = GM_addStyle; } // src/monkey/gitlab-enhance/settings/ci_cd.css var ci_cd_default = ".content-wrapper nav{max-width:100%}.content-wrapper .container-fluid{max-width:100%}.ci-variable-table table colgroup col:nth-child(3){width:100px}.ci-variable-table table colgroup col:nth-child(4){width:200px}.ci-variable-table table colgroup col:nth-child(5){width:50px}\n"; // src/monkey/gitlab-enhance/settings/ci_cd.ts if (location.pathname.endsWith("/-/settings/ci_cd")) { setTimeout(() => GM_addStyle(ci_cd_default), 25); } // src/monkey/gitlab-enhance/dashboard/merge_requests.ts var hyperlinkResource = () => { const mergeRequests = $(".merge-request:not([hyperlinked])"); mergeRequests.each((_, mergeRequestEle) => { const href = $(".js-prefetch-document", mergeRequestEle).attr("href"); if (!href) return; const resourceUrl = href.replace(/\/-\/merge_requests\/\d+$/, ""); const rawRefEle = $(".issuable-reference", mergeRequestEle); const rawRefName = rawRefEle.text(); const [resourceName, number] = rawRefName.split("!"); rawRefEle.html(`<a href="${resourceUrl}">${resourceName}</a>!${number}`); $(mergeRequestEle).attr("hyperlinked", ""); }); }; if (location.pathname.endsWith("/dashboard/merge_requests")) { $(".issuable-list").on("mouseenter", hyperlinkResource); setTimeout(hyperlinkResource, 1e3); } // src/monkey/gitlab-enhance/utils.ts var getButtonElement = (text) => { const classnames = "gl-font-sm! gl-ml-3 gl-button btn btn-default btn-sm"; return $(`<a class="${classnames}">${text}</a>`); }; // src/monkey/gitlab-enhance/merge_requests/templateUtils.ts var INDENT_SPACE_LENGTH = 2; var indent = (level) => Array(level * INDENT_SPACE_LENGTH).fill(" ").join(""); var enter = () => "\n"; var Template = class { constructor(text) { this.header = (level) => (text) => { this.templateContent += `${Array(level).fill("#").join("")} ${text}${enter()}${enter()}`; return this.headerNextUtils; }; this.templateContent = text; this.headerNextUtils = { text: this.text.bind(this), listItem: this.listItem.bind(this), taskItem: this.taskItem.bind(this) }; this.contentNextUtils = { ...this.headerNextUtils, end: this.emptyLine.bind(this) }; } h2(text) { return this.header(2)(text); } h3(text) { return this.header(3)(text); } h4(text) { return this.header(4)(text); } [Symbol.toStringTag]() { return this.templateContent.replace(/\n{3,}$/, "\n\n"); } text(text = "", { level = 1 } = {}) { this.templateContent += `${indent(level - 1)}${text}${enter()}`; return this.contentNextUtils; } listItem(text = "", { level = 1 } = {}) { return this.text(`- ${text}`, { level }); } taskItem(text = "", { selected = false, level = 1 } = {}) { return this.listItem(`[${selected ? "x" : " "}] ${text}`, { level }); } emptyLine() { this.templateContent += enter(); } }; var genTemplate = (callback = () => { }) => { const templateInst = new Template(""); callback(templateInst); return templateInst[Symbol.toStringTag](); }; // src/monkey/gitlab-enhance/merge_requests/template.css var template_default = ".gl-display-flex:has([for=merge_request_description]){align-items:baseline}\n"; // src/monkey/gitlab-enhance/merge_requests/template.ts var getBranchType = () => { const fromBranchName = $(".align-self-center code:not([data-branch-name])").text(); const prefixBranchName = fromBranchName.split("/")[0].toLowerCase(); switch (prefixBranchName) { case "feature": case "feat": return 0 /* FEATURE */; case "fix": case "bugfix": return 1 /* BUGFIX */; case "hotfix": return 2 /* HOTFIX */; case "devops": case "chore": case "test": case "doc": case "docs": return 3 /* TASKS */; default: return 4 /* OTHERS */; } }; var generateTemplate = () => { const branchType = getBranchType(); return genTemplate((utils) => { utils.h2("Type").taskItem("Feature (Story/Refactor)", { selected: branchType === 0 /* FEATURE */ }).taskItem(`Bugfix`, { selected: branchType === 1 /* BUGFIX */ }).taskItem(`Hotfix (Production Issues)`, { selected: branchType === 2 /* HOTFIX */ }).taskItem(`Tasks (DevOps / Unit Test / Document Update)`, { selected: branchType === 3 /* TASKS */ }).taskItem(`Others`, { selected: branchType === 4 /* OTHERS */ }).end(); utils.h2("Description"); if (branchType !== 0 /* FEATURE */) { utils.h3("Why (Why does this happen?)").listItem().end(); utils.h3("How (How can we avoid or solve it?)").listItem().end(); } utils.h3("What (What did you do this time?)").listItem().end(); if (branchType !== 3 /* TASKS */) { utils.h3("Results (Screenshot, etc)"); utils.h4("Before modification"); utils.h4("After modification"); utils.h2("Affected Zone").listItem("Affected Module(s):").listItem("Affected URL(s):").end(); } utils.h2("External resources (Mention, Resolves, or Closes)"); }); }; var appendTemplateButton = () => { GM_addStyle(template_default); const text = "Copy Template"; const btnElement = getButtonElement(text); const templateContent = generateTemplate(); const hint = $('<span class="gl-font-sm! gl-ml-3 gl-text-secondary"></span>'); let setTimeoutId; btnElement.on("click", async () => { hint.remove(); setTimeoutId && clearTimeout(setTimeoutId); hint.text("Copying..."); btnElement.after(hint); await GM_setClipboard(templateContent, "text", () => { hint.text("Copied!"); setTimeoutId = setTimeout(() => hint.remove(), 3e3); }); }); $(".gl-display-flex:has([for=merge_request_description])").append(btnElement); }; // src/monkey/gitlab-enhance/merge_requests/new.ts var appendAsTitleButton = () => { $(".commit-content").each((_, el) => { const titleElements = $(".item-title", el); const title = titleElements.text(); const btnElement = getButtonElement("As title"); btnElement.on("click", () => { $("input[data-testid=issuable-form-title-field]").val(title); $("input[data-testid=issuable-form-title-field]").focus(); }); $(".committer", el).before(btnElement); }); }; if (location.pathname.endsWith("/-/merge_requests/new")) { setTimeout(() => { appendTemplateButton(); appendAsTitleButton(); }, 1e3); } // src/monkey/gitlab-enhance/merge_requests/edit.ts if (/\/-\/merge_requests\/\d+\/edit$/.test(location.pathname)) { setTimeout(() => { appendTemplateButton(); }, 1e3); } })();