| 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 | "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 | "team.created", |
| 30 | "team.edited", |
| 31 | "team.deleted", |
| 32 | "team.member_added", |
| 33 | "team.member_role_changed", |
| 34 | "team.member_removed", |
| 35 | "team.repo_added", |
| 36 | "team.repo_role_changed", |
| 37 | "team.repo_removed", |
| 38 | "ruleset.created", |
| 39 | "ruleset.updated", |
| 40 | "ruleset.deleted", |
| 41 | "issue.opened", |
| 42 | "issue.updated", |
| 43 | "issue.assigned", |
| 44 | "issue.labeled", |
| 45 | "issue.unlabeled", |
| 46 | "issue.milestoned", |
| 47 | "issue.demilestoned", |
| 48 | "issue.closed", |
| 49 | "issue.reopened", |
| 50 | "comment.created", |
| 51 | "pull.opened", |
| 52 | "pull.ready", |
| 53 | "pull.updated", |
| 54 | "pull.merge_requested", |
| 55 | "pull.merged", |
| 56 | "pull.closed", |
| 57 | "pull.assigned", |
| 58 | "pull.review_requested", |
| 59 | "pull.review_request_removed", |
| 60 | "pull.labeled", |
| 61 | "pull.unlabeled", |
| 62 | "pull.milestoned", |
| 63 | "pull.demilestoned", |
| 64 | "pull.base_changed", |
| 65 | "pull.stalled", |
| 66 | "pull.resumed", |
| 67 | "agent.asked", |
| 68 | "checks.completed", |
| 69 | "status.created", |
| 70 | "check_run.created", |
| 71 | "check_run.completed", |
| 72 | "check_run.rerequested", |
| 73 | "check_run.requested_action", |
| 74 | "check_suite.completed", |
| 75 | "check_suite.rerequested", |
| 76 | "review.completed", |
| 77 | "workflow.completed", |
| 78 | "deployment.succeeded", |
| 79 | "deployment.failed", |
| 80 | "deployment.created", |
| 81 | "deployment_status.created", |
| 82 | "queue.changed", |
| 83 | "session.appended", |
| 84 | "package.published", |
| 85 | "package.version_deleted", |
| 86 | "package.deleted", |
| 87 | "package.visibility_changed", |
| 88 | "secret_scanning_alert.created", |
| 89 | "secret_scanning_alert.fixed", |
| 90 | "secret_scanning_alert.dismissed", |
| 91 | "secret_scanning_alert.reopened", |
| 92 | "secret_scanning.bypass_requested", |
| 93 | "secret_scanning.bypass_reviewed", |
| 94 | "code_scanning_alert.created", |
| 95 | "code_scanning_alert.fixed", |
| 96 | "code_scanning_alert.dismissed", |
| 97 | "code_scanning_alert.reopened", |
| 98 | "vulnerability_alert.created", |
| 99 | "vulnerability_alert.fixed", |
| 100 | "vulnerability_alert.dismissed", |
| 101 | "vulnerability_alert.reopened", |
| 102 | ] as const; |
| 103 | |
| 104 | export type Hook = { |
| 105 | id: string; |
| 106 | scope: "repo" | "workspace"; |
| 107 | workspace: string; |
| 108 | /** For a repository's webhook: `owner/name`. */ |
| 109 | repo: string | null; |
| 110 | url: string; |
| 111 | /** Event types, or `["*"]` for all. */ |
| 112 | events: string[]; |
| 113 | active: boolean; |
| 114 | secretHint: string; |
| 115 | createdBy: string; |
| 116 | createdAt: string; |
| 117 | lastStatus: "delivered" | "pending" | "failed" | null; |
| 118 | lastDeliveredAt: string | null; |
| 119 | }; |
| 120 | |
| 121 | export type HookDelivery = { |
| 122 | id: string; |
| 123 | hookId: string; |
| 124 | eventId: string; |
| 125 | event: string; |
| 126 | status: "pending" | "delivered" | "failed"; |
| 127 | attempts: number; |
| 128 | responseStatus: number | null; |
| 129 | responseBody: string | null; |
| 130 | error: string | null; |
| 131 | durationMs: number | null; |
| 132 | /** The JSON that was sent. */ |
| 133 | payload: string; |
| 134 | createdAt: string; |
| 135 | deliveredAt: string | null; |
| 136 | nextAttemptAt: string | null; |
| 137 | }; |
| 138 | |
| 139 | /** A repository's webhooks, or with `repo` left out, the workspace's own. */ |
| 140 | export type HookOwner = { workspace: string; repo?: RepoPath }; |
| 141 | |
| 142 | export interface WebhooksApi { |
| 143 | list(viewer: Viewer, owner: HookOwner): Promise<Result<Hook[]>>; |
| 144 | create( |
| 145 | actor: User, |
| 146 | owner: HookOwner, |
| 147 | input: { url: string; events?: string[]; secret?: string }, |
| 148 | ): Promise<Result<{ hook: Hook; secret: string | null }>>; |
| 149 | update(actor: User, owner: HookOwner, id: string, input: { url?: string; events?: string[]; active?: boolean }): Promise<Result<Hook>>; |
| 150 | delete(actor: User, owner: HookOwner, id: string): Promise<Result<boolean>>; |
| 151 | ping(actor: User, owner: HookOwner, id: string): Promise<Result<HookDelivery>>; |
| 152 | deliveries(viewer: Viewer, owner: HookOwner, id: string): Promise<Result<HookDelivery[]>>; |
| 153 | redeliver(actor: User, owner: HookOwner, deliveryId: string): Promise<Result<HookDelivery>>; |
| 154 | } |