//! Approval semantics (§7.1, §7.1a, §7.2, §7.4). //! //! Rule of the system: **we build exactly what was displayed, or nothing.** //! The approve POST carries the `identity_hash` and `diff_digest` that were //! rendered; if either has moved under the reviewer, the approval is refused //! with a conflict (HTTP 409) and a re-render, never silently retargeted. use crate::db::{self, approvals, candidate_state, candidates, *}; use crate::error::{Error, Result}; 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)] pub enum Stale { NotPending { state: String }, Identity { expected: String, actual: String }, Diff { expected: String, actual: String }, Base { expected: Option, actual: Option }, } 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, pub candidate_id: i64, pub base_release_id: Option, pub decision: String, pub diff_digest: String, pub identity_hash: String, } /// Approve a candidate. Returns the approval row on success, or the reason the /// approval was refused. pub fn approve( db: &Db, actor: &str, candidate_id: i64, identity_hash: &str, diff_digest: &str, note: Option<&str>, ) -> std::result::Result { 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> { let subject = candidates::review_subject_in(tx, candidate_id)? .ok_or_else(|| Error::not_found(format!("candidate {candidate_id}")))?; let id = subject.id; let base = subject.base_release_id; let current_base = subject.current_base_release_id; // 1. still open if subject.state != candidate_state::PENDING { return Ok(Err(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 })); } // 3. same rendered diff if subject.diff_digest != dd { return Ok(Err(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 })); } let approval_id = approvals::record_in( tx, &approvals::Decision { candidate_id: id, base_release_id: base, at: &at, decision: "approved", risk_level: &subject.risk_level, diff_digest: &dd, identity_hash: &ih, note: note.as_deref(), }, )?; candidates::resolve_in(tx, id, candidate_state::APPROVED, &at)?; db::audit_in( tx, &db::AuditEntry { at: &at, actor_kind: "human", actor: &actor, action: "approve", entity: "candidate", entity_id: &id.to_string(), detail: serde_json::json!({"approval": approval_id, "identity": ih, "pkgbase_id": subject.pkgbase_id}), }, )?; Ok(Ok(Approval { id: approval_id, candidate_id: id, base_release_id: base, decision: "approved".into(), diff_digest: dd, identity_hash: ih, })) }) .map_err(|e| Stale::NotPending { state: e.to_string() })? } /// Reject a candidate. Rejection is remembered per identity so the watcher stops /// nagging about the same version (§7.1), and can be re-opened later. pub fn reject(db: &Db, actor: &str, candidate_id: i64, note: &str) -> Result<()> { let actor = actor.to_string(); let note = note.to_string(); let at = time::now(); db.writer.tx(move |tx| { let state = candidates::state_in(tx, candidate_id)? .ok_or_else(|| Error::not_found(format!("candidate {candidate_id}")))?; if state != candidate_state::PENDING { return Err(Error::conflict(format!("candidate is {state}, not pending"))); } let identity = candidates::identity_in(tx, candidate_id)? .ok_or_else(|| Error::not_found(format!("candidate {candidate_id}")))?; approvals::record_rejection_in(tx, candidate_id, &at, &identity, ¬e)?; candidates::resolve_in(tx, candidate_id, candidate_state::REJECTED, &at)?; db::audit_in( tx, &db::AuditEntry { at: &at, actor_kind: "human", actor: &actor, action: "reject", entity: "candidate", entity_id: &candidate_id.to_string(), detail: serde_json::json!({"note": note}), }, )?; Ok(()) }) } /// §7.4 bulk decision. High-risk items are refused unless confirmed individually. pub fn bulk( db: &Db, actor: &str, items: &[(i64, String, String)], // (candidate_id, identity_hash, diff_digest) decision: &str, confirm_high_risk: bool, ) -> Result>> { 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(), })); continue; } 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) } /// The approval behind a candidate, if any. pub fn approval_for(db: &Db, candidate_id: i64) -> Result> { approvals::for_candidate(db, candidate_id) } /// Withdraw a published release (§9 `repo-remove` path, decision 23: warns, /// does not block). pub fn withdraw(db: &Db, actor: &str, release_id: i64, reason: &str) -> Result<()> { let actor = actor.to_string(); let reason = reason.to_string(); let at = time::now(); db.writer.tx(move |tx| { db::withdraw_release_in(tx, release_id)?; db::audit_in( tx, &db::AuditEntry { at: &at, actor_kind: "human", actor: &actor, action: "withdraw", entity: "release", entity_id: &release_id.to_string(), detail: serde_json::json!({"reason": reason}), }, )?; Ok(()) }) } /// The pkgbase's accepted release pointer, exposed for callers that need it. pub fn current_accepted(db: &Db, pkgbase: &str) -> Result> { db::accepted_release(db, pkgbase) }