guanghulab/scripts/bridge/update-queue-status.js
Guanghu Domestic Migration d1e47f4565
Some checks are pending
自动更新代码和重启 / update-and-restart (push) Waiting to run
CI检查 + 自动部署 / check (push) Waiting to run
CI检查 + 自动部署 / deploy (push) Blocked by required conditions
重启聊天服务 / restart (push) Waiting to run
chore: import sanitized domestic snapshot for REPO-002
Source snapshot: ca48d3ddf926d79aa138306164169baf764bb829
2026-07-17 15:54:41 +08:00

118 lines
3.6 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// scripts/bridge/update-queue-status.js
// 🌉 桥接·更新调度队列状态
//
// 将已处理的调度队列任务状态更新为「已完成」
//
// 环境变量:
// NOTION_TOKEN Notion API token
// BRIDGE_QUEUE_DB_ID 桥接调度队列数据库 ID
// QUEUE_FILE check-queue.js 输出的任务列表文件路径
'use strict';
const https = require('https');
const fs = require('fs');
const path = require('path');
const NOTION_VERSION = '2022-06-28';
const NOTION_API_HOSTNAME = 'api.notion.com';
// ══════════════════════════════════════════════════════════
// Notion API
// ══════════════════════════════════════════════════════════
function notionPatch(endpoint, body, token) {
return new Promise((resolve, reject) => {
const payload = JSON.stringify(body);
const opts = {
hostname: NOTION_API_HOSTNAME,
port: 443,
path: endpoint,
method: 'PATCH',
headers: {
'Authorization': 'Bearer ' + token,
'Content-Type': 'application/json',
'Notion-Version': NOTION_VERSION,
'Content-Length': Buffer.byteLength(payload),
},
};
const req = https.request(opts, res => {
let data = '';
res.on('data', chunk => data += chunk);
res.on('end', () => {
try {
const parsed = JSON.parse(data);
if (res.statusCode >= 200 && res.statusCode < 300) {
resolve(parsed);
} else {
reject(new Error(`Notion API ${res.statusCode}: ${parsed.message || data}`));
}
} catch (e) {
reject(new Error(`Notion API parse error: ${data}`));
}
});
});
req.on('error', reject);
req.write(payload);
req.end();
});
}
// ══════════════════════════════════════════════════════════
// 主逻辑
// ══════════════════════════════════════════════════════════
async function main() {
const token = process.env.NOTION_TOKEN;
const queueFile = process.env.QUEUE_FILE;
if (!token) {
console.log('⚠️ 缺少 NOTION_TOKEN跳过状态更新');
process.exit(0);
}
if (!queueFile || !fs.existsSync(queueFile)) {
console.log('📭 无待更新的任务,跳过');
process.exit(0);
}
const tasks = JSON.parse(fs.readFileSync(queueFile, 'utf8'));
if (tasks.length === 0) {
console.log('📭 任务列表为空,跳过');
process.exit(0);
}
console.log(`✏️ 更新 ${tasks.length} 条任务状态为「已完成」…`);
let ok = 0, failed = 0;
for (const task of tasks) {
try {
await notionPatch(`/v1/pages/${task.id}`, {
properties: {
'处理状态': { status: { name: '已完成' } },
},
}, token);
console.log(`${task.task_name} → 已完成`);
ok++;
} catch (e) {
console.error(`${task.task_name} 更新失败: ${e.message}`);
failed++;
}
}
console.log(`\n✅ 状态更新完成 · 成功 ${ok} 条 · 失败 ${failed}`);
if (process.env.GITHUB_OUTPUT) {
fs.appendFileSync(process.env.GITHUB_OUTPUT,
`updated_count=${ok}\n`
);
}
if (failed > 0) process.exit(1);
}
main().catch(e => { console.error(e); process.exit(1); });