Skip to content
515 linesCodeBlameRaw
1/**
2 * Every state change in g1t is published as an event. Services react to
3 * each other through events rather than direct calls, and the same stream
4 * feeds timelines, webhooks and workflows.
5 *
6 * The envelope follows CloudEvents: `type` says what happened, `subject`
7 * says to what, `data` is the type-specific payload.
8 */
9
10import type { Release } from "./about";
11import type { RepoRole } from "./access";
12import type { CheckRunEventData, CheckSuiteEventData, StatusEventData } from "./checks";
13import type { RepoMirror } from "./mirrors";
14import type { DeploymentStatus, RepoDeployment } from "./deployments";
15import type { TeamRole, TeamVisibility } from "./teams";
16import type { Confidence, Verdict } from "./work";
17
18/** What every `package.*` event names. */
19export type PackageEventData = {
20 packageId: string;
21 workspace: string;
22 ecosystem: string;
23 name: string;
24 repoId: string | null;
25};
26
27/** What `deployment.succeeded` and `deployment.failed` carry. */
28export type DeploymentEventData = {
29 deploymentId: string;
30 projectId: string;
31 repoId: string;
32 workspace: string;
33 project: string;
34 kind: "production" | "preview";
35 branch: string | null;
36 number: number | null;
37 commit: string;
38 path: string;
39 error: string | null;
40 recovered: boolean;
41 /** A username, or `g1t`. */
42 triggeredBy: string;
43};
44
45/** What every `release.*` event carries. */
46export type ReleaseEventData = {
47 releaseId: string;
48 repoId: string;
49 tagName: string;
50 release: Release;
51 /** On `release.edited`: what the title and notes were before. */
52 changes?: { name?: { from: string | null }; body?: { from: string } };
53 /** Set when a workflow job's token made the change: the run's id. */
54 causedByJob?: string;
55};
56
57/** The payload of the `repo.collaborator_*` events. */
58export type RepoCollaboratorData = {
59 repoId: string;
60 namespace: string;
61 name: string;
62 username: string;
63 role: RepoRole | null;
64 previousRole: RepoRole | null;
65};
66/** A team asked to review a pull request. */
67export type TeamRequested = { team: string; notified: string[]; assigned: string[] };
68
69/** The payload of the `team.*` events; each sets the fields that apply. */
70export type TeamChangedData = {
71 workspace: string;
72 teamId: string;
73 team: string;
74 name: string;
75 visibility: TeamVisibility | null;
76 parent?: string;
77 changes?: string[];
78 username?: string;
79 role?: TeamRole;
80 previousRole?: TeamRole;
81 repoId?: string;
82 repo?: string;
83 repoRole?: RepoRole;
84 previousRepoRole?: RepoRole;
85};
86
87export type EventPayloads = {
88 "repo.created": { repoId: string; namespace: string; name: string; isPrivate: boolean };
89 "repo.forked": { repoId: string; sourceRepoId: string; pullId: string };
90 /**
91 * A repository's description, topics or visibility changed.
92 * `visibilityChanged` says whether it went public or private, which
93 * `repo.visibility_changed` also announces on its own.
94 */
95 "repo.updated": { repoId: string; namespace: string; name: string; isPrivate: boolean; visibilityChanged: boolean };
96 "repo.visibility_changed": { repoId: string; isPrivate: boolean };
97 /**
98 * A repository's name changed within its workspace `namespace`, from
99 * `from` to `to`, keeping its id. A path change like `repo.transferred`:
100 * services move rows kept under its path to its *current* path (see
101 * `repoMove`, `currentMovedPath`).
102 */
103 "repo.renamed": { repoId: string; namespace: string; from: string; to: string };
104 /**
105 * A repository was deleted. It is hidden and git refuses it, but it can
106 * be restored until `purgeAfter`: services stop what runs for it and hide
107 * it, and keep their rows until `repo.purged`.
108 */
109 "repo.deleted": {
110 repoId: string;
111 namespace: string;
112 name: string;
113 isPrivate: boolean;
114 purgeAfter: string;
115 /** It went with its workspace (`workspace.deleting`); deployments leaves it to that. */
116 withWorkspace?: boolean;
117 };
118 /** A deleted repository is back, as it was. Services start again what they stopped. */
119 "repo.restored": {
120 repoId: string;
121 namespace: string;
122 name: string;
123 isPrivate: boolean;
124 /** It came back with its workspace (`workspace.restored`). */
125 withWorkspace?: boolean;
126 };
127 /**
128 * A deleted repository is gone for good, its git data with it. Services
129 * drop every row they keep for it, except a workspace's history (ledgers,
130 * invoices, the audit log).
131 */
132 "repo.purged": { repoId: string; namespace: string; name: string };
133 /**
134 * A repository was archived (read-only: pushes, merges, agents and
135 * workflows refused; issues and pull requests locked; deployments keep
136 * serving), or unarchived.
137 */
138 "repo.archived": { repoId: string; namespace: string; name: string; archived: true };
139 "repo.unarchived": { repoId: string; namespace: string; name: string; archived: false };
140 /** The default branch is now `to`; `renamed` when `from` was renamed to it. */
141 "repo.default_branch_changed": { repoId: string; from: string; to: string; renamed: boolean };
142 /** A branch was renamed. Pull requests from it follow. */
143 "branch.renamed": { repoId: string; from: string; to: string; defaultBranch: boolean };
144 /** An account was made, or changed what its profile shows. Ask identity for the profile. */
145 "user.updated": { username: string };
146 /**
147 * An account was deleted, by the person or by g1t's staff; staff can
148 * restore it until `purgeAfter`. Its sessions, tokens and keys have ended
149 * and it has left every workspace. Services stop what they do for it and
150 * keep their rows; `user.restored` undoes that, and `user.deleted` follows
151 * once `purgeAfter` passes.
152 */
153 "user.deleting": { userId: string; username: string; byStaff: boolean; purgeAfter: string };
154 /** Staff brought a deleted account back. Services undo what they did on `user.deleting`. */
155 "user.restored": { userId: string; username: string };
156 /**
157 * An account is gone for good. Services drop what they keep for it alone
158 * and show what it wrote as `ghost` (`GHOST_USERNAME`, `GHOST_ID`).
159 * Ledgers, invoices and audit logs keep its username, which is never given
160 * to anyone again.
161 */
162 "user.deleted": { userId: string; username: string };
163 /** A workspace was made, or its name, description or icon changed. */
164 "workspace.updated": { workspaceId: string; slug: string };
165 /** Someone, or g1t staff, made an invite. Never the code or the address. */
166 "invite.created": { inviteId: string; inviterId: string | null; workspaceId: string | null; bound: boolean };
167 /** An invite was used: by a new account, or by an account joining a workspace. */
168 "invite.redeemed": {
169 inviteId: string;
170 userId: string;
171 inviterId: string | null;
172 workspaceId: string | null;
173 createdAccount: boolean;
174 };
175 /** Someone asked for access while registration is invite-only. The address is not in the event. */
176 "waitlist.requested": { entryId: string };
177 /**
178 * One branch moved by a push. `ref` is the full ref, `after` the commit it
179 * points to now, and `defaultBranch` whether it is the default branch.
180 */
181 /**
182 * `before` is where the ref pointed before; absent for a new branch or
183 * tag. `causedByJob` is set when a workflow job's token pushed: the run's id.
184 */
185 "git.push": {
186 repoId: string;
187 ref: string;
188 before?: string;
189 after: string;
190 defaultBranch: boolean;
191 causedByJob?: string;
192 /** Set when it was copied in from the remote a mirror follows, not made on g1t. */
193 mirrored?: boolean;
194 /** The repository's mirror state when it landed; absent for one that leads. */
195 mirror?: RepoMirror;
196 };
197 /**
198 * `author` is who opened it: g1t, for one its agent filed while at work,
199 * with `requestedBy` the person it was working for. Every issue and pull
200 * request event carries both.
201 */
202 "issue.opened": {
203 issueId: string;
204 repoId: string;
205 number: number;
206 title: string;
207 author?: { id: string; username: string };
208 requestedBy?: { id: string; username: string };
209 };
210 "issue.updated": { issueId: string; repoId: string; number: number };
211 /** The people an issue is assigned to changed; `assignees` is the new set, `added` those newly assigned. */
212 "issue.assigned": { issueId: string; repoId: string; number: number; assignees: string[]; added?: string[] };
213 /** `resolvedBy` is the number of the pull request whose merge closed it. */
214 "issue.closed": {
215 issueId: string;
216 repoId: string;
217 number: number;
218 reason: "completed" | "not_planned";
219 resolvedBy?: number;
220 };
221 "issue.reopened": { issueId: string; repoId: string; number: number };
222 /**
223 * `issue` is the number of the issue the pull request is for. `author` is
224 * who opened it: g1t, for a change g1t made, with `requestedBy` the person
225 * who asked for it.
226 */
227 "pull.opened": {
228 pullId: string;
229 repoId: string;
230 number: number;
231 issue?: number;
232 agent: string;
233 author?: { id: string; username: string };
234 requestedBy?: { id: string; username: string };
235 };
236 /** `confidence`, on a g1t agent's change once g1t has worked it out, is on every pull request event. */
237 "pull.ready": { pullId: string; repoId: string; number: number; issue?: number; confidence?: Confidence };
238 /** A push moved the head of a pull request that is ready for review. */
239 "pull.updated": { pullId: string; repoId: string; number: number; issue?: number; commit: string; confidence?: Confidence };
240 /** A merge was asked for while the pull request was behind; it has to catch up first. */
241 "pull.merge_requested": { pullId: string; repoId: string; number: number; issue?: number; confidence?: Confidence };
242 "pull.closed": { pullId: string; repoId: string; number: number; issue?: number; confidence?: Confidence };
243 /** A closed pull request was opened again. `commit` is its head. */
244 "pull.reopened": { pullId: string; repoId: string; number: number; issue?: number; commit?: string; confidence?: Confidence };
245 /** A pull request that was ready for review was turned back into a draft. */
246 "pull.converted_to_draft": { pullId: string; repoId: string; number: number; issue?: number; confidence?: Confidence };
247 /** People were assigned to a pull request: `assignees` is the new set, `added` those newly assigned. */
248 "pull.assigned": { pullId: string; repoId: string; number: number; issue?: number; assignees: string[]; added: string[] };
249 /** Reviewers were asked for a pull request (`reviewers`), or no longer are. */
250 "pull.review_requested": {
251 pullId: string;
252 repoId: string;
253 number: number;
254 issue?: number;
255 reviewers?: string[];
256 /** Teams asked: `team` is `workspace/slug`, `notified` who is told, `assigned` who review assignment picked. */
257 teams?: TeamRequested[];
258 /** Asked because they own files it changes (its CODEOWNERS file). */
259 codeOwners?: boolean;
260 };
261 "pull.review_request_removed": {
262 pullId: string;
263 repoId: string;
264 number: number;
265 issue?: number;
266 reviewers?: string[];
267 teams?: TeamRequested[];
268 };
269 /** g1t stopped seeing a pull request through until a person steps in; `detail` says why. */
270 "pull.stalled": { pullId: string; repoId: string; number: number; issue?: number; detail: string };
271 /** A pull request g1t had stopped on is going again. */
272 "pull.resumed": { pullId: string; repoId: string; number: number; issue?: number };
273 /**
274 * The pull request's head or its target moved and the files both changed
275 * overlap: a sandbox should find out whether it still merges cleanly.
276 * `commit` is its head.
277 */
278 "pull.mergecheck": { pullId: string; repoId: string; number: number; issue?: number; commit: string };
279 /** Whether the pull request merges cleanly was settled. */
280 "pull.mergeability": { pullId: string; repoId: string; number: number; issue?: number };
281 /** Another agent asked the agent on a pull request, which was not at work, a question or handed it work. */
282 "agent.asked": { pullId: string; repoId: string; number: number; issue?: number };
283 "pull.merged": { pullId: string; repoId: string; number: number; issue?: number; commit: string; confidence?: Confidence };
284 /** A run of the acceptance checks finished. `commit` is what was checked. */
285 "checks.completed": {
286 pullId: string;
287 repoId: string;
288 number: number;
289 status: "passed" | "failed" | "errored";
290 commit: string;
291 };
292 /** A status was set on a commit through the API. */
293 "status.created": StatusEventData;
294 /** A check run was reported on a commit through the API, completed, asked to run again, or had one of its buttons pressed (`requestedAction`). */
295 "check_run.created": CheckRunEventData;
296 "check_run.completed": CheckRunEventData;
297 "check_run.rerequested": CheckRunEventData;
298 "check_run.requested_action": CheckRunEventData;
299 /** A reporter's check runs on a commit all completed, or it was asked to run them again. */
300 "check_suite.completed": CheckSuiteEventData;
301 "check_suite.rerequested": CheckSuiteEventData;
302 /** A g1t agent finished reviewing a pull request; no verdict if it could not. */
303 "review.completed": {
304 pullId: string;
305 repoId: string;
306 number: number;
307 verdict?: "approve" | "request_changes";
308 };
309 /** A person was given a role on one repository directly, had it changed, or lost it. `role` is null once removed. */
310 "repo.collaborator_added": RepoCollaboratorData;
311 "repo.collaborator_removed": RepoCollaboratorData;
312 "repo.collaborator_role_changed": RepoCollaboratorData;
313 /** A team of a workspace was created, changed (`changes` says what) or deleted. */
314 "team.created": TeamChangedData;
315 "team.edited": TeamChangedData;
316 "team.deleted": TeamChangedData;
317 /** Someone joined a team, had their role in it changed, or left it. */
318 "team.member_added": TeamChangedData;
319 "team.member_role_changed": TeamChangedData;
320 "team.member_removed": TeamChangedData;
321 /** A team was given a role on a repository, had it changed, or lost it. */
322 "team.repo_added": TeamChangedData;
323 "team.repo_role_changed": TeamChangedData;
324 "team.repo_removed": TeamChangedData;
325 /** A repository's merge queue gained, lost or settled an entry. */
326 "queue.changed": { repoId: string };
327 /** `number` is the issue or pull request commented on. */
328 "comment.created": {
329 commentId: string;
330 repoId: string;
331 number: number;
332 /** Set when the comment is on a pull request. */
333 pullId?: string;
334 /** Set when the comment is a review. */
335 verdict?: Verdict;
336 };
337 /** A comment's text changed; `changes.body.from` is what it said before. */
338 "comment.edited": {
339 commentId: string;
340 repoId: string;
341 number: number;
342 /** Set when the comment is on a pull request. */
343 pullId?: string;
344 changes: { body: { from: string } };
345 };
346 /** A comment was deleted; `comment` is the comment as it was. */
347 "comment.deleted": {
348 commentId: string;
349 repoId: string;
350 number: number;
351 /** Set when the comment was on a pull request. */
352 pullId?: string;
353 comment: {
354 id: string;
355 body: string;
356 author: { id: string; username: string };
357 createdAt: string;
358 path: string | null;
359 line: number | null;
360 };
361 };
362 "session.appended": { pullId: string; repoId: string; number: number; count: number };
363 /**
364 * A build of a project finished, for production or one pull request's
365 * preview (`number`). `path` is the deployment's page on the site;
366 * `recovered`, on a success, says the build before it failed.
367 */
368 "deployment.succeeded": DeploymentEventData;
369 "deployment.failed": DeploymentEventData;
370 /**
371 * A deployment was made, wherever it runs: reported through the API, by
372 * a g1t Actions job with an `environment:`, or a g1t.page build. Its
373 * `payload` is left out: read the deployment for it.
374 */
375 "deployment.created": { repoId: string; deployment: Omit<RepoDeployment, "payload">; causedByJob?: string };
376 /**
377 * A deployment has a new status; `deployment` is as it is now.
378 * `causedByJob` (as on every event a workflow job's token causes) is the
379 * run whose job made it, so no workflow starts for it.
380 */
381 "deployment_status.created": {
382 repoId: string;
383 deployment: Omit<RepoDeployment, "payload">;
384 deploymentStatus: DeploymentStatus;
385 causedByJob?: string;
386 };
387 /**
388 * A release changed, one event per GitHub release activity it amounts
389 * to: made (`created`; a published one is also `published`, and
390 * `released` or `prereleased`), a draft published, edited, made a draft
391 * again (`unpublished`) or deleted. `release` is as it is now (as it was,
392 * for `release.deleted`).
393 */
394 "release.created": ReleaseEventData;
395 "release.published": ReleaseEventData;
396 "release.released": ReleaseEventData;
397 "release.prereleased": ReleaseEventData;
398 "release.edited": ReleaseEventData;
399 "release.unpublished": ReleaseEventData;
400 "release.deleted": ReleaseEventData;
401 /**
402 * A package version was published, such as an image pushed by
403 * `docker push`. `tags` are the tags that now point to it; `repoId` (and
404 * the event's) is the repository the package is linked to, if any.
405 */
406 "package.published": PackageEventData & { version: string; digest: string; size: number; tags: string[] };
407 /** One version of a package was deleted, with the tags that pointed to it. */
408 "package.version_deleted": PackageEventData & { version: string; digest: string };
409 /** A package was deleted with every version it had. */
410 "package.deleted": PackageEventData;
411 /** A package became public or private. */
412 "package.visibility_changed": PackageEventData & { visibility: "public" | "private" };
413 /**
414 * A workspace's slug changed from `from` to `to`. Services that store a
415 * slug move their rows to the workspace's *current* slug (see
416 * `currentWorkspaceSlug`), so a repeated or late delivery after a second
417 * rename still lands in the right place.
418 */
419 "workspace.renamed": { workspaceId: string; from: string; to: string };
420 /**
421 * A repository moved from workspace `from` to `to`, keeping its id and
422 * name. Services that store its path or its workspace's slug move those
423 * rows to its *current* path (ask repos `path_by_id`), so a repeated or
424 * late delivery after a second transfer still lands in the right place.
425 */
426 "repo.transferred": { repoId: string; name: string; from: string; to: string };
427 /**
428 * A workspace is gone. Services drop what they keep for it alone; ledgers,
429 * invoices and the audit log stay under its slug, which is never reused.
430 */
431 "workspace.deleted": { workspaceId: string; slug: string };
432 /**
433 * An owner deleted a workspace; staff can restore it until `purgeAfter`,
434 * and nobody can reach it meanwhile. Services hide what they keep for it
435 * and stop what runs for it, keeping their rows: repos deletes its
436 * repositories softly (`repo.deleted` with `withWorkspace`), deployments
437 * pauses its apps, search drops it. `workspace.restored` undoes exactly
438 * that; `workspace.deleted` follows once `purgeAfter` passes. `by` is the
439 * owner's username.
440 */
441 "workspace.deleting": { workspaceId: string; slug: string; by: string; purgeAfter: string };
442 /**
443 * Staff brought a deleted workspace back with its members and tokens.
444 * Services undo what they did on `workspace.deleting`, and only that.
445 */
446 "workspace.restored": { workspaceId: string; slug: string };
447 /**
448 * A memory was added, changed, reviewed or forgotten. No text: ask the
449 * work service for it by id. `repoId` is the project's, or null for the
450 * workspace's memory. `status` is `deleted` once forgotten.
451 */
452 "memory.changed": { memoryId: string; workspace: string; status: "candidate" | "kept" | "dismissed" | "deleted" };
453 /**
454 * A sandbox was stopped because it looked like it was mining: CPU pinned
455 * with little I/O and no progress, or a miner seen by name. For g1t's
456 * staff, in sudo; published with no `repoId` so it never reaches a
457 * repository's timeline or webhooks. `metrics` is what the sandbox
458 * measured (`Verdict` in crates/runner abuse.rs), or null if it could
459 * not say.
460 */
461 "abuse.flagged": {
462 workspace: string;
463 repo: string | null;
464 /** The agent run, when the sandbox had one. */
465 run: string | null;
466 /** What the sandbox was for: agent, checks, queue, actions, deploy... */
467 kind: string;
468 sandbox: string;
469 metrics: Record<string, unknown> | null;
470 };
471};
472
473export type EventType = keyof EventPayloads;
474
475export type G1tEvent<T extends EventType = EventType> = {
476 [K in T]: {
477 id: string;
478 type: K;
479 /** The service that published it. */
480 source: string;
481 /** RFC 3339. */
482 time: string;
483 /** The repo the event concerns, used to scope timelines and deliveries. */
484 repoId: string | null;
485 /** The user or agent that caused it, if any. */
486 actor: string | null;
487 data: EventPayloads[K];
488 };
489}[T];
490
491/** What a publisher supplies; the bus fills in `id` and `time`. */
492export type NewEvent<T extends EventType = EventType> = {
493 [K in T]: Omit<G1tEvent<K>, "id" | "time">;
494}[T];
495
496export type EventQuery = {
497 repoId?: string;
498 types?: EventType[];
499 /** Return events older than this event id. */
500 before?: string;
501 /** Only events by this account id. */
502 actor?: string;
503 /** Only events about these issues or pull requests (`number`, or the `issue` a comment or review is on). */
504 numbers?: number[];
505 /** Only events at or after this RFC 3339 time. */
506 since?: string;
507 limit?: number;
508};
509
510/** The event bus and its durable log. */
511export interface EventsApi {
512 publish(events: NewEvent[]): Promise<void>;
513 /** Newest first. */
514 list(query: EventQuery): Promise<G1tEvent[]>;
515}