g1t/crates/contracts/src/events.rs
| 1 | //! Events published on the bus, and the events service that carries them. |
| 2 | //! Mirrors `packages/contracts/src/events.ts`. |
| 3 | |
| 4 | use 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")] |
| 9 | pub 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")] |
| 23 | pub 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")] |
| 32 | pub 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")] |
| 41 | pub 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")] |
| 58 | pub 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")] |
| 79 | pub 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. On update and when |
| 89 | /// marked ready: the head of the change. |
| 90 | #[serde(skip_serializing_if = "Option::is_none")] |
| 91 | pub commit: Option<String>, |
| 92 | /// On close: the pull request that was merged instead. |
| 93 | #[serde(skip_serializing_if = "Option::is_none")] |
| 94 | pub superseded_by: Option<u32>, |
| 95 | } |
| 96 | |
| 97 | /// `checks.completed`: a run of an issue's acceptance checks against a pull |
| 98 | /// request finished. |
| 99 | #[derive(Debug, Serialize)] |
| 100 | #[serde(rename_all = "camelCase")] |
| 101 | pub struct ChecksEvent { |
| 102 | pub pull_id: String, |
| 103 | pub repo_id: String, |
| 104 | pub number: u32, |
| 105 | /// `passed`, `failed` or `errored`. |
| 106 | pub status: &'static str, |
| 107 | /// The commit that was checked. |
| 108 | pub commit: String, |
| 109 | } |
| 110 | |
| 111 | /// `workflow.completed`: a GitHub Actions run finished. |
| 112 | #[derive(Debug, Serialize)] |
| 113 | #[serde(rename_all = "camelCase")] |
| 114 | pub struct WorkflowEvent { |
| 115 | pub run_id: String, |
| 116 | pub repo_id: String, |
| 117 | /// The workflow's name, and its file. |
| 118 | pub workflow: String, |
| 119 | pub path: String, |
| 120 | /// The run's number among the workflow's runs. |
| 121 | pub number: u64, |
| 122 | /// The GitHub event that started it, such as `push`. |
| 123 | pub event: String, |
| 124 | /// `success`, `failure`, `cancelled` or `skipped`. |
| 125 | pub conclusion: String, |
| 126 | #[serde(rename = "ref")] |
| 127 | pub git_ref: String, |
| 128 | pub sha: String, |
| 129 | /// The pull request it ran for, if any. |
| 130 | #[serde(skip_serializing_if = "Option::is_none")] |
| 131 | pub pull: Option<u32>, |
| 132 | } |
| 133 | |
| 134 | /// `review.completed`: a g1t agent finished reviewing a pull request, or |
| 135 | /// could not. |
| 136 | #[derive(Debug, Serialize)] |
| 137 | #[serde(rename_all = "camelCase")] |
| 138 | pub struct ReviewEvent { |
| 139 | pub pull_id: String, |
| 140 | pub repo_id: String, |
| 141 | pub number: u32, |
| 142 | /// `approve` or `request_changes`; absent when no review was written. |
| 143 | #[serde(skip_serializing_if = "Option::is_none")] |
| 144 | pub verdict: Option<&'static str>, |
| 145 | } |
| 146 | |
| 147 | /// `comment.created`. `number` is the issue or pull request commented on. |
| 148 | #[derive(Debug, Serialize)] |
| 149 | #[serde(rename_all = "camelCase")] |
| 150 | pub struct CommentCreated { |
| 151 | pub comment_id: String, |
| 152 | pub repo_id: String, |
| 153 | pub number: u32, |
| 154 | /// Set when the comment is on a pull request. |
| 155 | #[serde(skip_serializing_if = "Option::is_none")] |
| 156 | pub pull_id: Option<String>, |
| 157 | /// Set when the comment is a review: approve or request changes. |
| 158 | #[serde(skip_serializing_if = "Option::is_none")] |
| 159 | pub verdict: Option<crate::work::Verdict>, |
| 160 | } |
| 161 | |
| 162 | #[derive(Debug, Serialize)] |
| 163 | #[serde(rename_all = "camelCase")] |
| 164 | pub struct SessionAppended { |
| 165 | pub pull_id: String, |
| 166 | pub repo_id: String, |
| 167 | pub number: u32, |
| 168 | pub count: u32, |
| 169 | } |
| 170 | |
| 171 | /// An event as stored in the log and delivered to subscribers. `data` is |
| 172 | /// left as JSON; each reader decodes the types it cares about. |
| 173 | #[derive(Clone, Debug, Serialize, serde::Deserialize)] |
| 174 | #[serde(rename_all = "camelCase")] |
| 175 | pub struct Event { |
| 176 | /// Sorts by the time the event was published. |
| 177 | pub id: String, |
| 178 | #[serde(rename = "type")] |
| 179 | pub kind: String, |
| 180 | /// The service that published it. |
| 181 | pub source: String, |
| 182 | /// RFC 3339. |
| 183 | pub time: String, |
| 184 | /// The repo the event concerns. |
| 185 | pub repo_id: Option<String>, |
| 186 | /// The user or agent that caused it, if any. |
| 187 | pub actor: Option<String>, |
| 188 | pub data: serde_json::Value, |
| 189 | } |
| 190 | |
| 191 | /// `publish`, as a publisher sends it. Returns nothing. |
| 192 | #[derive(Debug, Serialize)] |
| 193 | pub struct Publish<T: Serialize> { |
| 194 | pub events: Vec<NewEvent<T>>, |
| 195 | } |
| 196 | |
| 197 | /// `publish`, as the events service reads it. |
| 198 | #[derive(Debug, serde::Deserialize)] |
| 199 | pub struct PublishArgs { |
| 200 | pub events: Vec<Published>, |
| 201 | } |
| 202 | |
| 203 | /// A [`NewEvent`] of any type, as received. |
| 204 | #[derive(Debug, serde::Deserialize)] |
| 205 | #[serde(rename_all = "camelCase")] |
| 206 | pub struct Published { |
| 207 | #[serde(rename = "type")] |
| 208 | pub kind: String, |
| 209 | pub source: String, |
| 210 | #[serde(default)] |
| 211 | pub repo_id: Option<String>, |
| 212 | #[serde(default)] |
| 213 | pub actor: Option<String>, |
| 214 | pub data: serde_json::Value, |
| 215 | } |
| 216 | |
| 217 | /// `list`: events from the log, newest first. Returns `Vec<Event>`. |
| 218 | #[derive(Debug, Default, Serialize, serde::Deserialize)] |
| 219 | #[serde(rename_all = "camelCase")] |
| 220 | pub struct ListArgs { |
| 221 | #[serde(default)] |
| 222 | pub repo_id: Option<String>, |
| 223 | /// Only these types; all types when empty. |
| 224 | #[serde(default)] |
| 225 | pub types: Vec<String>, |
| 226 | /// Only events older than this event id. |
| 227 | #[serde(default)] |
| 228 | pub before: Option<String>, |
| 229 | #[serde(default)] |
| 230 | pub limit: Option<u32>, |
| 231 | } |
| 232 | |
| 233 | /// `queue.changed`: a repository's merge queue gained, lost or settled an |
| 234 | /// entry, so the next batch may be ready to test. |
| 235 | #[derive(Debug, Serialize)] |
| 236 | #[serde(rename_all = "camelCase")] |
| 237 | pub struct QueueChanged { |
| 238 | pub repo_id: String, |
| 239 | } |