//! Reviewing: look at a candidate, decide, and queue what the decision buys. use crate::config::Config; use crate::db::{CandidateRow, Db}; use crate::error::Result; use crate::pipeline; use crate::review::{approve as ap, candidates, diff}; /// The diff a reviewer is being asked to look at, already recorded. pub struct Prepared { pub candidate: CandidateRow, pub text: String, pub digest: String, } /// Render the current diff and store its digest, so the approval that follows /// can be checked against exactly what was shown rather than whatever the tree /// looked like a moment later. /// /// `Ok(None)` means there is no such candidate; each front end says so in its /// own words. pub fn prepare_review(cfg: &Config, db: &Db, candidate_id: i64) -> Result> { let Some(c) = candidates::candidate_by_id(db, candidate_id)? else { return Ok(None); }; let (text, digest) = diff::render_current(db, cfg, &c)?; candidates::set_diff_digest(db, c.id, &digest)?; Ok(Some(Prepared { candidate: c, text, digest })) } /// `prepare_review`, plus recording that a human actually looked at it. /// /// The review queue uses "viewed" to stop surfacing candidates nobody is /// reading, so looking and approving are separate facts. pub fn view_review(cfg: &Config, db: &Db, candidate_id: i64) -> Result> { let Some(p) = prepare_review(cfg, db, candidate_id)? else { return Ok(None); }; candidates::mark_viewed(db, p.candidate.id)?; Ok(Some(p)) } /// An approval that was recorded, and whether a build came out of it. pub struct Approved { pub approval: ap::Approval, /// Queuing is fatal in the CLI — an approved candidate with no build is a /// silent stall — and a soft failure in the UI, which has already committed /// the decision and can retry from the package page. So the workflow /// reports it rather than choosing for both. pub queued: Result, } pub enum Approval { Recorded(Approved), /// The review moved underneath the reviewer; the reason says how. Refused(ap::Stale), } /// Record an approval and queue the build it authorises. /// /// These are one workflow because an approval that never queues is the bug the /// whole review queue exists to prevent, and both front ends had to remember to /// pair them. pub fn approve( cfg: &Config, db: &Db, actor: &str, c: &CandidateRow, identity: &str, digest: &str, note: Option<&str>, ) -> Result { let approval = match ap::approve(db, actor, c.id, identity, digest, note) { Ok(a) => a, Err(ap::ApproveError::Stale(stale)) => return Ok(Approval::Refused(stale)), Err(ap::ApproveError::System(e)) => return Err(e), }; Ok(Approval::Recorded(Approved { approval, queued: pipeline::enqueue_for_candidate(cfg, db, c) })) } /// Record a rejection. A rejection always needs a reason; it is the only record /// of why a candidate was passed over. pub fn reject(db: &Db, actor: &str, candidate_id: i64, note: &str) -> Result<()> { ap::reject(db, actor, candidate_id, note) } /// One candidate's result from a bulk decision. pub enum Decision { Approved { pkgbase: String, queued: Result }, Rejected { pkgbase: String }, Refused { pkgbase: String, why: String }, } /// Apply one decision to many candidates, queueing a build for each that /// passed. Items that were refused — high risk without a confirmation, say — /// come back with the reason rather than aborting the batch. pub fn bulk( cfg: &Config, db: &Db, actor: &str, items: &[(i64, String, String)], decision: &str, confirmed: &[i64], ) -> Result> { let outcomes = ap::bulk(db, actor, items, decision, confirmed)?; Ok(outcomes .into_iter() .map(|item| match &item.outcome { ap::BulkOutcome::Approved(_) => match &item.candidate { Some(c) => Decision::Approved { pkgbase: c.pkgbase.clone(), queued: pipeline::enqueue_for_candidate(cfg, db, c), }, None => Decision::Rejected { pkgbase: item.pkgbase().to_string() }, }, ap::BulkOutcome::Rejected => Decision::Rejected { pkgbase: item.pkgbase().to_string() }, ap::BulkOutcome::Refused(r) => { Decision::Refused { pkgbase: item.pkgbase().to_string(), why: r.to_string() } } }) .collect()) }