Skip to content
505 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 /** `before` is where the ref pointed before; absent for a new branch or tag. */
182 "git.push": {
183 repoId: string;
184 ref: string;
185 before?: string;
186 after: string;
187 defaultBranch: boolean;
188 /** Set when it was copied in from the remote a mirror follows, not made on g1t. */
189 mirrored?: boolean;
190 /** The repository's mirror state when it landed; absent for one that leads. */
191 mirror?: RepoMirror;
192 };
193 /**
194 * `author` is who opened it: g1t, for one its agent filed while at work,
195 * with `requestedBy` the person it was working for. Every issue and pull
196 * request event carries both.
197 */
198 "issue.opened": {
199 issueId: string;
200 repoId: string;
201 number: number;
202 title: string;
203 author?: { id: string; username: string };
204 requestedBy?: { id: string; username: string };
205 };
206 "issue.updated": { issueId: string; repoId: string; number: number };
207 /** The people an issue is assigned to changed; `assignees` is the new set, `added` those newly assigned. */
208 "issue.assigned": { issueId: string; repoId: string; number: number; assignees: string[]; added?: string[] };
209 /** `resolvedBy` is the number of the pull request whose merge closed it. */
210 "issue.closed": {
211 issueId: string;
212 repoId: string;
213 number: number;
214 reason: "completed" | "not_planned";
215 resolvedBy?: number;
216 };
217 "issue.reopened": { issueId: string; repoId: string; number: number };
218 /**
219 * `issue` is the number of the issue the pull request is for. `author` is
220 * who opened it: g1t, for a change g1t made, with `requestedBy` the person
221 * who asked for it.
222 */
223 "pull.opened": {
224 pullId: string;
225 repoId: string;
226 number: number;
227 issue?: number;
228 agent: string;
229 author?: { id: string; username: string };
230 requestedBy?: { id: string; username: string };
231 };
232 /** `confidence`, on a g1t agent's change once g1t has worked it out, is on every pull request event. */
233 "pull.ready": { pullId: string; repoId: string; number: number; issue?: number; confidence?: Confidence };
234 /** A push moved the head of a pull request that is ready for review. */
235 "pull.updated": { pullId: string; repoId: string; number: number; issue?: number; commit: string; confidence?: Confidence };
236 /** A merge was asked for while the pull request was behind; it has to catch up first. */
237 "pull.merge_requested": { pullId: string; repoId: string; number: number; issue?: number; confidence?: Confidence };
238 "pull.closed": { pullId: string; repoId: string; number: number; issue?: number; confidence?: Confidence };
239 /** A closed pull request was opened again. `commit` is its head. */
240 "pull.reopened": { pullId: string; repoId: string; number: number; issue?: number; commit?: string; confidence?: Confidence };
241 /** A pull request that was ready for review was turned back into a draft. */
242 "pull.converted_to_draft": { pullId: string; repoId: string; number: number; issue?: number; confidence?: Confidence };
243 /** People were assigned to a pull request: `assignees` is the new set, `added` those newly assigned. */
244 "pull.assigned": { pullId: string; repoId: string; number: number; issue?: number; assignees: string[]; added: string[] };
245 /** Reviewers were asked for a pull request (`reviewers`), or no longer are. */
246 "pull.review_requested": {
247 pullId: string;
248 repoId: string;
249 number: number;
250 issue?: number;
251 reviewers?: string[];
252 /** Teams asked: `team` is `workspace/slug`, `notified` who is told, `assigned` who review assignment picked. */
253 teams?: TeamRequested[];
254 /** Asked because they own files it changes (its CODEOWNERS file). */
255 codeOwners?: boolean;
256 };
257 "pull.review_request_removed": {
258 pullId: string;
259 repoId: string;
260 number: number;
261 issue?: number;
262 reviewers?: string[];
263 teams?: TeamRequested[];
264 };
265 /** g1t stopped seeing a pull request through until a person steps in; `detail` says why. */
266 "pull.stalled": { pullId: string; repoId: string; number: number; issue?: number; detail: string };
267 /** A pull request g1t had stopped on is going again. */
268 "pull.resumed": { pullId: string; repoId: string; number: number; issue?: number };
269 /**
270 * The pull request's head or its target moved and the files both changed
271 * overlap: a sandbox should find out whether it still merges cleanly.
272 * `commit` is its head.
273 */
274 "pull.mergecheck": { pullId: string; repoId: string; number: number; issue?: number; commit: string };
275 /** Whether the pull request merges cleanly was settled. */
276 "pull.mergeability": { pullId: string; repoId: string; number: number; issue?: number };
277 /** Another agent asked the agent on a pull request, which was not at work, a question or handed it work. */
278 "agent.asked": { pullId: string; repoId: string; number: number; issue?: number };
279 "pull.merged": { pullId: string; repoId: string; number: number; issue?: number; commit: string; confidence?: Confidence };
280 /** A run of the acceptance checks finished. `commit` is what was checked. */
281 "checks.completed": {
282 pullId: string;
283 repoId: string;
284 number: number;
285 status: "passed" | "failed" | "errored";
286 commit: string;
287 };
288 /** A status was set on a commit through the API. */
289 "status.created": StatusEventData;
290 /** A check run was reported on a commit through the API, completed, asked to run again, or had one of its buttons pressed (`requestedAction`). */
291 "check_run.created": CheckRunEventData;
292 "check_run.completed": CheckRunEventData;
293 "check_run.rerequested": CheckRunEventData;
294 "check_run.requested_action": CheckRunEventData;
295 /** A reporter's check runs on a commit all completed, or it was asked to run them again. */
296 "check_suite.completed": CheckSuiteEventData;
297 "check_suite.rerequested": CheckSuiteEventData;
298 /** A g1t agent finished reviewing a pull request; no verdict if it could not. */
299 "review.completed": {
300 pullId: string;
301 repoId: string;
302 number: number;
303 verdict?: "approve" | "request_changes";
304 };
305 /** A person was given a role on one repository directly, had it changed, or lost it. `role` is null once removed. */
306 "repo.collaborator_added": RepoCollaboratorData;
307 "repo.collaborator_removed": RepoCollaboratorData;
308 "repo.collaborator_role_changed": RepoCollaboratorData;
309 /** A team of a workspace was created, changed (`changes` says what) or deleted. */
310 "team.created": TeamChangedData;
311 "team.edited": TeamChangedData;
312 "team.deleted": TeamChangedData;
313 /** Someone joined a team, had their role in it changed, or left it. */
314 "team.member_added": TeamChangedData;
315 "team.member_role_changed": TeamChangedData;
316 "team.member_removed": TeamChangedData;
317 /** A team was given a role on a repository, had it changed, or lost it. */
318 "team.repo_added": TeamChangedData;
319 "team.repo_role_changed": TeamChangedData;
320 "team.repo_removed": TeamChangedData;
321 /** A repository's merge queue gained, lost or settled an entry. */
322 "queue.changed": { repoId: string };
323 /** `number` is the issue or pull request commented on. */
324 "comment.created": {
325 commentId: string;
326 repoId: string;
327 number: number;
328 /** Set when the comment is on a pull request. */
329 pullId?: string;
330 /** Set when the comment is a review. */
331 verdict?: Verdict;
332 };
333 /** A comment's text changed; `changes.body.from` is what it said before. */
334 "comment.edited": {
335 commentId: string;
336 repoId: string;
337 number: number;
338 /** Set when the comment is on a pull request. */
339 pullId?: string;
340 changes: { body: { from: string } };
341 };
342 /** A comment was deleted; `comment` is the comment as it was. */
343 "comment.deleted": {
344 commentId: string;
345 repoId: string;
346 number: number;
347 /** Set when the comment was on a pull request. */
348 pullId?: string;
349 comment: {
350 id: string;
351 body: string;
352 author: { id: string; username: string };
353 createdAt: string;
354 path: string | null;
355 line: number | null;
356 };
357 };
358 "session.appended": { pullId: string; repoId: string; number: number; count: number };
359 /**
360 * A build of a project finished, for production or one pull request's
361 * preview (`number`). `path` is the deployment's page on the site;
362 * `recovered`, on a success, says the build before it failed.
363 */
364 "deployment.succeeded": DeploymentEventData;
365 "deployment.failed": DeploymentEventData;
366 /**
367 * A deployment was made, wherever it runs: reported through the API, by
368 * a g1t Actions job with an `environment:`, or a g1t.page build. Its
369 * `payload` is left out: read the deployment for it.
370 */
371 "deployment.created": { repoId: string; deployment: Omit<RepoDeployment, "payload">; causedByJob?: string };
372 /**
373 * A deployment has a new status; `deployment` is as it is now.
374 * `causedByJob` (as on every event a workflow job's token causes) is the
375 * run whose job made it, so no workflow starts for it.
376 */
377 "deployment_status.created": {
378 repoId: string;
379 deployment: Omit<RepoDeployment, "payload">;
380 deploymentStatus: DeploymentStatus;
381 causedByJob?: string;
382 };
383 /**
384 * A release changed, one event per GitHub release activity it amounts
385 * to: made (`created`; a published one is also `published`, and
386 * `released` or `prereleased`), a draft published, edited, made a draft
387 * again (`unpublished`) or deleted. `release` is as it is now (as it was,
388 * for `release.deleted`).
389 */
390 "release.created": ReleaseEventData;
391 "release.published": ReleaseEventData;
392 "release.released": ReleaseEventData;
393 "release.prereleased": ReleaseEventData;
394 "release.edited": ReleaseEventData;
395 "release.unpublished": ReleaseEventData;
396 "release.deleted": ReleaseEventData;
397 /**
398 * A package version was published, such as an image pushed by
399 * `docker push`. `tags` are the tags that now point to it; `repoId` (and
400 * the event's) is the repository the package is linked to, if any.
401 */
402 "package.published": PackageEventData & { version: string; digest: string; size: number; tags: string[] };
403 /** One version of a package was deleted, with the tags that pointed to it. */
404 "package.version_deleted": PackageEventData & { version: string; digest: string };
405 /** A package was deleted with every version it had. */
406 "package.deleted": PackageEventData;
407 /** A package became public or private. */
408 "package.visibility_changed": PackageEventData & { visibility: "public" | "private" };
409 /**
410 * A workspace's slug changed from `from` to `to`. Services that store a
411 * slug move their rows to the workspace's *current* slug (see
412 * `currentWorkspaceSlug`), so a repeated or late delivery after a second
413 * rename still lands in the right place.
414 */
415 "workspace.renamed": { workspaceId: string; from: string; to: string };
416 /**
417 * A repository moved from workspace `from` to `to`, keeping its id and
418 * name. Services that store its path or its workspace's slug move those
419 * rows to its *current* path (ask repos `path_by_id`), so a repeated or
420 * late delivery after a second transfer still lands in the right place.
421 */
422 "repo.transferred": { repoId: string; name: string; from: string; to: string };
423 /**
424 * A workspace is gone. Services drop what they keep for it alone; ledgers,
425 * invoices and the audit log stay under its slug, which is never reused.
426 */
427 "workspace.deleted": { workspaceId: string; slug: string };
428 /**
429 * An owner deleted a workspace; staff can restore it until `purgeAfter`,
430 * and nobody can reach it meanwhile. Services hide what they keep for it
431 * and stop what runs for it, keeping their rows: repos deletes its
432 * repositories softly (`repo.deleted` with `withWorkspace`), deployments
433 * pauses its apps, search drops it. `workspace.restored` undoes exactly
434 * that; `workspace.deleted` follows once `purgeAfter` passes. `by` is the
435 * owner's username.
436 */
437 "workspace.deleting": { workspaceId: string; slug: string; by: string; purgeAfter: string };
438 /**
439 * Staff brought a deleted workspace back with its members and tokens.
440 * Services undo what they did on `workspace.deleting`, and only that.
441 */
442 "workspace.restored": { workspaceId: string; slug: string };
443 /**
444 * A memory was added, changed, reviewed or forgotten. No text: ask the
445 * work service for it by id. `repoId` is the project's, or null for the
446 * workspace's memory. `status` is `deleted` once forgotten.
447 */
448 "memory.changed": { memoryId: string; workspace: string; status: "candidate" | "kept" | "dismissed" | "deleted" };
449 /**
450 * A sandbox was stopped because it looked like it was mining: CPU pinned
451 * with little I/O and no progress, or a miner seen by name. For g1t's
452 * staff, in sudo; published with no `repoId` so it never reaches a
453 * repository's timeline or webhooks. `metrics` is what the sandbox
454 * measured (`Verdict` in crates/runner abuse.rs), or null if it could
455 * not say.
456 */
457 "abuse.flagged": {
458 workspace: string;
459 repo: string | null;
460 /** The agent run, when the sandbox had one. */
461 run: string | null;
462 /** What the sandbox was for: agent, checks, queue, actions, deploy... */
463 kind: string;
464 sandbox: string;
465 metrics: Record<string, unknown> | null;
466 };
467};
468
469export type EventType = keyof EventPayloads;
470
471export type G1tEvent<T extends EventType = EventType> = {
472 [K in T]: {
473 id: string;
474 type: K;
475 /** The service that published it. */
476 source: string;
477 /** RFC 3339. */
478 time: string;
479 /** The repo the event concerns, used to scope timelines and deliveries. */
480 repoId: string | null;
481 /** The user or agent that caused it, if any. */
482 actor: string | null;
483 data: EventPayloads[K];
484 };
485}[T];
486
487/** What a publisher supplies; the bus fills in `id` and `time`. */
488export type NewEvent<T extends EventType = EventType> = {
489 [K in T]: Omit<G1tEvent<K>, "id" | "time">;
490}[T];
491
492export type EventQuery = {
493 repoId?: string;
494 types?: EventType[];
495 /** Return events older than this event id. */
496 before?: string;
497 limit?: number;
498};
499
500/** The event bus and its durable log. */
501export interface EventsApi {
502 publish(events: NewEvent[]): Promise<void>;
503 /** Newest first. */
504 list(query: EventQuery): Promise<G1tEvent[]>;
505}