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
358 linesCodeBlameRaw
1//! Which events each subscribing service's queue is sent.
2//!
3//! The events service passes each batch on to one queue per subscriber
4//! (`SUBSCRIBER_*` bindings). Each is sent only the types its consumer acts
5//! on, so an event costs a queue write for the services that read it, not
6//! for all of them. A consumer that starts acting on a new type adds it
7//! here in the same change, or it never hears of it; the tests below and
8//! beside each consumer check what they can.
9//!
10//! A pattern is an exact type (`git.push`), a family (`pull.*`, every type
11//! that starts `pull.`), or `*`, every type. A binding not named here is
12//! sent everything, so a new subscriber is never left out.
13
14/// What the shared helpers in `g1t_kit` act on (`rename`, `transfer`,
15/// `lifecycle`, `deleted`, `user_deleted`): rows that follow a workspace
16/// or repository, or go with it. Rare, so every subscriber is sent them.
17const LIFECYCLE: [&str; 8] = [
18 "workspace.renamed",
19 "workspace.deleted",
20 "repo.renamed",
21 "repo.transferred",
22 "repo.deleted",
23 "repo.restored",
24 "repo.purged",
25 "user.deleted",
26];
27
28/// Each subscriber's binding and the types it is sent beside [`LIFECYCLE`].
29pub const ROUTES: &[(&str, &[&str])] = &[
30 // Memory captured from merges and comments, mentions, pull request
31 // bases, runs stopped in archived repositories (work/src/lib.rs queue).
32 (
33 "SUBSCRIBER_WORK",
34 &[
35 "git.push",
36 "branch.renamed",
37 "repo.created",
38 "repo.archived",
39 "repo.default_branch_changed",
40 "pull.merged",
41 "comment.created",
42 ],
43 ),
44 // Pull requests g1t is seeing through, mentions, queued issues and the
45 // merge queue (runner/src/index.ts queue).
46 (
47 "SUBSCRIBER_RUNNER",
48 &[
49 "pull.opened",
50 "pull.ready",
51 "pull.updated",
52 "pull.mergeability",
53 "pull.mergecheck",
54 "pull.merge_requested",
55 "pull.merged",
56 "pull.closed",
57 "checks.completed",
58 "review.completed",
59 "queue.changed",
60 "comment.created",
61 "issue.opened",
62 "issue.updated",
63 "issue.closed",
64 "agent.asked",
65 "repo.archived",
66 ],
67 ),
68 // Write-back to linked trackers (integrations/src lib.rs).
69 ("SUBSCRIBER_INTEGRATIONS", &["issue.closed", "pull.opened"]),
70 // A hook may ask for any type, or for every one.
71 ("SUBSCRIBER_WEBHOOKS", &["*"]),
72 // Every event a workflow can run on (g1t_actions::events::github_events),
73 // and runs stopped in archived repositories.
74 (
75 "SUBSCRIBER_ACTIONS",
76 &[
77 "git.push",
78 "git.deleted",
79 "pull.*",
80 "issue.*",
81 "comment.*",
82 "release.*",
83 "deployment.created",
84 "deployment_status.created",
85 "review.completed",
86 "workflow.completed",
87 "repo.archived",
88 ],
89 ),
90 // Previews and production deploys (deployments/src/index.ts queue).
91 (
92 "SUBSCRIBER_DEPLOYMENTS",
93 &[
94 "git.push",
95 "branch.renamed",
96 "repo.default_branch_changed",
97 "pull.opened",
98 "pull.ready",
99 "pull.updated",
100 "pull.reopened",
101 "pull.closed",
102 "pull.merged",
103 "workspace.deleting",
104 "workspace.restored",
105 ],
106 ),
107 // Projects follow their repositories, and wake on activity
108 // (projects/src/index.ts, activity.ts). A production deployment
109 // reported from elsewhere asks for its screenshot.
110 (
111 "SUBSCRIBER_PROJECTS",
112 &[
113 "git.push",
114 "repo.*",
115 "issue.opened",
116 "issue.closed",
117 "issue.reopened",
118 "pull.opened",
119 "pull.updated",
120 "pull.merged",
121 "pull.closed",
122 "review.completed",
123 "comment.created",
124 "deployment.succeeded",
125 "deployment.failed",
126 "deployment_status.created",
127 "package.published",
128 "package.deleted",
129 "package.version_deleted",
130 "workspace.deleting",
131 "workspace.restored",
132 ],
133 ),
134 // Pull request working copies, workspaces' repositories, contributors
135 // shown as ghost (repos/src/lib.rs handle_event).
136 (
137 "SUBSCRIBER_REPOS",
138 &[
139 "pull.merged",
140 "pull.closed",
141 "pull.reopened",
142 "workspace.deleting",
143 "workspace.restored",
144 "user.deleting",
145 "user.restored",
146 ],
147 ),
148 // Usage rows that follow a renamed workspace or repository.
149 ("SUBSCRIBER_BILLING", &[]),
150 // Secret and dependency scans, and version update pull requests
151 // (security/src/lib.rs on_event).
152 (
153 "SUBSCRIBER_SECURITY",
154 &[
155 "git.push",
156 "pull.opened",
157 "pull.updated",
158 "pull.ready",
159 "pull.merged",
160 "pull.closed",
161 "comment.created",
162 "repo.created",
163 "repo.visibility_changed",
164 ],
165 ),
166 // What agents are told about a repository (context/src/index.ts).
167 (
168 "SUBSCRIBER_CONTEXT",
169 &[
170 "git.push",
171 "issue.opened",
172 "issue.updated",
173 "issue.closed",
174 "pull.ready",
175 "pull.merged",
176 "memory.changed",
177 ],
178 ),
179 // The site-wide search index (search/src/index.rs).
180 ("SUBSCRIBER_SEARCH", &["git.push", "repo.*", "issue.*", "pull.*", "user.*", "workspace.*"]),
181 // Composer packages read from repositories, and packages that follow
182 // their workspace, team or repository (packages/src/lib.rs on_event).
183 (
184 "SUBSCRIBER_PACKAGES",
185 &[
186 "git.push",
187 "repo.visibility_changed",
188 "workspace.deleting",
189 "workspace.restored",
190 "team.edited",
191 "team.deleted",
192 ],
193 ),
194 // Pages that cite code a merge or a push to the default branch
195 // changed become possibly out of date, and projects' docs folders shown
196 // in Docs are read again (docs/src/staleness.ts, repo-spaces.ts).
197 ("SUBSCRIBER_DOCS", &["git.push", "pull.merged"]),
198 // Agents' routines that run on events: a pull request ready for
199 // review or merged, checks or a deploy failing, an issue opened
200 // (agents/src/triggers.ts); pushes, which skill libraries that
201 // follow a repository read again (agents/src/skill-library.ts); and
202 // a person joining a workspace, whom its @g1t welcomes
203 // (agents/src/welcome.ts).
204 (
205 "SUBSCRIBER_AGENTS",
206 &[
207 "git.push",
208 "pull.opened",
209 "pull.ready",
210 "pull.merged",
211 "checks.completed",
212 "issue.opened",
213 "deployment.failed",
214 "workspace.member_joined",
215 ],
216 ),
217];
218
219/// Whether `pattern` (see the module's notes) matches the type `kind`.
220fn matches(pattern: &str, kind: &str) -> bool {
221 match pattern.strip_suffix('*') {
222 Some(prefix) => kind.starts_with(prefix),
223 None => pattern == kind,
224 }
225}
226
227/// Whether the subscriber behind `binding` is sent events of type `kind`.
228pub fn routed(binding: &str, kind: &str) -> bool {
229 match ROUTES.iter().find(|(name, _)| *name == binding) {
230 Some((_, patterns)) => {
231 LIFECYCLE.contains(&kind) || patterns.iter().any(|pattern| matches(pattern, kind))
232 }
233 None => true,
234 }
235}
236
237#[cfg(test)]
238mod tests {
239 use super::*;
240
241 /// Types published on the bus that hooks are not offered.
242 const UNOFFERED: [&str; 30] = [
243 "pull.mergecheck",
244 "pull.mergeability",
245 "deployment.review_requested",
246 "memory.changed",
247 "abuse.flagged",
248 "invite.created",
249 "invite.redeemed",
250 "waitlist.requested",
251 "user.updated",
252 "user.deleting",
253 "user.restored",
254 "user.deleted",
255 "user.email_added",
256 "workspace.updated",
257 "workspace.renamed",
258 "workspace.deleting",
259 "workspace.restored",
260 "workspace.deleted",
261 // Docs pages: no repository, so no repository's hooks.
262 "doc.page.created",
263 "doc.page.updated",
264 "doc.page.archived",
265 "doc.page.stale",
266 "doc.space.deleted",
267 // Artifacts (folios): no repository either, and a folio may be private.
268 "folio.created",
269 "folio.updated",
270 "folio.trashed",
271 "folio.restored",
272 "folio.deleted",
273 "folio.shared",
274 "folio.stale",
275 ];
276
277 fn published(kind: &str) -> bool {
278 crate::webhooks::EVENT_TYPES.contains(&kind) || UNOFFERED.contains(&kind)
279 }
280
281 #[test]
282 fn every_exact_route_names_a_type_that_is_published() {
283 for (binding, patterns) in ROUTES {
284 for pattern in patterns.iter().chain(LIFECYCLE.iter()) {
285 if pattern.ends_with('*') {
286 let prefix = pattern.trim_end_matches('*');
287 assert!(
288 prefix.is_empty()
289 || crate::webhooks::EVENT_TYPES.iter().chain(UNOFFERED.iter()).any(|kind| kind.starts_with(prefix)),
290 "{binding}: {pattern} matches nothing"
291 );
292 } else {
293 assert!(published(pattern), "{binding}: {pattern} is not a type anything publishes");
294 }
295 }
296 }
297 }
298
299 #[test]
300 fn every_route_is_a_subscriber_binding_named_once() {
301 let mut names: Vec<&str> = ROUTES.iter().map(|(name, _)| *name).collect();
302 assert!(names.iter().all(|name| name.starts_with("SUBSCRIBER_")));
303 let count = names.len();
304 names.sort_unstable();
305 names.dedup();
306 assert_eq!(names.len(), count);
307 assert_eq!(count, 15);
308 }
309
310 #[test]
311 fn subscribers_hear_what_they_act_on_and_not_the_rest() {
312 assert!(routed("SUBSCRIBER_WEBHOOKS", "secret_scanning_alert.created"));
313 assert!(routed("SUBSCRIBER_WEBHOOKS", "anything.new"));
314 assert!(routed("SUBSCRIBER_ACTIONS", "pull.labeled"));
315 assert!(routed("SUBSCRIBER_ACTIONS", "release.published"));
316 assert!(!routed("SUBSCRIBER_ACTIONS", "check_run.completed"));
317 assert!(routed("SUBSCRIBER_RUNNER", "comment.created"));
318 assert!(!routed("SUBSCRIBER_RUNNER", "status.created"));
319 assert!(routed("SUBSCRIBER_INTEGRATIONS", "issue.closed"));
320 assert!(!routed("SUBSCRIBER_INTEGRATIONS", "git.push"));
321 assert!(!routed("SUBSCRIBER_INTEGRATIONS", "check_run.completed"));
322 assert!(routed("SUBSCRIBER_BILLING", "workspace.renamed"));
323 assert!(!routed("SUBSCRIBER_BILLING", "git.push"));
324 assert!(routed("SUBSCRIBER_SEARCH", "user.updated"));
325 assert!(routed("SUBSCRIBER_REPOS", "user.deleting"));
326 assert!(!routed("SUBSCRIBER_REPOS", "git.push"));
327 assert!(routed("SUBSCRIBER_DOCS", "pull.merged"));
328 assert!(!routed("SUBSCRIBER_DOCS", "pull.opened"));
329 assert!(routed("SUBSCRIBER_AGENTS", "git.push"));
330 assert!(routed("SUBSCRIBER_AGENTS", "workspace.member_joined"));
331 assert!(!routed("SUBSCRIBER_AGENTS", "comment.created"));
332 assert!(!routed("SUBSCRIBER_AGENTS", "workspace.updated"));
333 assert!(routed("SUBSCRIBER_PROJECTS", "deployment_status.created"));
334 assert!(!routed("SUBSCRIBER_PROJECTS", "deployment.created"));
335 // Every subscriber follows what moves or removes a repository.
336 for (binding, _) in ROUTES {
337 for kind in LIFECYCLE {
338 assert!(routed(binding, kind), "{binding} misses {kind}");
339 }
340 }
341 }
342
343 #[test]
344 fn a_subscriber_not_in_the_table_hears_everything() {
345 assert!(routed("SUBSCRIBER_NEW", "git.push"));
346 assert!(routed("SUBSCRIBER_NEW", "status.created"));
347 }
348
349 #[test]
350 fn a_family_matches_only_its_own_types() {
351 assert!(matches("pull.*", "pull.opened"));
352 assert!(!matches("pull.*", "pulls.opened"));
353 assert!(!matches("pull.*", "pull"));
354 assert!(matches("*", "x"));
355 assert!(matches("git.push", "git.push"));
356 assert!(!matches("git.push", "git.pushed"));
357 }
358}