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