Skip to content
353 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 "pull.*",
81 "issue.*",
82 "comment.*",
83 "release.*",
84 "deployment.created",
85 "deployment_status.created",
86 "review.completed",
87 "workflow.completed",
88 "repo.archived",
89 ],
90 ),
91 // Previews and production deploys (deployments/src/index.ts queue).
92 (
93 "SUBSCRIBER_DEPLOYMENTS",
94 &[
95 "git.push",
96 "branch.renamed",
97 "repo.default_branch_changed",
98 "pull.opened",
99 "pull.ready",
100 "pull.updated",
101 "pull.reopened",
102 "pull.closed",
103 "pull.merged",
104 "workspace.deleting",
105 "workspace.restored",
106 ],
107 ),
108 // Projects follow their repositories, and wake on activity
109 // (projects/src/index.ts, activity.ts).
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 "package.published",
127 "package.deleted",
128 "package.version_deleted",
129 "workspace.deleting",
130 "workspace.restored",
131 ],
132 ),
133 // Pull request working copies, workspaces' repositories, contributors
134 // shown as ghost (repos/src/lib.rs handle_event).
135 (
136 "SUBSCRIBER_REPOS",
137 &[
138 "pull.merged",
139 "pull.closed",
140 "pull.reopened",
141 "workspace.deleting",
142 "workspace.restored",
143 "user.deleting",
144 "user.restored",
145 ],
146 ),
147 // Usage rows that follow a renamed workspace or repository.
148 ("SUBSCRIBER_BILLING", &[]),
149 // Secret and dependency scans, and version update pull requests
150 // (security/src/lib.rs on_event).
151 (
152 "SUBSCRIBER_SECURITY",
153 &[
154 "git.push",
155 "pull.opened",
156 "pull.updated",
157 "pull.ready",
158 "pull.merged",
159 "pull.closed",
160 "checks.completed",
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; 28] = [
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 // Artifacts (folios): no repository either, and a folio may be private.
267 "folio.created",
268 "folio.updated",
269 "folio.trashed",
270 "folio.restored",
271 "folio.shared",
272 "folio.stale",
273 ];
274
275 fn published(kind: &str) -> bool {
276 crate::webhooks::EVENT_TYPES.contains(&kind) || UNOFFERED.contains(&kind)
277 }
278
279 #[test]
280 fn every_exact_route_names_a_type_that_is_published() {
281 for (binding, patterns) in ROUTES {
282 for pattern in patterns.iter().chain(LIFECYCLE.iter()) {
283 if pattern.ends_with('*') {
284 let prefix = pattern.trim_end_matches('*');
285 assert!(
286 prefix.is_empty()
287 || crate::webhooks::EVENT_TYPES.iter().chain(UNOFFERED.iter()).any(|kind| kind.starts_with(prefix)),
288 "{binding}: {pattern} matches nothing"
289 );
290 } else {
291 assert!(published(pattern), "{binding}: {pattern} is not a type anything publishes");
292 }
293 }
294 }
295 }
296
297 #[test]
298 fn every_route_is_a_subscriber_binding_named_once() {
299 let mut names: Vec<&str> = ROUTES.iter().map(|(name, _)| *name).collect();
300 assert!(names.iter().all(|name| name.starts_with("SUBSCRIBER_")));
301 let count = names.len();
302 names.sort_unstable();
303 names.dedup();
304 assert_eq!(names.len(), count);
305 assert_eq!(count, 15);
306 }
307
308 #[test]
309 fn subscribers_hear_what_they_act_on_and_not_the_rest() {
310 assert!(routed("SUBSCRIBER_WEBHOOKS", "secret_scanning_alert.created"));
311 assert!(routed("SUBSCRIBER_WEBHOOKS", "anything.new"));
312 assert!(routed("SUBSCRIBER_ACTIONS", "pull.labeled"));
313 assert!(routed("SUBSCRIBER_ACTIONS", "release.published"));
314 assert!(!routed("SUBSCRIBER_ACTIONS", "check_run.completed"));
315 assert!(routed("SUBSCRIBER_RUNNER", "comment.created"));
316 assert!(!routed("SUBSCRIBER_RUNNER", "status.created"));
317 assert!(routed("SUBSCRIBER_INTEGRATIONS", "status.created"));
318 assert!(!routed("SUBSCRIBER_INTEGRATIONS", "check_run.completed"));
319 assert!(routed("SUBSCRIBER_BILLING", "workspace.renamed"));
320 assert!(!routed("SUBSCRIBER_BILLING", "git.push"));
321 assert!(routed("SUBSCRIBER_SEARCH", "user.updated"));
322 assert!(routed("SUBSCRIBER_REPOS", "user.deleting"));
323 assert!(!routed("SUBSCRIBER_REPOS", "git.push"));
324 assert!(routed("SUBSCRIBER_DOCS", "pull.merged"));
325 assert!(!routed("SUBSCRIBER_DOCS", "pull.opened"));
326 assert!(routed("SUBSCRIBER_AGENTS", "git.push"));
327 assert!(routed("SUBSCRIBER_AGENTS", "workspace.member_joined"));
328 assert!(!routed("SUBSCRIBER_AGENTS", "comment.created"));
329 assert!(!routed("SUBSCRIBER_AGENTS", "workspace.updated"));
330 // Every subscriber follows what moves or removes a repository.
331 for (binding, _) in ROUTES {
332 for kind in LIFECYCLE {
333 assert!(routed(binding, kind), "{binding} misses {kind}");
334 }
335 }
336 }
337
338 #[test]
339 fn a_subscriber_not_in_the_table_hears_everything() {
340 assert!(routed("SUBSCRIBER_NEW", "git.push"));
341 assert!(routed("SUBSCRIBER_NEW", "status.created"));
342 }
343
344 #[test]
345 fn a_family_matches_only_its_own_types() {
346 assert!(matches("pull.*", "pull.opened"));
347 assert!(!matches("pull.*", "pulls.opened"));
348 assert!(!matches("pull.*", "pull"));
349 assert!(matches("*", "x"));
350 assert!(matches("git.push", "git.push"));
351 assert!(!matches("git.push", "git.pushed"));
352 }
353}