mirror of
https://gitee.com/ant-design/ant-design.git
synced 2024-12-04 13:08:41 +08:00
dd17e3bac2
* chore: speed up publishing * chore: check runs * chore: check runs * chore: check runs * chore: check runs * chore: check runs * chore: check runs * chore: check runs * chore: check runs * chore: check runs * chore: check runs * chore: check runs * chore: skip test * fix: types * revert demo change * fix: script * chore: fix duplicate parts * chore: fix duplicate parts * chore: fix duplicate parts * improve code * use latest.hash
101 lines
3.5 KiB
TypeScript
101 lines
3.5 KiB
TypeScript
import { Octokit } from '@octokit/rest';
|
||
import ora from 'ora';
|
||
import chalk from 'chalk';
|
||
|
||
const simpleGit = require('simple-git');
|
||
const { Notification: Notifier } = require('node-notifier');
|
||
|
||
const emojify = (status: string = '') => {
|
||
if (!status) {
|
||
return '';
|
||
}
|
||
const emoji = {
|
||
/* status */
|
||
completed: '✅',
|
||
queued: '🕒',
|
||
in_progress: '⌛',
|
||
/* conclusion */
|
||
success: '✅',
|
||
failure: '❌',
|
||
neutral: '⚪',
|
||
cancelled: '❌',
|
||
skipped: '⏭️',
|
||
timed_out: '⌛',
|
||
action_required: '🔴',
|
||
}[status];
|
||
return `${emoji || ''} ${(status || '').padEnd(15)}`;
|
||
};
|
||
|
||
const runPrePublish = async () => {
|
||
const spinner = ora('Loading unicorns').start();
|
||
spinner.info(chalk.black.bgGreenBright('本次发布将跳过本地 CI 检查,远程 CI 通过后方可发布'));
|
||
const git = simpleGit();
|
||
if (!process.env.GITHUB_ACCESS_TOKEN) {
|
||
spinner.fail(
|
||
'请先设置 GITHUB_ACCESS_TOKEN 环境变量到本地,请不要泄露给任何在线页面: https://octokit.github.io/rest.js/v20#authentication',
|
||
);
|
||
process.exit(1);
|
||
}
|
||
const octokit = new Octokit({ auth: process.env.GITHUB_ACCESS_TOKEN });
|
||
const { current: currentBranch } = await git.branch();
|
||
spinner.start(`正在拉取远程分支 ${currentBranch}`);
|
||
await git.pull('origin', currentBranch);
|
||
spinner.succeed(`成功拉取远程分支 ${currentBranch}`);
|
||
spinner.start(`正在推送本地分支 ${currentBranch}`);
|
||
await git.push('origin', currentBranch);
|
||
spinner.succeed(`成功推送远程分支 ${currentBranch}`);
|
||
spinner.succeed(`已经和远程分支保持同步 ${currentBranch}`);
|
||
spinner.succeed(`找到本地最新 commit:`);
|
||
const { latest } = await git.log();
|
||
spinner.info(` hash: ${latest.hash}`);
|
||
spinner.info(` date: ${latest.date}`);
|
||
spinner.info(` message: ${latest.message}`);
|
||
spinner.info(` body: ${latest.body}`);
|
||
spinner.info(` author_name: ${latest.author_name}`);
|
||
const owner = 'ant-design';
|
||
const repo = 'ant-design';
|
||
spinner.start(`开始检查远程分支 CI 状态 ${currentBranch}`);
|
||
const result = await octokit.checks.listForRef({
|
||
owner,
|
||
repo,
|
||
ref: latest.hash,
|
||
});
|
||
spinner.succeed(`远程分支 CI 状态:`);
|
||
result.data.check_runs.forEach((run) => {
|
||
spinner.info(
|
||
` ${run.name.padEnd(30)} ${emojify(run.status)} ${emojify(run.conclusion || '')}`,
|
||
);
|
||
});
|
||
const conclusions = result.data.check_runs.map((run) => run.conclusion);
|
||
if (
|
||
conclusions.includes('failure') ||
|
||
conclusions.includes('cancelled') ||
|
||
conclusions.includes('timed_out')
|
||
) {
|
||
spinner.fail('远程分支 CI 执行异常,无法继续发布,请尝试修复或重试');
|
||
spinner.info(` 点此查看状态:https://github.com/${owner}/${repo}/commit/${latest.hash}`);
|
||
process.exit(1);
|
||
}
|
||
const statuses = result.data.check_runs.map((run) => run.status);
|
||
if (
|
||
result.data.check_runs.length < 1 ||
|
||
statuses.includes('queued') ||
|
||
statuses.includes('in_progress')
|
||
) {
|
||
spinner.fail('远程分支 CI 还在执行中,请稍候再试');
|
||
spinner.info(` 点此查看状态:https://github.com/${owner}/${repo}/commit/${latest.hash}`);
|
||
process.exit(1);
|
||
}
|
||
|
||
// 远程分支 CI 跑过才能继续
|
||
spinner.succeed(`远程分支 CI 已通过,准备开始发布`);
|
||
new Notifier().notify({
|
||
title: '✅ 准备发布到 npm',
|
||
message: '测试用例执行完毕,快回来输入 npm 校验码了!',
|
||
sound: 'Crystal',
|
||
});
|
||
process.exit(0);
|
||
};
|
||
|
||
runPrePublish();
|