Skip to content
350 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 // Push mirrors and write-back to linked trackers (integrations/src).
69 ("SUBSCRIBER_INTEGRATIONS", &["git.push", "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).
109 (
110 "SUBSCRIBER_PROJECTS",
111 &[
112 "git.push",
113 "repo.*",
114 "issue.opened",
115 "issue.closed",
116 "issue.reopened",
117 "pull.opened",
118 "pull.updated",
119 "pull.merged",
120 "pull.closed",
121 "review.completed",
122 "comment.created",
123 "deployment.succeeded",
124 "deployment.failed",
125 "package.published",
126 "package.deleted",
127 "package.version_deleted",
128 "workspace.deleting",
129 "workspace.restored",
130 ],
131 ),
132 // Pull request working copies, workspaces' repositories, contributors
133 // shown as ghost (repos/src/lib.rs handle_event).
134 (
135 "SUBSCRIBER_REPOS",
136 &[
137 "pull.merged",
138 "pull.closed",
139 "pull.reopened",
140 "workspace.deleting",
141 "workspace.restored",
142 "user.deleting",
143 "user.restored",
144 ],
145 ),
146 // Usage rows that follow a renamed workspace or repository.
147 ("SUBSCRIBER_BILLING", &[]),
148 // Secret and dependency scans, and version update pull requests
149 // (security/src/lib.rs on_event).
150 (
151 "SUBSCRIBER_SECURITY",
152 &[
153 "git.push",
154 "pull.opened",
155 "pull.updated",
156 "pull.ready",
157 "pull.merged",
158 "pull.closed",
159 "checks.completed",
160 "comment.created",
161 "repo.created",
162 "repo.visibility_changed",
163 ],
164 ),
165 // What agents are told about a repository (context/src/index.ts).
166 (
167 "SUBSCRIBER_CONTEXT",
168 &[
169 "git.push",
170 "issue.opened",
171 "issue.updated",
172 "issue.closed",
173 "pull.ready",
174 "pull.merged",
175 "memory.changed",
176 ],
177 ),
178 // The site-wide search index (search/src/index.rs).
179 ("SUBSCRIBER_SEARCH", &["git.push", "repo.*", "issue.*", "pull.*", "user.*", "workspace.*"]),
180 // Composer packages read from repositories, and packages that follow
181 // their workspace, team or repository (packages/src/lib.rs on_event).
182 (
183 "SUBSCRIBER_PACKAGES",
184 &[
185 "git.push",
186 "repo.visibility_changed",
187 "workspace.deleting",
188 "workspace.restored",
189 "team.edited",
190 "team.deleted",
191 ],
192 ),
193 // Pages that cite code a merge or a push to the default branch
194 // changed become possibly out of date, and projects' docs folders shown
195 // in Docs are read again (docs/src/staleness.ts, repo-spaces.ts).
196 ("SUBSCRIBER_DOCS", &["git.push", "pull.merged"]),
197 // Agents' routines that run on events: a pull request ready for
198 // review or merged, checks or a deploy failing, an issue opened
199 // (agents/src/triggers.ts); pushes, which skill libraries that
200 // follow a repository read again (agents/src/skill-library.ts); and
201 // a person joining a workspace, whom its @g1t welcomes
202 // (agents/src/welcome.ts).
203 (
204 "SUBSCRIBER_AGENTS",
205 &[
206 "git.push",
207 "pull.opened",
208 "pull.ready",
209 "pull.merged",
210 "checks.completed",
211 "issue.opened",
212 "deployment.failed",
213 "workspace.member_joined",
214 ],
215 ),
216];
217
218/// Whether `pattern` (see the module's notes) matches the type `kind`.
219fn matches(pattern: &str, kind: &str) -> bool {
220 match pattern.strip_suffix('*') {
221 Some(prefix) => kind.starts_with(prefix),
222 None => pattern == kind,
223 }
224}
225
226/// Whether the subscriber behind `binding` is sent events of type `kind`.
227pub fn routed(binding: &str, kind: &str) -> bool {
228 match ROUTES.iter().find(|(name, _)| *name == binding) {
229 Some((_, patterns)) => {
230 LIFECYCLE.contains(&kind) || patterns.iter().any(|pattern| matches(pattern, kind))
231 }
232 None => true,
233 }
234}
235
236#[cfg(test)]
237mod tests {
238 use super::*;
239
240 /// Types published on the bus that hooks are not offered.
241 const UNOFFERED: [&str; 28] = [
242 "pull.mergecheck",
243 "pull.mergeability",
244 "deployment.review_requested",
245 "memory.changed",
246 "abuse.flagged",
247 "invite.created",
248 "invite.redeemed",
249 "waitlist.requested",
250 "user.updated",
251 "user.deleting",
252 "user.restored",
253 "user.deleted",
254 "user.email_added",
255 "workspace.updated",
256 "workspace.renamed",
257 "workspace.deleting",
258 "workspace.restored",
259 "workspace.deleted",
260 // Docs pages: no repository, so no repository's hooks.
261 "doc.page.created",
262 "doc.page.updated",
263 "doc.page.archived",
264 "doc.page.stale",
265 // Artifacts (folios): no repository either, and a folio may be private.
266 "folio.created",
267 "folio.updated",
268 "folio.trashed",
269 "folio.restored",
270 "folio.shared",
271 "folio.stale",
272 ];
273
274 fn published(kind: &str) -> bool {
275 crate::webhooks::EVENT_TYPES.contains(&kind) || UNOFFERED.contains(&kind)
276 }
277
278 #[test]
279 fn every_exact_route_names_a_type_that_is_published() {
280 for (binding, patterns) in ROUTES {
281 for pattern in patterns.iter().chain(LIFECYCLE.iter()) {
282 if pattern.ends_with('*') {
283 let prefix = pattern.trim_end_matches('*');
284 assert!(
285 prefix.is_empty()
286 || crate::webhooks::EVENT_TYPES.iter().chain(UNOFFERED.iter()).any(|kind| kind.starts_with(prefix)),
287 "{binding}: {pattern} matches nothing"
288 );
289 } else {
290 assert!(published(pattern), "{binding}: {pattern} is not a type anything publishes");
291 }
292 }
293 }
294 }
295
296 #[test]
297 fn every_route_is_a_subscriber_binding_named_once() {
298 let mut names: Vec<&str> = ROUTES.iter().map(|(name, _)| *name).collect();
299 assert!(names.iter().all(|name| name.starts_with("SUBSCRIBER_")));
300 let count = names.len();
301 names.sort_unstable();
302 names.dedup();
303 assert_eq!(names.len(), count);
304 assert_eq!(count, 15);
305 }
306
307 #[test]
308 fn subscribers_hear_what_they_act_on_and_not_the_rest() {
309 assert!(routed("SUBSCRIBER_WEBHOOKS", "secret_scanning_alert.created"));
310 assert!(routed("SUBSCRIBER_WEBHOOKS", "anything.new"));
311 assert!(routed("SUBSCRIBER_ACTIONS", "pull.labeled"));
312 assert!(routed("SUBSCRIBER_ACTIONS", "release.published"));
313 assert!(!routed("SUBSCRIBER_ACTIONS", "check_run.completed"));
314 assert!(routed("SUBSCRIBER_RUNNER", "comment.created"));
315 assert!(!routed("SUBSCRIBER_RUNNER", "status.created"));
316 assert!(routed("SUBSCRIBER_BILLING", "workspace.renamed"));
317 assert!(!routed("SUBSCRIBER_BILLING", "git.push"));
318 assert!(routed("SUBSCRIBER_SEARCH", "user.updated"));
319 assert!(routed("SUBSCRIBER_REPOS", "user.deleting"));
320 assert!(!routed("SUBSCRIBER_REPOS", "git.push"));
321 assert!(routed("SUBSCRIBER_DOCS", "pull.merged"));
322 assert!(!routed("SUBSCRIBER_DOCS", "pull.opened"));
323 assert!(routed("SUBSCRIBER_AGENTS", "git.push"));
324 assert!(routed("SUBSCRIBER_AGENTS", "workspace.member_joined"));
325 assert!(!routed("SUBSCRIBER_AGENTS", "comment.created"));
326 assert!(!routed("SUBSCRIBER_AGENTS", "workspace.updated"));
327 // Every subscriber follows what moves or removes a repository.
328 for (binding, _) in ROUTES {
329 for kind in LIFECYCLE {
330 assert!(routed(binding, kind), "{binding} misses {kind}");
331 }
332 }
333 }
334
335 #[test]
336 fn a_subscriber_not_in_the_table_hears_everything() {
337 assert!(routed("SUBSCRIBER_NEW", "git.push"));
338 assert!(routed("SUBSCRIBER_NEW", "status.created"));
339 }
340
341 #[test]
342 fn a_family_matches_only_its_own_types() {
343 assert!(matches("pull.*", "pull.opened"));
344 assert!(!matches("pull.*", "pulls.opened"));
345 assert!(!matches("pull.*", "pull"));
346 assert!(matches("*", "x"));
347 assert!(matches("git.push", "git.push"));
348 assert!(!matches("git.push", "git.pushed"));
349 }
350}