Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

g1t.sh
318 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; 119] = [
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 "checks.completed",
88 "status.created",
89 "check_run.created",
90 "check_run.completed",
91 "check_run.rerequested",
92 "check_run.requested_action",
93 "check_suite.completed",
94 "check_suite.rerequested",
95 "workflow.completed",
96 "deployment.succeeded",
97 "deployment.failed",
98 "deployment.created",
99 "deployment_status.created",
100 "queue.changed",
101 "integration.failed",
102 "integration.passed",
103 "decision.recorded",
104 "session.appended",
105 "session.started",
106 "session.ended",
107 "session.progressed",
108 "work.claimed",
109 "work.released",
110 "work.claim_expired",
111 "work.handoff_requested",
112 "dependency.changed",
113 "overlap.detected",
114 "package.published",
115 "package.version_deleted",
116 "package.deleted",
117 "package.visibility_changed",
118 "secret_scanning_alert.created",
119 "secret_scanning_alert.fixed",
120 "secret_scanning_alert.dismissed",
121 "secret_scanning_alert.reopened",
122 "secret_scanning.bypass_requested",
123 "secret_scanning.bypass_reviewed",
124 "code_scanning_alert.created",
125 "code_scanning_alert.fixed",
126 "code_scanning_alert.dismissed",
127 "code_scanning_alert.reopened",
128 "vulnerability_alert.created",
129 "vulnerability_alert.fixed",
130 "vulnerability_alert.dismissed",
131 "secret_scanning_alert.summary",
132 "code_scanning_alert.summary",
133 "vulnerability_alert.reopened",
134 "vulnerability_alert.summary",
135 "security_advisory.published",
136 "security_advisory.withdrawn",
137 "security_advisory.reported",
138];
139
140/// Event types webhooks were once offered that g1t no longer sends: they
141/// came from g1t's own agents, since removed. A webhook saved with one
142/// still reads, and saving its list again leaves them out.
143pub const RETIRED_EVENT_TYPES: [&str; 2] = ["agent.asked", "review.completed"];
144
145/// What a webhook belongs to.
146#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
147#[serde(rename_all = "snake_case")]
148pub enum HookScope {
149 /// One repository's events.
150 Repo,
151 /// The events of every repository in a workspace.
152 Workspace,
153}
154
155#[derive(Clone, Debug, Serialize, Deserialize)]
156#[serde(rename_all = "camelCase")]
157pub struct Hook {
158 pub id: String,
159 pub scope: HookScope,
160 pub workspace: String,
161 /// For a repository's webhook: `owner/name`.
162 pub repo: Option<String>,
163 pub url: String,
164 /// The event types it is sent, or `["*"]` for all.
165 pub events: Vec<String>,
166 pub active: bool,
167 /// The last four characters of its secret.
168 pub secret_hint: String,
169 pub created_by: String,
170 /// RFC 3339.
171 pub created_at: String,
172 /// How its latest delivery went: `delivered`, `pending` or `failed`.
173 pub last_status: Option<String>,
174 pub last_delivered_at: Option<String>,
175}
176
177/// One event sent, or being sent, to a webhook.
178#[derive(Clone, Debug, Serialize, Deserialize)]
179#[serde(rename_all = "camelCase")]
180pub struct HookDelivery {
181 pub id: String,
182 pub hook_id: String,
183 /// The event's id, or empty for a ping.
184 pub event_id: String,
185 pub event: String,
186 /// `pending` while it will be tried again, `delivered`, or `failed` once
187 /// it has been tried as often as it will be.
188 pub status: String,
189 pub attempts: u32,
190 /// The receiver's HTTP status, the last time it answered.
191 pub response_status: Option<u16>,
192 /// The start of what it answered.
193 pub response_body: Option<String>,
194 /// Why the last attempt failed, when the receiver could not be reached.
195 pub error: Option<String>,
196 pub duration_ms: Option<u32>,
197 /// The JSON that was sent.
198 pub payload: String,
199 /// RFC 3339.
200 pub created_at: String,
201 pub delivered_at: Option<String>,
202 pub next_attempt_at: Option<String>,
203}
204
205/// Which webhooks a call is about: a repository's, or with `repo` left out,
206/// the workspace's own.
207#[derive(Clone, Debug, Serialize, Deserialize)]
208pub struct HookOwner {
209 pub workspace: String,
210 #[serde(default)]
211 pub repo: Option<RepoPath>,
212}
213
214/// `list`. Returns `Outcome<Vec<Hook>>`. Members of the workspace only.
215#[derive(Debug, Serialize, Deserialize)]
216pub struct ListArgs {
217 pub viewer: Viewer,
218 #[serde(flatten)]
219 pub owner: HookOwner,
220}
221
222/// `create`. Returns `Outcome<CreatedHook>`. Members, for a repository's
223/// webhooks; owners, for the workspace's.
224#[derive(Debug, Serialize, Deserialize)]
225pub struct CreateArgs {
226 pub actor: User,
227 #[serde(flatten)]
228 pub owner: HookOwner,
229 pub url: String,
230 /// Event types, or `["*"]` for all. All when empty.
231 #[serde(default)]
232 pub events: Vec<String>,
233 /// Made by g1t when left out.
234 #[serde(default)]
235 pub secret: Option<String>,
236}
237
238#[derive(Clone, Debug, Serialize, Deserialize)]
239pub struct CreatedHook {
240 pub hook: Hook,
241 /// The secret, when g1t made it: shown this once.
242 pub secret: Option<String>,
243}
244
245/// `update`: only the fields given change. Returns `Outcome<Hook>`.
246#[derive(Debug, Serialize, Deserialize)]
247pub struct UpdateArgs {
248 pub actor: User,
249 #[serde(flatten)]
250 pub owner: HookOwner,
251 pub id: String,
252 #[serde(default)]
253 pub url: Option<String>,
254 #[serde(default)]
255 pub events: Option<Vec<String>>,
256 #[serde(default)]
257 pub active: Option<bool>,
258}
259
260/// `delete` (returns `Outcome<bool>`) and `ping` (sends a `ping` event and
261/// returns `Outcome<HookDelivery>`).
262#[derive(Debug, Serialize, Deserialize)]
263pub struct HookArgs {
264 pub actor: User,
265 #[serde(flatten)]
266 pub owner: HookOwner,
267 pub id: String,
268}
269
270/// `deliveries`: a webhook's latest deliveries, newest first. Returns
271/// `Outcome<Vec<HookDelivery>>`.
272#[derive(Debug, Serialize, Deserialize)]
273pub struct DeliveriesArgs {
274 pub viewer: Viewer,
275 #[serde(flatten)]
276 pub owner: HookOwner,
277 pub id: String,
278}
279
280/// `redeliver`: sends a delivery's payload again, as a new delivery.
281/// Returns `Outcome<HookDelivery>`.
282#[derive(Debug, Serialize, Deserialize)]
283#[serde(rename_all = "camelCase")]
284pub struct RedeliverArgs {
285 pub actor: User,
286 #[serde(flatten)]
287 pub owner: HookOwner,
288 pub delivery_id: String,
289}
290
291#[cfg(test)]
292mod tests {
293 use super::{EVENT_TYPES, RETIRED_EVENT_TYPES};
294
295 #[test]
296 fn retired_events_are_not_offered() {
297 for kind in RETIRED_EVENT_TYPES {
298 assert!(!EVENT_TYPES.contains(&kind), "{kind}");
299 }
300 }
301
302 /// The TypeScript mirror lists the same events, in the same order.
303 #[test]
304 fn the_typescript_mirror_lists_the_same_events() {
305 let ts = include_str!("../../../packages/contracts/src/webhooks.ts");
306 let list = ts
307 .split_once("export const EVENT_TYPES = [")
308 .and_then(|(_, rest)| rest.split_once("] as const"))
309 .map(|(list, _)| list)
310 .expect("EVENT_TYPES in webhooks.ts");
311 let mirrored: Vec<&str> = list
312 .split(',')
313 .map(|item| item.trim().trim_matches('"'))
314 .filter(|item| !item.is_empty())
315 .collect();
316 assert_eq!(mirrored, EVENT_TYPES);
317 }
318}