brendmanager/src/deploy-status.js
Peppa 4e50436d60
All checks were successful
peppa/lint lint: success за 1с
peppa/test test: success за 1с
ci / checks (pull_request) Successful in 11s
deploy / deploy (pull_request) Successful in 29s
BRM-68: на главной вместо «Обновлений пока не было» — время запуска версии; тесты
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-17 17:09:03 +05:00

56 lines
2.7 KiB
JavaScript
Raw 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.

// Статус выкладки версии для проверки (BRM-32): после каждой принятой задачи скрипт деплоя
// сообщает сюда итог. Хранится в томе данных, чтобы пережить перезапуск и откат.
import fs from "node:fs";
import path from "node:path";
import crypto from "node:crypto";
const ALMATY_OFFSET_MS = 5 * 3600e3;
// BRM-68: когда запущена эта версия — запасной ответ, если записи об обновлении ещё нет.
const STARTED_AT = new Date().toISOString();
export const STATUSES = ["ok", "failed"];
// «16.11.2026 14:05» по Алматы (UTC+5), как в расписании сводок.
export function almatyTime(iso) {
const d = new Date(Date.parse(iso) + ALMATY_OFFSET_MS);
if (Number.isNaN(d.getTime())) return "";
const p = (n) => String(n).padStart(2, "0");
return `${p(d.getUTCDate())}.${p(d.getUTCMonth() + 1)}.${d.getUTCFullYear()} ${p(d.getUTCHours())}:${p(d.getUTCMinutes())}`;
}
const clean = (s, max) => String(s ?? "").replace(/\s+/g, " ").trim().slice(0, max);
export const tokenOk = (given, secret) => {
if (!secret || typeof given !== "string") return false;
const a = crypto.createHash("sha256").update(given).digest();
const b = crypto.createHash("sha256").update(secret).digest();
return crypto.timingSafeEqual(a, b);
};
export function createDeployStatus(file) {
let data = { last: null, lastOk: null };
try { data = { ...data, ...JSON.parse(fs.readFileSync(file, "utf8")) }; } catch { /* пока пусто */ }
const save = () => {
fs.mkdirSync(path.dirname(file), { recursive: true });
fs.writeFileSync(file, JSON.stringify(data, null, 2));
};
return {
// body: { status: ok|failed, task: "BRM-32", title, reason }
record(body, now = new Date().toISOString()) {
if (!STATUSES.includes(body?.status)) return { err: "err.deploy.status" };
const task = clean(body.task, 20);
if (task && !/^[A-Z]+-\d+$/.test(task)) return { err: "err.deploy.task" };
const rec = { status: body.status, task, title: clean(body.title, 160), at: now };
if (body.status === "failed") rec.reason = clean(body.reason, 200);
data.last = rec;
if (rec.status === "ok") data.lastOk = rec;
save();
return { record: rec };
},
// Что показывать на главной: последняя выкладка и что сейчас видно по ссылке.
view() {
const f = (r) => r && { ...r, atText: almatyTime(r.at) };
return { last: f(data.last), shown: f(data.lastOk), running: { at: STARTED_AT, atText: almatyTime(STARTED_AT) } };
},
};
}