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