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

Parked opened this
Closed without merging

torque/forge:torque/8082-an-admin-pauses-and-resumes-a-webhook-fr → main 3 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
6 files changed +96 −2
src/actions/repos.ts +21
@@ -44,6 +44,7 @@ import type { User } from "../core/users.ts";
4444 import {
4545 createWebhook as insertWebhook,
4646 deleteWebhook as removeWebhook,
47 setWebhookActive,
4748 type CreateWebhookError,
4849 type Webhook,
4950 } from "../core/webhooks.ts";
@@ -649,6 +650,26 @@ export function deleteWebhook(
649650 return ok();
650651 }
651652
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
652673 export type CommitToBranchError =
653674 | AccessError
654675 | CommitFilesError
src/core/webhooks.ts +14
@@ -133,6 +133,20 @@ export function deleteWebhook(
133133 );
134134 }
135135
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
136150 /** Does a webhook's `kinds` spec match an event kind? */
137151 export function kindMatches(spec: string, kind: string): boolean {
138152 for (const pattern of spec.split(/\s+/).filter(Boolean)) {
src/design/activity.ts +2
@@ -138,6 +138,8 @@ const WORDS: Record<string, string> = {
138138 "label.deleted": "removed a label",
139139 "webhook.created": "added a webhook",
140140 "webhook.deleted": "removed a webhook",
141 "webhook.paused": "paused a webhook",
142 "webhook.resumed": "resumed a webhook",
141143 "origin.connected": "connected an origin",
142144 "origin.disconnected": "disconnected an origin",
143145 "origin.needs_reauth": "needs to reconnect an origin",
src/tui/screens/hooks.ts +10 −2
@@ -2,11 +2,11 @@
22 * A repository's webhooks in the terminal, an admin's: each hook's url,
33 * kinds and last delivery, never its secret. `enter` opens its last 20
44 * 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
66 * `deleteWebhook`. The words of a refusal are MCP `hooks`'s.
77 */
88
9 import { createWebhook, deleteWebhook } from "../../actions/repos.ts";
9 import { createWebhook, deleteWebhook, pauseWebhook } from "../../actions/repos.ts";
1010 import { canAdmin, type Repo } from "../../core/repos.ts";
1111 import { listWebhooks, recentDeliveries, type DeliveryInfo, type Webhook } from "../../core/webhooks.ts";
1212 import type { Ctx, Screen } from "../app/app.ts";
@@ -39,6 +39,7 @@ export function hooksCommands(f: Session, ctx: Ctx, repo: Repo | undefined): Com
3939 const clean = (s: string): string => s.replace(/[\p{Cc}\p{Cf}\u2028\u2029]/gu, "").trim();
4040
4141 function lastDelivery(f: Session, hook: Webhook): string {
42 if (!hook.active) return "paused";
4243 const d = recentDeliveries(f.db, hook.id, 1)[0];
4344 if (d === undefined) return "no delivery yet";
4445 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 {
149150 keys: [
150151 binding(["j", "k"], "move"),
151152 binding(["a"], "add a webhook"),
153 binding(["p"], "pause or resume the selected one"),
152154 binding(["d"], "remove the selected one"),
153155 binding(["enter"], "its deliveries"),
154156 ],
@@ -180,6 +182,12 @@ export function hooksScreen(f: Session, ctx: Ctx, repo: Repo): Screen {
180182 const hook = hooks()[state.index];
181183 if (hook === undefined) return false;
182184 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();
183191 } else if (e.key === "d") {
184192 const hook = hooks()[state.index];
185193 if (hook === undefined) return false;
test/tui-hooks.test.ts +15
@@ -12,6 +12,7 @@ import { createApp, type App } from "../src/app.ts";
1212 import { canAdmin, canWrite, type Repo } from "../src/core/repos.ts";
1313 import { createOrg, createTeam, addTeamMember } from "../src/core/orgs.ts";
1414 import { createUser, type User } from "../src/core/users.ts";
15 import { listEvents } from "../src/core/events.ts";
1516 import { enqueueForEvent, listWebhooks } from "../src/core/webhooks.ts";
1617 import { createForgeApp } from "../src/tui/forge.ts";
1718 import { hooksCommands } from "../src/tui/screens/hooks.ts";
@@ -195,6 +196,20 @@ describe("webhooks in the terminal", () => {
195196 h.stop();
196197 });
197198
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
198213 test("a writer who is not an admin is never offered it and cannot reach it", async () => {
199214 const org = createOrg(app.db, "acme", alice.id);
200215 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";
1212 import {
1313 createWebhook,
1414 deliverDue,
15 enqueueForEvent,
16 getWebhook,
17 setWebhookActive,
1518 kindMatches,
1619 verifySignature,
1720 } from "../src/core/webhooks.ts";
@@ -171,6 +174,37 @@ describe("webhook delivery", () => {
171174 });
172175 });
173176
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
174208 describe("webhook urls and refused deliveries", () => {
175209 test("only https is saved, with invalid_url", () => {
176210 const app = createApp({ dataDir: tmp(), port: 0 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs