CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 | /**
* Block N1 — Enable auto-merge on a single repo + branch pattern.
*
* Operator-driven, single-shot bootstrap script. After PR #62 lands and
* the K3 autopilot sweep is live, the operator flips one repo into
* "auto-merge mode" with:
*
* bun run scripts/enable-auto-merge.ts ccantynz/Gluecron.com
* bun run scripts/enable-auto-merge.ts ccantynz/Gluecron.com release/*
*
* Behaviour (idempotent):
* 1. Resolve repo by `owner/name`. Fail loudly if not found.
* 2. If a `branch_protection` row already exists for (repo, pattern):
* flip `enableAutoMerge=true` (only that field — everything else
* stays as the owner configured it).
* 3. If no row exists: INSERT a new row with the safety defaults below,
* with `enableAutoMerge=true`.
* 4. Print a before / after diff so the operator sees exactly what
* changed.
* 5. Write an `auto_merge.enabled_on_main` audit row so we can trace
* who flipped the switch.
*
* Safety defaults on a fresh insert:
* - pattern = the supplied branch (default `main`)
* - requireGreenGates = true (no flaky tests slipping through)
* - requireAiApproval = true (Claude must approve — that's the point)
* - requireHumanReview = false (otherwise auto-merge can't fire)
* - requiredApprovals = 0
* - enableAutoMerge = true
* - dismissStaleReviews = false (we don't want stale-review churn)
* - requirePullRequest = true
* - allowForcePush = false
* - allowDeletion = false
*
* Revert: re-run with `--off`, or hand-toggle the column.
*
* Reads DATABASE_URL from env. The pure orchestrator `runEnableAutoMerge`
* is exported and takes a DB-shaped dependency so tests can drive every
* branch without touching Neon.
*/
import { and, eq } from "drizzle-orm";
import {
branchProtection,
repositories,
users,
auditLog,
} from "../src/db/schema";
import type { BranchProtection } from "../src/db/schema";
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
export interface EnableAutoMergeArgs {
ownerSlash: string; // "owner/name"
pattern: string; // e.g. "main" or "release/*"
off?: boolean; // when true, set enableAutoMerge=false instead
actorUserId?: string | null; // optional audit attribution
}
export interface EnableAutoMergeResult {
action: "inserted" | "updated" | "noop";
before: BranchProtection | null;
after: BranchProtection;
auditWritten: boolean;
}
/**
* Minimal DB surface this script needs. Mirrors the Drizzle methods used
* directly so tests can supply a fake without standing up a real Drizzle
* instance.
*/
export interface DbLike {
select: (...args: any[]) => any;
insert: (...args: any[]) => any;
update: (...args: any[]) => any;
}
// ---------------------------------------------------------------------------
// Core orchestrator
// ---------------------------------------------------------------------------
const SAFETY_DEFAULTS = {
requirePullRequest: true,
requireGreenGates: true,
requireAiApproval: true,
requireHumanReview: false,
requiredApprovals: 0,
allowForcePush: false,
allowDeletion: false,
dismissStaleReviews: false,
} as const;
/**
* Resolve a repo row by `owner/name`. Returns null when either the owner
* or the repo is missing — the caller turns that into a clean exit code.
*/
export async function resolveRepo(
db: DbLike,
ownerSlash: string
): Promise<{ id: string; ownerId: string; name: string; defaultBranch: string } | null> {
const slashIdx = ownerSlash.indexOf("/");
if (slashIdx <= 0 || slashIdx === ownerSlash.length - 1) return null;
const ownerName = ownerSlash.slice(0, slashIdx);
const repoName = ownerSlash.slice(slashIdx + 1);
const ownerRows = await db
.select({ id: users.id })
.from(users)
.where(eq(users.username, ownerName))
.limit(1);
const owner = (ownerRows as Array<{ id: string }>)[0];
if (!owner) return null;
const repoRows = await db
.select({
id: repositories.id,
ownerId: repositories.ownerId,
name: repositories.name,
defaultBranch: repositories.defaultBranch,
})
.from(repositories)
.where(
and(eq(repositories.ownerId, owner.id), eq(repositories.name, repoName))
)
.limit(1);
const repo = (repoRows as Array<{
id: string;
ownerId: string;
name: string;
defaultBranch: string;
}>)[0];
return repo ?? null;
}
/**
* Pure orchestrator. All DB access goes through `db` so tests can inject
* a fake. Returns the before/after rows so the CLI can render a diff and
* the test suite can assert on the transition.
*/
export async function runEnableAutoMerge(
db: DbLike,
args: EnableAutoMergeArgs,
audit: (opts: {
userId?: string | null;
repositoryId?: string | null;
action: string;
targetType?: string;
targetId?: string;
metadata?: Record<string, unknown>;
}) => Promise<void>
): Promise<EnableAutoMergeResult> {
const repo = await resolveRepo(db, args.ownerSlash);
if (!repo) {
throw new Error(
`Repository not found: ${args.ownerSlash}. Pass owner/name (e.g. ccantynz/Gluecron.com).`
);
}
const desiredEnableAutoMerge = !args.off;
// Look up existing rule for this (repo, pattern).
const existingRows = await db
.select()
.from(branchProtection)
.where(
and(
eq(branchProtection.repositoryId, repo.id),
eq(branchProtection.pattern, args.pattern)
)
)
.limit(1);
const existing = (existingRows as BranchProtection[])[0] ?? null;
if (existing) {
// Idempotent: when the row is already in the desired state, skip
// both the UPDATE and the audit write. Operators running the script
// twice in a row should see "no-op", not a noisy audit trail.
if (existing.enableAutoMerge === desiredEnableAutoMerge) {
return {
action: "noop",
before: existing,
after: existing,
auditWritten: false,
};
}
// Snapshot BEFORE the mutation — `existing` may be the same row
// reference the DB layer hands to `update().set()`, so we copy here
// to keep the returned `before` field stable for callers.
const beforeSnapshot: BranchProtection = { ...existing };
const now = new Date();
await db
.update(branchProtection)
.set({ enableAutoMerge: desiredEnableAutoMerge, updatedAt: now })
.where(eq(branchProtection.id, existing.id));
const after: BranchProtection = {
...beforeSnapshot,
enableAutoMerge: desiredEnableAutoMerge,
updatedAt: now,
};
await audit({
userId: args.actorUserId ?? null,
repositoryId: repo.id,
action: desiredEnableAutoMerge
? "auto_merge.enabled_on_main"
: "auto_merge.disabled_on_main",
targetType: "branch_protection",
targetId: existing.id,
metadata: {
pattern: args.pattern,
before: { enableAutoMerge: beforeSnapshot.enableAutoMerge },
after: { enableAutoMerge: desiredEnableAutoMerge },
},
});
return {
action: "updated",
before: beforeSnapshot,
after,
auditWritten: true,
};
}
// No existing row — INSERT a new one with the documented safety
// defaults plus the requested auto-merge bit.
const inserted = await db
.insert(branchProtection)
.values({
repositoryId: repo.id,
pattern: args.pattern,
...SAFETY_DEFAULTS,
enableAutoMerge: desiredEnableAutoMerge,
})
.returning();
const after = (inserted as BranchProtection[])[0];
if (!after) {
throw new Error(
"INSERT into branch_protection returned no row — refusing to record audit."
);
}
await audit({
userId: args.actorUserId ?? null,
repositoryId: repo.id,
action: desiredEnableAutoMerge
? "auto_merge.enabled_on_main"
: "auto_merge.disabled_on_main",
targetType: "branch_protection",
targetId: after.id,
metadata: {
pattern: args.pattern,
created: true,
defaults: { ...SAFETY_DEFAULTS, enableAutoMerge: desiredEnableAutoMerge },
},
});
return {
action: "inserted",
before: null,
after,
auditWritten: true,
};
}
// ---------------------------------------------------------------------------
// Diff renderer (pure, test-friendly)
// ---------------------------------------------------------------------------
const TRACKED_FIELDS: Array<keyof BranchProtection> = [
"pattern",
"requirePullRequest",
"requireGreenGates",
"requireAiApproval",
"requireHumanReview",
"requiredApprovals",
"allowForcePush",
"allowDeletion",
"dismissStaleReviews",
"enableAutoMerge",
];
export function renderDiff(
before: BranchProtection | null,
after: BranchProtection
): string {
const lines: string[] = [];
if (!before) {
lines.push("(no previous branch_protection row)");
lines.push("AFTER:");
for (const f of TRACKED_FIELDS) {
lines.push(` + ${f} = ${JSON.stringify((after as any)[f])}`);
}
return lines.join("\n");
}
lines.push("BEFORE -> AFTER (only changed fields shown):");
let anyChanged = false;
for (const f of TRACKED_FIELDS) {
const b = (before as any)[f];
const a = (after as any)[f];
if (JSON.stringify(b) !== JSON.stringify(a)) {
anyChanged = true;
lines.push(` ~ ${f}: ${JSON.stringify(b)} -> ${JSON.stringify(a)}`);
}
}
if (!anyChanged) lines.push(" (no field changes)");
return lines.join("\n");
}
// ---------------------------------------------------------------------------
// CLI entry
// ---------------------------------------------------------------------------
function usage(): never {
console.error(
"Usage: bun run scripts/enable-auto-merge.ts <owner/name> [pattern] [--off]"
);
console.error(
" pattern defaults to 'main'. Pass --off to flip the switch back to disabled."
);
process.exit(2);
}
async function main() {
const argv = process.argv.slice(2);
if (argv.length === 0) usage();
let off = false;
const positional: string[] = [];
for (const a of argv) {
if (a === "--off") {
off = true;
} else if (a.startsWith("--")) {
console.error(`Unknown flag: ${a}`);
usage();
} else {
positional.push(a);
}
}
const [ownerSlash, patternArg] = positional;
if (!ownerSlash) usage();
const pattern = patternArg ?? "main";
// Lazy import the real DB only at CLI-entry time so unit tests can
// import this module without booting a Neon connection.
const { db } = await import("../src/db");
const { audit } = await import("../src/lib/notify");
const result = await runEnableAutoMerge(
db as unknown as DbLike,
{ ownerSlash, pattern, off },
audit
);
console.log(`gluecron enable-auto-merge — ${ownerSlash} @ ${pattern}`);
console.log(`action: ${result.action}`);
console.log(renderDiff(result.before, result.after));
if (result.auditWritten) {
console.log(
`audit: wrote ${off ? "auto_merge.disabled_on_main" : "auto_merge.enabled_on_main"}`
);
} else {
console.log("audit: skipped (no-op, row already in desired state)");
}
if (result.action === "inserted") {
console.log(
"note: created a fresh branch_protection row with the safety defaults."
);
}
}
// Only run when invoked as a script (not when imported by tests).
if (import.meta.main) {
main().catch((err) => {
console.error(err instanceof Error ? err.message : String(err));
process.exit(1);
});
}
// Silence unused-import warning when this module is only used as a CLI.
void auditLog;
|