pr_01m47d15m3e54sn21z27rpy5n9/packages/contracts/src/webhooks.ts
| 1 | import type { User, Viewer } from "./identity"; |
| 2 | import type { RepoPath } from "./repos"; |
| 3 | import 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. */ |
| 11 | export const EVENT_TYPES = [ |
| 12 | "git.push", |
| 13 | "repo.created", |
| 14 | "repo.forked", |
| 15 | "issue.opened", |
| 16 | "issue.updated", |
| 17 | "issue.assigned", |
| 18 | "issue.closed", |
| 19 | "issue.reopened", |
| 20 | "comment.created", |
| 21 | "pull.opened", |
| 22 | "pull.ready", |
| 23 | "pull.updated", |
| 24 | "pull.merge_requested", |
| 25 | "pull.merged", |
| 26 | "pull.closed", |
| 27 | "agent.asked", |
| 28 | "checks.completed", |
| 29 | "review.completed", |
| 30 | "workflow.completed", |
| 31 | "queue.changed", |
| 32 | "session.appended", |
| 33 | ] as const; |
| 34 | |
| 35 | export type Hook = { |
| 36 | id: string; |
| 37 | scope: "repo" | "workspace"; |
| 38 | workspace: string; |
| 39 | /** For a repository's webhook: `owner/name`. */ |
| 40 | repo: string | null; |
| 41 | url: string; |
| 42 | /** Event types, or `["*"]` for all. */ |
| 43 | events: string[]; |
| 44 | active: boolean; |
| 45 | secretHint: string; |
| 46 | createdBy: string; |
| 47 | createdAt: string; |
| 48 | lastStatus: "delivered" | "pending" | "failed" | null; |
| 49 | lastDeliveredAt: string | null; |
| 50 | }; |
| 51 | |
| 52 | export type HookDelivery = { |
| 53 | id: string; |
| 54 | hookId: string; |
| 55 | eventId: string; |
| 56 | event: string; |
| 57 | status: "pending" | "delivered" | "failed"; |
| 58 | attempts: number; |
| 59 | responseStatus: number | null; |
| 60 | responseBody: string | null; |
| 61 | error: string | null; |
| 62 | durationMs: number | null; |
| 63 | /** The JSON that was sent. */ |
| 64 | payload: string; |
| 65 | createdAt: string; |
| 66 | deliveredAt: string | null; |
| 67 | nextAttemptAt: string | null; |
| 68 | }; |
| 69 | |
| 70 | /** A repository's webhooks, or with `repo` left out, the workspace's own. */ |
| 71 | export type HookOwner = { workspace: string; repo?: RepoPath }; |
| 72 | |
| 73 | export interface WebhooksApi { |
| 74 | list(viewer: Viewer, owner: HookOwner): Promise<Result<Hook[]>>; |
| 75 | create( |
| 76 | actor: User, |
| 77 | owner: HookOwner, |
| 78 | input: { url: string; events?: string[]; secret?: string }, |
| 79 | ): Promise<Result<{ hook: Hook; secret: string | null }>>; |
| 80 | update(actor: User, owner: HookOwner, id: string, input: { url?: string; events?: string[]; active?: boolean }): Promise<Result<Hook>>; |
| 81 | delete(actor: User, owner: HookOwner, id: string): Promise<Result<boolean>>; |
| 82 | ping(actor: User, owner: HookOwner, id: string): Promise<Result<HookDelivery>>; |
| 83 | deliveries(viewer: Viewer, owner: HookOwner, id: string): Promise<Result<HookDelivery[]>>; |
| 84 | redeliver(actor: User, owner: HookOwner, deliveryId: string): Promise<Result<HookDelivery>>; |
| 85 | } |