pr_01m47d24b0e6n91zwymwxg0vpx/packages/contracts/src/webhooks.ts

99 lines2,854 bytesCodeBlame
1import type { User, Viewer } from "./identity";
2import type { RepoPath } from "./repos";
3import type { Result } from "./result";
4
5/**
6 * Events, delivered to the addresses a repository or a workspace registers.
7 * Mirrors `crates/contracts/src/webhooks.rs`.
8 */
9
10/** Every event a webhook can be sent, in the order people are shown them. */
11export const EVENT_TYPES = [
12 "git.push",
13 "branch.renamed",
14 "repo.created",
15 "repo.forked",
16 "repo.updated",
17 "repo.visibility_changed",
18 "repo.renamed",
19 "repo.transferred",
20 "repo.default_branch_changed",
21 "repo.archived",
22 "repo.unarchived",
23 "repo.deleted",
24 "repo.restored",
25 "repo.purged",
26 "repo.collaborator_added",
27 "repo.collaborator_removed",
28 "repo.collaborator_role_changed",
29 "issue.opened",
30 "issue.updated",
31 "issue.assigned",
32 "issue.closed",
33 "issue.reopened",
34 "comment.created",
35 "pull.opened",
36 "pull.ready",
37 "pull.updated",
38 "pull.merge_requested",
39 "pull.merged",
40 "pull.closed",
41 "agent.asked",
42 "checks.completed",
43 "review.completed",
44 "workflow.completed",
45 "queue.changed",
46 "session.appended",
47] as const;
48
49export type Hook = {
50 id: string;
51 scope: "repo" | "workspace";
52 workspace: string;
53 /** For a repository's webhook: `owner/name`. */
54 repo: string | null;
55 url: string;
56 /** Event types, or `["*"]` for all. */
57 events: string[];
58 active: boolean;
59 secretHint: string;
60 createdBy: string;
61 createdAt: string;
62 lastStatus: "delivered" | "pending" | "failed" | null;
63 lastDeliveredAt: string | null;
64};
65
66export type HookDelivery = {
67 id: string;
68 hookId: string;
69 eventId: string;
70 event: string;
71 status: "pending" | "delivered" | "failed";
72 attempts: number;
73 responseStatus: number | null;
74 responseBody: string | null;
75 error: string | null;
76 durationMs: number | null;
77 /** The JSON that was sent. */
78 payload: string;
79 createdAt: string;
80 deliveredAt: string | null;
81 nextAttemptAt: string | null;
82};
83
84/** A repository's webhooks, or with `repo` left out, the workspace's own. */
85export type HookOwner = { workspace: string; repo?: RepoPath };
86
87export interface WebhooksApi {
88 list(viewer: Viewer, owner: HookOwner): Promise<Result<Hook[]>>;
89 create(
90 actor: User,
91 owner: HookOwner,
92 input: { url: string; events?: string[]; secret?: string },
93 ): Promise<Result<{ hook: Hook; secret: string | null }>>;
94 update(actor: User, owner: HookOwner, id: string, input: { url?: string; events?: string[]; active?: boolean }): Promise<Result<Hook>>;
95 delete(actor: User, owner: HookOwner, id: string): Promise<Result<boolean>>;
96 ping(actor: User, owner: HookOwner, id: string): Promise<Result<HookDelivery>>;
97 deliveries(viewer: Viewer, owner: HookOwner, id: string): Promise<Result<HookDelivery[]>>;
98 redeliver(actor: User, owner: HookOwner, deliveryId: string): Promise<Result<HookDelivery>>;
99}