refactor: improve code formatting and readability in autobuild and telegram scripts

This commit is contained in:
Tunglies
2025-08-21 21:23:38 +08:00
parent 435318cf1d
commit 2277d7232e
3 changed files with 48 additions and 23 deletions

View File

@@ -459,7 +459,13 @@ jobs:
notify-telegram: notify-telegram:
name: Notify Telegram name: Notify Telegram
runs-on: ubuntu-latest runs-on: ubuntu-latest
needs: [update_tag, autobuild-x86-windows-macos-linux, autobuild-arm-linux, autobuild-x86-arm-windows_webview2] needs:
[
update_tag,
autobuild-x86-windows-macos-linux,
autobuild-arm-linux,
autobuild-x86-arm-windows_webview2,
]
steps: steps:
- name: Checkout repository - name: Checkout repository
uses: actions/checkout@v4 uses: actions/checkout@v4

View File

@@ -53,7 +53,9 @@ function getGitShortCommit() {
*/ */
function getLatestTauriCommit() { function getLatestTauriCommit() {
try { try {
const fullHash = execSync("bash ./scripts-workflow/get_latest_tauri_commit.bash") const fullHash = execSync(
"bash ./scripts-workflow/get_latest_tauri_commit.bash",
)
.toString() .toString()
.trim(); .trim();
return execSync(`git rev-parse --short ${fullHash}`).toString().trim(); return execSync(`git rev-parse --short ${fullHash}`).toString().trim();

View File

@@ -10,14 +10,19 @@ async function sendTelegramNotification() {
throw new Error("TELEGRAM_BOT_TOKEN is required"); throw new Error("TELEGRAM_BOT_TOKEN is required");
} }
const version = process.env.VERSION || (() => { const version =
process.env.VERSION ||
(() => {
const pkg = readFileSync("package.json", "utf-8"); const pkg = readFileSync("package.json", "utf-8");
return JSON.parse(pkg).version; return JSON.parse(pkg).version;
})(); })();
const downloadUrl = process.env.DOWNLOAD_URL || `https://github.com/clash-verge-rev/clash-verge-rev/releases/download/v${version}`; const downloadUrl =
process.env.DOWNLOAD_URL ||
`https://github.com/clash-verge-rev/clash-verge-rev/releases/download/v${version}`;
const isAutobuild = process.env.BUILD_TYPE === "autobuild" || version.includes("autobuild"); const isAutobuild =
process.env.BUILD_TYPE === "autobuild" || version.includes("autobuild");
const chatId = isAutobuild ? CHAT_ID_TEST : CHAT_ID_RELEASE; const chatId = isAutobuild ? CHAT_ID_TEST : CHAT_ID_RELEASE;
const buildType = isAutobuild ? "滚动更新版" : "正式版"; const buildType = isAutobuild ? "滚动更新版" : "正式版";
@@ -39,7 +44,7 @@ async function sendTelegramNotification() {
function convertMarkdownToTelegramHTML(content) { function convertMarkdownToTelegramHTML(content) {
return content return content
.split("\n") .split("\n")
.map(line => { .map((line) => {
if (line.trim().length === 0) { if (line.trim().length === 0) {
return ""; return "";
} else if (line.startsWith("## ")) { } else if (line.startsWith("## ")) {
@@ -49,8 +54,14 @@ async function sendTelegramNotification() {
} else if (line.startsWith("#### ")) { } else if (line.startsWith("#### ")) {
return `<b>${line.replace("#### ", "")}</b>`; return `<b>${line.replace("#### ", "")}</b>`;
} else { } else {
let processedLine = line.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2">$1</a>'); let processedLine = line.replace(
processedLine = processedLine.replace(/\*\*([^*]+)\*\*/g, '<b>$1</b>'); /\[([^\]]+)\]\(([^)]+)\)/g,
'<a href="$2">$1</a>',
);
processedLine = processedLine.replace(
/\*\*([^*]+)\*\*/g,
"<b>$1</b>",
);
return processedLine; return processedLine;
} }
}) })
@@ -64,7 +75,9 @@ async function sendTelegramNotification() {
// 发送到 Telegram // 发送到 Telegram
try { try {
await axios.post(`https://api.telegram.org/bot${process.env.TELEGRAM_BOT_TOKEN}/sendMessage`, { await axios.post(
`https://api.telegram.org/bot${process.env.TELEGRAM_BOT_TOKEN}/sendMessage`,
{
chat_id: chatId, chat_id: chatId,
text: content, text: content,
link_preview_options: { link_preview_options: {
@@ -73,10 +86,14 @@ async function sendTelegramNotification() {
prefer_large_media: true, prefer_large_media: true,
}, },
parse_mode: "HTML", parse_mode: "HTML",
}); },
);
log_success(`✅ Telegram 通知发送成功到 ${chatId}`); log_success(`✅ Telegram 通知发送成功到 ${chatId}`);
} catch (error) { } catch (error) {
log_error(`❌ Telegram 通知发送失败到 ${chatId}:`, error.response?.data || error.message); log_error(
`❌ Telegram 通知发送失败到 ${chatId}:`,
error.response?.data || error.message,
);
process.exit(1); process.exit(1);
} }
} }