| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (c) 2026 UtopiaIA |
| 3 | |
| 4 | //! Change requests: reviews, comments and landing (section 19): the bodies |
| 5 | //! of the routes under `/v1/r/{owner}/{name}/requests`. |
| 6 | //! |
| 7 | //! A change request proposes the work of a branch, its head, for another |
| 8 | //! branch of the repository, its base: from the same repository, or from |
| 9 | //! another one of its fork network. What it proposes is pinned: its |
| 10 | //! snapshot is the changeset its head held when it was opened or last |
| 11 | //! updated, each update is a new revision, and landing names the snapshot |
| 12 | //! it lands, so that nothing lands that was not shown. Change requests and |
| 13 | //! issues share one numbering in their repository. Ids are 64 lowercase |
| 14 | //! hex digits; times are RFC 3339, in UTC; logins are lowercase. |
| 15 | |
| 16 | use braid_format::ObjectId; |
| 17 | use serde::{Deserialize, Serialize}; |
| 18 | |
| 19 | use crate::api::{LandMode, LandResponse}; |
| 20 | use crate::cards::RepoRef; |
| 21 | use crate::hex; |
| 22 | use crate::history::ChangesetSummary; |
| 23 | use crate::issues::Comment; |
| 24 | |
| 25 | /// A change request that is open. |
| 26 | pub const OPEN: &str = "open"; |
| 27 | |
| 28 | /// A change request closed without landing. |
| 29 | pub const CLOSED: &str = "closed"; |
| 30 | |
| 31 | /// A change request whose work landed on its base. |
| 32 | pub const LANDED: &str = "landed"; |
| 33 | |
| 34 | /// A review that approves the snapshot it reviewed. |
| 35 | pub const APPROVE: &str = "approve"; |
| 36 | |
| 37 | /// A review that asks for changes before it lands. |
| 38 | pub const REQUEST_CHANGES: &str = "request_changes"; |
| 39 | |
| 40 | /// A review that only comments. |
| 41 | pub const COMMENT: &str = "comment"; |
| 42 | |
| 43 | /// The verdicts of a review. |
| 44 | pub const VERDICTS: [&str; 3] = [APPROVE, REQUEST_CHANGES, COMMENT]; |
| 45 | |
| 46 | /// Whether `verdict` is one a review may give. |
| 47 | pub fn is_verdict(verdict: &str) -> bool { |
| 48 | VERDICTS.contains(&verdict) |
| 49 | } |
| 50 | |
| 51 | /// A request that lands on its base as it stands: no conflict. |
| 52 | pub const CLEAN: &str = "clean"; |
| 53 | |
| 54 | /// A request whose work conflicts with its base as it stands. |
| 55 | pub const CONFLICTS: &str = "conflicts"; |
| 56 | |
| 57 | /// A request whose stack is no longer built on its base, which moved to a |
| 58 | /// history without the changeset the stack starts from: its author updates |
| 59 | /// it before it lands. |
| 60 | pub const NEEDS_UPDATE: &str = "needs_update"; |
| 61 | |
| 62 | /// A request whose mergeability is not worked out yet: ask again in a |
| 63 | /// moment, or `POST …/check`. |
| 64 | pub const UNKNOWN: &str = "unknown"; |
| 65 | |
| 66 | /// The side of a diff a line comment is on: the version before. |
| 67 | pub const OLD: &str = "old"; |
| 68 | |
| 69 | /// The side of a diff a line comment is on: the version after, the |
| 70 | /// default. |
| 71 | pub const NEW: &str = "new"; |
| 72 | |
| 73 | /// What a new request proposes: a branch, in the target repository or in |
| 74 | /// another repository of its fork network. |
| 75 | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 76 | pub struct HeadSpec { |
| 77 | /// The repository the branch is in, `owner/name`; the target itself |
| 78 | /// when left out. |
| 79 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 80 | pub repo: Option<String>, |
| 81 | /// The branch. |
| 82 | #[serde(rename = "ref")] |
| 83 | pub reference: String, |
| 84 | } |
| 85 | |
| 86 | /// `POST …/requests`: a new change request. |
| 87 | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 88 | pub struct NewRequest { |
| 89 | /// Its title: 1 to 256 characters, one line. |
| 90 | pub title: String, |
| 91 | /// Its text, Markdown, at most 64 KiB. |
| 92 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 93 | pub body: Option<String>, |
| 94 | /// What it proposes. |
| 95 | pub head: HeadSpec, |
| 96 | /// The branch of the target it proposes it for; its default branch |
| 97 | /// when left out. |
| 98 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 99 | pub base: Option<String>, |
| 100 | /// For a head in a private repository other than the target: that its |
| 101 | /// admin publishes the request's work to everyone who reads the target. |
| 102 | #[serde(default, skip_serializing_if = "is_false")] |
| 103 | pub expose: bool, |
| 104 | /// Whether it opens as a draft, which does not land until it is marked |
| 105 | /// ready. |
| 106 | #[serde(default, skip_serializing_if = "is_false")] |
| 107 | pub draft: bool, |
| 108 | /// Accounts asked to review it: the target's writers only. |
| 109 | #[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 110 | pub reviewers: Vec<String>, |
| 111 | /// The label of the agent that opens it, recorded with it. |
| 112 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 113 | pub agent: Option<String>, |
| 114 | } |
| 115 | |
| 116 | fn is_false(value: &bool) -> bool { |
| 117 | !*value |
| 118 | } |
| 119 | |
| 120 | /// The head of a change request, as its snapshot pins it. |
| 121 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 122 | pub struct RequestHead { |
| 123 | /// The repository it comes from: `null` for a caller who may not read |
| 124 | /// that repository, or once it is gone. |
| 125 | #[serde(default)] |
| 126 | pub repo: Option<RepoRef>, |
| 127 | /// The branch. |
| 128 | #[serde(rename = "ref")] |
| 129 | pub reference: String, |
| 130 | /// The changeset the branch held at the current revision: what the |
| 131 | /// request proposes, and what landing names. |
| 132 | #[serde(with = "hex::id")] |
| 133 | pub snapshot: ObjectId, |
| 134 | /// Whether the hub still holds what the snapshot brings, to show, |
| 135 | /// fetch and land: false once a closed request's work was let go. |
| 136 | #[serde(default)] |
| 137 | pub available: bool, |
| 138 | } |
| 139 | |
| 140 | /// The base of a change request. |
| 141 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 142 | pub struct RequestBase { |
| 143 | /// The branch. |
| 144 | #[serde(rename = "ref")] |
| 145 | pub reference: String, |
| 146 | /// Its head when the current revision was made. |
| 147 | #[serde(default, with = "hex::opt_id")] |
| 148 | pub head_at_snapshot: Option<ObjectId>, |
| 149 | /// The best common ancestor of that head and the snapshot. |
| 150 | #[serde(default, with = "hex::opt_id")] |
| 151 | pub merge_base: Option<ObjectId>, |
| 152 | } |
| 153 | |
| 154 | /// Whether a change request lands cleanly on its base now. |
| 155 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 156 | pub struct Mergeable { |
| 157 | /// `clean`, `conflicts`, `needs_update` or `unknown`. |
| 158 | pub state: String, |
| 159 | /// The base's head it was worked out against. |
| 160 | #[serde(default, with = "hex::opt_id")] |
| 161 | pub against: Option<ObjectId>, |
| 162 | /// With `conflicts`: the conflicts of the first changeset that does |
| 163 | /// not land, as `braid land --json` prints them. |
| 164 | #[serde(default)] |
| 165 | pub conflicts: Vec<serde_json::Value>, |
| 166 | } |
| 167 | |
| 168 | impl Default for Mergeable { |
| 169 | fn default() -> Self { |
| 170 | Mergeable { |
| 171 | state: UNKNOWN.to_owned(), |
| 172 | against: None, |
| 173 | conflicts: Vec::new(), |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | /// The approvals a change request has at its current revision. |
| 179 | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 180 | pub struct Approvals { |
| 181 | /// How many writers approved it, besides its author. |
| 182 | pub count: u64, |
| 183 | /// How many it needs before it lands. |
| 184 | pub required: u32, |
| 185 | /// Who approved it. |
| 186 | #[serde(default)] |
| 187 | pub by: Vec<String>, |
| 188 | } |
| 189 | |
| 190 | /// The last landing of a change request that did not land it. |
| 191 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 192 | pub struct Attempt { |
| 193 | /// `conflicts` or `refused`, as the landing answered. |
| 194 | pub outcome: String, |
| 195 | /// Who tried. |
| 196 | pub by: String, |
| 197 | /// The label of the agent that tried, if one said. |
| 198 | #[serde(default)] |
| 199 | pub agent: Option<String>, |
| 200 | /// When. |
| 201 | pub at: String, |
| 202 | /// The base's head then. |
| 203 | #[serde(default, with = "hex::opt_id")] |
| 204 | pub head: Option<ObjectId>, |
| 205 | /// With `conflicts`: what conflicted, as `braid land --json` prints |
| 206 | /// it. |
| 207 | #[serde(default)] |
| 208 | pub conflicts: Vec<serde_json::Value>, |
| 209 | } |
| 210 | |
| 211 | /// How a change request landed. |
| 212 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 213 | pub struct Landing { |
| 214 | /// The base's head once it landed. |
| 215 | #[serde(with = "hex::id")] |
| 216 | pub head: ObjectId, |
| 217 | /// The op log operation that moved the base, `None` when the base had |
| 218 | /// the work already. |
| 219 | #[serde(default)] |
| 220 | pub operation: Option<u64>, |
| 221 | /// Who landed it. |
| 222 | pub by: String, |
| 223 | /// The label of the agent that landed it, if one said. |
| 224 | #[serde(default)] |
| 225 | pub agent: Option<String>, |
| 226 | /// When. |
| 227 | pub at: String, |
| 228 | } |
| 229 | |
| 230 | /// A change request. |
| 231 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 232 | pub struct Request { |
| 233 | /// Its number in its repository. |
| 234 | pub number: u64, |
| 235 | /// Its title. |
| 236 | pub title: String, |
| 237 | /// Its text, Markdown. |
| 238 | #[serde(default)] |
| 239 | pub body: String, |
| 240 | /// `open`, `closed` or `landed`. |
| 241 | pub state: String, |
| 242 | /// Whether it is a draft, which does not land. |
| 243 | #[serde(default)] |
| 244 | pub draft: bool, |
| 245 | /// Who opened it. |
| 246 | pub author: String, |
| 247 | /// The label of the agent that opened it, if one said. |
| 248 | #[serde(default)] |
| 249 | pub agent: Option<String>, |
| 250 | /// What it proposes. |
| 251 | pub head: RequestHead, |
| 252 | /// Where it lands. |
| 253 | pub base: RequestBase, |
| 254 | /// Its revision: 1 when it opens, one more at each update. |
| 255 | pub revision: u64, |
| 256 | /// The changesets it lands, oldest first: the snapshot's first-parent |
| 257 | /// chain down to an ancestor of the base. Given by the routes that |
| 258 | /// answer one request. |
| 259 | #[serde(default)] |
| 260 | pub stack: Vec<ChangesetSummary>, |
| 261 | /// What the stack's merges bring in besides, parents before children. |
| 262 | #[serde(default)] |
| 263 | pub brings: Vec<ChangesetSummary>, |
| 264 | /// Whether it lands cleanly now. |
| 265 | #[serde(default)] |
| 266 | pub mergeable: Mergeable, |
| 267 | /// Its approvals. |
| 268 | #[serde(default)] |
| 269 | pub approvals: Approvals, |
| 270 | /// The accounts asked to review it. |
| 271 | #[serde(default)] |
| 272 | pub review_requests: Vec<String>, |
| 273 | /// The last landing that did not land it, while it is open. |
| 274 | #[serde(default)] |
| 275 | pub last_attempt: Option<Attempt>, |
| 276 | /// How it landed, once it did. |
| 277 | #[serde(default)] |
| 278 | pub landed: Option<Landing>, |
| 279 | /// The issues of the repository its text closes (`closes #4`): those |
| 280 | /// that landing it on the default branch closes, and once it landed, |
| 281 | /// those it closed. |
| 282 | #[serde(default)] |
| 283 | pub closes: Vec<u64>, |
| 284 | /// How many comments its conversation has. |
| 285 | #[serde(default)] |
| 286 | pub comments: u64, |
| 287 | /// Whether only the repository's writers may comment on it. |
| 288 | #[serde(default)] |
| 289 | pub locked: bool, |
| 290 | /// When it was opened. |
| 291 | pub created: String, |
| 292 | /// When it last changed. |
| 293 | pub updated: String, |
| 294 | /// When it was closed or landed, while it is. |
| 295 | #[serde(default)] |
| 296 | pub closed: Option<String>, |
| 297 | /// Who closed or landed it. |
| 298 | #[serde(default)] |
| 299 | pub closed_by: Option<String>, |
| 300 | /// Its text as HTML, as the caller's pages show it: given by the |
| 301 | /// routes that answer one request. |
| 302 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 303 | pub html: Option<String>, |
| 304 | } |
| 305 | |
| 306 | /// A page of change requests, newest first. |
| 307 | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 308 | pub struct Requests { |
| 309 | /// The requests, without their stacks. |
| 310 | pub requests: Vec<Request>, |
| 311 | /// The cursor of the next page, or `None` on the last one. |
| 312 | #[serde(default)] |
| 313 | pub next: Option<String>, |
| 314 | } |
| 315 | |
| 316 | /// `PATCH …/requests/{n}`: the fields to change. |
| 317 | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 318 | pub struct RequestPatch { |
| 319 | /// A new title. |
| 320 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 321 | pub title: Option<String>, |
| 322 | /// A new text. |
| 323 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 324 | pub body: Option<String>, |
| 325 | /// `open` or `closed`. |
| 326 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 327 | pub state: Option<String>, |
| 328 | /// Whether it is a draft. |
| 329 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 330 | pub draft: Option<bool>, |
| 331 | } |
| 332 | |
| 333 | /// `POST …/requests/{n}/update`: a new revision, from the head's branch as |
| 334 | /// it is now. |
| 335 | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 336 | pub struct RequestUpdate { |
| 337 | /// The changeset the new revision proposes, which the head's branch |
| 338 | /// must hold on its first-parent chain; its head when left out. |
| 339 | #[serde(default, with = "hex::opt_id", skip_serializing_if = "Option::is_none")] |
| 340 | pub head: Option<ObjectId>, |
| 341 | } |
| 342 | |
| 343 | /// `POST …/requests/{n}/comments`: a comment on the conversation or, with |
| 344 | /// `path` and `line`, on a line of a revision's diff. |
| 345 | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 346 | pub struct NewRequestComment { |
| 347 | /// Its text, Markdown: 1 byte to 64 KiB. |
| 348 | pub body: String, |
| 349 | /// The file it is about, as the diff names it. |
| 350 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 351 | pub path: Option<String>, |
| 352 | /// The line it is about, counted from 1 in the version `side` names. |
| 353 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 354 | pub line: Option<u64>, |
| 355 | /// `old` or `new`, the default. |
| 356 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 357 | pub side: Option<String>, |
| 358 | /// The revision whose diff it is about; the current one when left out. |
| 359 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 360 | pub revision: Option<u64>, |
| 361 | /// The label of the agent that writes it, recorded with it. |
| 362 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 363 | pub agent: Option<String>, |
| 364 | } |
| 365 | |
| 366 | /// A comment of a change request. |
| 367 | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 368 | pub struct RequestComment { |
| 369 | /// The comment. |
| 370 | #[serde(flatten)] |
| 371 | pub comment: Comment, |
| 372 | /// For a line comment: the file. |
| 373 | #[serde(default)] |
| 374 | pub path: Option<String>, |
| 375 | /// For a line comment: the line. |
| 376 | #[serde(default)] |
| 377 | pub line: Option<u64>, |
| 378 | /// For a line comment: `old` or `new`. |
| 379 | #[serde(default)] |
| 380 | pub side: Option<String>, |
| 381 | /// For a line comment: the revision whose diff it is about. |
| 382 | #[serde(default)] |
| 383 | pub revision: Option<u64>, |
| 384 | /// Whether it is about a revision before the current one. |
| 385 | #[serde(default)] |
| 386 | pub outdated: bool, |
| 387 | /// The review it belongs to, if any. |
| 388 | #[serde(default)] |
| 389 | pub review: Option<u64>, |
| 390 | } |
| 391 | |
| 392 | /// A page of the comments of a change request's conversation, oldest |
| 393 | /// first. |
| 394 | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 395 | pub struct RequestComments { |
| 396 | /// The comments. |
| 397 | pub comments: Vec<RequestComment>, |
| 398 | /// The cursor of the next page, or `None` on the last one. |
| 399 | #[serde(default)] |
| 400 | pub next: Option<String>, |
| 401 | } |
| 402 | |
| 403 | /// A comment on a line, as a review carries it. |
| 404 | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 405 | pub struct LineComment { |
| 406 | /// The file, as the diff names it. |
| 407 | pub path: String, |
| 408 | /// The line, counted from 1 in the version `side` names. |
| 409 | pub line: u64, |
| 410 | /// `old` or `new`, the default. |
| 411 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 412 | pub side: Option<String>, |
| 413 | /// Its text, Markdown: 1 byte to 64 KiB. |
| 414 | pub body: String, |
| 415 | } |
| 416 | |
| 417 | /// `POST …/requests/{n}/reviews`: a review of the current revision. |
| 418 | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 419 | pub struct NewReview { |
| 420 | /// `approve`, `request_changes` or `comment`. |
| 421 | pub verdict: String, |
| 422 | /// Its text, Markdown, at most 64 KiB. |
| 423 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 424 | pub body: Option<String>, |
| 425 | /// Comments on lines of the revision's diff. |
| 426 | #[serde(default, skip_serializing_if = "Vec::is_empty")] |
| 427 | pub comments: Vec<LineComment>, |
| 428 | /// The revision reviewed, which must still be the current one: what |
| 429 | /// was read is what is approved. |
| 430 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 431 | pub revision: Option<u64>, |
| 432 | /// The label of the agent that writes it, recorded with it. |
| 433 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 434 | pub agent: Option<String>, |
| 435 | } |
| 436 | |
| 437 | /// A review. |
| 438 | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 439 | pub struct Review { |
| 440 | /// Its id in its repository. |
| 441 | pub id: u64, |
| 442 | /// Who wrote it. |
| 443 | pub author: String, |
| 444 | /// The label of the agent that wrote it, if one said. |
| 445 | #[serde(default)] |
| 446 | pub agent: Option<String>, |
| 447 | /// `approve`, `request_changes` or `comment`. |
| 448 | pub verdict: String, |
| 449 | /// Whether its verdict counts: an approval or a request for changes |
| 450 | /// from a writer of the repository other than the request's author. |
| 451 | #[serde(default)] |
| 452 | pub binding: bool, |
| 453 | /// Whether it reviewed a revision before the current one: then it no |
| 454 | /// longer counts. |
| 455 | #[serde(default)] |
| 456 | pub stale: bool, |
| 457 | /// The revision it reviewed. |
| 458 | pub revision: u64, |
| 459 | /// Its text, Markdown; empty when it is hidden from the caller. |
| 460 | #[serde(default)] |
| 461 | pub body: String, |
| 462 | /// When it was written. |
| 463 | pub created: String, |
| 464 | /// Its comments on lines. |
| 465 | #[serde(default)] |
| 466 | pub comments: Vec<RequestComment>, |
| 467 | } |
| 468 | |
| 469 | /// A page of the reviews of a change request, oldest first. |
| 470 | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 471 | pub struct Reviews { |
| 472 | /// The reviews. |
| 473 | pub reviews: Vec<Review>, |
| 474 | /// The cursor of the next page, or `None` on the last one. |
| 475 | #[serde(default)] |
| 476 | pub next: Option<String>, |
| 477 | } |
| 478 | |
| 479 | /// `PUT …/requests/{n}/reviewers`, and its answer: the accounts asked to |
| 480 | /// review a change request, all of them. |
| 481 | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 482 | pub struct Reviewers { |
| 483 | /// Their logins. |
| 484 | pub logins: Vec<String>, |
| 485 | } |
| 486 | |
| 487 | /// `POST …/requests/{n}/land`: lands the snapshot named. |
| 488 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 489 | pub struct RequestLand { |
| 490 | /// The snapshot to land, which must still be the request's: what was |
| 491 | /// reviewed is what lands. |
| 492 | #[serde(with = "hex::id")] |
| 493 | pub snapshot: ObjectId, |
| 494 | /// How: fast-forward or rebase onto the base (the default), or |
| 495 | /// fast-forward only. |
| 496 | #[serde(default)] |
| 497 | pub mode: LandMode, |
| 498 | /// The label of the agent that lands it, for the op log. |
| 499 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 500 | pub agent: Option<String>, |
| 501 | } |
| 502 | |
| 503 | /// The answer of `POST …/requests/{n}/land`: the request as it is now, and |
| 504 | /// what the landing did, as `POST …/land` answers. |
| 505 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 506 | pub struct RequestLanded { |
| 507 | /// The request: `landed`, or still open with its `last_attempt`. |
| 508 | pub request: Request, |
| 509 | /// The landing. |
| 510 | pub land: LandResponse, |
| 511 | } |
| 512 | |
| 513 | #[cfg(test)] |
| 514 | mod tests { |
| 515 | use super::*; |
| 516 | |
| 517 | fn id(byte: u8) -> ObjectId { |
| 518 | ObjectId::from_bytes([byte; 32]) |
| 519 | } |
| 520 | |
| 521 | #[test] |
| 522 | fn new_requests_as_clients_send_them() { |
| 523 | let new: NewRequest = |
| 524 | serde_json::from_str(r#"{"title":"x","head":{"ref":"feature"}}"#).unwrap(); |
| 525 | assert_eq!(new.head.repo, None); |
| 526 | assert_eq!(new.head.reference, "feature"); |
| 527 | assert!(!new.expose && !new.draft && new.reviewers.is_empty()); |
| 528 | assert_eq!( |
| 529 | serde_json::to_string(&new).unwrap(), |
| 530 | r#"{"title":"x","head":{"ref":"feature"}}"# |
| 531 | ); |
| 532 | let other: NewRequest = serde_json::from_str( |
| 533 | r#"{"title":"x","head":{"repo":"bob/demo","ref":"f"},"base":"main","expose":true}"#, |
| 534 | ) |
| 535 | .unwrap(); |
| 536 | assert_eq!(other.head.repo.as_deref(), Some("bob/demo")); |
| 537 | assert!(other.expose); |
| 538 | let update: RequestUpdate = serde_json::from_str("{}").unwrap(); |
| 539 | assert_eq!(update.head, None); |
| 540 | assert_eq!(serde_json::to_string(&update).unwrap(), "{}"); |
| 541 | let land: RequestLand = |
| 542 | serde_json::from_str(&format!(r#"{{"snapshot":"{}"}}"#, id(1).to_hex())).unwrap(); |
| 543 | assert_eq!((land.snapshot, land.mode), (id(1), LandMode::Rebase)); |
| 544 | assert!(serde_json::from_str::<RequestLand>(r#"{"snapshot":"x"}"#).is_err()); |
| 545 | } |
| 546 | |
| 547 | #[test] |
| 548 | fn requests_on_the_wire() { |
| 549 | let request = Request { |
| 550 | number: 2, |
| 551 | title: "Faster".into(), |
| 552 | body: String::new(), |
| 553 | state: OPEN.into(), |
| 554 | draft: false, |
| 555 | author: "bob".into(), |
| 556 | agent: None, |
| 557 | head: RequestHead { |
| 558 | repo: None, |
| 559 | reference: "feature".into(), |
| 560 | snapshot: id(2), |
| 561 | available: true, |
| 562 | }, |
| 563 | base: RequestBase { |
| 564 | reference: "main".into(), |
| 565 | head_at_snapshot: Some(id(3)), |
| 566 | merge_base: Some(id(3)), |
| 567 | }, |
| 568 | revision: 1, |
| 569 | stack: Vec::new(), |
| 570 | brings: Vec::new(), |
| 571 | mergeable: Mergeable::default(), |
| 572 | approvals: Approvals::default(), |
| 573 | review_requests: Vec::new(), |
| 574 | last_attempt: None, |
| 575 | landed: None, |
| 576 | closes: vec![1], |
| 577 | comments: 0, |
| 578 | locked: false, |
| 579 | created: "2026-10-18T12:00:00Z".into(), |
| 580 | updated: "2026-10-18T12:00:00Z".into(), |
| 581 | closed: None, |
| 582 | closed_by: None, |
| 583 | html: None, |
| 584 | }; |
| 585 | let text = serde_json::to_string(&request).unwrap(); |
| 586 | // An unreadable head names no repository, and what is unknown is |
| 587 | // null, never missing: but the HTML. |
| 588 | assert!(text.contains(r#""repo":null"#), "{text}"); |
| 589 | assert!(text.contains(r#""ref":"feature""#), "{text}"); |
| 590 | assert!(text.contains(r#""state":"unknown""#), "{text}"); |
| 591 | assert!(text.contains(r#""landed":null"#), "{text}"); |
| 592 | assert!(!text.contains("html"), "{text}"); |
| 593 | assert_eq!(serde_json::from_str::<Request>(&text).unwrap(), request); |
| 594 | } |
| 595 | |
| 596 | #[test] |
| 597 | fn comments_and_reviews_on_the_wire() { |
| 598 | let comment = RequestComment { |
| 599 | comment: Comment { |
| 600 | id: 7, |
| 601 | author: "ada".into(), |
| 602 | body: "here".into(), |
| 603 | created: "t".into(), |
| 604 | ..Comment::default() |
| 605 | }, |
| 606 | path: Some("src/lib.rs".into()), |
| 607 | line: Some(3), |
| 608 | side: Some(NEW.into()), |
| 609 | revision: Some(1), |
| 610 | outdated: true, |
| 611 | review: None, |
| 612 | }; |
| 613 | let value = serde_json::to_value(&comment).unwrap(); |
| 614 | // The comment's fields sit beside those of its line. |
| 615 | assert_eq!(value["id"], 7); |
| 616 | assert_eq!(value["path"], "src/lib.rs"); |
| 617 | assert_eq!(value["outdated"], true); |
| 618 | assert_eq!( |
| 619 | serde_json::from_value::<RequestComment>(value).unwrap(), |
| 620 | comment |
| 621 | ); |
| 622 | let review: NewReview = |
| 623 | serde_json::from_str(r#"{"verdict":"approve","revision":2}"#).unwrap(); |
| 624 | assert_eq!( |
| 625 | (review.verdict.as_str(), review.revision), |
| 626 | (APPROVE, Some(2)) |
| 627 | ); |
| 628 | assert!(review.comments.is_empty()); |
| 629 | for verdict in VERDICTS { |
| 630 | assert!(is_verdict(verdict)); |
| 631 | } |
| 632 | assert!(!is_verdict("lgtm")); |
| 633 | } |
| 634 | } |