Skip to content
356 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, write-back to linked trackers, and commit statuses
69 // reported to a GitHub mirror's commits (integrations/src lib.rs,
70 // remotes.rs on_status).
71 ("SUBSCRIBER_INTEGRATIONS", &["git.push", "issue.closed", "pull.opened", "status.created"]),
72 // A hook may ask for any type, or for every one.
73 ("SUBSCRIBER_WEBHOOKS", &["*"]),
74 // Every event a workflow can run on (g1t_actions::events::github_events),
75 // and runs stopped in archived repositories.
76 (
77 "SUBSCRIBER_ACTIONS",
78 &[
79 "git.push",
80 "git.deleted",
81 "pull.*",
82 "issue.*",
83 "comment.*",
84 "release.*",
85 "deployment.created",
86 "deployment_status.created",
87 "review.completed",
88 "workflow.completed",
89 "repo.archived",
90 ],
91 ),
92 // Previews and production deploys (deployments/src/index.ts queue).
93 (
94 "SUBSCRIBER_DEPLOYMENTS",
95 &[
96 "git.push",
97 "branch.renamed",
98 "repo.default_branch_changed",
99 "pull.opened",
100 "pull.ready",
101 "pull.updated",
102 "pull.reopened",
103 "pull.closed",
104 "pull.merged",
105 "workspace.deleting",
106 "workspace.restored",
107 ],
108 ),
109 // Projects follow their repositories, and wake on activity
110 // (projects/src/index.ts, activity.ts).
111 (
112 "SUBSCRIBER_PROJECTS",
113 &[
114 "git.push",
115 "repo.*",
116 "issue.opened",
117 "issue.closed",
118 "issue.reopened",
119 "pull.opened",
120 "pull.updated",
121 "pull.merged",
122 "pull.closed",
123 "review.completed",
124 "comment.created",
125 "deployment.succeeded",
126 "deployment.failed",
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 "checks.completed",
162 "comment.created",
163 "repo.created",
164 "repo.visibility_changed",
165 ],
166 ),
167 // What agents are told about a repository (context/src/index.ts).
168 (
169 "SUBSCRIBER_CONTEXT",
170 &[
171 "git.push",
172 "issue.opened",
173 "issue.updated",
174 "issue.closed",
175 "pull.ready",
176 "pull.merged",
177 "memory.changed",
178 ],
179 ),
180 // The site-wide search index (search/src/index.rs).
181 ("SUBSCRIBER_SEARCH", &["git.push", "repo.*", "issue.*", "pull.*", "user.*", "workspace.*"]),
182 // Composer packages read from repositories, and packages that follow
183 // their workspace, team or repository (packages/src/lib.rs on_event).
184 (
185 "SUBSCRIBER_PACKAGES",
186 &[
187 "git.push",
188 "repo.visibility_changed",
189 "workspace.deleting",
190 "workspace.restored",
191 "team.edited",
192 "team.deleted",
193 ],
194 ),
195 // Pages that cite code a merge or a push to the default branch
196 // changed become possibly out of date, and projects' docs folders shown
197 // in Docs are read again (docs/src/staleness.ts, repo-spaces.ts).
198 ("SUBSCRIBER_DOCS", &["git.push", "pull.merged"]),
199 // Agents' routines that run on events: a pull request ready for
200 // review or merged, checks or a deploy failing, an issue opened
201 // (agents/src/triggers.ts); pushes, which skill libraries that
202 // follow a repository read again (agents/src/skill-library.ts); and
203 // a person joining a workspace, whom its @g1t welcomes
204 // (agents/src/welcome.ts).
205 (
206 "SUBSCRIBER_AGENTS",
207 &[
208 "git.push",
209 "pull.opened",
210 "pull.ready",
211 "pull.merged",
212 "checks.completed",
213 "issue.opened",
214 "deployment.failed",
215 "workspace.member_joined",
216 ],
217 ),
218];
219
220/// Whether `pattern` (see the module's notes) matches the type `kind`.
221fn matches(pattern: &str, kind: &str) -> bool {
222 match pattern.strip_suffix('*') {
223 Some(prefix) => kind.starts_with(prefix),
224 None => pattern == kind,
225 }
226}
227
228/// Whether the subscriber behind `binding` is sent events of type `kind`.
229pub fn routed(binding: &str, kind: &str) -> bool {
230 match ROUTES.iter().find(|(name, _)| *name == binding) {
231 Some((_, patterns)) => {
232 LIFECYCLE.contains(&kind) || patterns.iter().any(|pattern| matches(pattern, kind))
233 }
234 None => true,
235 }
236}
237
238#[cfg(test)]
239mod tests {
240 use super::*;
241
242 /// Types published on the bus that hooks are not offered.
243 const UNOFFERED: [&str; 30] = [
244 "pull.mergecheck",
245 "pull.mergeability",
246 "deployment.review_requested",
247 "memory.changed",
248 "abuse.flagged",
249 "invite.created",
250 "invite.redeemed",
251 "waitlist.requested",
252 "user.updated",
253 "user.deleting",
254 "user.restored",
255 "user.deleted",
256 "user.email_added",
257 "workspace.updated",
258 "workspace.renamed",
259 "workspace.deleting",
260 "workspace.restored",
261 "workspace.deleted",
262 // Docs pages: no repository, so no repository's hooks.
263 "doc.page.created",
264 "doc.page.updated",
265 "doc.page.archived",
266 "doc.page.stale",
267 "doc.space.deleted",
268 // Artifacts (folios): no repository either, and a folio may be private.
269 "folio.created",
270 "folio.updated",
271 "folio.trashed",
272 "folio.restored",
273 "folio.deleted",
274 "folio.shared",
275 "folio.stale",
276 ];
277
278 fn published(kind: &str) -> bool {
279 crate::webhooks::EVENT_TYPES.contains(&kind) || UNOFFERED.contains(&kind)
280 }
281
282 #[test]
283 fn every_exact_route_names_a_type_that_is_published() {
284 for (binding, patterns) in ROUTES {
285 for pattern in patterns.iter().chain(LIFECYCLE.iter()) {
286 if pattern.ends_with('*') {
287 let prefix = pattern.trim_end_matches('*');
288 assert!(
289 prefix.is_empty()
290 || crate::webhooks::EVENT_TYPES.iter().chain(UNOFFERED.iter()).any(|kind| kind.starts_with(prefix)),
291 "{binding}: {pattern} matches nothing"
292 );
293 } else {
294 assert!(published(pattern), "{binding}: {pattern} is not a type anything publishes");
295 }
296 }
297 }
298 }
299
300 #[test]
301 fn every_route_is_a_subscriber_binding_named_once() {
302 let mut names: Vec<&str> = ROUTES.iter().map(|(name, _)| *name).collect();
303 assert!(names.iter().all(|name| name.starts_with("SUBSCRIBER_")));
304 let count = names.len();
305 names.sort_unstable();
306 names.dedup();
307 assert_eq!(names.len(), count);
308 assert_eq!(count, 15);
309 }
310
311 #[test]
312 fn subscribers_hear_what_they_act_on_and_not_the_rest() {
313 assert!(routed("SUBSCRIBER_WEBHOOKS", "secret_scanning_alert.created"));
314 assert!(routed("SUBSCRIBER_WEBHOOKS", "anything.new"));
315 assert!(routed("SUBSCRIBER_ACTIONS", "pull.labeled"));
316 assert!(routed("SUBSCRIBER_ACTIONS", "release.published"));
317 assert!(!routed("SUBSCRIBER_ACTIONS", "check_run.completed"));
318 assert!(routed("SUBSCRIBER_RUNNER", "comment.created"));
319 assert!(!routed("SUBSCRIBER_RUNNER", "status.created"));
320 assert!(routed("SUBSCRIBER_INTEGRATIONS", "status.created"));
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 // Every subscriber follows what moves or removes a repository.
334 for (binding, _) in ROUTES {
335 for kind in LIFECYCLE {
336 assert!(routed(binding, kind), "{binding} misses {kind}");
337 }
338 }
339 }
340
341 #[test]
342 fn a_subscriber_not_in_the_table_hears_everything() {
343 assert!(routed("SUBSCRIBER_NEW", "git.push"));
344 assert!(routed("SUBSCRIBER_NEW", "status.created"));
345 }
346
347 #[test]
348 fn a_family_matches_only_its_own_types() {
349 assert!(matches("pull.*", "pull.opened"));
350 assert!(!matches("pull.*", "pulls.opened"));
351 assert!(!matches("pull.*", "pull"));
352 assert!(matches("*", "x"));
353 assert!(matches("git.push", "git.push"));
354 assert!(!matches("git.push", "git.pushed"));
355 }
356}