125 lines
5.7 KiB
TypeScript
125 lines
5.7 KiB
TypeScript
/**
|
|
* Send status-change preview cards through the REAL Streamline bot — the same
|
|
* `@larksuiteoapi/node-sdk` client and LARK_APP_ID/LARK_APP_SECRET that
|
|
* `notify.ts` uses in production. What lands in Lark is then byte-identical to
|
|
* what a real status change will send: same sender identity, same app, same
|
|
* rendering path. (Previewing through some other Lark app only proves the JSON
|
|
* parses — it doesn't prove *this* app can send it.)
|
|
*
|
|
* Deliberately DM-only: it refuses `oc_…` chat ids so a design preview can never
|
|
* be blasted into the shared report group.
|
|
*
|
|
* Usage, from repo root (bun auto-loads .env):
|
|
* bun scripts/send-notify-preview.ts someone@bytedance.com
|
|
* bun scripts/send-notify-preview.ts someone@bytedance.com bug:manual_review feature:dispatched
|
|
* bun scripts/send-notify-preview.ts someone@bytedance.com real:bug:<uuid>
|
|
*
|
|
* With no card names it sends one representative card per category. Card names
|
|
* are the same `<category>:<kind>` ids that `preview-notify-cards.ts` prints.
|
|
*
|
|
* `real:<category>:<id>` pulls the ACTUAL row from the internal read-only API
|
|
* and renders the card the notifier would really send for its current status.
|
|
* Use it whenever the question is "how long/ugly does this get with production
|
|
* text?" — the built-in samples are hand-written fixtures, so their plans and
|
|
* failure reasons are far shorter and tidier than the real thing.
|
|
*/
|
|
import { larkEnabled, sendCard } from "../control-server/src/lark";
|
|
import type { Task, Feature, Security } from "../control-server/src/types";
|
|
import { extractPlainSummary } from "../control-server/src/lark_card_media";
|
|
|
|
/**
|
|
* Previews must render the PRODUCTION link, whatever the local `.env` says.
|
|
*
|
|
* A developer's `.env` points APP_BASE_URL at their own host, so a preview built
|
|
* with it carries a domain no recipient can open — and the reviewer of the
|
|
* preview has no way to tell that apart from a genuine bug in the card. Set
|
|
* before any card module loads. Override with PREVIEW_BASE_URL when you really
|
|
* do want to preview against a local build.
|
|
*/
|
|
const PREVIEW_BASE_URL = process.env.PREVIEW_BASE_URL || "https://streamline.bytedance.net";
|
|
process.env.APP_BASE_URL = PREVIEW_BASE_URL;
|
|
|
|
const {
|
|
buildCard, buildFeatureCard, buildSecurityCard, meta, featureMeta, securityMeta,
|
|
} = await import("../control-server/src/notify_cards");
|
|
|
|
const READONLY_API = (process.env.STREAMLINE_READONLY_API
|
|
|| "https://streamline-internal-api.bytedance.net").replace(/\/+$/, "");
|
|
|
|
/**
|
|
* The 大白话 a real manual_review card would carry, read through the read-only
|
|
* API. Worth the extra request: the fixtures' summary is hand-written, and the
|
|
* summary is now the only content on a review card.
|
|
*/
|
|
async function realPlain(path: string, id: string): Promise<string | null> {
|
|
try {
|
|
const r = await fetch(`${READONLY_API}/api/${path}/${encodeURIComponent(id)}/readable-report`);
|
|
if (!r.ok) return null;
|
|
const rr = await r.json() as { html?: string | null };
|
|
return extractPlainSummary(rr?.html ?? null);
|
|
} catch { return null; }
|
|
}
|
|
|
|
/** Fetch one live row and render its card for whatever status it's currently in. */
|
|
async function realCard(category: string, id: string): Promise<unknown> {
|
|
const path = category === "feature" ? "features" : category === "security" ? "security" : "tasks";
|
|
const res = await fetch(`${READONLY_API}/api/${path}/${encodeURIComponent(id)}`);
|
|
if (!res.ok) throw new Error(`GET /api/${path}/${id} → ${res.status}`);
|
|
const row = await res.json() as Record<string, unknown>;
|
|
const status = String(row.status);
|
|
const extras: { plain?: string | null } = {};
|
|
if (status === "manual_review") {
|
|
extras.plain = await realPlain(path, id);
|
|
console.log(` ${id}: 大白话=${extras.plain ? "有" : "无"}`);
|
|
}
|
|
if (category === "feature") return buildFeatureCard(row as unknown as Feature, featureMeta(status as never), extras);
|
|
if (category === "security") return buildSecurityCard(row as unknown as Security, securityMeta(status as never), extras);
|
|
return buildCard(row as unknown as Task, meta(status as never), extras);
|
|
}
|
|
|
|
const [target, ...names] = process.argv.slice(2);
|
|
|
|
if (!target) {
|
|
console.error("usage: bun scripts/send-notify-preview.ts <email|ou_open_id> [card ...]");
|
|
process.exit(1);
|
|
}
|
|
if (target.startsWith("oc_")) {
|
|
console.error("refusing to send previews to a chat id — DM a person instead (email or ou_…).");
|
|
process.exit(1);
|
|
}
|
|
if (!larkEnabled()) {
|
|
console.error("LARK_APP_ID / LARK_APP_SECRET are unset — nothing to send with (see .env.example).");
|
|
process.exit(1);
|
|
}
|
|
|
|
const picked = names.length
|
|
? names
|
|
: ["bug:pending_confirmation", "bug:manual_review", "bug:fix_failed", "feature:manual_review", "security:dispatched"];
|
|
|
|
// Dynamic import: the card modules read APP_BASE_URL at build time, so the
|
|
// override above has to be in place before they load.
|
|
const { CARDS } = await import("./_notify-preview-cards");
|
|
|
|
const unknown = picked.filter((n) => !CARDS[n] && !n.startsWith("real:"));
|
|
if (unknown.length) {
|
|
console.error(`unknown card(s): ${unknown.join(", ")}`);
|
|
console.error(`available: ${Object.keys(CARDS).join(", ")}`);
|
|
console.error(`or: real:<bug|feature|security>:<id>`);
|
|
process.exit(1);
|
|
}
|
|
|
|
|
|
console.log(`links point at ${PREVIEW_BASE_URL}`);
|
|
const receiveIdType = target.includes("@") ? "email" : "open_id";
|
|
for (const name of picked) {
|
|
try {
|
|
const card = name.startsWith("real:")
|
|
? await realCard(name.split(":")[1]!, name.split(":").slice(2).join(":"))
|
|
: CARDS[name]!();
|
|
const ok = await sendCard(receiveIdType, target, card);
|
|
console.log(`${ok ? "sent" : "FAILED"} ${name} → ${target}`);
|
|
} catch (err) {
|
|
console.log(`FAILED ${name} — ${(err as Error).message}`);
|
|
}
|
|
}
|