File size: 8,839 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
import { spawn } from 'child_process';
import path from 'path';
import { input, select } from '@inquirer/prompts';
import chalk from 'chalk';
import fs from 'fs-extra';
import fetch from 'isomorphic-fetch';
import jQuery from 'jquery';
import jsdom from 'jsdom';
import openWindow from 'open';
import simpleGit from 'simple-git';
const { JSDOM } = jsdom;
const { window } = new JSDOM();
const { document } = new JSDOM('').window;
global.document = document;
const $ = jQuery<jsdom.DOMWindow>(window) as unknown as JQueryStatic;
const QUERY_TITLE = '.gh-header-title .js-issue-title';
const QUERY_DESCRIPTION_LINES = '.comment-body table tbody tr';
const QUERY_AUTHOR = '.pull-discussion-timeline .TimelineItem:first .author:first';
// https://github.com/orgs/ant-design/teams/ant-design-collaborators/members
// no need filter: https://github.com/ant-design/ant-design/pull/49878#issuecomment-2227853899
const MAINTAINERS = [
// 'zombiej',
// 'afc163',
// 'chenshuai2144',
// 'shaodahong',
// 'xrkffgg',
// 'AshoneA',
// 'yesmeck',
// 'bang88',
// 'yoyo837',
// 'hengkx',
// 'Rustin-Liu',
// 'fireairforce',
// 'kerm1it',
// 'madccc',
// 'MadCcc',
// 'li-jia-nan',
// 'kiner-tang',
// 'Wxh16144',
].map((author: string) => author.toLowerCase());
const cwd = process.cwd();
const git = simpleGit(cwd);
interface Line {
text: string;
element: JQuery<HTMLElement>;
}
interface PR {
pr?: string;
hash: string;
title: string;
author: string;
english: string;
chinese: string;
}
const getDescription = (entity?: Line): string => {
if (!entity) {
return '';
}
const descEle = entity.element.find('td:last');
let htmlContent = descEle.html();
htmlContent = htmlContent.replace(/<code class="notranslate">([^<]*)<\/code>/g, '`$1`');
return htmlContent.trim();
};
async function printLog() {
const tags = await git.tags();
const fromVersion = await select({
message: '๐ท Please choose tag to compare with current branch:',
choices: tags.all
.filter((item) => !item.includes('experimental'))
.filter((item) => !item.includes('alpha'))
.filter((item) => !item.includes('resource'))
.reverse()
.slice(0, 50)
.map((item) => ({ name: item, value: item })),
});
let toVersion = await select({
message: `๐ Please choose branch to compare with ${chalk.magenta(fromVersion)}:`,
choices: ['master', '4.x-stable', '3.x-stable', 'feature', 'custom input โจ๏ธ'].map((i) => ({
name: i,
value: i,
})),
});
if (toVersion.startsWith('custom input')) {
toVersion = await input({
default: 'master',
message: `๐ Please input custom git hash id or branch name to compare with ${chalk.magenta(
fromVersion,
)}:`,
});
}
if (!/\d+\.\d+\.\d+/.test(fromVersion)) {
console.log(chalk.red(`๐คช tag (${chalk.magenta(fromVersion)}) is not valid.`));
}
const logs = await git.log({ from: fromVersion, to: toVersion });
let prList: PR[] = [];
for (let i = 0; i < logs.all.length; i += 1) {
const { message, body, hash, author_name: author } = logs.all[i];
const text = `${message} ${body}`;
const match = text.match(/#\d+/g);
const prs = match?.map((pr) => pr.slice(1)) || [];
const validatePRs: PR[] = [];
console.log(
chalk.green(
`[${i + 1}/${logs.all.length}]`,
hash.slice(0, 6),
'-',
prs.length
? prs.map((pr) => `https://github.com/ant-design/ant-design/pull/${pr}`).join(',')
: '?',
),
);
for (let j = 0; j < prs.length; j += 1) {
const pr = prs[j];
// Use jquery to get full html page since it don't need auth token
let res: Response | undefined;
let html: string | undefined;
let tryTimes = 0;
const timeout = 30000;
const fetchPullRequest = async () => {
try {
res = await new Promise<Response>((resolve, reject) => {
setTimeout(() => {
reject(new Error(`Fetch timeout of ${timeout}ms exceeded`));
}, timeout);
fetch(`https://github.com/ant-design/ant-design/pull/${pr}`)
.then((response) => {
response.text().then((htmlRes) => {
html = htmlRes;
resolve(response);
});
})
.catch(reject);
});
} catch (err) {
tryTimes++;
if (tryTimes < 100) {
console.log(chalk.red(`โ Fetch error, reason: ${err}`));
console.log(chalk.red(`โ๏ธ Retrying...(Retry times: ${tryTimes})`));
await fetchPullRequest();
}
}
};
await fetchPullRequest();
if (res?.url.includes('/issues/')) {
continue;
}
const $html = $(html!);
const prTitle: string = $html.find(QUERY_TITLE).text().trim();
const prAuthor: string = $html.find(QUERY_AUTHOR).text().trim();
const prLines: JQuery<HTMLElement> = $html.find(QUERY_DESCRIPTION_LINES);
const lines: Line[] = [];
prLines.each(function getDesc() {
lines.push({
text: $(this).text().trim(),
element: $(this),
});
});
let english = getDescription(lines.find((line) => line.text.includes('๐บ๐ธ English')));
let chinese = getDescription(lines.find((line) => line.text.includes('๐จ๐ณ Chinese')));
if (/^-*$/.test(english)) {
english = prTitle;
} else {
english = english || chinese || prTitle;
}
if (/^-*$/.test(chinese)) {
chinese = prTitle;
} else {
chinese = chinese || english || prTitle;
}
if (english) {
console.log(` ๐บ๐ธ ${english}`);
}
if (chinese) {
console.log(` ๐จ๐ณ ${chinese}`);
}
validatePRs.push({
pr,
hash,
title: prTitle,
author: prAuthor,
english,
chinese,
});
}
if (validatePRs.length === 1) {
console.log(chalk.cyan(' - Match PR:', `#${validatePRs[0].pr}`));
prList = prList.concat(validatePRs);
} else if (message.includes('docs:')) {
console.log(chalk.cyan(' - Skip document!'));
} else {
console.log(chalk.yellow(' - Miss match!'));
prList.push({
hash,
title: message,
author,
english: message,
chinese: message,
});
}
}
console.log('\n', chalk.green('Done. Here is the log:'));
function printPR(lang: string, postLang: (str: string) => string) {
prList.forEach((entity) => {
const { pr, author, hash, title } = entity;
if (pr) {
const str = postLang(entity[lang as keyof PR]!);
let icon = '';
if (str.toLowerCase().includes('fix') || str.includes('ไฟฎๅค')) {
icon = '๐';
}
if (str.toLowerCase().includes('feat')) {
icon = '๐';
}
let authorText = '';
if (!MAINTAINERS.includes(author.toLowerCase())) {
authorText = ` [@${author}](https://github.com/${author})`;
}
console.log(
`- ${icon} ${str}[#${pr}](https://github.com/ant-design/ant-design/pull/${pr})${authorText}`,
);
} else {
console.log(
`๐ Miss Match: ${title} -> https://github.com/ant-design/ant-design/commit/${hash}`,
);
}
});
}
// Chinese
console.log('\n');
console.log(chalk.yellow('๐จ๐ณ Chinese changelog:'));
console.log('\n');
printPR('chinese', (chinese: string) =>
chinese[chinese.length - 1] === 'ใ' || !chinese ? chinese : `${chinese}ใ`,
);
console.log('\n-----\n');
// English
console.log(chalk.yellow('๐บ๐ธ English changelog:'));
console.log('\n');
printPR('english', (english: string) => {
english = english.trim();
if (english[english.length - 1] !== '.' || !english) {
english = `${english}.`;
}
if (english) {
return `${english} `;
}
return '';
});
// Preview editor generate
// Web source: https://github.com/ant-design/antd-changelog-editor
let html = fs.readFileSync(path.join(__dirname, 'previewEditor', 'template.html'), 'utf8');
html = html.replace('// [Replacement]', `window.changelog = ${JSON.stringify(prList)};`);
fs.writeFileSync(path.join(__dirname, 'previewEditor', 'index.html'), html, 'utf8');
// Start preview
const ls = spawn(
'npx',
['http-server', path.join(__dirname, 'previewEditor'), '-c-1', '-p', '2893'],
{ shell: true },
);
ls.stdout.on('data', (data) => {
console.log(data.toString());
});
console.log(chalk.green('Start changelog preview editor...'));
setTimeout(() => {
openWindow('http://localhost:2893/');
}, 1000);
}
printLog();
|