//! 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 /// is checked against what was actually shown. `Ok(None)` means no such /// candidate. 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 looked at it — 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, /// Fatal in the CLI, soft in the UI: reported, not decided 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; one workflow, so an /// approval can never fail to queue. 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. A refused item comes back with its 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()) }