Blame · Line-by-line history
post-receive.ts
Each line is annotated with the commit that last touched it. Click any SHA to jump to that commit and see the surrounding change.
| fc1817a | 1 | /** |
| 2 | * Post-receive hook logic. | |
| 3 | * Called after a successful git push to trigger GateTest scans | |
| 4 | * and Crontech deploys. | |
| 5 | */ | |
| 6 | ||
| 7 | import { config } from "../lib/config"; | |
| 8 | ||
| 9 | interface PushRef { | |
| 10 | oldSha: string; | |
| 11 | newSha: string; | |
| 12 | refName: string; | |
| 13 | } | |
| 14 | ||
| 15 | export async function onPostReceive( | |
| 16 | owner: string, | |
| 17 | repo: string, | |
| 18 | refs: PushRef[] | |
| 19 | ): Promise<void> { | |
| 20 | const promises: Promise<void>[] = []; | |
| 21 | ||
| 22 | // GateTest scan on every push | |
| 23 | promises.push(triggerGateTest(owner, repo, refs)); | |
| 24 | ||
| 25 | // Crontech deploy on push to main | |
| 26 | const mainPush = refs.find( | |
| 27 | (r) => r.refName === "refs/heads/main" && !r.newSha.startsWith("0000") | |
| 28 | ); | |
| 29 | if (mainPush) { | |
| 30 | promises.push(triggerCrontechDeploy(owner, repo, mainPush.newSha)); | |
| 31 | } | |
| 32 | ||
| 33 | await Promise.allSettled(promises); | |
| 34 | } | |
| 35 | ||
| 36 | async function triggerGateTest( | |
| 37 | owner: string, | |
| 38 | repo: string, | |
| 39 | refs: PushRef[] | |
| 40 | ): Promise<void> { | |
| 41 | try { | |
| 42 | const response = await fetch(config.gatetestUrl, { | |
| 43 | method: "POST", | |
| 44 | headers: { "Content-Type": "application/json" }, | |
| 45 | body: JSON.stringify({ | |
| 46 | repository: `${owner}/${repo}`, | |
| 47 | refs: refs.map((r) => ({ | |
| 48 | ref: r.refName, | |
| 49 | before: r.oldSha, | |
| 50 | after: r.newSha, | |
| 51 | })), | |
| 52 | source: "gluecron", | |
| 53 | }), | |
| 54 | }); | |
| 55 | console.log( | |
| 56 | `[gatetest] scan triggered for ${owner}/${repo}: ${response.status}` | |
| 57 | ); | |
| 58 | } catch (err) { | |
| 59 | console.error(`[gatetest] failed to trigger scan:`, err); | |
| 60 | } | |
| 61 | } | |
| 62 | ||
| 63 | async function triggerCrontechDeploy( | |
| 64 | owner: string, | |
| 65 | repo: string, | |
| 66 | sha: string | |
| 67 | ): Promise<void> { | |
| 68 | try { | |
| 69 | const response = await fetch(config.crontechDeployUrl, { | |
| 70 | method: "POST", | |
| 71 | headers: { "Content-Type": "application/json" }, | |
| 72 | body: JSON.stringify({ | |
| 73 | repository: `${owner}/${repo}`, | |
| 74 | sha, | |
| 75 | branch: "main", | |
| 76 | source: "gluecron", | |
| 77 | }), | |
| 78 | }); | |
| 79 | console.log( | |
| 80 | `[crontech] deploy triggered for ${owner}/${repo}@${sha.slice(0, 7)}: ${response.status}` | |
| 81 | ); | |
| 82 | } catch (err) { | |
| 83 | console.error(`[crontech] failed to trigger deploy:`, err); | |
| 84 | } | |
| 85 | } |