Skip to content

Commit

Permalink
fix(auto-release): fix release content empty (#552)
Browse files Browse the repository at this point in the history
* fix(auto-release):  fix release content empty

* chore: adjustment auto-release

* chore: format code

* chore: add log

* chore: update package description

---------

Co-authored-by: wū yāng <[email protected]>
  • Loading branch information
liweijie0812 and uyarn authored Nov 8, 2024
1 parent 03d6b32 commit d9da15c
Show file tree
Hide file tree
Showing 10 changed files with 794 additions and 334 deletions.
17 changes: 8 additions & 9 deletions .github/workflows/auto-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Node
uses: actions/setup-node@v4
- uses: actions/setup-node@v4
with:
# https://nodejs.org/en/learn/typescript/run-natively#running-typescript-code-with-nodejs
node-version: "22.6.0"
# https://docs.npmjs.com/cli/v7/using-npm/config#strict-peer-deps
node-version: 18
- uses: pnpm/action-setup@v4
with:
version: 9
- name: Install Dependencies
run: npm install @actions/github --legacy-peer-deps
- name: run JavaScript
run: pnpm install
- name: run auto release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
node --experimental-strip-types scripts/auto-release.ts
run: pnpm run auto-release
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"site": "npm run build:components && cd site && npm run build",
"site:intranet": "npm run build:components && cd site && npm run site:intranet",
"site:preview": "npm run build:components && cd site && npm run site:preview",
"auto-release": "pnpm run --filter 'tdesign-auto-release' start",
"prepare": "husky install"
},
"license": "MIT",
Expand All @@ -29,6 +30,7 @@
"husky": "^8.0.0",
"lint-staged": "8.1.5",
"prettier": "^2.8.8",
"rimraf": "^3.0.2",
"stylelint": "^15.10.3",
"stylelint-config-standard": "^34.0.0",
"stylelint-config-standard-less": "^2.0.0"
Expand Down
8 changes: 8 additions & 0 deletions packages/auto-release/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
root: true,
extends: ['plugin:@typescript-eslint/recommended'],
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
ignorePatterns: ['dist', 'node_modules'],
rules: {},
};
28 changes: 28 additions & 0 deletions packages/auto-release/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "tdesign-auto-release",
"private": true,
"version": "1.0.0",
"description": "the project for TDesign weekly versions collection and auto release",
"main": "dist/index.js",
"scripts": {
"prebuild": "rimraf dist/*",
"dev": "nodemon --watch 'src/**/*.ts' --exec ts-node src/index.ts",
"start": "ts-node src/index.ts",
"lint": "eslint --ext .ts src",
"build": "tsc",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@actions/github": "^6.0.0"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.6.0",
"@typescript-eslint/parser": "^5.6.0",
"nodemon": "^3.1.7",
"ts-node": "^10.9.2",
"typescript": "^4.9.5"
}
}
42 changes: 42 additions & 0 deletions packages/auto-release/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
export const submodules = {
'tdesign-vue': {
title: '## Vue2 for Web 发布',
changelogUrl: 'https://raw.githubusercontent.com/Tencent/tdesign-vue/refs/heads/main/CHANGELOG.md',
},
'tdesign-vue-next': {
title: '## Vue3 for Web 发布',
changelogUrl: 'https://raw.githubusercontent.com/Tencent/tdesign-vue-next/refs/heads/main/CHANGELOG.md',
},
'tdesign-react': {
title: '## React for Web 发布 ',
changelogUrl: 'https://raw.githubusercontent.com/Tencent/tdesign-react/refs/heads/main/CHANGELOG.md',
},
'tdesign-miniprogram': {
title: '## Miniprogram for WeChat 发布',
changelogUrl: 'https://raw.githubusercontent.com/Tencent/tdesign-miniprogram/refs/heads/main/CHANGELOG.md',
},
'tdesign-mobile-vue': {
title: '## Vue3 for Mobile 发布',
changelogUrl: 'https://raw.githubusercontent.com/Tencent/tdesign-mobile-vue/refs/heads/main/CHANGELOG.md',
},
'tdesign-flutter': {
title: '## Flutter for Mobile 发布',
changelogUrl: 'https://raw.githubusercontent.com/Tencent/tdesign-flutter/refs/heads/main/tdesign-site/CHANGELOG.md',
},
'tdesign-mobile-react': {
title: '## React for Mobile 发布',
changelogUrl: 'https://raw.githubusercontent.com/Tencent/tdesign-mobile-react/refs/heads/main/CHANGELOG.md',
},
};

export type SubmoduleItem = keyof typeof submodules;

export const submodulesKeys: SubmoduleItem[] = [
'tdesign-vue',
'tdesign-vue-next',
'tdesign-react',
'tdesign-miniprogram',
'tdesign-mobile-vue',
'tdesign-mobile-react',
'tdesign-flutter',
];
47 changes: 47 additions & 0 deletions packages/auto-release/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { submodules, submodulesKeys } from './constants';
import { createRelease, getProjectDesc, getVersionContent, getReleaseData, getReleaseContent } from './utils';

// 开始执行
const { tag, title, START_DATE, END_DATE } = getReleaseData();
const fetchPromises = submodulesKeys.map(async (project) => {
console.log(`开始处理${project}项目`);
const changelogUrl = submodules[project].changelogUrl;
console.log(changelogUrl);
// 读取 CHANGELOG.md 文件内容
const response = await fetch(changelogUrl);
const changelogContent = await response.text();

// 使用正则表达式提取版本标题行,确保以"##"开头并以日期结尾
const versions = changelogContent.match(/^##.+?`\d{4}-\d{2}-\d{2}`\s*$/gm);

const projectOutput: string[] = [];

versions?.forEach((versionLine) => {
const dateMatch = versionLine.match(/\d{4}-\d{2}-\d{2}/);
if (dateMatch) {
const versionContent = getVersionContent(versionLine, changelogContent);
const tag = versionLine.match(/\d+\.\d+\.\d+/);
if (!tag) {
throw new Error('tag is null');
}

const date = new Date(dateMatch[0]);
// 检查日期是否在指定范围内,[2024-06-01, 2024-06-07] 闭区间
if (date >= new Date(START_DATE) && date <= new Date(END_DATE)) {
const desc = getProjectDesc(project, tag[0], submodules[project]['title'], '详情见:');
projectOutput.push(`${desc[0]}\n${versionContent}\n${desc[1]}\n`);
}
}
});

return projectOutput.join('\n');
});

Promise.all(fetchPromises).then((projectOutputs) => {
const output = getReleaseContent(projectOutputs);

console.log('output', output);

// 发布release
createRelease(tag, title, output);
});
135 changes: 135 additions & 0 deletions packages/auto-release/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import * as github from '@actions/github';
import { SubmoduleItem } from './constants';

/**
* @description 获取内容
* @param {string} version
*/
export function getVersionContent(version: string, markdown: string) {
const regex = new RegExp(`(${version}.*?(?=## 🌈|$))`, 's');
const match = markdown.match(regex);

if (match) {
return match[0];
} else {
return 'Version Content not found';
}
}

/**
* @description 获取项目具体详情描述地址
* @example:
* ['## Vue3 for Web 发布 [1.10.1,1.10.1](https://github.com/Tencent/tdesign-vue-next/releases/tag/1.10.1)'
* '详情见: https://github.com/Tencent/tdesign-vue-next/releases/tag/1.10.0'
* ]
* @param {string} project
* @param {string} tag
* @param {string} prefixDesc
* @param {string} suffixDesc
*/
export function getProjectDesc(project: SubmoduleItem, tag: string, prefixDesc: string, suffixDesc: string) {
return [
`${prefixDesc} [${tag}](https://github.com/Tencent/${project}/releases/tag/${tag})`,
`${suffixDesc} https://github.com/Tencent/${project}/releases/tag/${tag}`,
];
}

export function getReleaseData() {
/**
* @description 检查日期是否在指定日期范围内
* 在每个月的1号、8号、15号、22号生成
* 1号生成上个月的22号到最后一天的版本
* 8号生成这个月的1号到7号的版本
* 15号生成这个月的8号到14号的版本
* 22号生成这个月的15号到21号的版本
*/

let START_DATE = '';
let END_DATE = '';

const today = new Date();

// 获取昨天的日期并且只取年月日格式化成'yyyy-mm-dd'
const someDaysAgo = new Date(today);
if (today.getDate() === 1) {
// 获取上个月的22号到最后一天的版本
START_DATE = new Date(today.getFullYear(), today.getMonth() - 1, 22).toISOString().split('T')[0];
END_DATE = new Date(someDaysAgo.setDate(today.getDate() - 1)).toISOString().split('T')[0];
} else {
START_DATE = new Date(someDaysAgo.setDate(today.getDate() - 7)).toISOString().split('T')[0];
END_DATE = new Date(someDaysAgo.setDate(today.getDate() - 1)).toISOString().split('T')[0];
}
console.log(`今天是:${today},开始日期:${START_DATE},结束日期:${END_DATE}`);

// 标题
const monthShort = today.toLocaleString('en-US', { month: 'short' });
const year = today.getFullYear();
let times = '1st';

console.log('today', today.getDate());
switch (today.getDate()) {
case 1:
times = '4th';
break;
case 8:
times = '1st';
break;
case 15:
times = '2nd';
break;
case 22:
times = '3rd';
break;
default:
times = '1st';
break;
}

const tag = `v${today.getFullYear()}.${today.getMonth() + 1}.${today.getDate()}`;
const title = `TDesign Weekly Release (${monthShort} ${times} ${year})`;

console.log('tag', tag);
console.log('title', title);

return {
tag,
title,
START_DATE,
END_DATE,
};
}

export function getReleaseContent(projectOutputs: string[]) {
return projectOutputs.filter((output) => output.trim() != '').join('\n');
}

export function createRelease(tag: string, title: string, content: string) {
const githubToken = process.env.GITHUB_TOKEN || '';
const isCI = process.env.CI;
if (content === '') {
console.log('没有需要发布的内容');
return;
}
if (!githubToken && isCI) {
throw new Error('GITHUB_TOKEN is undefined');
}

if (isCI) {
const octokit = github.getOctokit(githubToken);
console.log('身份验证成功');
try {
octokit.rest.repos.createRelease({
owner: 'Tencent',
repo: 'tdesign',
tag_name: tag, // 添加缺少的 tag_name 参数
name: title, // 可选参数,使用 title 作为发布名称
body: content, // 可选参数,发布说明内容
draft: true, // 可选参数,是否为草稿
});
} catch (error) {
console.error('Error:', error);
}
} else {
console.log('非CI环境,不运行创建 github release');
}
}
15 changes: 15 additions & 0 deletions packages/auto-release/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"target": "es6",
"baseUrl": "./",
"rootDir": "./src",
"module": "commonjs",
"strict": true,
"noImplicitAny": false,
"useUnknownInCatchVariables": false,
"outDir": "./dist",
"sourceMap": false,
"esModuleInterop": true
},
"include": ["src"],
}
Loading

0 comments on commit d9da15c

Please sign in to comment.