Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

g1t.sh
321 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 // Mentions, pull request bases, runs stopped in archived repositories
31 // (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 // and what each production deployment shipped (deployments/src/shipped.ts).
89 (
90 "SUBSCRIBER_DEPLOYMENTS",
91 &[
92 "git.push",
93 "deployment_status.created",
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). A production deployment
108 // reported from elsewhere asks for its screenshot.
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 "comment.created",
122 "deployment.succeeded",
123 "deployment.failed",
124 "deployment_status.created",
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 "comment.created",
160 "repo.created",
161 "repo.visibility_changed",
162 ],
163 ),
164 // The site-wide search index (search/src/index.rs).
165 ("SUBSCRIBER_SEARCH", &["git.push", "repo.*", "issue.*", "pull.*", "user.*", "workspace.*"]),
166 // Composer packages read from repositories, and packages that follow
167 // their workspace, team or repository (packages/src/lib.rs on_event).
168 (
169 "SUBSCRIBER_PACKAGES",
170 &[
171 "git.push",
172 "repo.visibility_changed",
173 "workspace.deleting",
174 "workspace.restored",
175 "team.edited",
176 "team.deleted",
177 ],
178 ),
179 // Pages that cite code a merge or a push to the default branch
180 // changed become possibly out of date, and projects' docs folders shown
181 // in Docs are read again (docs/src/staleness.ts, repo-spaces.ts).
182 ("SUBSCRIBER_DOCS", &["git.push", "pull.merged"]),
183];
184
185/// Whether `pattern` (see the module's notes) matches the type `kind`.
186fn matches(pattern: &str, kind: &str) -> bool {
187 match pattern.strip_suffix('*') {
188 Some(prefix) => kind.starts_with(prefix),
189 None => pattern == kind,
190 }
191}
192
193/// Whether the subscriber behind `binding` is sent events of type `kind`.
194pub fn routed(binding: &str, kind: &str) -> bool {
195 match ROUTES.iter().find(|(name, _)| *name == binding) {
196 Some((_, patterns)) => {
197 LIFECYCLE.contains(&kind) || patterns.iter().any(|pattern| matches(pattern, kind))
198 }
199 None => true,
200 }
201}
202
203#[cfg(test)]
204mod tests {
205 use super::*;
206
207 /// Types published on the bus that hooks are not offered.
208 const UNOFFERED: [&str; 28] = [
209 "pull.mergecheck",
210 "pull.mergeability",
211 "deployment.review_requested",
212 "abuse.flagged",
213 "invite.created",
214 "invite.redeemed",
215 "user.updated",
216 "user.deleting",
217 "user.restored",
218 "user.deleted",
219 "user.email_added",
220 "workspace.updated",
221 "workspace.renamed",
222 "workspace.deleting",
223 "workspace.restored",
224 "workspace.deleted",
225 // Docs pages: no repository, so no repository's hooks.
226 "doc.page.created",
227 "doc.page.updated",
228 "doc.page.archived",
229 "doc.page.stale",
230 "doc.space.deleted",
231 // Artifacts (folios): no repository either, and a folio may be private.
232 "folio.created",
233 "folio.updated",
234 "folio.trashed",
235 "folio.restored",
236 "folio.deleted",
237 "folio.shared",
238 "folio.stale",
239 ];
240
241 fn published(kind: &str) -> bool {
242 crate::webhooks::EVENT_TYPES.contains(&kind) || UNOFFERED.contains(&kind)
243 }
244
245 #[test]
246 fn every_exact_route_names_a_type_that_is_published() {
247 for (binding, patterns) in ROUTES {
248 for pattern in patterns.iter().chain(LIFECYCLE.iter()) {
249 if pattern.ends_with('*') {
250 let prefix = pattern.trim_end_matches('*');
251 assert!(
252 prefix.is_empty()
253 || crate::webhooks::EVENT_TYPES.iter().chain(UNOFFERED.iter()).any(|kind| kind.starts_with(prefix)),
254 "{binding}: {pattern} matches nothing"
255 );
256 } else {
257 assert!(published(pattern), "{binding}: {pattern} is not a type anything publishes");
258 }
259 }
260 }
261 }
262
263 #[test]
264 fn every_route_is_a_subscriber_binding_named_once() {
265 let mut names: Vec<&str> = ROUTES.iter().map(|(name, _)| *name).collect();
266 assert!(names.iter().all(|name| name.starts_with("SUBSCRIBER_")));
267 let count = names.len();
268 names.sort_unstable();
269 names.dedup();
270 assert_eq!(names.len(), count);
271 assert_eq!(count, 13);
272 }
273
274 #[test]
275 fn subscribers_hear_what_they_act_on_and_not_the_rest() {
276 assert!(routed("SUBSCRIBER_WEBHOOKS", "secret_scanning_alert.created"));
277 assert!(routed("SUBSCRIBER_WEBHOOKS", "anything.new"));
278 assert!(routed("SUBSCRIBER_ACTIONS", "pull.labeled"));
279 assert!(routed("SUBSCRIBER_ACTIONS", "release.published"));
280 assert!(!routed("SUBSCRIBER_ACTIONS", "check_run.completed"));
281 assert!(routed("SUBSCRIBER_RUNNER", "comment.created"));
282 assert!(!routed("SUBSCRIBER_RUNNER", "status.created"));
283 assert!(routed("SUBSCRIBER_INTEGRATIONS", "issue.closed"));
284 assert!(!routed("SUBSCRIBER_INTEGRATIONS", "git.push"));
285 assert!(!routed("SUBSCRIBER_INTEGRATIONS", "check_run.completed"));
286 assert!(routed("SUBSCRIBER_BILLING", "workspace.renamed"));
287 assert!(!routed("SUBSCRIBER_BILLING", "git.push"));
288 assert!(routed("SUBSCRIBER_SEARCH", "user.updated"));
289 assert!(routed("SUBSCRIBER_REPOS", "user.deleting"));
290 assert!(!routed("SUBSCRIBER_REPOS", "git.push"));
291 assert!(routed("SUBSCRIBER_DOCS", "pull.merged"));
292 assert!(!routed("SUBSCRIBER_DOCS", "pull.opened"));
293 assert!(routed("SUBSCRIBER_PROJECTS", "deployment_status.created"));
294 assert!(!routed("SUBSCRIBER_PROJECTS", "deployment.created"));
295 // A production deployment's success writes down what it shipped.
296 assert!(routed("SUBSCRIBER_DEPLOYMENTS", "deployment_status.created"));
297 assert!(!routed("SUBSCRIBER_DEPLOYMENTS", "deployment.created"));
298 // Every subscriber follows what moves or removes a repository.
299 for (binding, _) in ROUTES {
300 for kind in LIFECYCLE {
301 assert!(routed(binding, kind), "{binding} misses {kind}");
302 }
303 }
304 }
305
306 #[test]
307 fn a_subscriber_not_in_the_table_hears_everything() {
308 assert!(routed("SUBSCRIBER_NEW", "git.push"));
309 assert!(routed("SUBSCRIBER_NEW", "status.created"));
310 }
311
312 #[test]
313 fn a_family_matches_only_its_own_types() {
314 assert!(matches("pull.*", "pull.opened"));
315 assert!(!matches("pull.*", "pulls.opened"));
316 assert!(!matches("pull.*", "pull"));
317 assert!(matches("*", "x"));
318 assert!(matches("git.push", "git.push"));
319 assert!(!matches("git.push", "git.pushed"));
320 }
321}