diff options
Diffstat (limited to 'src/review/approve.rs')
| -rw-r--r-- | src/review/approve.rs | 177 |
1 files changed, 66 insertions, 111 deletions
diff --git a/src/review/approve.rs b/src/review/approve.rs index 0fdf2b2..704166c 100644 --- a/src/review/approve.rs +++ b/src/review/approve.rs @@ -5,9 +5,7 @@ //! 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 rusqlite::OptionalExtension; - -use crate::db::{self, *}; +use crate::db::{self, approvals, candidate_state, candidates, *}; use crate::error::{Error, Result}; use crate::time; @@ -68,78 +66,54 @@ pub fn approve( let at = time::now(); db.writer.tx(move |tx| -> Result<std::result::Result<Approval, Stale>> { - type CandidateState = (i64, String, String, Option<i64>, Option<i64>, String, String); - let row: Option<CandidateState> = tx - .query_row( - "SELECT c.id, c.state, c.identity_hash, c.base_release_id, p.current_accepted_release_id, - c.risk_level, p.name - FROM candidate c JOIN pkgbase p ON p.id = c.pkgbase_id - WHERE c.id = ?1", - rusqlite::params![candidate_id], - |r| { - Ok(( - r.get(0)?, - r.get(1)?, - r.get(2)?, - r.get(3)?, - r.get(4)?, - r.get(5)?, - r.get(6)?, - )) - }, - ) - .optional()?; - let (id, state, actual_identity, base, current_base, risk, _pkgbase) = - row.ok_or_else(|| Error::not_found(format!("candidate {candidate_id}")))?; + 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 state != candidate_state::PENDING { - return Ok(Err(Stale::NotPending { state })); + if subject.state != candidate_state::PENDING { + return Ok(Err(Stale::NotPending { state: subject.state })); } // 2. same candidate bytes - if actual_identity != ih { - return Ok(Err(Stale::Identity { expected: ih.clone(), actual: actual_identity })); + if subject.identity_hash != ih { + return Ok(Err(Stale::Identity { expected: ih.clone(), actual: subject.identity_hash })); } // 3. same rendered diff - let stored_diff: String = tx.query_row( - "SELECT diff_digest FROM candidate WHERE id=?1", - rusqlite::params![id], - |r| r.get(0), - )?; - if stored_diff != dd { - return Ok(Err(Stale::Diff { expected: dd.clone(), actual: stored_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 })); } - tx.execute( - "INSERT INTO approval (candidate_id, base_release_id, decided_at, decision, risk_level, - diff_digest, identity_hash, note) - VALUES (?1,?2,?3,'approved',?4,?5,?6,?7)", - rusqlite::params![id, base, at, risk, dd, ih, note], - )?; - let approval_id = tx.last_insert_rowid(); - tx.execute( - "UPDATE candidate SET state='approved', resolved_at=?1 WHERE id=?2", - rusqlite::params![at, id], - )?; - let pkgbase_id: i64 = tx.query_row( - "SELECT pkgbase_id FROM candidate WHERE id=?1", - rusqlite::params![id], - |r| r.get(0), + 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(), + }, )?; - tx.execute( - "INSERT INTO audit (at, actor_kind, actor, action, entity, entity_id, detail_json) - VALUES (?1,'human',?2,'approve','candidate',?3,?4)", - rusqlite::params![ - at, - actor, - id.to_string(), - serde_json::json!({"approval": approval_id, "identity": ih, "pkgbase_id": pkgbase_id}) - .to_string() - ], + 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, @@ -160,33 +134,26 @@ pub fn reject(db: &Db, actor: &str, candidate_id: i64, note: &str) -> Result<()> let note = note.to_string(); let at = time::now(); db.writer.tx(move |tx| { - let state: String = tx - .query_row("SELECT state FROM candidate WHERE id=?1", rusqlite::params![candidate_id], |r| r.get(0)) - .optional()? + 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: String = tx.query_row( - "SELECT identity_hash FROM candidate WHERE id=?1", - rusqlite::params![candidate_id], - |r| r.get(0), - )?; - tx.execute( - "INSERT INTO approval (candidate_id, base_release_id, decided_at, decision, risk_level, - diff_digest, identity_hash, note) - SELECT id, base_release_id, ?1, 'rejected', risk_level, diff_digest, ?2, ?3 - FROM candidate WHERE id=?4", - rusqlite::params![at, identity, note, candidate_id], - )?; - tx.execute( - "UPDATE candidate SET state='rejected', resolved_at=?1 WHERE id=?2", - rusqlite::params![at, candidate_id], - )?; - tx.execute( - "INSERT INTO audit (at, actor_kind, actor, action, entity, entity_id, detail_json) - VALUES (?1,'human',?2,'reject','candidate',?3,?4)", - rusqlite::params![at, actor, candidate_id.to_string(), serde_json::json!({"note": note}).to_string()], + 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(()) }) @@ -202,15 +169,7 @@ pub fn bulk( ) -> Result<Vec<std::result::Result<Approval, Stale>>> { let mut out = Vec::new(); for (id, ih, dd) in items { - let risk: String = db.read(|c| { - Ok(c.query_row( - "SELECT risk_level FROM candidate WHERE id=?1", - rusqlite::params![id], - |r| r.get(0), - ) - .optional()?) - })? - .unwrap_or_default(); + 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(), @@ -230,14 +189,7 @@ pub fn bulk( /// The approval behind a candidate, if any. pub fn approval_for(db: &Db, candidate_id: i64) -> Result<Option<(i64, String)>> { - db.read(|c| { - Ok(c.query_row( - "SELECT id, decision FROM approval WHERE candidate_id=?1", - rusqlite::params![candidate_id], - |r| Ok((r.get(0)?, r.get(1)?)), - ) - .optional()?) - }) + approvals::for_candidate(db, candidate_id) } /// Withdraw a published release (ยง9 `repo-remove` path, decision 23: warns, @@ -247,15 +199,18 @@ pub fn withdraw(db: &Db, actor: &str, release_id: i64, reason: &str) -> Result<( let reason = reason.to_string(); let at = time::now(); db.writer.tx(move |tx| { - tx.execute("UPDATE release SET state='withdrawn' WHERE id=?1", rusqlite::params![release_id])?; - tx.execute( - "UPDATE package SET state='removed' WHERE release_id=?1", - rusqlite::params![release_id], - )?; - tx.execute( - "INSERT INTO audit (at, actor_kind, actor, action, entity, entity_id, detail_json) - VALUES (?1,'human',?2,'withdraw','release',?3,?4)", - rusqlite::params![at, actor, release_id.to_string(), serde_json::json!({"reason": reason}).to_string()], + 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(()) }) |
