diff options
| author | aur-build <aur-build@localhost> | 2026-11-18 15:48:02 +0000 |
|---|---|---|
| committer | aur-build <aur-build@localhost> | 2026-11-18 15:48:02 +0000 |
| commit | 062efa6583b7fea292a4f485659661d266461611 (patch) | |
| tree | a9a54bba0dad9fbd07115f5bfad8db948ab6b30e /src/review/approve.rs | |
| parent | 753e05a4f588d3e8a3ea0b88b2e03d74f9f00be8 (diff) | |
| download | aur-build-062efa6583b7fea292a4f485659661d266461611.tar.gz aur-build-062efa6583b7fea292a4f485659661d266461611.zip | |
review: a database failure is never reported to a reviewer as a stale review
`approve()` returned `Result<Approval, Stale>` and then collapsed every inner
failure into `Stale::NotPending { state: e.to_string() }`, so a SQLite error
reached the human as "review is no longer pending (state: sqlite: …)". That is
the one message that tells a reviewer to look again when the truth is that the
write failed — and the transaction had already rolled back, so their review was
still pending and nothing on screen explained why. `bulk()` bent the same
variant to mean "high risk", "unknown decision" and "the database failed", and
handed back a fabricated `Approval { id: 0 }` for a rejection that recorded no
approval at all.
`approve()` now returns `Result<Approval, ApproveError>` with two families:
`Stale(Stale)` — what is stored is not what was displayed, look again — and
`System(Error)` — the write failed, nothing about the review is at fault. The
UI answers the first with the 409 and fresh diff it already gave, and the
second with a 500 that names the cause; the CLI exits 2 for the first and
propagates the second as an error.
`bulk()` got its own vocabulary — `BulkOutcome::{Approved, Rejected,
Refused(BulkRefusal)}` with `HighRiskUnconfirmed`, `UnknownDecision`,
`NotFound`, `Stale` and `System` — and returns no approval row for a rejection,
because there is none. The invented `id: 0` is gone. The web handler had its own
copy of the bulk loop; it now calls `approve::bulk`, so the page and the
library cannot disagree about what §7.4 says.
Three tests: a dropped `audit` table mid-transaction must surface as `System`
and must leave the review pending and the approval table empty; bulk refusals
must name the risk, the unknown decision, and (after a real approval) the
genuine staleness as three different things; a bulk rejection must record a
`rejected` row and hand back nothing.
Diffstat (limited to 'src/review/approve.rs')
| -rw-r--r-- | src/review/approve.rs | 160 |
1 files changed, 117 insertions, 43 deletions
diff --git a/src/review/approve.rs b/src/review/approve.rs index 704166c..a76eeac 100644 --- a/src/review/approve.rs +++ b/src/review/approve.rs @@ -11,34 +11,19 @@ use crate::time; /// Why an approval was refused (§7.1a). Rendered as HTTP 409 with the current /// diff, plus a CLI message. -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] pub enum Stale { + #[error("review is no longer pending (state: {state})")] NotPending { state: String }, + #[error("the candidate changed while you were looking: you were shown {expected}, it is now {actual}")] Identity { expected: String, actual: String }, + #[error("the diff changed while you were looking (shown {expected}, now {actual})")] Diff { expected: String, actual: String }, + /// The base moved: a newer version was accepted while the review was open. + #[error("a newer version was accepted since this review was opened")] Base { expected: Option<i64>, actual: Option<i64> }, } -impl std::fmt::Display for Stale { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Stale::NotPending { state } => write!(f, "review is no longer pending (state: {state})"), - Stale::Identity { expected, actual } => write!( - f, - "the candidate changed while you were looking: you were shown {expected}, it is now {actual}" - ), - Stale::Diff { expected, actual } => write!( - f, - "the diff changed while you were looking (shown {expected}, now {actual})" - ), - Stale::Base { .. } => write!( - f, - "a newer version was accepted since this review was opened" - ), - } - } -} - #[derive(Debug, Clone)] pub struct Approval { pub id: i64, @@ -49,6 +34,26 @@ pub struct Approval { pub identity_hash: String, } +/// Why a decision did not happen (§7.1a, §6 M-3). +/// +/// Two families, and the difference is the whole point. `Stale` means the +/// reviewer's screen no longer matches the database: the answer is to look +/// again, and the UI says so with a 409 and a fresh diff. `System` means the +/// database itself refused: looking again changes nothing, and the operator has +/// a broken thing to fix. They used to be the same variant — every failure, +/// SQLite included, arrived as "review is no longer pending (state: sqlite: +/// …)", which told a reviewer their eyes were wrong when the truth was that +/// the write failed. +#[derive(Debug, thiserror::Error)] +pub enum ApproveError { + /// What is stored is not what was displayed. + #[error(transparent)] + Stale(#[from] Stale), + /// The write itself failed. Nothing about the review is at fault. + #[error("the decision could not be recorded: {0}")] + System(#[from] Error), +} + /// Approve a candidate. Returns the approval row on success, or the reason the /// approval was refused. pub fn approve( @@ -58,14 +63,14 @@ pub fn approve( identity_hash: &str, diff_digest: &str, note: Option<&str>, -) -> std::result::Result<Approval, Stale> { +) -> std::result::Result<Approval, ApproveError> { let actor = actor.to_string(); let ih = identity_hash.to_string(); let dd = diff_digest.to_string(); let note = note.map(|s| s.to_string()); let at = time::now(); - db.writer.tx(move |tx| -> Result<std::result::Result<Approval, Stale>> { + db.writer.tx(move |tx| -> Result<std::result::Result<Approval, ApproveError>> { let subject = candidates::review_subject_in(tx, candidate_id)? .ok_or_else(|| Error::not_found(format!("candidate {candidate_id}")))?; let id = subject.id; @@ -74,19 +79,19 @@ pub fn approve( // 1. still open if subject.state != candidate_state::PENDING { - return Ok(Err(Stale::NotPending { state: subject.state })); + return Ok(Err(ApproveError::Stale(Stale::NotPending { state: subject.state }))); } // 2. same candidate bytes if subject.identity_hash != ih { - return Ok(Err(Stale::Identity { expected: ih.clone(), actual: subject.identity_hash })); + return Ok(Err(ApproveError::Stale(Stale::Identity { expected: ih.clone(), actual: subject.identity_hash }))); } // 3. same rendered diff if subject.diff_digest != dd { - return Ok(Err(Stale::Diff { expected: dd.clone(), actual: subject.diff_digest })); + return Ok(Err(ApproveError::Stale(Stale::Diff { expected: dd.clone(), actual: subject.diff_digest }))); } // 4. base is still the accepted release (I2, end to end) if base != current_base { - return Ok(Err(Stale::Base { expected: base, actual: current_base })); + return Ok(Err(ApproveError::Stale(Stale::Base { expected: base, actual: current_base }))); } let approval_id = approvals::record_in( @@ -124,7 +129,8 @@ pub fn approve( identity_hash: ih, })) }) - .map_err(|e| Stale::NotPending { state: e.to_string() })? + // A failure of the transaction is a system failure, never a stale review. + .map_err(ApproveError::System)? } /// Reject a candidate. Rejection is remembered per identity so the watcher stops @@ -159,30 +165,98 @@ pub fn reject(db: &Db, actor: &str, candidate_id: i64, note: &str) -> Result<()> }) } -/// §7.4 bulk decision. High-risk items are refused unless confirmed individually. +/// One item of a bulk decision, and what happened to it (§7.4). +#[derive(Debug)] +pub struct BulkItem { + /// The review this outcome is about; `None` when the id matched nothing — + /// which is exactly the case a caller must be able to report, so the row + /// cannot be the key. + pub candidate: Option<CandidateRow>, + pub outcome: BulkOutcome, +} + +impl BulkItem { + /// The pkgbase to name in a summary line. + pub fn pkgbase(&self) -> &str { + self.candidate.as_ref().map(|c| c.pkgbase.as_str()).unwrap_or("(gone)") + } +} + +/// What a bulk decision did with one item (§7.4). +#[derive(Debug)] +pub enum BulkOutcome { + /// Approved; the approval row is here so the caller can queue the build. + Approved(Approval), + /// Rejected. A rejection records no approval, so there is no row to hand + /// back — the old code fabricated `Approval { id: 0 }`, which a caller + /// could not tell from a real one (§6 M-3). + Rejected, + /// Not decided, and why. + Refused(BulkRefusal), +} + +/// Why a bulk decision skipped one of its items (§7.4). +/// +/// Each reason is its own variant because they are different answers to give a +/// human: one wants a confirmation click, one is a form bug, one means the +/// review moved while the page was open, one means the database is broken. They +/// used to all be `Stale::NotPending`, which made the flash message the only +/// place the truth survived. +#[derive(Debug, thiserror::Error)] +pub enum BulkRefusal { + /// High-risk items need their own confirmation, per item (§7.4). + #[error("high risk: confirm it individually")] + HighRiskUnconfirmed, + /// The form said neither approve nor reject. + #[error("unknown decision `{0}`")] + UnknownDecision(String), + /// The review is gone — deleted, or never existed. + #[error("no such review")] + NotFound, + /// The review no longer matches what was displayed. + #[error(transparent)] + Stale(Stale), + /// The write itself failed. + #[error("{0}")] + System(Error), +} + +/// §7.4 bulk decision. High-risk items are refused unless confirmed individually, +/// and one refused item never rolls back the ones that were decided. +/// +/// `items` carries the identity and diff each review was displayed with, so the +/// approval-echo rule applies to bulk exactly as it does to a single approval. pub fn bulk( db: &Db, actor: &str, items: &[(i64, String, String)], // (candidate_id, identity_hash, diff_digest) decision: &str, - confirm_high_risk: bool, -) -> Result<Vec<std::result::Result<Approval, Stale>>> { + confirmed: &[i64], +) -> Result<Vec<BulkItem>> { let mut out = Vec::new(); for (id, ih, dd) in items { - let risk = candidates::risk_level(db, *id)?.unwrap_or_default(); - if decision == "approve" && risk == "high" && !confirm_high_risk { - out.push(Err(Stale::NotPending { - state: "high-risk: needs individual confirmation".into(), - })); + let candidate = candidates::by_id(db, *id)?; + let mut push = |candidate, outcome| out.push(BulkItem { candidate, outcome }); + let Some(c) = &candidate else { + push(None, BulkOutcome::Refused(BulkRefusal::NotFound)); continue; + }; + if decision == "approve" && c.risk_level == crate::review::risk::level::HIGH && !confirmed.contains(id) { + push(Some(c.clone()), BulkOutcome::Refused(BulkRefusal::HighRiskUnconfirmed)); + continue; + } + match decision { + "approve" => match approve(db, actor, *id, ih, dd, Some("bulk")) { + Ok(a) => push(Some(c.clone()), BulkOutcome::Approved(a)), + Err(ApproveError::Stale(s)) => push(Some(c.clone()), BulkOutcome::Refused(BulkRefusal::Stale(s))), + Err(ApproveError::System(e)) => push(Some(c.clone()), BulkOutcome::Refused(BulkRefusal::System(e))), + }, + "reject" => match reject(db, actor, *id, "bulk") { + Ok(()) => push(Some(c.clone()), BulkOutcome::Rejected), + Err(e) => push(Some(c.clone()), BulkOutcome::Refused(BulkRefusal::System(e))), + }, + other => push(Some(c.clone()), BulkOutcome::Refused(BulkRefusal::UnknownDecision(other.to_string()))), } - out.push(match decision { - "approve" => approve(db, actor, *id, ih, dd, Some("bulk")), - "reject" => reject(db, actor, *id, "bulk") - .map(|_| Approval { id: 0, candidate_id: *id, base_release_id: None, decision: "rejected".into(), diff_digest: dd.clone(), identity_hash: ih.clone() }) - .map_err(|e| Stale::NotPending { state: e.to_string() }), - other => Err(Stale::NotPending { state: format!("unknown decision {other}") }), - }); } Ok(out) } |
