pr_01m47d15m3e54sn21z27rpy5n9/crates/contracts/src/events.rs

215 lines6,822 bytesCodeBlame
1//! Events published on the bus, and the events service that carries them.
2//! Mirrors `packages/contracts/src/events.ts`.
3
4use serde::Serialize;
5
6/// What a publisher supplies; the bus fills in the id and time.
7#[derive(Debug, Serialize)]
8#[serde(rename_all = "camelCase")]
9pub struct NewEvent<T: Serialize> {
10 #[serde(rename = "type")]
11 pub kind: &'static str,
12 /// The service that published it.
13 pub source: &'static str,
14 /// The repo the event concerns.
15 pub repo_id: Option<String>,
16 /// The user or agent that caused it, if any.
17 pub actor: Option<String>,
18 pub data: T,
19}
20
21#[derive(Debug, Serialize)]
22#[serde(rename_all = "camelCase")]
23pub struct RepoCreated {
24 pub repo_id: String,
25 pub namespace: String,
26 pub name: String,
27 pub is_private: bool,
28}
29
30#[derive(Debug, Serialize)]
31#[serde(rename_all = "camelCase")]
32pub struct RepoForked {
33 pub repo_id: String,
34 pub source_repo_id: String,
35 pub pull_id: String,
36}
37
38/// One branch or tag moved by a push. `after` is the commit it points to now.
39#[derive(Debug, Serialize)]
40#[serde(rename_all = "camelCase")]
41pub struct GitPush {
42 pub repo_id: String,
43 /// The full ref, such as `refs/heads/main`.
44 #[serde(rename = "ref")]
45 pub git_ref: String,
46 /// Where it pointed before; absent for a new branch or tag.
47 #[serde(skip_serializing_if = "Option::is_none")]
48 pub before: Option<String>,
49 pub after: String,
50 /// Whether the ref is the repository's default branch.
51 pub default_branch: bool,
52}
53
54/// The payload of `issue.opened`, `issue.updated`, `issue.assigned`,
55/// `issue.closed` and `issue.reopened`; each uses the fields that apply to it.
56#[derive(Debug, Default, Serialize)]
57#[serde(rename_all = "camelCase")]
58pub struct IssueEvent {
59 pub issue_id: String,
60 pub repo_id: String,
61 pub number: u32,
62 #[serde(skip_serializing_if = "Option::is_none")]
63 pub title: Option<String>,
64 /// On close: `completed` or `not_planned`.
65 #[serde(skip_serializing_if = "Option::is_none")]
66 pub reason: Option<&'static str>,
67 /// On close: the number of the pull request whose merge closed it.
68 #[serde(skip_serializing_if = "Option::is_none")]
69 pub resolved_by: Option<u32>,
70 /// On `issue.assigned`: the people it is now assigned to.
71 #[serde(skip_serializing_if = "Option::is_none")]
72 pub assignees: Option<Vec<String>>,
73}
74
75/// The payload of `pull.opened`, `pull.ready`, `pull.updated` (its head
76/// moved), `pull.closed` and `pull.merged`; each uses the fields that apply to it.
77#[derive(Debug, Default, Serialize)]
78#[serde(rename_all = "camelCase")]
79pub struct PullEvent {
80 pub pull_id: String,
81 pub repo_id: String,
82 pub number: u32,
83 /// The number of the issue it is for.
84 #[serde(skip_serializing_if = "Option::is_none")]
85 pub issue: Option<u32>,
86 #[serde(skip_serializing_if = "Option::is_none")]
87 pub agent: Option<String>,
88 /// On merge: the commit the branch now points to.
89 #[serde(skip_serializing_if = "Option::is_none")]
90 pub commit: Option<String>,
91 /// On close: the pull request that was merged instead.
92 #[serde(skip_serializing_if = "Option::is_none")]
93 pub superseded_by: Option<u32>,
94}
95
96/// `checks.completed`: a run of an issue's acceptance checks against a pull
97/// request finished.
98#[derive(Debug, Serialize)]
99#[serde(rename_all = "camelCase")]
100pub struct ChecksEvent {
101 pub pull_id: String,
102 pub repo_id: String,
103 pub number: u32,
104 /// `passed`, `failed` or `errored`.
105 pub status: &'static str,
106 /// The commit that was checked.
107 pub commit: String,
108}
109
110/// `review.completed`: a g1t agent finished reviewing a pull request, or
111/// could not.
112#[derive(Debug, Serialize)]
113#[serde(rename_all = "camelCase")]
114pub struct ReviewEvent {
115 pub pull_id: String,
116 pub repo_id: String,
117 pub number: u32,
118 /// `approve` or `request_changes`; absent when no review was written.
119 #[serde(skip_serializing_if = "Option::is_none")]
120 pub verdict: Option<&'static str>,
121}
122
123/// `comment.created`. `number` is the issue or pull request commented on.
124#[derive(Debug, Serialize)]
125#[serde(rename_all = "camelCase")]
126pub struct CommentCreated {
127 pub comment_id: String,
128 pub repo_id: String,
129 pub number: u32,
130 /// Set when the comment is on a pull request.
131 #[serde(skip_serializing_if = "Option::is_none")]
132 pub pull_id: Option<String>,
133 /// Set when the comment is a review: approve or request changes.
134 #[serde(skip_serializing_if = "Option::is_none")]
135 pub verdict: Option<crate::work::Verdict>,
136}
137
138#[derive(Debug, Serialize)]
139#[serde(rename_all = "camelCase")]
140pub struct SessionAppended {
141 pub pull_id: String,
142 pub repo_id: String,
143 pub number: u32,
144 pub count: u32,
145}
146
147/// An event as stored in the log and delivered to subscribers. `data` is
148/// left as JSON; each reader decodes the types it cares about.
149#[derive(Clone, Debug, Serialize, serde::Deserialize)]
150#[serde(rename_all = "camelCase")]
151pub struct Event {
152 /// Sorts by the time the event was published.
153 pub id: String,
154 #[serde(rename = "type")]
155 pub kind: String,
156 /// The service that published it.
157 pub source: String,
158 /// RFC 3339.
159 pub time: String,
160 /// The repo the event concerns.
161 pub repo_id: Option<String>,
162 /// The user or agent that caused it, if any.
163 pub actor: Option<String>,
164 pub data: serde_json::Value,
165}
166
167/// `publish`, as a publisher sends it. Returns nothing.
168#[derive(Debug, Serialize)]
169pub struct Publish<T: Serialize> {
170 pub events: Vec<NewEvent<T>>,
171}
172
173/// `publish`, as the events service reads it.
174#[derive(Debug, serde::Deserialize)]
175pub struct PublishArgs {
176 pub events: Vec<Published>,
177}
178
179/// A [`NewEvent`] of any type, as received.
180#[derive(Debug, serde::Deserialize)]
181#[serde(rename_all = "camelCase")]
182pub struct Published {
183 #[serde(rename = "type")]
184 pub kind: String,
185 pub source: String,
186 #[serde(default)]
187 pub repo_id: Option<String>,
188 #[serde(default)]
189 pub actor: Option<String>,
190 pub data: serde_json::Value,
191}
192
193/// `list`: events from the log, newest first. Returns `Vec<Event>`.
194#[derive(Debug, Default, Serialize, serde::Deserialize)]
195#[serde(rename_all = "camelCase")]
196pub struct ListArgs {
197 #[serde(default)]
198 pub repo_id: Option<String>,
199 /// Only these types; all types when empty.
200 #[serde(default)]
201 pub types: Vec<String>,
202 /// Only events older than this event id.
203 #[serde(default)]
204 pub before: Option<String>,
205 #[serde(default)]
206 pub limit: Option<u32>,
207}
208
209/// `queue.changed`: a repository's merge queue gained, lost or settled an
210/// entry, so the next batch may be ready to test.
211#[derive(Debug, Serialize)]
212#[serde(rename_all = "camelCase")]
213pub struct QueueChanged {
214 pub repo_id: String,
215}