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