Create & copy release content for coding project.

Make your release real quick!

当前为 2020-03-03 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Create & copy release content for coding project.
// @namespace    https://greasyfork.org/zh-CN/scripts/395451
// @version      1.4
// @description  Make your release real quick!
// @author       You
// @match        https://*.coding.net/p/*/d/*/git/compare/*
// @grant        GM_setClipboard
// @grant        GM_notification
// @grant        GM_addStyle
// ==/UserScript==

(function () {
    const PageRegExp = /\/p\/(.*)[\/d\/(.*)]?\/git\/compare\/(.*)\.\.\.(.*)/;

    const getDateStr = () => {
        const t = new Date()
        const y = t.getUTCFullYear();
        const m = `0${t.getUTCMonth() + 1}`.slice(-2);
        const d = `0${t.getUTCDate()}`.slice(-2);
        const dateStr = `${y}-${m}-${d}`;
        const tag = `${y}${m}${d}.1`;
        return [dateStr, tag];
    }

    const getContent = (source, target, content, date, tag, depot, project, corp) => `# 更新日期

> 开始时间:${date} 10:00
> 结束时间:


# 发布类型

常规发布


# 负责人

@name


# 改动说明

${content}


# ${depot} diff

https://${corp}.coding.net/p/${project}/d/${depot}/git/compare/${source}...${target}


# 更新服务

服务 | 标签 | 顺序
:---------- | :----------: | ----------:
e-${depot}  |  ${tag}  |  1


# 发布后 ${depot} master 指向

\`\`\`
${target}
\`\`\`


# Checklist

`;

    const getCompareResult = (source, target, depot, project, corp) =>
        fetch(`https://${corp}.coding.net/api/user/${corp}/project/${project}/depot/${depot}/git/compare_v2?source=${source}&target=${target}&w=&prefix=`);

    const getMRId = url => {
        const match = url.match(/\d+(\/)?$/) || [];
        return match.shift() || 0;
    }

    const matchFromPath = () => {
        const { pathname, host } = window.location;
        const match = pathname.match(/\/p\/(.*)[\/d\/(.*)]?\/git\/compare\/(.*)\.\.\.(.*)/) || [];

        const len = match.length;
        if (len < 3 || !match[1]) {
            throw new Error(`failed to parse url.`);
        }

        const corp = host.replace('.coding.net', '');
        const [project, depot] = match[1].split(`/d/`);

        return [
            ...match.slice(-2),
            depot || project,
            project,
            corp,
        ];
    }

    async function makeRelease() {
        const [source, target, depot, project, corp] = matchFromPath()
        const res = await getCompareResult(source, target, depot, project, corp);
        const { data: { commits = [] } } = await res.json()
        const mrs = commits.filter(c => c.rawMessage.startsWith(`Accept Merge Request #`))

        const list = mrs.map(m => {
            const arr = m.rawMessage.split('\n');
            const title = arr.find(i => i.includes(`Merge Request:`)).replace(`Merge Request:`, '').trim();
            const creator = arr.find(i => i.includes(`Created By:`)).replace(`Created By:`, '').trim();
            const link = arr.find(i => i.includes(`URL:`)).replace(`URL:`, '').trim();
            const mrId = getMRId(link);
            const mrText = `<a href="${link}">#${mrId}</a>`;
            return `- ` + [title, creator, mrText].join(`, `);
        });
        const content = list.join(`\n`);

        const [date, tag] = getDateStr();
        const text = getContent(source, target, content, date, tag, depot, project, corp);
        GM_setClipboard(text, `text`);
        GM_notification(`Copied to 📝`, `💯`);
    }

    const style = `button#create-copy-release-btn {
z-index: 100;
position: absolute;
right: 30px;
top: 80px;
display: inline-block;
border: none;
padding: 1rem 2rem;
margin: 0;
text-decoration: none;
background: #0069ed;
color: #ffffff;
font-family: sans-serif;
font-size: 1rem;
cursor: pointer;
text-align: center;
transition: background 250ms ease-in-out,
transform 150ms ease;
-webkit-appearance: none;
-moz-appearance: none;
}

button#create-copy-release-btn:hover,
button#create-copy-release-btn:focus {
background: #0053ba;
}

button#create-copy-release-btn:focus {
outline: 1px solid #fff;
outline-offset: -4px;
}

button#create-copy-release-btn:active {
transform: scale(0.99);
}`;
    GM_addStyle(style);

    const btn = document.createElement('button');
    btn.innerText = `Create & Copy Release`;
    btn.id = `create-copy-release-btn`;
    btn.onclick = makeRelease;

    let timer = null;
    let inserted = false;
    timer = setInterval(() => {
        const isComparePage = PageRegExp.test(window.location.pathname);

        if (inserted) {
            if (isComparePage) return;
            btn.parentNode.removeChild(btn);
            inserted = false;
            return;
        }

        if (!isComparePage) {
            return;
        }

        const s = document.querySelector(`[class*=ref-selector]`);
        if (!s) return;

        if (!inserted) {
            s.parentNode.appendChild(btn);
            inserted = true;
        }
        // clearInterval(timer);
    }, 200);
})()