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