Skip to content
294 linesCodeBlameRaw
1//! The webhooks service: events, delivered to the addresses a repository or
2//! a workspace registers.
3//!
4//! Every event g1t publishes can be delivered: an HTTPS `POST` of JSON,
5//! signed with the webhook's secret in `X-G1t-Signature-256`, and retried
6//! with growing waits when the receiver does not answer with a 2xx. Each
7//! delivery is kept, with what was sent and what came back, and can be sent
8//! again.
9//!
10//! Mirrors `packages/contracts/src/webhooks.ts`.
11
12use serde::{Deserialize, Serialize};
13
14use crate::repos::RepoPath;
15use crate::{User, Viewer};
16
17/// Every event a webhook can be sent, in the order people are shown them.
18pub const EVENT_TYPES: [&str; 107] = [
19 "git.push",
20 "git.deleted",
21 "branch.renamed",
22 "repo.created",
23 "repo.forked",
24 "repo.updated",
25 "repo.visibility_changed",
26 "repo.renamed",
27 "repo.transferred",
28 "repo.default_branch_changed",
29 "repo.archived",
30 "repo.unarchived",
31 "repo.deleted",
32 "repo.restored",
33 "repo.purged",
34 "repo.collaborator_added",
35 "repo.collaborator_removed",
36 "repo.collaborator_role_changed",
37 "team.created",
38 "team.edited",
39 "team.deleted",
40 "team.member_added",
41 "team.member_role_changed",
42 "team.member_removed",
43 "team.repo_added",
44 "team.repo_role_changed",
45 "team.repo_removed",
46 "workspace.member_joined",
47 "ruleset.created",
48 "ruleset.updated",
49 "ruleset.deleted",
50 "issue.opened",
51 "issue.updated",
52 "issue.assigned",
53 "issue.labeled",
54 "issue.unlabeled",
55 "issue.milestoned",
56 "issue.demilestoned",
57 "issue.closed",
58 "issue.reopened",
59 "comment.created",
60 "comment.edited",
61 "comment.deleted",
62 "pull.opened",
63 "pull.ready",
64 "pull.updated",
65 "pull.merge_requested",
66 "pull.merged",
67 "pull.closed",
68 "pull.reopened",
69 "pull.converted_to_draft",
70 "pull.assigned",
71 "pull.review_requested",
72 "pull.review_request_removed",
73 "pull.labeled",
74 "pull.unlabeled",
75 "pull.milestoned",
76 "pull.demilestoned",
77 "pull.base_changed",
78 "pull.stalled",
79 "pull.resumed",
80 "release.created",
81 "release.published",
82 "release.released",
83 "release.prereleased",
84 "release.edited",
85 "release.unpublished",
86 "release.deleted",
87 "agent.asked",
88 "checks.completed",
89 "status.created",
90 "check_run.created",
91 "check_run.completed",
92 "check_run.rerequested",
93 "check_run.requested_action",
94 "check_suite.completed",
95 "check_suite.rerequested",
96 "review.completed",
97 "workflow.completed",
98 "deployment.succeeded",
99 "deployment.failed",
100 "deployment.created",
101 "deployment_status.created",
102 "queue.changed",
103 "session.appended",
104 "package.published",
105 "package.version_deleted",
106 "package.deleted",
107 "package.visibility_changed",
108 "secret_scanning_alert.created",
109 "secret_scanning_alert.fixed",
110 "secret_scanning_alert.dismissed",
111 "secret_scanning_alert.reopened",
112 "secret_scanning.bypass_requested",
113 "secret_scanning.bypass_reviewed",
114 "code_scanning_alert.created",
115 "code_scanning_alert.fixed",
116 "code_scanning_alert.dismissed",
117 "code_scanning_alert.reopened",
118 "vulnerability_alert.created",
119 "vulnerability_alert.fixed",
120 "vulnerability_alert.dismissed",
121 "vulnerability_alert.reopened",
122 "mirror.unreachable",
123 "mirror.reachable",
124 "mirror.state_changed",
125 "mirror.moved_in",
126];
127
128/// What a webhook belongs to.
129#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
130#[serde(rename_all = "snake_case")]
131pub enum HookScope {
132 /// One repository's events.
133 Repo,
134 /// The events of every repository in a workspace.
135 Workspace,
136}
137
138#[derive(Clone, Debug, Serialize, Deserialize)]
139#[serde(rename_all = "camelCase")]
140pub struct Hook {
141 pub id: String,
142 pub scope: HookScope,
143 pub workspace: String,
144 /// For a repository's webhook: `owner/name`.
145 pub repo: Option<String>,
146 pub url: String,
147 /// The event types it is sent, or `["*"]` for all.
148 pub events: Vec<String>,
149 pub active: bool,
150 /// The last four characters of its secret.
151 pub secret_hint: String,
152 pub created_by: String,
153 /// RFC 3339.
154 pub created_at: String,
155 /// How its latest delivery went: `delivered`, `pending` or `failed`.
156 pub last_status: Option<String>,
157 pub last_delivered_at: Option<String>,
158}
159
160/// One event sent, or being sent, to a webhook.
161#[derive(Clone, Debug, Serialize, Deserialize)]
162#[serde(rename_all = "camelCase")]
163pub struct HookDelivery {
164 pub id: String,
165 pub hook_id: String,
166 /// The event's id, or empty for a ping.
167 pub event_id: String,
168 pub event: String,
169 /// `pending` while it will be tried again, `delivered`, or `failed` once
170 /// it has been tried as often as it will be.
171 pub status: String,
172 pub attempts: u32,
173 /// The receiver's HTTP status, the last time it answered.
174 pub response_status: Option<u16>,
175 /// The start of what it answered.
176 pub response_body: Option<String>,
177 /// Why the last attempt failed, when the receiver could not be reached.
178 pub error: Option<String>,
179 pub duration_ms: Option<u32>,
180 /// The JSON that was sent.
181 pub payload: String,
182 /// RFC 3339.
183 pub created_at: String,
184 pub delivered_at: Option<String>,
185 pub next_attempt_at: Option<String>,
186}
187
188/// Which webhooks a call is about: a repository's, or with `repo` left out,
189/// the workspace's own.
190#[derive(Clone, Debug, Serialize, Deserialize)]
191pub struct HookOwner {
192 pub workspace: String,
193 #[serde(default)]
194 pub repo: Option<RepoPath>,
195}
196
197/// `list`. Returns `Outcome<Vec<Hook>>`. Members of the workspace only.
198#[derive(Debug, Serialize, Deserialize)]
199pub struct ListArgs {
200 pub viewer: Viewer,
201 #[serde(flatten)]
202 pub owner: HookOwner,
203}
204
205/// `create`. Returns `Outcome<CreatedHook>`. Members, for a repository's
206/// webhooks; owners, for the workspace's.
207#[derive(Debug, Serialize, Deserialize)]
208pub struct CreateArgs {
209 pub actor: User,
210 #[serde(flatten)]
211 pub owner: HookOwner,
212 pub url: String,
213 /// Event types, or `["*"]` for all. All when empty.
214 #[serde(default)]
215 pub events: Vec<String>,
216 /// Made by g1t when left out.
217 #[serde(default)]
218 pub secret: Option<String>,
219}
220
221#[derive(Clone, Debug, Serialize, Deserialize)]
222pub struct CreatedHook {
223 pub hook: Hook,
224 /// The secret, when g1t made it: shown this once.
225 pub secret: Option<String>,
226}
227
228/// `update`: only the fields given change. Returns `Outcome<Hook>`.
229#[derive(Debug, Serialize, Deserialize)]
230pub struct UpdateArgs {
231 pub actor: User,
232 #[serde(flatten)]
233 pub owner: HookOwner,
234 pub id: String,
235 #[serde(default)]
236 pub url: Option<String>,
237 #[serde(default)]
238 pub events: Option<Vec<String>>,
239 #[serde(default)]
240 pub active: Option<bool>,
241}
242
243/// `delete` (returns `Outcome<bool>`) and `ping` (sends a `ping` event and
244/// returns `Outcome<HookDelivery>`).
245#[derive(Debug, Serialize, Deserialize)]
246pub struct HookArgs {
247 pub actor: User,
248 #[serde(flatten)]
249 pub owner: HookOwner,
250 pub id: String,
251}
252
253/// `deliveries`: a webhook's latest deliveries, newest first. Returns
254/// `Outcome<Vec<HookDelivery>>`.
255#[derive(Debug, Serialize, Deserialize)]
256pub struct DeliveriesArgs {
257 pub viewer: Viewer,
258 #[serde(flatten)]
259 pub owner: HookOwner,
260 pub id: String,
261}
262
263/// `redeliver`: sends a delivery's payload again, as a new delivery.
264/// Returns `Outcome<HookDelivery>`.
265#[derive(Debug, Serialize, Deserialize)]
266#[serde(rename_all = "camelCase")]
267pub struct RedeliverArgs {
268 pub actor: User,
269 #[serde(flatten)]
270 pub owner: HookOwner,
271 pub delivery_id: String,
272}
273
274#[cfg(test)]
275mod tests {
276 use super::EVENT_TYPES;
277
278 /// The TypeScript mirror lists the same events, in the same order.
279 #[test]
280 fn the_typescript_mirror_lists_the_same_events() {
281 let ts = include_str!("../../../packages/contracts/src/webhooks.ts");
282 let list = ts
283 .split_once("export const EVENT_TYPES = [")
284 .and_then(|(_, rest)| rest.split_once("] as const"))
285 .map(|(list, _)| list)
286 .expect("EVENT_TYPES in webhooks.ts");
287 let mirrored: Vec<&str> = list
288 .split(',')
289 .map(|item| item.trim().trim_matches('"'))
290 .filter(|item| !item.is_empty())
291 .collect();
292 assert_eq!(mirrored, EVENT_TYPES);
293 }
294}