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