| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (c) 2026 UtopiaIA |
| 3 | |
| 4 | //! Verification (section 25): keeping a repository's default branch green. |
| 5 | //! The bodies of the routes under `/v1/r/{owner}/{name}/verify`, the closed |
| 6 | //! sets of words they use, and the names of the hub's own refs that say |
| 7 | //! where the branch stands. |
| 8 | //! |
| 9 | //! A reporter, such as a CI job, tells the hub how a suite of tests went on |
| 10 | //! a changeset of the default branch ([`VerifyReport`]). The hub keeps |
| 11 | //! `braid/green/<branch>` at the newest changeset with a pass it accepts |
| 12 | //! and, while the branch is red, `braid/red/<branch>` at the changeset it |
| 13 | //! blames, or at the first fail it cannot explain. It names a culprit only |
| 14 | //! when a single changeset separates a pass from a fail. The hub runs no |
| 15 | //! code of a repository: every verdict comes from outside it. |
| 16 | //! |
| 17 | //! Every field of these bodies has a default, and none refuses a field it |
| 18 | //! does not know, so that a client reads what a newer hub answers. The |
| 19 | //! words of a closed set are strings: a client keeps one it does not know |
| 20 | //! as it came, and takes a state it does not know for one that is not |
| 21 | //! green. Ids are 64 lowercase hex digits; times are RFC 3339, in UTC; |
| 22 | //! logins are lowercase. |
| 23 | |
| 24 | use braid_format::ObjectId; |
| 25 | use serde::{Deserialize, Serialize}; |
| 26 | |
| 27 | use crate::hex; |
| 28 | |
| 29 | /// What `GET /v1` lists in `features` when the hub verifies branches: the |
| 30 | /// routes of section 25 answer, and landings say how the branch stands. |
| 31 | pub const FEATURE: &str = "verify"; |
| 32 | |
| 33 | /// The prefix of the green refs, `braid/green/<branch>`. |
| 34 | pub const GREEN_PREFIX: &str = "braid/green/"; |
| 35 | |
| 36 | /// The prefix of the red refs, `braid/red/<branch>`. |
| 37 | pub const RED_PREFIX: &str = "braid/red/"; |
| 38 | |
| 39 | /// The hub's green ref of `branch`: the newest changeset of the branch with |
| 40 | /// a pass the hub accepts. |
| 41 | pub fn green_ref(branch: &str) -> String { |
| 42 | format!("{GREEN_PREFIX}{branch}") |
| 43 | } |
| 44 | |
| 45 | /// The hub's red ref of `branch`: while the branch is red, the changeset |
| 46 | /// the hub blames, or the first fail it cannot explain. |
| 47 | pub fn red_ref(branch: &str) -> String { |
| 48 | format!("{RED_PREFIX}{branch}") |
| 49 | } |
| 50 | |
| 51 | /// Mode: the hub verifies nothing, and keeps neither ref. |
| 52 | pub const MODE_OFF: &str = "off"; |
| 53 | |
| 54 | /// Mode: the hub takes the verdicts of its reporters, and keeps the refs. |
| 55 | pub const MODE_REPORT: &str = "report"; |
| 56 | |
| 57 | /// The modes of a policy. |
| 58 | pub const MODES: [&str; 2] = [MODE_OFF, MODE_REPORT]; |
| 59 | |
| 60 | /// Source: the verdicts reporters post. |
| 61 | pub const SOURCE_REPORT: &str = "report"; |
| 62 | |
| 63 | /// The sources a policy's `green_from` may name. |
| 64 | pub const SOURCES: [&str; 1] = [SOURCE_REPORT]; |
| 65 | |
| 66 | /// Outcome: the suite passed. |
| 67 | pub const PASS: &str = "pass"; |
| 68 | |
| 69 | /// Outcome: the suite failed. |
| 70 | pub const FAIL: &str = "fail"; |
| 71 | |
| 72 | /// Outcome: the suite failed, then passed when run again. |
| 73 | pub const FLAKY: &str = "flaky"; |
| 74 | |
| 75 | /// The outcomes a report gives. |
| 76 | pub const OUTCOMES: [&str; 3] = [PASS, FAIL, FLAKY]; |
| 77 | |
| 78 | /// State: the policy's mode is `off`. |
| 79 | pub const STATE_OFF: &str = "off"; |
| 80 | |
| 81 | /// State: nothing is known yet, such as before the first verdict. |
| 82 | pub const STATE_UNKNOWN: &str = "unknown"; |
| 83 | |
| 84 | /// State: the head of the branch is green. |
| 85 | pub const STATE_GREEN: &str = "green"; |
| 86 | |
| 87 | /// State: no fail is left unexplained, and the changesets above green wait |
| 88 | /// for a verdict. |
| 89 | pub const STATE_PENDING: &str = "pending"; |
| 90 | |
| 91 | /// State: a fail is left unexplained; `braid/red/<branch>` names the |
| 92 | /// culprit, or the first such fail. |
| 93 | pub const STATE_RED: &str = "red"; |
| 94 | |
| 95 | /// State: a revert of the culprit is landing. |
| 96 | pub const STATE_REVERTING: &str = "reverting"; |
| 97 | |
| 98 | /// State: a revert of the culprit landed, and waits for a verdict. |
| 99 | pub const STATE_REVERTED: &str = "reverted"; |
| 100 | |
| 101 | /// State: red, and the hub cannot go further by itself; `code` says why. |
| 102 | pub const STATE_STUCK: &str = "stuck"; |
| 103 | |
| 104 | /// State: verification waits on something outside the branch. |
| 105 | pub const STATE_PAUSED: &str = "paused"; |
| 106 | |
| 107 | /// The states of a branch, as `VerifyStatus.state` gives them. |
| 108 | pub const STATES: [&str; 9] = [ |
| 109 | STATE_OFF, |
| 110 | STATE_UNKNOWN, |
| 111 | STATE_GREEN, |
| 112 | STATE_PENDING, |
| 113 | STATE_RED, |
| 114 | STATE_REVERTING, |
| 115 | STATE_REVERTED, |
| 116 | STATE_STUCK, |
| 117 | STATE_PAUSED, |
| 118 | ]; |
| 119 | |
| 120 | /// Code: the branch is red, and has no green changeset to start from. |
| 121 | pub const CODE_NO_GREEN: &str = "no_green"; |
| 122 | |
| 123 | /// Code: more than one changeset separates the last pass from the first |
| 124 | /// fail, so none is named the culprit. |
| 125 | pub const CODE_CULPRIT_UNKNOWN: &str = "culprit_unknown"; |
| 126 | |
| 127 | /// Code: a revert fails itself, so it hides what it reverted. |
| 128 | pub const CODE_HIDDEN: &str = "hidden"; |
| 129 | |
| 130 | /// Code: more than 10,000 changesets are above green, too many to look at. |
| 131 | pub const CODE_RANGE_TOO_LONG: &str = "range_too_long"; |
| 132 | |
| 133 | /// Code: green was no longer on the branch's first-parent chain, as after |
| 134 | /// the branch was made again, so the hub started over. |
| 135 | pub const CODE_GREEN_OFF_CHAIN: &str = "green_off_chain"; |
| 136 | |
| 137 | /// Code: the newest verdict is flaky. |
| 138 | pub const CODE_FLAKY: &str = "flaky"; |
| 139 | |
| 140 | /// The codes that say why a branch is in its state. |
| 141 | pub const CODES: [&str; 6] = [ |
| 142 | CODE_NO_GREEN, |
| 143 | CODE_CULPRIT_UNKNOWN, |
| 144 | CODE_HIDDEN, |
| 145 | CODE_RANGE_TOO_LONG, |
| 146 | CODE_GREEN_OFF_CHAIN, |
| 147 | CODE_FLAKY, |
| 148 | ]; |
| 149 | |
| 150 | /// A culprit named because one changeset separates a pass from a fail. |
| 151 | pub const DECIDED_SINGLE: &str = "single"; |
| 152 | |
| 153 | /// A culprit named by bisecting. |
| 154 | pub const DECIDED_BISECT: &str = "bisect"; |
| 155 | |
| 156 | /// How a culprit was named. |
| 157 | pub const DECIDED_BY: [&str; 2] = [DECIDED_SINGLE, DECIDED_BISECT]; |
| 158 | |
| 159 | /// The window starts at green. |
| 160 | pub const LOWER_GREEN: &str = "green"; |
| 161 | |
| 162 | /// The window starts at a changeset that passed. |
| 163 | pub const LOWER_PASS: &str = "pass"; |
| 164 | |
| 165 | /// The window starts at a revert. |
| 166 | pub const LOWER_REVERT: &str = "revert"; |
| 167 | |
| 168 | /// The window has no lower bound. |
| 169 | pub const LOWER_NONE: &str = "none"; |
| 170 | |
| 171 | /// What the lower bound of a window is. |
| 172 | pub const LOWER_KINDS: [&str; 4] = [LOWER_GREEN, LOWER_PASS, LOWER_REVERT, LOWER_NONE]; |
| 173 | |
| 174 | /// Effect: the report turned the branch red. |
| 175 | pub const EFFECT_RED_SET: &str = "red_set"; |
| 176 | |
| 177 | /// Effect: the report moved green forward. |
| 178 | pub const EFFECT_GREEN_MOVED: &str = "green_moved"; |
| 179 | |
| 180 | /// Effect: the report ended the branch's red. |
| 181 | pub const EFFECT_RED_CLEARED: &str = "red_cleared"; |
| 182 | |
| 183 | /// Effect: the report was recorded, and moved no ref. |
| 184 | pub const EFFECT_RECORDED: &str = "recorded"; |
| 185 | |
| 186 | /// Effect: the hub had this verdict from this reporter already. |
| 187 | pub const EFFECT_DUPLICATE: &str = "duplicate"; |
| 188 | |
| 189 | /// Effect: the changeset is at or below green: nothing was recorded. |
| 190 | pub const EFFECT_STALE: &str = "stale"; |
| 191 | |
| 192 | /// What a report did. |
| 193 | pub const EFFECTS: [&str; 6] = [ |
| 194 | EFFECT_RED_SET, |
| 195 | EFFECT_GREEN_MOVED, |
| 196 | EFFECT_RED_CLEARED, |
| 197 | EFFECT_RECORDED, |
| 198 | EFFECT_DUPLICATE, |
| 199 | EFFECT_STALE, |
| 200 | ]; |
| 201 | |
| 202 | /// Event: an admin changed the policy. |
| 203 | pub const EVENT_POLICY: &str = "policy"; |
| 204 | |
| 205 | /// Event: green moved. |
| 206 | pub const EVENT_GREEN: &str = "green"; |
| 207 | |
| 208 | /// Event: the branch turned red. |
| 209 | pub const EVENT_RED: &str = "red"; |
| 210 | |
| 211 | /// Event: the hub named a culprit. |
| 212 | pub const EVENT_CULPRIT: &str = "culprit"; |
| 213 | |
| 214 | /// Event: the branch is no longer red. |
| 215 | pub const EVENT_FIXED: &str = "fixed"; |
| 216 | |
| 217 | /// Event: the branch is stuck. |
| 218 | pub const EVENT_STUCK: &str = "stuck"; |
| 219 | |
| 220 | /// Event: green or the fails were reset, by an admin or by the hub. |
| 221 | pub const EVENT_RESET: &str = "reset"; |
| 222 | |
| 223 | /// Event: a verdict was flaky. |
| 224 | pub const EVENT_FLAKY: &str = "flaky"; |
| 225 | |
| 226 | /// The events of the log. |
| 227 | pub const EVENTS: [&str; 8] = [ |
| 228 | EVENT_POLICY, |
| 229 | EVENT_GREEN, |
| 230 | EVENT_RED, |
| 231 | EVENT_CULPRIT, |
| 232 | EVENT_FIXED, |
| 233 | EVENT_STUCK, |
| 234 | EVENT_RESET, |
| 235 | EVENT_FLAKY, |
| 236 | ]; |
| 237 | |
| 238 | /// Notice: the default branch turned red. |
| 239 | pub const NOTICE_RED: &str = "verify_red"; |
| 240 | |
| 241 | /// Notice: the default branch is no longer red. |
| 242 | pub const NOTICE_FIXED: &str = "verify_fixed"; |
| 243 | |
| 244 | /// Notice: the default branch is stuck. |
| 245 | pub const NOTICE_STUCK: &str = "verify_stuck"; |
| 246 | |
| 247 | /// The codes of the notices of verification in the inbox (section 21), |
| 248 | /// whose reason is `system`. |
| 249 | pub const NOTICES: [&str; 3] = [NOTICE_RED, NOTICE_FIXED, NOTICE_STUCK]; |
| 250 | |
| 251 | /// The longest suite label. |
| 252 | pub const MAX_SUITE: usize = 32; |
| 253 | |
| 254 | /// The most reporters a policy names. |
| 255 | pub const MAX_REPORTERS: usize = 10; |
| 256 | |
| 257 | /// The largest body of `PUT …/verify/policy`, in bytes. |
| 258 | pub const MAX_POLICY_BODY: u64 = 16 * 1024; |
| 259 | |
| 260 | /// The largest body of `POST …/verify/report`, in bytes. |
| 261 | pub const MAX_REPORT_BODY: u64 = 4 * 1024; |
| 262 | |
| 263 | /// Whether `text` is a suite label: 1 to 32 of `a-z 0-9 . _ -`, starting |
| 264 | /// with a letter or a digit. |
| 265 | pub fn is_suite(text: &str) -> bool { |
| 266 | let bytes = text.as_bytes(); |
| 267 | let Some(first) = bytes.first() else { |
| 268 | return false; |
| 269 | }; |
| 270 | bytes.len() <= MAX_SUITE |
| 271 | && (first.is_ascii_lowercase() || first.is_ascii_digit()) |
| 272 | && bytes |
| 273 | .iter() |
| 274 | .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b"._-".contains(b)) |
| 275 | } |
| 276 | |
| 277 | /// Whether `text` is a git commit as the protocol writes one: 40 lowercase |
| 278 | /// hex digits. |
| 279 | pub fn is_git_commit(text: &str) -> bool { |
| 280 | text.len() == 40 && text.bytes().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f')) |
| 281 | } |
| 282 | |
| 283 | /// `GET` and `PUT …/verify/policy`: how a repository's default branch is |
| 284 | /// verified. `PUT` replaces it whole. |
| 285 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 286 | #[serde(default)] |
| 287 | pub struct VerifyPolicy { |
| 288 | /// [`MODE_OFF`] or [`MODE_REPORT`]. |
| 289 | pub mode: String, |
| 290 | /// The sources whose passes move green: [`SOURCE_REPORT`]. |
| 291 | pub green_from: Vec<String>, |
| 292 | /// Whether a flaky changeset moves green too. |
| 293 | pub flaky_moves_green: bool, |
| 294 | /// Who reports, and on which suite; needed with [`MODE_REPORT`]. |
| 295 | #[serde(skip_serializing_if = "Option::is_none")] |
| 296 | pub report: Option<ReportPolicy>, |
| 297 | } |
| 298 | |
| 299 | impl Default for VerifyPolicy { |
| 300 | /// The policy of a repository whose verification was never set: off. |
| 301 | fn default() -> Self { |
| 302 | VerifyPolicy { |
| 303 | mode: MODE_OFF.to_owned(), |
| 304 | green_from: Vec::new(), |
| 305 | flaky_moves_green: false, |
| 306 | report: None, |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | /// The reporters of a policy, and the suite they report on. |
| 312 | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 313 | #[serde(default)] |
| 314 | pub struct ReportPolicy { |
| 315 | /// The suite's label (see [`is_suite`]). |
| 316 | pub suite: String, |
| 317 | /// The logins whose reports count, 1 to [`MAX_REPORTERS`], lowercase. |
| 318 | pub reporters: Vec<String>, |
| 319 | } |
| 320 | |
| 321 | /// `GET …/verify`: how the default branch stands. Also in the answers of |
| 322 | /// `POST …/verify/report` and `POST …/verify/reset`. |
| 323 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 324 | #[serde(default)] |
| 325 | pub struct VerifyStatus { |
| 326 | /// The policy's mode. |
| 327 | pub mode: String, |
| 328 | /// The branch verified, the default branch; `None` with mode `off`. |
| 329 | pub branch: Option<String>, |
| 330 | /// One of [`STATES`]. |
| 331 | pub state: String, |
| 332 | /// Why, one of [`CODES`], when there is something to say. |
| 333 | pub code: Option<String>, |
| 334 | /// The head of the branch the hub last looked at. |
| 335 | #[serde(with = "hex::opt_id")] |
| 336 | pub head: Option<ObjectId>, |
| 337 | /// `braid/green/<branch>`. |
| 338 | #[serde(with = "hex::opt_id")] |
| 339 | pub green: Option<ObjectId>, |
| 340 | /// `braid/red/<branch>`. |
| 341 | #[serde(with = "hex::opt_id")] |
| 342 | pub red: Option<ObjectId>, |
| 343 | /// The changeset the hub blames, when it names one. |
| 344 | #[serde(with = "hex::opt_id")] |
| 345 | pub culprit: Option<ObjectId>, |
| 346 | /// How the culprit was named: one of [`DECIDED_BY`]. |
| 347 | pub decided_by: Option<String>, |
| 348 | /// Since when the branch is red. |
| 349 | pub since: Option<String>, |
| 350 | /// Where the culprit is looked for, while the branch is red. |
| 351 | pub window: Option<VerifyWindow>, |
| 352 | /// Verdicts the hub waits for; empty in this version of the protocol. |
| 353 | pub pending: Vec<PendingVerdict>, |
| 354 | /// Kept as the hub gave it: `None` in this version of the protocol. |
| 355 | pub round: Option<serde_json::Value>, |
| 356 | /// Kept as the hub gave it: `None` in this version of the protocol. |
| 357 | pub revert: Option<serde_json::Value>, |
| 358 | /// Kept as the hub gave it: `None` in this version of the protocol. |
| 359 | pub hold: Option<serde_json::Value>, |
| 360 | /// What only the repository's admins see. |
| 361 | pub admin: Option<AdminView>, |
| 362 | } |
| 363 | |
| 364 | impl Default for VerifyStatus { |
| 365 | /// The status of a repository whose verification is off. |
| 366 | fn default() -> Self { |
| 367 | VerifyStatus { |
| 368 | mode: MODE_OFF.to_owned(), |
| 369 | branch: None, |
| 370 | state: STATE_OFF.to_owned(), |
| 371 | code: None, |
| 372 | head: None, |
| 373 | green: None, |
| 374 | red: None, |
| 375 | culprit: None, |
| 376 | decided_by: None, |
| 377 | since: None, |
| 378 | window: None, |
| 379 | pending: Vec::new(), |
| 380 | round: None, |
| 381 | revert: None, |
| 382 | hold: None, |
| 383 | admin: None, |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | /// The changesets a culprit is looked for among: above `lower`, up to and |
| 389 | /// including `upper`. |
| 390 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 391 | #[serde(default)] |
| 392 | pub struct VerifyWindow { |
| 393 | /// The last changeset known to be good, or `None`. |
| 394 | #[serde(with = "hex::opt_id")] |
| 395 | pub lower: Option<ObjectId>, |
| 396 | /// What it is: one of [`LOWER_KINDS`]. |
| 397 | pub lower_kind: String, |
| 398 | /// The first fail the hub cannot explain. |
| 399 | #[serde(with = "hex::opt_id")] |
| 400 | pub upper: Option<ObjectId>, |
| 401 | /// How many changesets the window holds. |
| 402 | pub candidates: u64, |
| 403 | } |
| 404 | |
| 405 | impl Default for VerifyWindow { |
| 406 | fn default() -> Self { |
| 407 | VerifyWindow { |
| 408 | lower: None, |
| 409 | lower_kind: LOWER_NONE.to_owned(), |
| 410 | upper: None, |
| 411 | candidates: 0, |
| 412 | } |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | /// A verdict the hub waits for. |
| 417 | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 418 | #[serde(default)] |
| 419 | pub struct PendingVerdict { |
| 420 | /// The changeset. |
| 421 | #[serde(with = "hex::opt_id")] |
| 422 | pub changeset: Option<ObjectId>, |
| 423 | /// Where the verdict will come from. |
| 424 | pub source: String, |
| 425 | /// Since when the hub waits. |
| 426 | pub since: String, |
| 427 | } |
| 428 | |
| 429 | /// What [`VerifyStatus`] shows the repository's admins only. |
| 430 | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 431 | #[serde(default)] |
| 432 | pub struct AdminView { |
| 433 | /// The account that landed the culprit, when the hub knows it. |
| 434 | pub lander: Option<String>, |
| 435 | } |
| 436 | |
| 437 | /// `POST …/verify/report`: how a suite went on a changeset of the default |
| 438 | /// branch, named by its git commit or by its id, exactly one of them. |
| 439 | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 440 | #[serde(default)] |
| 441 | pub struct VerifyReport { |
| 442 | /// The git commit the changeset exports to, 40 lowercase hex digits. |
| 443 | #[serde(skip_serializing_if = "Option::is_none")] |
| 444 | pub git_commit: Option<String>, |
| 445 | /// The changeset. |
| 446 | #[serde(with = "hex::opt_id", skip_serializing_if = "Option::is_none")] |
| 447 | pub changeset: Option<ObjectId>, |
| 448 | /// One of [`OUTCOMES`]. |
| 449 | pub outcome: String, |
| 450 | /// The suite, as the policy names it. |
| 451 | pub suite: String, |
| 452 | } |
| 453 | |
| 454 | /// The answer of `POST …/verify/report`. |
| 455 | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 456 | #[serde(default)] |
| 457 | pub struct VerifyReportAnswer { |
| 458 | /// What the report did: one of [`EFFECTS`]. |
| 459 | pub effect: String, |
| 460 | /// How the branch stands now. |
| 461 | pub status: VerifyStatus, |
| 462 | } |
| 463 | |
| 464 | /// One event of `GET …/verify/log`. |
| 465 | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 466 | #[serde(default)] |
| 467 | pub struct VerifyEvent { |
| 468 | /// Its id, which grows with each event. |
| 469 | pub id: u64, |
| 470 | /// When. |
| 471 | pub at: String, |
| 472 | /// What happened: one of [`EVENTS`]. |
| 473 | pub code: String, |
| 474 | /// The changeset it is about, if any. |
| 475 | #[serde(with = "hex::opt_id")] |
| 476 | pub changeset: Option<ObjectId>, |
| 477 | /// Another id, or a code, that goes with it. |
| 478 | pub other: Option<String>, |
| 479 | /// The login that did it; `None` for the hub, and for callers who are |
| 480 | /// not the repository's admins. |
| 481 | pub actor: Option<String>, |
| 482 | } |
| 483 | |
| 484 | /// `GET …/verify/log`: a page of events, newest first. |
| 485 | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 486 | #[serde(default)] |
| 487 | pub struct VerifyEvents { |
| 488 | /// The events. |
| 489 | pub events: Vec<VerifyEvent>, |
| 490 | /// The `after` of the next page, or `None` on the last one. |
| 491 | pub next: Option<u64>, |
| 492 | } |
| 493 | |
| 494 | /// `POST …/verify/reset`: move green, or dismiss the fails there are now. |
| 495 | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 496 | #[serde(default)] |
| 497 | pub struct VerifyReset { |
| 498 | /// Where green goes, on the head's first-parent chain, backward too: |
| 499 | /// the verdicts given before the reset on the changesets above it are |
| 500 | /// forgotten, so that only new ones move green or bring red back. |
| 501 | /// `None` dismisses the fails there are now instead. |
| 502 | #[serde(with = "hex::opt_id")] |
| 503 | pub green: Option<ObjectId>, |
| 504 | /// What the caller saw: when the refs moved since, the hub answers 409 |
| 505 | /// `moved` and does nothing. |
| 506 | pub expect: Option<ResetExpect>, |
| 507 | } |
| 508 | |
| 509 | /// The refs a reset expects. |
| 510 | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 511 | #[serde(default)] |
| 512 | pub struct ResetExpect { |
| 513 | /// `braid/green/<branch>`, or `None` when there is none. |
| 514 | #[serde(with = "hex::opt_id")] |
| 515 | pub green: Option<ObjectId>, |
| 516 | /// `braid/red/<branch>`, or `None` when there is none. |
| 517 | #[serde(with = "hex::opt_id")] |
| 518 | pub red: Option<ObjectId>, |
| 519 | } |
| 520 | |
| 521 | /// `verify` in the answer of a landing on the default branch of a |
| 522 | /// repository whose verification is on: how the branch stands. |
| 523 | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 524 | #[serde(default)] |
| 525 | pub struct LandVerify { |
| 526 | /// One of [`STATES`]. |
| 527 | pub state: String, |
| 528 | /// `braid/green/<branch>`. |
| 529 | #[serde(with = "hex::opt_id")] |
| 530 | pub green: Option<ObjectId>, |
| 531 | /// `braid/red/<branch>`. |
| 532 | #[serde(with = "hex::opt_id")] |
| 533 | pub red: Option<ObjectId>, |
| 534 | /// The changeset the hub blames, when it names one. |
| 535 | #[serde(with = "hex::opt_id")] |
| 536 | pub culprit: Option<ObjectId>, |
| 537 | /// Reverts the hub landed that the landing was rebased over; empty in |
| 538 | /// this version of the protocol. |
| 539 | pub reverted: Vec<RevertedPair>, |
| 540 | } |
| 541 | |
| 542 | /// A changeset, and the revert of it the hub landed. |
| 543 | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 544 | #[serde(default)] |
| 545 | pub struct RevertedPair { |
| 546 | /// The changeset reverted. |
| 547 | #[serde(with = "hex::opt_id")] |
| 548 | pub culprit: Option<ObjectId>, |
| 549 | /// The revert. |
| 550 | #[serde(with = "hex::opt_id")] |
| 551 | pub revert: Option<ObjectId>, |
| 552 | } |
| 553 | |
| 554 | #[cfg(test)] |
| 555 | mod tests { |
| 556 | use super::*; |
| 557 | use braid_format::Kind; |
| 558 | use serde::de::DeserializeOwned; |
| 559 | use serde_json::{Value, json}; |
| 560 | |
| 561 | fn id(text: &[u8]) -> ObjectId { |
| 562 | ObjectId::hash(Kind::Changeset, text) |
| 563 | } |
| 564 | |
| 565 | /// Written and read again, `value` is itself. |
| 566 | fn round_trips<T: Serialize + DeserializeOwned + PartialEq + std::fmt::Debug>(value: &T) { |
| 567 | let text = serde_json::to_string(value).unwrap(); |
| 568 | assert_eq!(&serde_json::from_str::<T>(&text).unwrap(), value, "{text}"); |
| 569 | } |
| 570 | |
| 571 | #[test] |
| 572 | fn refs_and_closed_sets() { |
| 573 | assert_eq!(green_ref("main"), "braid/green/main"); |
| 574 | assert_eq!(red_ref("release/1"), "braid/red/release/1"); |
| 575 | assert!(green_ref("x").starts_with(GREEN_PREFIX)); |
| 576 | assert!(red_ref("x").starts_with(RED_PREFIX)); |
| 577 | // Each set holds each word once, and the words are what the wire |
| 578 | // says. |
| 579 | for set in [ |
| 580 | &MODES[..], |
| 581 | &SOURCES, |
| 582 | &OUTCOMES, |
| 583 | &STATES, |
| 584 | &CODES, |
| 585 | &DECIDED_BY, |
| 586 | &LOWER_KINDS, |
| 587 | &EFFECTS, |
| 588 | &EVENTS, |
| 589 | &NOTICES, |
| 590 | ] { |
| 591 | let mut words = set.to_vec(); |
| 592 | words.sort_unstable(); |
| 593 | words.dedup(); |
| 594 | assert_eq!(words.len(), set.len(), "{set:?}"); |
| 595 | for word in set { |
| 596 | assert!( |
| 597 | word.bytes().all(|b| b.is_ascii_lowercase() || b == b'_'), |
| 598 | "{word}" |
| 599 | ); |
| 600 | } |
| 601 | } |
| 602 | assert!(NOTICES.iter().all(|code| code.starts_with("verify_"))); |
| 603 | assert_eq!(FEATURE, "verify"); |
| 604 | } |
| 605 | |
| 606 | #[test] |
| 607 | fn suites_and_commits() { |
| 608 | for good in ["ci", "0", "unit-tests", "a.b_c-d", &"a".repeat(32)] { |
| 609 | assert!(is_suite(good), "{good:?}"); |
| 610 | } |
| 611 | for bad in [ |
| 612 | "", |
| 613 | "CI", |
| 614 | "-ci", |
| 615 | ".ci", |
| 616 | "_ci", |
| 617 | "c i", |
| 618 | "ci/x", |
| 619 | "é", |
| 620 | &"a".repeat(33), |
| 621 | ] { |
| 622 | assert!(!is_suite(bad), "{bad:?}"); |
| 623 | } |
| 624 | assert!(is_git_commit(&"0a".repeat(20))); |
| 625 | for bad in [ |
| 626 | "", |
| 627 | &"0A".repeat(20), |
| 628 | &"0".repeat(39), |
| 629 | &"0".repeat(64), |
| 630 | &"g".repeat(40), |
| 631 | ] { |
| 632 | assert!(!is_git_commit(bad), "{bad:?}"); |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | #[test] |
| 637 | fn every_body_round_trips() { |
| 638 | let (a, b, c) = (id(b"a"), id(b"b"), id(b"c")); |
| 639 | round_trips(&VerifyPolicy { |
| 640 | mode: MODE_REPORT.into(), |
| 641 | green_from: vec![SOURCE_REPORT.into()], |
| 642 | flaky_moves_green: true, |
| 643 | report: Some(ReportPolicy { |
| 644 | suite: "ci".into(), |
| 645 | reporters: vec!["ci-bot".into()], |
| 646 | }), |
| 647 | }); |
| 648 | round_trips(&VerifyPolicy::default()); |
| 649 | let status = VerifyStatus { |
| 650 | mode: MODE_REPORT.into(), |
| 651 | branch: Some("main".into()), |
| 652 | state: STATE_RED.into(), |
| 653 | code: Some(CODE_CULPRIT_UNKNOWN.into()), |
| 654 | head: Some(c), |
| 655 | green: Some(a), |
| 656 | red: Some(c), |
| 657 | culprit: None, |
| 658 | decided_by: None, |
| 659 | since: Some("2026-10-22T12:03:00Z".into()), |
| 660 | window: Some(VerifyWindow { |
| 661 | lower: Some(a), |
| 662 | lower_kind: LOWER_GREEN.into(), |
| 663 | upper: Some(c), |
| 664 | candidates: 2, |
| 665 | }), |
| 666 | pending: vec![PendingVerdict { |
| 667 | changeset: Some(b), |
| 668 | source: SOURCE_REPORT.into(), |
| 669 | since: "2026-10-22T12:00:00Z".into(), |
| 670 | }], |
| 671 | round: Some(json!({"n": 4})), |
| 672 | revert: None, |
| 673 | hold: None, |
| 674 | admin: Some(AdminView { |
| 675 | lander: Some("ada".into()), |
| 676 | }), |
| 677 | }; |
| 678 | round_trips(&status); |
| 679 | round_trips(&VerifyStatus::default()); |
| 680 | round_trips(&VerifyReport { |
| 681 | git_commit: Some("0a".repeat(20)), |
| 682 | changeset: None, |
| 683 | outcome: FAIL.into(), |
| 684 | suite: "ci".into(), |
| 685 | }); |
| 686 | round_trips(&VerifyReport { |
| 687 | git_commit: None, |
| 688 | changeset: Some(b), |
| 689 | outcome: PASS.into(), |
| 690 | suite: "ci".into(), |
| 691 | }); |
| 692 | round_trips(&VerifyReportAnswer { |
| 693 | effect: EFFECT_RED_SET.into(), |
| 694 | status: status.clone(), |
| 695 | }); |
| 696 | round_trips(&VerifyEvents { |
| 697 | events: vec![VerifyEvent { |
| 698 | id: 17, |
| 699 | at: "2026-10-22T12:03:00Z".into(), |
| 700 | code: EVENT_RED.into(), |
| 701 | changeset: Some(c), |
| 702 | other: Some(CODE_CULPRIT_UNKNOWN.into()), |
| 703 | actor: None, |
| 704 | }], |
| 705 | next: Some(17), |
| 706 | }); |
| 707 | round_trips(&VerifyReset { |
| 708 | green: Some(a), |
| 709 | expect: Some(ResetExpect { |
| 710 | green: Some(b), |
| 711 | red: None, |
| 712 | }), |
| 713 | }); |
| 714 | round_trips(&VerifyReset::default()); |
| 715 | round_trips(&LandVerify { |
| 716 | state: STATE_RED.into(), |
| 717 | green: Some(a), |
| 718 | red: Some(c), |
| 719 | culprit: Some(c), |
| 720 | reverted: vec![RevertedPair { |
| 721 | culprit: Some(b), |
| 722 | revert: Some(c), |
| 723 | }], |
| 724 | }); |
| 725 | } |
| 726 | |
| 727 | #[test] |
| 728 | fn on_the_wire() { |
| 729 | let a = id(b"a"); |
| 730 | // A report names exactly what it was given. |
| 731 | let report = VerifyReport { |
| 732 | git_commit: Some("0a".repeat(20)), |
| 733 | changeset: None, |
| 734 | outcome: PASS.into(), |
| 735 | suite: "ci".into(), |
| 736 | }; |
| 737 | assert_eq!( |
| 738 | serde_json::to_value(&report).unwrap(), |
| 739 | json!({"git_commit": "0a".repeat(20), "outcome": "pass", "suite": "ci"}) |
| 740 | ); |
| 741 | // A policy never set says so, and leaves out the block it lacks. |
| 742 | assert_eq!( |
| 743 | serde_json::to_value(VerifyPolicy::default()).unwrap(), |
| 744 | json!({"mode": "off", "green_from": [], "flaky_moves_green": false}) |
| 745 | ); |
| 746 | // Ids the hub has none of are null, and so are the parts of the |
| 747 | // status this version of the protocol leaves empty. |
| 748 | let status = serde_json::to_value(VerifyStatus::default()).unwrap(); |
| 749 | for key in ["branch", "head", "green", "red", "culprit", "round", "hold"] { |
| 750 | assert_eq!(status[key], Value::Null, "{key}"); |
| 751 | } |
| 752 | assert_eq!(status["state"], "off"); |
| 753 | assert_eq!(status["pending"], json!([])); |
| 754 | // A reset that expects no green says so with null. |
| 755 | let reset = VerifyReset { |
| 756 | green: None, |
| 757 | expect: Some(ResetExpect { |
| 758 | green: None, |
| 759 | red: Some(a), |
| 760 | }), |
| 761 | }; |
| 762 | assert_eq!( |
| 763 | serde_json::to_value(&reset).unwrap(), |
| 764 | json!({"green": null, "expect": {"green": null, "red": a.to_hex()}}) |
| 765 | ); |
| 766 | // A bad id is refused. |
| 767 | assert!(serde_json::from_str::<LandVerify>(r#"{"state":"red","red":"xyz"}"#).is_err()); |
| 768 | } |
| 769 | |
| 770 | #[test] |
| 771 | fn what_a_hub_leaves_out_or_adds_still_reads() { |
| 772 | // An answer of a newer hub, with fields this crate does not know. |
| 773 | let status: VerifyStatus = serde_json::from_str( |
| 774 | r#"{"mode":"auto","state":"held","hold":{"until":"x"},"later":[1,2],"window":{"upper":null,"later":true}}"#, |
| 775 | ) |
| 776 | .unwrap(); |
| 777 | assert_eq!( |
| 778 | (status.mode.as_str(), status.state.as_str()), |
| 779 | ("auto", "held") |
| 780 | ); |
| 781 | assert_eq!(status.hold, Some(json!({"until": "x"}))); |
| 782 | assert_eq!(status.window.unwrap().lower_kind, LOWER_NONE); |
| 783 | // A body with nothing in it takes every default. |
| 784 | assert_eq!( |
| 785 | serde_json::from_str::<VerifyStatus>("{}").unwrap(), |
| 786 | VerifyStatus::default() |
| 787 | ); |
| 788 | assert_eq!( |
| 789 | serde_json::from_str::<VerifyPolicy>("{}").unwrap(), |
| 790 | VerifyPolicy::default() |
| 791 | ); |
| 792 | let answer: VerifyReportAnswer = serde_json::from_str(r#"{"effect":"stale"}"#).unwrap(); |
| 793 | assert_eq!(answer.status.state, STATE_OFF); |
| 794 | let land: LandVerify = serde_json::from_str(r#"{"state":"green"}"#).unwrap(); |
| 795 | assert!(land.reverted.is_empty() && land.culprit.is_none()); |
| 796 | let events: VerifyEvents = serde_json::from_str(r#"{"events":[{"id":3}]}"#).unwrap(); |
| 797 | assert_eq!((events.events[0].id, events.next), (3, None)); |
| 798 | } |
| 799 | } |