Files
2026-11-16 17:26:44 -08:00

91 lines
3.3 KiB
TypeScript

#!/usr/bin/env bun
/**
* Drive the server-side Bits MR pipeline end-to-end against a real Meego ticket.
* Usage:
* bun scripts/test_bits_mr.ts <meegoId> [sourceBranch]
*
* Looks up the task in ~/.streamline/data.sqlite when sourceBranch is omitted.
* Prints every Bits API step + reviewer roster so partial failures are visible.
*/
import { Database } from "bun:sqlite";
import { homedir } from "node:os";
import path from "node:path";
import { fetchMeegoUserContacts } from "../control-server/src/meego";
import { submitMrForFix, composeMrTitle, emailPrefix, pickBitsUser, firstEmailWatcher, BITS_FIXED_OPERATOR } from "../control-server/src/bits";
import type { Platform } from "../control-server/src/types";
function arg(i: number): string | undefined {
return Bun.argv[i + 2];
}
const meegoId = arg(0) || "7321935078";
let sourceBranch = arg(1);
let title: string | null = null;
let meegoUrl = `https://meego.larkoffice.com/tiktok/issue/detail/${meegoId}`;
let platform: Platform = "android";
let creatorEmail: string | null = null;
let watchers: string[] = [];
if (!sourceBranch) {
const dbPath = path.join(homedir(), ".streamline", "data.sqlite");
const db = new Database(dbPath, { readonly: true });
const row = db
.query("SELECT meegoUrl, title, branchName, platform, creatorEmail, watchers FROM tasks WHERE meegoId = $m")
.get({ $m: meegoId }) as { meegoUrl: string; title: string; branchName: string; platform: Platform; creatorEmail: string | null; watchers: string | null } | null;
db.close();
if (!row?.branchName) {
console.error(`No branchName found for meegoId=${meegoId} in ${dbPath}`);
process.exit(2);
}
sourceBranch = row.branchName;
title = row.title;
meegoUrl = row.meegoUrl;
platform = row.platform;
creatorEmail = row.creatorEmail;
try { watchers = JSON.parse(row.watchers || "[]"); } catch { watchers = []; }
}
const composedTitle = composeMrTitle(title || `Meego ${meegoId}`);
console.log("=== Inputs ===");
console.log(JSON.stringify({ meegoId, meegoUrl, sourceBranch, title, composedTitle, fixedOperator: BITS_FIXED_OPERATOR }, null, 2));
console.log("\n=== fetchMeegoUserContacts ===");
const contacts = await fetchMeegoUserContacts(meegoId, meegoUrl);
console.log(JSON.stringify({
...contacts,
assigneePrefix: emailPrefix(contacts.assignee),
reporterPrefix: emailPrefix(contacts.reporter),
}, null, 2));
const bitsUser = pickBitsUser([contacts.assignee, creatorEmail, firstEmailWatcher(watchers)]);
console.log(`bits-user (MR owner): ${bitsUser}`);
async function runOnce(label: string) {
console.log(`\n=== ${label} ===`);
const result = await submitMrForFix({
platform,
sourceBranch: sourceBranch!,
targetBranch: "develop",
title: composedTitle,
mrType: "bug",
meegoTaskId: meegoId,
assigneeEmail: contacts.assignee,
reporterEmail: contacts.reporter,
bitsUser,
});
console.log(JSON.stringify(result, null, 2));
return result;
}
const first = await runOnce("submitMrForFix · attempt #1");
const second = await runOnce("submitMrForFix · attempt #2 (duplicate path)");
console.log("\n=== Summary ===");
console.table([
{ attempt: 1, ok: first.ok, mrId: first.mrId, failed: first.steps.filter((s) => !s.ok).map((s) => s.step).join(",") || "-" },
{ attempt: 2, ok: second.ok, mrId: second.mrId, failed: second.steps.filter((s) => !s.ok).map((s) => s.step).join(",") || "-" },
]);