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 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 | /**
* Post-receive hook logic.
* Runs after a successful git push.
*
* 1. Update repo.pushedAt and push activity
* 2. Sync CODEOWNERS from the default branch
* 3. Run gates (GateTest + secret + security) on the new ref
* 4. Auto-deploy to Crontech ONLY if gates are green and settings allow it
* 5. Fan out webhooks
*/
import { and, eq } from "drizzle-orm";
import { config } from "../lib/config";
import { db } from "../db";
import {
activityFeed,
deployments,
repoSettings,
repositories,
users,
} from "../db/schema";
import {
runGateTestScan,
runSecretAndSecurityScan,
} from "../lib/gate";
import { getOrCreateSettings } from "../lib/repo-bootstrap";
import { getBlob, getDefaultBranch, getTree } from "../git/repository";
import { parseCodeowners, syncCodeowners } from "../lib/codeowners";
import { notify, audit } from "../lib/notify";
import { workflows, pagesSettings } from "../db/schema";
import { parseWorkflow } from "../lib/workflow-parser";
import { enqueueRun } from "../lib/workflow-runner";
import { onPagesPush } from "../lib/pages";
import { requiresApprovalFor } from "../lib/environments";
import { onDeployFailure } from "../lib/ai-incident";
import { matchProtectedTag } from "../lib/protected-tags";
interface PushRef {
oldSha: string;
newSha: string;
refName: string;
}
export async function onPostReceive(
owner: string,
repo: string,
refs: PushRef[]
): Promise<void> {
const [ownerRow] = await db
.select()
.from(users)
.where(eq(users.username, owner))
.limit(1);
const repoRow = ownerRow
? (
await db
.select()
.from(repositories)
.where(
and(
eq(repositories.ownerId, ownerRow.id),
eq(repositories.name, repo)
)
)
.limit(1)
)[0]
: null;
const defaultBranch =
(await getDefaultBranch(owner, repo)) || repoRow?.defaultBranch || "main";
// --- 1. pushedAt + activity ---
if (repoRow) {
try {
await db
.update(repositories)
.set({ pushedAt: new Date(), updatedAt: new Date() })
.where(eq(repositories.id, repoRow.id));
for (const ref of refs) {
if (!ref.newSha.startsWith("0000")) {
await db.insert(activityFeed).values({
repositoryId: repoRow.id,
userId: ownerRow?.id || null,
action: "push",
targetType: "commit",
targetId: ref.newSha,
metadata: JSON.stringify({ ref: ref.refName }),
});
}
}
} catch (err) {
console.error("[post-receive] activity/pushedAt:", err);
}
}
// --- 1b. Protected-tag advisory logging (Block E7) ---
// v1 is non-blocking: we log violations to the audit log and fan a notify
// event out to the repo owner. Actual pre-receive blocking is future work.
if (repoRow) {
for (const ref of refs) {
if (!ref.refName.startsWith("refs/tags/")) continue;
const rule = await matchProtectedTag(repoRow.id, ref.refName);
if (!rule) continue;
const isDelete = ref.newSha.startsWith("0000");
const isCreate = ref.oldSha.startsWith("0000");
const action = isDelete
? "delete"
: isCreate
? "create"
: "update";
try {
await audit({
userId: ownerRow?.id || null,
repositoryId: repoRow.id,
action: `protected_tags.${action}_violation_candidate`,
targetType: "ref",
targetId: ref.refName,
metadata: {
pattern: rule.pattern,
oldSha: ref.oldSha,
newSha: ref.newSha,
},
});
} catch {}
}
}
// --- 2. CODEOWNERS sync (only when default branch changed) ---
const mainRef = refs.find(
(r) =>
r.refName === `refs/heads/${defaultBranch}` &&
!r.newSha.startsWith("0000")
);
if (mainRef && repoRow) {
try {
const paths = ["CODEOWNERS", ".github/CODEOWNERS", "docs/CODEOWNERS"];
for (const p of paths) {
const blob = await getBlob(owner, repo, defaultBranch, p);
if (blob && !blob.isBinary) {
const rules = parseCodeowners(blob.content);
await syncCodeowners(repoRow.id, rules);
break;
}
}
} catch (err) {
console.error("[post-receive] codeowners sync:", err);
}
}
// --- 2b. Workflow sync + trigger (Block C1) ---
// On pushes to the default branch, discover `.gluecron/workflows/*.yml`,
// upsert them in the workflows table, and enqueue a run for each workflow
// whose `on` triggers include `push`.
if (mainRef && repoRow) {
try {
const entries = await getTree(
owner,
repo,
defaultBranch,
".gluecron/workflows"
);
const existing = await db
.select({ id: workflows.id, path: workflows.path })
.from(workflows)
.where(eq(workflows.repositoryId, repoRow.id));
const existingByPath = new Map(existing.map((e) => [e.path, e.id]));
const seenPaths = new Set<string>();
for (const entry of entries) {
if (entry.type !== "blob") continue;
if (!/\.ya?ml$/i.test(entry.name)) continue;
const path = `.gluecron/workflows/${entry.name}`;
seenPaths.add(path);
const blob = await getBlob(owner, repo, defaultBranch, path);
if (!blob || blob.isBinary) continue;
const parsed = parseWorkflow(blob.content);
if (!parsed.ok) {
console.error(
`[workflow-sync] ${owner}/${repo}:${path} invalid — ${parsed.error}`
);
continue;
}
const onEvents = JSON.stringify(parsed.workflow.on);
const parsedJson = JSON.stringify(parsed.workflow);
const existingId = existingByPath.get(path);
let workflowId: string;
if (existingId) {
await db
.update(workflows)
.set({
name: parsed.workflow.name,
yaml: blob.content,
parsed: parsedJson,
onEvents,
updatedAt: new Date(),
})
.where(eq(workflows.id, existingId));
workflowId = existingId;
} else {
const [row] = await db
.insert(workflows)
.values({
repositoryId: repoRow.id,
name: parsed.workflow.name,
path,
yaml: blob.content,
parsed: parsedJson,
onEvents,
})
.returning({ id: workflows.id });
workflowId = row.id;
}
// Enqueue a run if this workflow subscribes to the push event.
if (parsed.workflow.on.includes("push")) {
try {
await enqueueRun({
workflowId,
repositoryId: repoRow.id,
event: "push",
ref: mainRef.refName,
commitSha: mainRef.newSha,
triggeredBy: ownerRow?.id || null,
});
} catch (err) {
console.error(
`[workflow-enqueue] ${owner}/${repo}:${path}:`,
err
);
}
}
}
// Mark workflows whose files have been removed as disabled (soft-delete).
for (const [p, id] of existingByPath) {
if (!seenPaths.has(p)) {
await db
.update(workflows)
.set({ disabled: true, updatedAt: new Date() })
.where(eq(workflows.id, id));
}
}
} catch (err) {
console.error("[post-receive] workflow sync:", err);
}
}
// --- 2c. Pages (Block C3) ---
// On any push, if the ref matches the configured pages source branch,
// record a pages_deployments row. Fire-and-forget; onPagesPush never throws.
if (repoRow) {
let pagesBranch = "gh-pages";
try {
const [pSettings] = await db
.select()
.from(pagesSettings)
.where(eq(pagesSettings.repositoryId, repoRow.id))
.limit(1);
if (pSettings) {
if (pSettings.enabled === false) pagesBranch = "";
else pagesBranch = pSettings.sourceBranch || "gh-pages";
}
} catch {
/* fall back to default */
}
if (pagesBranch) {
for (const ref of refs) {
if (ref.newSha.startsWith("0000")) continue;
if (ref.refName === `refs/heads/${pagesBranch}`) {
void onPagesPush({
ownerLogin: owner,
repoName: repo,
repositoryId: repoRow.id,
ref: ref.refName,
newSha: ref.newSha,
triggeredByUserId: ownerRow?.id || null,
});
}
}
}
}
// --- 3. Gates ---
const settings = repoRow ? await getOrCreateSettings(repoRow.id) : null;
const promises: Promise<void>[] = [];
for (const ref of refs) {
if (ref.newSha.startsWith("0000")) continue;
if (settings?.gateTestEnabled !== false) {
const branch = ref.refName.replace(/^refs\/heads\//, "");
promises.push(
runGateTestScan(owner, repo, ref.refName, ref.newSha)
.then(async (result) => {
console.log(
`[gatetest] ${owner}/${repo} ${ref.refName}: ${result.passed ? "PASSED" : "FAILED"} — ${result.details}`
);
// Self-healing: if gate failed, trigger the heal loop
if (!result.passed && !result.skipped && settings?.autoFixEnabled !== false && repoRow) {
try {
const { runHealLoop, isHealLoopEnabled } = await import("../lib/heal-loop");
if (isHealLoopEnabled()) {
console.log(`[heal-loop] Triggering for ${owner}/${repo}:${branch}`);
runHealLoop(owner, repo, branch, {
repositoryId: repoRow.id,
triggerSource: "post-receive",
}).catch((err) => console.error("[heal-loop] Error:", err));
}
} catch {}
}
})
.catch((err) => {
console.error(`[gatetest] scan error for ${owner}/${repo}:`, err);
})
);
}
if (
settings?.secretScanEnabled !== false ||
settings?.securityScanEnabled !== false
) {
promises.push(
runSecretAndSecurityScan(owner, repo, ref.refName, ref.newSha, {
scanSecrets: settings?.secretScanEnabled !== false,
scanSecurity: false, // semantic scan needs a diff — deferred to PR gate
})
.then((result) => {
if (
!result.secretResult.passed &&
ownerRow &&
repoRow &&
result.secrets.length > 0
) {
void notify(ownerRow.id, {
kind: "security_alert",
title: `Secret detected in ${owner}/${repo}`,
body: result.secretResult.details,
url: `/${owner}/${repo}/gates`,
repositoryId: repoRow.id,
});
}
})
.catch((err) => {
console.error(`[secret-scan] error for ${owner}/${repo}:`, err);
})
);
}
}
// --- 4. Auto-deploy (only on default branch + green settings) ---
// Block C4: if a "production" environment is configured with approval
// required, insert a pending_approval deployment row instead of firing.
if (mainRef && settings?.autoDeployEnabled !== false && repoRow) {
const gate = await requiresApprovalFor(
repoRow.id,
"production",
mainRef.refName
).catch(() => ({ required: false, env: null as null }));
if (gate.required && gate.env) {
try {
await db.insert(deployments).values({
repositoryId: repoRow.id,
environment: "production",
commitSha: mainRef.newSha,
ref: mainRef.refName,
status: "pending_approval",
target: "crontech",
blockedReason: `awaiting approval for environment '${gate.env.name}'`,
});
} catch (err) {
console.error("[post-receive] pending_approval insert:", err);
}
} else {
promises.push(
triggerCrontechDeploy(owner, repo, mainRef.newSha, repoRow.id)
);
}
}
// --- 5. Webhook fan-out ---
if (repoRow) {
promises.push(fanoutWebhooks(repoRow.id, owner, repo, refs));
}
await Promise.allSettled(promises);
}
async function triggerCrontechDeploy(
owner: string,
repo: string,
sha: string,
repositoryId: string
): Promise<void> {
let deployId = "";
try {
const [row] = await db
.insert(deployments)
.values({
repositoryId,
environment: "production",
commitSha: sha,
ref: "refs/heads/main",
status: "pending",
target: "crontech",
})
.returning();
deployId = row?.id || "";
} catch {
/* ignore */
}
try {
const response = await fetch(config.crontechDeployUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
repository: `${owner}/${repo}`,
sha,
branch: "main",
source: "gluecron",
}),
});
console.log(
`[crontech] deploy triggered for ${owner}/${repo}@${sha.slice(0, 7)}: ${response.status}`
);
if (deployId) {
await db
.update(deployments)
.set({
status: response.ok ? "success" : "failed",
completedAt: new Date(),
})
.where(eq(deployments.id, deployId));
}
// D4: when Crontech returns a non-ok HTTP status, kick off the AI
// incident responder AFTER the deployment row is flipped to "failed".
if (!response.ok && deployId) {
void onDeployFailure({
repositoryId,
deploymentId: deployId,
ref: "refs/heads/main",
commitSha: sha,
target: "crontech",
errorMessage: `HTTP ${response.status}`,
}).catch((e) => console.error("[ai-incident]", e));
}
} catch (err) {
console.error(`[crontech] failed to trigger deploy:`, err);
if (deployId) {
await db
.update(deployments)
.set({
status: "failed",
blockedReason: (err as Error).message,
completedAt: new Date(),
})
.where(eq(deployments.id, deployId));
// D4: fire-and-forget incident analysis AFTER marking the row failed.
void onDeployFailure({
repositoryId,
deploymentId: deployId,
ref: "refs/heads/main",
commitSha: sha,
target: "crontech",
errorMessage: (err as Error).message,
}).catch((e) => console.error("[ai-incident]", e));
}
}
}
async function fanoutWebhooks(
repositoryId: string,
owner: string,
repo: string,
refs: PushRef[]
): Promise<void> {
try {
const { fireWebhooks } = await import("../routes/webhooks");
await fireWebhooks(repositoryId, "push", {
repository: `${owner}/${repo}`,
refs: refs.map((r) => ({
ref: r.refName,
before: r.oldSha,
after: r.newSha,
})),
});
} catch {
// best-effort
}
}
|