#!/usr/bin/env bun /** * One-off: re-submit a single bug's Bits MR through the NEW flow (service-account * token + x-bits-user owner), KEEPING the existing branch, then persist the new * mrUrl and reset the mr-sync fields so the poller picks up the fresh MR. * * Usage: bun scripts/resubmit_mr.ts [--write] * Without --write it does a dry run of the bits-user resolution only. */ 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, pickBitsUser, firstEmailWatcher, emailPrefix } from "../control-server/src/bits"; import type { Platform } from "../control-server/src/types"; const meegoId = Bun.argv[2]; const doWrite = Bun.argv.includes("--write"); if (!meegoId) { console.error("usage: bun scripts/resubmit_mr.ts [--write]"); process.exit(2); } const dbPath = path.join(homedir(), ".streamline", "data.sqlite"); const db = new Database(dbPath); // read-write — we persist below on success const row = db .query("SELECT id, meegoUrl, title, branchName, platform, creatorEmail, watchers, mrUrl FROM tasks WHERE meegoId = $m") .get({ $m: meegoId }) as | { id: string; meegoUrl: string; title: string; branchName: string | null; platform: Platform; creatorEmail: string | null; watchers: string | null; mrUrl: string | null } | null; if (!row) { console.error(`task meegoId=${meegoId} not found in ${dbPath}`); process.exit(2); } if (!row.branchName) { console.error(`task ${row.id} has no branchName — cannot submit`); process.exit(2); } let watchers: string[] = []; try { watchers = JSON.parse(row.watchers || "[]"); } catch { watchers = []; } const contacts = await fetchMeegoUserContacts(meegoId, row.meegoUrl); const bitsUser = pickBitsUser([contacts.assignee, row.creatorEmail, firstEmailWatcher(watchers)]); console.log("=== Re-submit (new flow) ==="); console.log(JSON.stringify({ taskId: row.id, meegoId, platform: row.platform, branch: row.branchName, oldMrUrl: row.mrUrl, assignee: contacts.assignee, assigneePrefix: emailPrefix(contacts.assignee), creatorEmail: row.creatorEmail, firstWatcher: firstEmailWatcher(watchers), resolvedBitsUser: bitsUser, }, null, 2)); if (!doWrite) { console.log("\n(dry run — pass --write to actually create the MR + update db)"); process.exit(0); } const result = await submitMrForFix({ platform: row.platform, sourceBranch: row.branchName, targetBranch: process.env.BASE_BRANCH || "develop", title: composeMrTitle(row.title), mrType: "bug", meegoTaskId: meegoId, assigneeEmail: contacts.assignee, reporterEmail: contacts.reporter, bitsUser, }); console.log("\n=== submitMrForFix result ==="); console.log(JSON.stringify(result, null, 2)); if (result.ok && result.mrUrl) { db.query( "UPDATE tasks SET mrUrl = $u, mrState = NULL, mrMergedAt = NULL, mrSyncedAt = NULL WHERE id = $id", ).run({ $u: result.mrUrl, $id: row.id }); console.log(`\n✅ db updated: tasks.${row.id}.mrUrl = ${result.mrUrl} (mr-sync fields reset)`); } else { console.error("\n❌ create failed — db NOT updated"); process.exit(1); }