Public: anyone can read this; the crew can change it.
An admin pauses and resumes a webhook from the terminal's webhook list with `p`, and a paused hook gets no deliveries #8088
torque/forge:torque/8082-an-admin-pauses-and-resumes-a-webhook-fr → main 3 commits fork
6 files
changed +96
−2
src/actions/repos.ts +21
| @@ -44,6 +44,7 @@ import type { User } from "../core/users.ts"; | ||
| 44 | 44 | import { |
| 45 | 45 | createWebhook as insertWebhook, |
| 46 | 46 | deleteWebhook as removeWebhook, |
| 47 | setWebhookActive, | |
| 47 | 48 | type CreateWebhookError, |
| 48 | 49 | type Webhook, |
| 49 | 50 | } from "../core/webhooks.ts"; |
| @@ -649,6 +650,26 @@ export function deleteWebhook( | ||
| 649 | 650 | return ok(); |
| 650 | 651 | } |
| 651 | 652 | |
| 653 | /** An admin pauses or resumes a webhook: a paused hook is sent nothing. */ | |
| 654 | export function pauseWebhook( | |
| 655 | app: App, | |
| 656 | actor: User | undefined, | |
| 657 | repo: Repo, | |
| 658 | webhookId: string, | |
| 659 | active: boolean, | |
| 660 | ): Result<void, "not_found"> { | |
| 661 | const admin = requireRepoAdmin(app, actor, repo); | |
| 662 | if (!admin.ok) return admin; | |
| 663 | if (!setWebhookActive(app.db, repo.id, webhookId, active)) return err("not_found"); | |
| 664 | app.bus.append({ | |
| 665 | kind: active ? "webhook.resumed" : "webhook.paused", | |
| 666 | repoId: repo.id, | |
| 667 | actorId: admin.value.id, | |
| 668 | payload: { webhookId }, | |
| 669 | }); | |
| 670 | return ok(); | |
| 671 | } | |
| 672 | ||
| 652 | 673 | export type CommitToBranchError = |
| 653 | 674 | | AccessError |
| 654 | 675 | | CommitFilesError |
src/core/webhooks.ts +14
| @@ -133,6 +133,20 @@ export function deleteWebhook( | ||
| 133 | 133 | ); |
| 134 | 134 | } |
| 135 | 135 | |
| 136 | /** Pause (false) or resume (true) a webhook; false for one of another repository. */ | |
| 137 | export function setWebhookActive( | |
| 138 | db: Database, | |
| 139 | repoId: string, | |
| 140 | webhookId: string, | |
| 141 | active: boolean, | |
| 142 | ): boolean { | |
| 143 | return ( | |
| 144 | db | |
| 145 | .query("UPDATE webhook SET active = ?3 WHERE id = ?1 AND repo_id = ?2") | |
| 146 | .run(webhookId, repoId, active ? 1 : 0).changes > 0 | |
| 147 | ); | |
| 148 | } | |
| 149 | ||
| 136 | 150 | /** Does a webhook's `kinds` spec match an event kind? */ |
| 137 | 151 | export function kindMatches(spec: string, kind: string): boolean { |
| 138 | 152 | for (const pattern of spec.split(/\s+/).filter(Boolean)) { |
src/design/activity.ts +2
| @@ -138,6 +138,8 @@ const WORDS: Record<string, string> = { | ||
| 138 | 138 | "label.deleted": "removed a label", |
| 139 | 139 | "webhook.created": "added a webhook", |
| 140 | 140 | "webhook.deleted": "removed a webhook", |
| 141 | "webhook.paused": "paused a webhook", | |
| 142 | "webhook.resumed": "resumed a webhook", | |
| 141 | 143 | "origin.connected": "connected an origin", |
| 142 | 144 | "origin.disconnected": "disconnected an origin", |
| 143 | 145 | "origin.needs_reauth": "needs to reconnect an origin", |
src/tui/screens/hooks.ts +10 −2
| @@ -2,11 +2,11 @@ | ||
| 2 | 2 | * A repository's webhooks in the terminal, an admin's: each hook's url, |
| 3 | 3 | * kinds and last delivery, never its secret. `enter` opens its last 20 |
| 4 | 4 | * deliveries. `a` asks for url, kinds and a secret (typed masked, 16–200 |
| 5 | * characters) and adds it through `createWebhook`; `d` removes the selected one after a confirm through | |
| 5 | * characters) and adds it through `createWebhook`; `p` pauses or resumes the selected one (a paused hook is sent nothing); `d` removes the selected one after a confirm through | |
| 6 | 6 | * `deleteWebhook`. The words of a refusal are MCP `hooks`'s. |
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | import { createWebhook, deleteWebhook } from "../../actions/repos.ts"; | |
| 9 | import { createWebhook, deleteWebhook, pauseWebhook } from "../../actions/repos.ts"; | |
| 10 | 10 | import { canAdmin, type Repo } from "../../core/repos.ts"; |
| 11 | 11 | import { listWebhooks, recentDeliveries, type DeliveryInfo, type Webhook } from "../../core/webhooks.ts"; |
| 12 | 12 | import type { Ctx, Screen } from "../app/app.ts"; |
| @@ -39,6 +39,7 @@ export function hooksCommands(f: Session, ctx: Ctx, repo: Repo | undefined): Com | ||
| 39 | 39 | const clean = (s: string): string => s.replace(/[\p{Cc}\p{Cf}\u2028\u2029]/gu, "").trim(); |
| 40 | 40 | |
| 41 | 41 | function lastDelivery(f: Session, hook: Webhook): string { |
| 42 | if (!hook.active) return "paused"; | |
| 42 | 43 | const d = recentDeliveries(f.db, hook.id, 1)[0]; |
| 43 | 44 | if (d === undefined) return "no delivery yet"; |
| 44 | 45 | const how = d.lastStatus !== null ? ` ${d.lastStatus}` : d.lastError !== null ? ` ${clean(d.lastError)}` : ""; |
| @@ -149,6 +150,7 @@ export function hooksScreen(f: Session, ctx: Ctx, repo: Repo): Screen { | ||
| 149 | 150 | keys: [ |
| 150 | 151 | binding(["j", "k"], "move"), |
| 151 | 152 | binding(["a"], "add a webhook"), |
| 153 | binding(["p"], "pause or resume the selected one"), | |
| 152 | 154 | binding(["d"], "remove the selected one"), |
| 153 | 155 | binding(["enter"], "its deliveries"), |
| 154 | 156 | ], |
| @@ -180,6 +182,12 @@ export function hooksScreen(f: Session, ctx: Ctx, repo: Repo): Screen { | ||
| 180 | 182 | const hook = hooks()[state.index]; |
| 181 | 183 | if (hook === undefined) return false; |
| 182 | 184 | ctx.push(deliveriesScreen(f, ctx, hook, allowed)); |
| 185 | } else if (e.key === "p") { | |
| 186 | const hook = hooks()[state.index]; | |
| 187 | if (hook === undefined) return false; | |
| 188 | const done = pauseWebhook(f.app, f.user, repo, hook.id, !hook.active); | |
| 189 | note = done.ok ? `${hook.active ? "paused" : "resumed"} ${clean(hook.url)}` : "no such webhook here"; | |
| 190 | ctx.invalidate(); | |
| 183 | 191 | } else if (e.key === "d") { |
| 184 | 192 | const hook = hooks()[state.index]; |
| 185 | 193 | if (hook === undefined) return false; |
test/tui-hooks.test.ts +15
| @@ -12,6 +12,7 @@ import { createApp, type App } from "../src/app.ts"; | ||
| 12 | 12 | import { canAdmin, canWrite, type Repo } from "../src/core/repos.ts"; |
| 13 | 13 | import { createOrg, createTeam, addTeamMember } from "../src/core/orgs.ts"; |
| 14 | 14 | import { createUser, type User } from "../src/core/users.ts"; |
| 15 | import { listEvents } from "../src/core/events.ts"; | |
| 15 | 16 | import { enqueueForEvent, listWebhooks } from "../src/core/webhooks.ts"; |
| 16 | 17 | import { createForgeApp } from "../src/tui/forge.ts"; |
| 17 | 18 | import { hooksCommands } from "../src/tui/screens/hooks.ts"; |
| @@ -195,6 +196,20 @@ describe("webhooks in the terminal", () => { | ||
| 195 | 196 | h.stop(); |
| 196 | 197 | }); |
| 197 | 198 | |
| 199 | test("p pauses the selected hook and p again resumes it", async () => { | |
| 200 | const hook = listWebhooks(app.db, repo.id)[0]!; | |
| 201 | const h = open(alice); | |
| 202 | await palette(h); | |
| 203 | await h.feed("p"); | |
| 204 | expect(listWebhooks(app.db, repo.id)[0]!.active).toBe(false); | |
| 205 | expect(h.text()).toContain("paused"); | |
| 206 | expect(listEvents(app.db, { limit: 1000 }).some((e) => e.kind === "webhook.paused")).toBe(true); | |
| 207 | await h.feed("p"); | |
| 208 | expect(listWebhooks(app.db, repo.id).find((x) => x.id === hook.id)!.active).toBe(true); | |
| 209 | expect(listEvents(app.db, { limit: 1000 }).some((e) => e.kind === "webhook.resumed")).toBe(true); | |
| 210 | h.stop(); | |
| 211 | }); | |
| 212 | ||
| 198 | 213 | test("a writer who is not an admin is never offered it and cannot reach it", async () => { |
| 199 | 214 | const org = createOrg(app.db, "acme", alice.id); |
| 200 | 215 | if (!org.ok) throw new Error(org.error); |
test/webhooks.test.ts +34
| @@ -12,6 +12,9 @@ import { LOOPBACK, plainHook } from "./helpers/loopback-hook.ts"; | ||
| 12 | 12 | import { |
| 13 | 13 | createWebhook, |
| 14 | 14 | deliverDue, |
| 15 | enqueueForEvent, | |
| 16 | getWebhook, | |
| 17 | setWebhookActive, | |
| 15 | 18 | kindMatches, |
| 16 | 19 | verifySignature, |
| 17 | 20 | } from "../src/core/webhooks.ts"; |
| @@ -171,6 +174,37 @@ describe("webhook delivery", () => { | ||
| 171 | 174 | }); |
| 172 | 175 | }); |
| 173 | 176 | |
| 177 | describe("pausing a webhook", () => { | |
| 178 | test("a paused hook enqueues nothing, a resumed one does again, another repo's is refused", async () => { | |
| 179 | const dir = await mkdtemp(join(tmpdir(), "forge-hook-pause-")); | |
| 180 | const app = createApp({ dataDir: dir, port: 0 }); | |
| 181 | const u = await createUser(app.db, { origin: "seed", trusted: true, username: "pauser", email: "[email protected]", password: "webhooks-are-fun" }); | |
| 182 | if (!u.ok) throw new Error("user"); | |
| 183 | const r = await createRepo(app.db, app.reposRoot, { ownerId: u.value.id, name: "r" }); | |
| 184 | if (!r.ok) throw new Error("repo"); | |
| 185 | const made = createWebhook(app.db, { repoId: r.value.id, url: "https://example.com/h" }); | |
| 186 | if (!made.ok) throw new Error("hook"); | |
| 187 | const hookId = made.value.webhook.id; | |
| 188 | const ev = (kind: string) => app.bus.append({ kind, repoId: r.value.id, actorId: u.value.id, payload: {} }); | |
| 189 | const count = () => app.db.query<{ n: number }, []>("SELECT count(*) n FROM webhook_delivery").get()!.n; | |
| 190 | ||
| 191 | expect(setWebhookActive(app.db, "other-repo", hookId, false)).toBe(false); | |
| 192 | expect(getWebhook(app.db, r.value.id, hookId)!.active).toBe(true); | |
| 193 | expect(setWebhookActive(app.db, r.value.id, hookId, false)).toBe(true); | |
| 194 | expect(getWebhook(app.db, r.value.id, hookId)!.active).toBe(false); | |
| 195 | const paused = ev("pause.one"); | |
| 196 | enqueueForEvent(app.db, paused); | |
| 197 | expect(count()).toBe(0); | |
| 198 | ||
| 199 | expect(setWebhookActive(app.db, r.value.id, hookId, true)).toBe(true); | |
| 200 | expect(count()).toBe(0); // nothing is backfilled on resume | |
| 201 | enqueueForEvent(app.db, ev("pause.two")); | |
| 202 | expect(count()).toBe(1); | |
| 203 | app.db.close(); | |
| 204 | await rm(dir, { recursive: true, force: true }); | |
| 205 | }); | |
| 206 | }); | |
| 207 | ||
| 174 | 208 | describe("webhook urls and refused deliveries", () => { |
| 175 | 209 | test("only https is saved, with invalid_url", () => { |
| 176 | 210 | const app = createApp({ dataDir: tmp(), port: 0 }); |
new above
offline