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 | /**
* Block K3 — Shared PR merge executor.
*
* Factors the side-effecting merge mechanics out of the
* `POST /:owner/:repo/pulls/:number/merge` HTTP handler in `src/routes/pulls.tsx`
* so the autopilot's `auto-merge-sweep` task can perform a merge without
* replicating route logic. The HTTP handler retains its own gating chain
* (gate checks, branch-protection re-evaluation, error redirects); this
* module only covers the post-decision mechanics:
*
* 1. Run the actual ref update (`git update-ref` for ff/clean merges,
* delegating to `mergeWithAutoResolve` when the K2-style decision
* flagged conflicts and AI conflict-resolution is enabled).
* 2. Flip `pull_requests.state` to `merged`, stamp `mergedAt` / `mergedBy`.
* 3. Run J7 close-keyword scanning — close any referenced open issues in
* the same repo and post the back-link comment.
*
* Pure error-funnel: every failure is returned as `{ok:false, error}`; we
* never throw. Callers decide how to surface the error (HTTP redirect vs.
* audit row).
*
* Intentionally NOT in this file:
* - Gate evaluation / branch-protection (use `evaluateAutoMerge` in K2,
* or the inline chain in the HTTP handler).
* - AI review comment posting (the auto-merge audit/comment is the
* autopilot task's responsibility).
*/
import { and, eq } from "drizzle-orm";
import { db } from "../db";
import {
issueComments,
issues,
pullRequests,
type PullRequest,
} from "../db/schema";
import { getRepoPath } from "../git/repository";
import { mergeWithAutoResolve } from "./merge-resolver";
import { isAiReviewEnabled } from "./ai-review";
import { extractClosingRefsMulti } from "./close-keywords";
import { logActivity } from "./notify";
export interface PerformMergeArgs {
/** Full PR row — we need title/body/baseBranch/headBranch/repositoryId. */
pr: Pick<
PullRequest,
| "id"
| "number"
| "title"
| "body"
| "baseBranch"
| "headBranch"
| "repositoryId"
| "authorId"
| "state"
| "isDraft"
>;
ownerName: string;
repoName: string;
/** Whose user id to stamp on `merged_by` + close-keyword comments. */
actorUserId: string;
/**
* When true, indicates the caller's gate matrix saw a `Merge check` failure
* — we should route through `mergeWithAutoResolve` (Claude-assisted
* resolution) instead of a plain ref update. The autopilot sweep currently
* passes `false` because `evaluateAutoMerge` already requires green gates.
*/
hasConflicts?: boolean;
}
export interface PerformMergeResult {
ok: boolean;
error?: string;
/**
* Issue numbers that were auto-closed by J7 close-keyword scanning.
* Empty array on no matches or on close-keyword failure (never throws).
*/
closedIssueNumbers: number[];
/**
* Files that the AI conflict resolver touched, when `hasConflicts` routed
* through `mergeWithAutoResolve`. Empty when a plain ref update was used.
*/
resolvedFiles: string[];
}
/**
* Internal helper: run the actual git operation (ref update or
* Claude-assisted merge). Returns `{ok}` so the caller decides whether to
* flip DB state.
*/
async function executeGitMerge(args: {
ownerName: string;
repoName: string;
baseBranch: string;
headBranch: string;
prNumber: number;
prTitle: string;
hasConflicts: boolean;
}): Promise<{ ok: true; resolvedFiles: string[] } | { ok: false; error: string }> {
const repoDir = getRepoPath(args.ownerName, args.repoName);
if (args.hasConflicts && isAiReviewEnabled()) {
const mergeResult = await mergeWithAutoResolve(
args.ownerName,
args.repoName,
args.baseBranch,
args.headBranch,
`Merge pull request #${args.prNumber}: ${args.prTitle}`
);
if (!mergeResult.success) {
return {
ok: false,
error: mergeResult.error || "Auto-merge failed",
};
}
return { ok: true, resolvedFiles: mergeResult.resolvedFiles };
}
try {
const proc = Bun.spawn(
[
"git",
"update-ref",
`refs/heads/${args.baseBranch}`,
`refs/heads/${args.headBranch}`,
],
{ cwd: repoDir, stdout: "pipe", stderr: "pipe" }
);
const exit = await proc.exited;
if (exit !== 0) {
const errText = await new Response(proc.stderr).text();
return {
ok: false,
error: `git update-ref failed: ${errText.trim() || `exit ${exit}`}`,
};
}
return { ok: true, resolvedFiles: [] };
} catch (err) {
return {
ok: false,
error: `git update-ref threw: ${err instanceof Error ? err.message : String(err)}`,
};
}
}
/**
* Apply J7 close-keyword scanning. Best-effort — failures swallowed and
* surfaced via the returned array (which is empty on any error).
*/
async function applyCloseKeywords(args: {
pr: PerformMergeArgs["pr"];
actorUserId: string;
}): Promise<number[]> {
const closed: number[] = [];
try {
const refs = extractClosingRefsMulti([args.pr.title, args.pr.body]);
for (const n of refs) {
const [issue] = await db
.select()
.from(issues)
.where(
and(
eq(issues.repositoryId, args.pr.repositoryId),
eq(issues.number, n)
)
)
.limit(1);
if (!issue || issue.state !== "open") continue;
await db
.update(issues)
.set({ state: "closed", closedAt: new Date(), updatedAt: new Date() })
.where(eq(issues.id, issue.id));
await db.insert(issueComments).values({
issueId: issue.id,
authorId: args.actorUserId,
body: `Closed by pull request #${args.pr.number}.`,
});
closed.push(n);
}
} catch {
// J7 invariant: close-keyword failures never block the merge.
}
return closed;
}
/**
* Run a PR merge end-to-end (git + DB + close-keywords). Caller is
* responsible for having pre-validated that the merge is allowed.
*
* Returns:
* - ok=true with `closedIssueNumbers` + `resolvedFiles` on full success.
* - ok=false with `error` if the git step failed; DB is left untouched.
* (DB-update failures are bubbled up the same way.)
*/
export async function performMerge(
args: PerformMergeArgs
): Promise<PerformMergeResult> {
// Defence-in-depth: refuse to act on PRs that aren't actually open/non-draft.
if (args.pr.state !== "open") {
return {
ok: false,
error: `PR is not open (state=${args.pr.state}).`,
closedIssueNumbers: [],
resolvedFiles: [],
};
}
if (args.pr.isDraft) {
return {
ok: false,
error: "PR is a draft — drafts cannot be merged.",
closedIssueNumbers: [],
resolvedFiles: [],
};
}
const gitResult = await executeGitMerge({
ownerName: args.ownerName,
repoName: args.repoName,
baseBranch: args.pr.baseBranch,
headBranch: args.pr.headBranch,
prNumber: args.pr.number,
prTitle: args.pr.title,
hasConflicts: args.hasConflicts === true,
});
if (!gitResult.ok) {
return {
ok: false,
error: gitResult.error,
closedIssueNumbers: [],
resolvedFiles: [],
};
}
try {
await db
.update(pullRequests)
.set({
state: "merged",
mergedAt: new Date(),
mergedBy: args.actorUserId,
updatedAt: new Date(),
})
.where(eq(pullRequests.id, args.pr.id));
} catch (err) {
return {
ok: false,
error: `DB update failed after git merge: ${
err instanceof Error ? err.message : String(err)
}`,
closedIssueNumbers: [],
resolvedFiles: gitResult.resolvedFiles,
};
}
const closedIssueNumbers = await applyCloseKeywords({
pr: args.pr,
actorUserId: args.actorUserId,
});
void logActivity({
repositoryId: args.pr.repositoryId,
userId: args.actorUserId,
action: "pr_merge",
targetType: "pull_request",
targetId: String(args.pr.number),
metadata: { baseBranch: args.pr.baseBranch, headBranch: args.pr.headBranch },
});
return {
ok: true,
closedIssueNumbers,
resolvedFiles: gitResult.resolvedFiles,
};
}
/** Test-only surface. */
export const __test = {
executeGitMerge,
applyCloseKeywords,
};
|