|
| 1 | +#!/usr/bin/env node |
| 2 | +import {createHmac} from "node:crypto"; |
| 3 | +import {mkdtempSync, readFileSync, writeFileSync} from "node:fs"; |
| 4 | +import {tmpdir} from "node:os"; |
| 5 | +import {join} from "node:path"; |
| 6 | + |
| 7 | +import {createConfig} from "../dist/src/adapter.js"; |
| 8 | +import {createWebhookServer} from "../dist/src/server.js"; |
| 9 | + |
| 10 | +const root = new URL("..", import.meta.url).pathname; |
| 11 | +const stateDir = mkdtempSync(join(tmpdir(), "coven-github-demo-")); |
| 12 | +const policyPath = join(stateDir, "policy.json"); |
| 13 | +const secret = "local-demo-webhook-secret"; |
| 14 | +const deliveryId = "demo-delivery-issues-labeled"; |
| 15 | +const port = Number.parseInt(process.env.PORT || "3137", 10); |
| 16 | + |
| 17 | +writeFileSync( |
| 18 | + policyPath, |
| 19 | + readFileSync(new URL("../config/example-policy.json", import.meta.url)), |
| 20 | +); |
| 21 | + |
| 22 | +const payload = { |
| 23 | + action: "labeled", |
| 24 | + installation: {id: 123456}, |
| 25 | + repository: { |
| 26 | + id: 987654321, |
| 27 | + full_name: "OpenCoven/example", |
| 28 | + clone_url: "https://github.com/OpenCoven/example.git", |
| 29 | + default_branch: "main", |
| 30 | + }, |
| 31 | + issue: { |
| 32 | + number: 42, |
| 33 | + title: "Wire the app", |
| 34 | + body: "Make the first app route functional.", |
| 35 | + labels: [{name: "coven:fix"}], |
| 36 | + }, |
| 37 | +}; |
| 38 | +const body = Buffer.from(JSON.stringify(payload)); |
| 39 | +const signature = `sha256=${createHmac("sha256", secret).update(body).digest("hex")}`; |
| 40 | + |
| 41 | +const config = createConfig( |
| 42 | + { |
| 43 | + COVEN_GITHUB_DEMO_MODE: "1", |
| 44 | + COVEN_GITHUB_STATE_DIR: stateDir, |
| 45 | + COVEN_GITHUB_POLICY_PATH: policyPath, |
| 46 | + GITHUB_WEBHOOK_SECRET: secret, |
| 47 | + }, |
| 48 | + root, |
| 49 | +); |
| 50 | + |
| 51 | +const server = createWebhookServer(config); |
| 52 | +await new Promise((resolve) => server.listen(port, "127.0.0.1", resolve)); |
| 53 | + |
| 54 | +try { |
| 55 | + const response = await fetch(`http://127.0.0.1:${port}/webhook`, { |
| 56 | + method: "POST", |
| 57 | + headers: { |
| 58 | + "Content-Type": "application/json", |
| 59 | + "X-GitHub-Event": "issues", |
| 60 | + "X-GitHub-Delivery": deliveryId, |
| 61 | + "X-Hub-Signature-256": signature, |
| 62 | + }, |
| 63 | + body, |
| 64 | + }); |
| 65 | + const result = await response.json(); |
| 66 | + const delivery = JSON.parse(readFileSync(join(stateDir, "deliveries", `${deliveryId}.json`), "utf8")); |
| 67 | + const task = JSON.parse(readFileSync(join(stateDir, "tasks", `${deliveryId}.json`), "utf8")); |
| 68 | + const demoResult = JSON.parse(readFileSync(task.result_path, "utf8")); |
| 69 | + |
| 70 | + console.log(JSON.stringify({ |
| 71 | + ok: response.ok, |
| 72 | + status: response.status, |
| 73 | + response: result, |
| 74 | + state_dir: stateDir, |
| 75 | + delivery: { |
| 76 | + id: delivery.delivery_id, |
| 77 | + event: delivery.event, |
| 78 | + routing_result: delivery.routing_result, |
| 79 | + state: delivery.state, |
| 80 | + repository: delivery.repository, |
| 81 | + }, |
| 82 | + task: { |
| 83 | + id: task.task_id, |
| 84 | + state: task.state, |
| 85 | + trigger: task.trigger, |
| 86 | + familiar: task.familiar, |
| 87 | + issue_number: task.task?.issue_number, |
| 88 | + publication_state: task.publication_state, |
| 89 | + session_brief_path: task.session_brief_path, |
| 90 | + result_path: task.result_path, |
| 91 | + }, |
| 92 | + result: { |
| 93 | + status: demoResult.status, |
| 94 | + summary: demoResult.summary, |
| 95 | + evidence_status: demoResult.review?.evidence_status, |
| 96 | + tests_run: demoResult.review?.tests_run, |
| 97 | + limitations: demoResult.review?.limitations, |
| 98 | + }, |
| 99 | + }, null, 2)); |
| 100 | +} finally { |
| 101 | + await new Promise((resolve, reject) => server.close((error) => error ? reject(error) : resolve())); |
| 102 | +} |
0 commit comments