Create & copy release for coding project.

try to take over the world!

Ekde 2020/01/20. Vidu La ĝisdata versio.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Create & copy release for coding project.
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  try to take over the world!
// @author       You
// @match        https://*.coding.net/p/*/d/*/git/compare/*
// @grant        GM_setClipboard
// @grant        GM_notification
// @grant        GM_addStyle
// ==/UserScript==

(function () {
    const getDateStr = () => {
        const t = new Date()
        const y = t.getUTCFullYear();
        const m = `0${t.getUTCMonth() + 1}`.slice(-2);
        const d = t.getUTCDate();
        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 btn = document.createElement('button');
    const style = `button#create-copy-release-btn {
zIndex: 100;
position: absolute;
right: 50px;
top: 10vh;
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);
}`;
    btn.innerText = `Create & Copy Release`;
    btn.id = `create-copy-release-btn`;
    btn.onclick = makeRelease;
    GM_addStyle(style);
    document.body.appendChild(btn);
})()