diff options
Diffstat (limited to 'src/review/approve.rs')
| -rw-r--r-- | src/review/approve.rs | 121 |
1 files changed, 65 insertions, 56 deletions
diff --git a/src/review/approve.rs b/src/review/approve.rs index c5b2f07..26963d6 100644 --- a/src/review/approve.rs +++ b/src/review/approve.rs @@ -1,4 +1,4 @@ -use crate::db::{self, approvals, candidates, CandidateState, *}; +use crate::db::{self, approvals, candidates, CandidateRow, CandidateState, Db}; use crate::error::{Error, Missing, Refused, Result}; use crate::time; @@ -43,64 +43,68 @@ pub fn approve( 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 note = note.map(std::string::ToString::to_string); let at = time::now(); - 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(Missing::Candidate(candidate_id)))?; - let id = subject.id; - let base = subject.base_release_id; - let current_base = subject.current_base_release_id; + 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(Missing::Candidate(candidate_id)))?; + let id = subject.id; + let base = subject.base_release_id; + let current_base = subject.current_base_release_id; - if !subject.state.is_open() { - return Ok(Err(ApproveError::Stale(Stale::NotPending { state: subject.state.to_string() }))); - } - if subject.identity_hash != ih { - return Ok(Err(ApproveError::Stale(Stale::Identity { expected: ih.clone(), actual: subject.identity_hash }))); - } - if subject.diff_digest != dd { - return Ok(Err(ApproveError::Stale(Stale::Diff { expected: dd.clone(), actual: subject.diff_digest }))); - } - if base != current_base { - return Ok(Err(ApproveError::Stale(Stale::Base { expected: base, actual: current_base }))); - } + if !subject.state.is_open() { + return Ok(Err(ApproveError::Stale(Stale::NotPending { state: subject.state.to_string() }))); + } + if subject.identity_hash != ih { + return Ok(Err(ApproveError::Stale(Stale::Identity { + expected: ih.clone(), + actual: subject.identity_hash, + }))); + } + if subject.diff_digest != dd { + return Ok(Err(ApproveError::Stale(Stale::Diff { expected: dd.clone(), actual: subject.diff_digest }))); + } + if base != current_base { + return Ok(Err(ApproveError::Stale(Stale::Base { expected: base, actual: current_base }))); + } - let approval_id = approvals::record_in( - tx, - &approvals::Decision { + 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, CandidateState::Approved, &at)?; + db::audit_in( + tx, + &db::AuditEntry::human( + &at, + &actor, + "approve", + "candidate", + &id.to_string(), + 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, - at: &at, - decision: "approved", - risk_level: &subject.risk_level, - diff_digest: &dd, - identity_hash: &ih, - note: note.as_deref(), - }, - )?; - candidates::resolve_in(tx, id, CandidateState::Approved, &at)?; - db::audit_in( - tx, - &db::AuditEntry::human( - &at, - &actor, - "approve", - "candidate", - &id.to_string(), - 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(ApproveError::System)? + decision: "approved".into(), + diff_digest: dd, + identity_hash: ih, + })) + }) + .map_err(ApproveError::System)? } pub fn reject(db: &Db, actor: &str, candidate_id: i64, note: &str) -> Result<()> { @@ -111,7 +115,12 @@ pub fn reject(db: &Db, actor: &str, candidate_id: i64, note: &str) -> Result<()> let state = candidates::state_in(tx, candidate_id)? .ok_or_else(|| Error::not_found(Missing::Candidate(candidate_id)))?; if !state.is_open() { - return Err(Error::conflict(Refused::WrongState { entity: "candidate", id: candidate_id.to_string(), found: state.to_string(), expected: "pending" })); + return Err(Error::conflict(Refused::WrongState { + entity: "candidate", + id: candidate_id.to_string(), + found: state.to_string(), + expected: "pending", + })); } let identity = candidates::identity_in(tx, candidate_id)? .ok_or_else(|| Error::not_found(Missing::Candidate(candidate_id)))?; @@ -141,7 +150,7 @@ pub struct BulkItem { impl BulkItem { pub fn pkgbase(&self) -> &str { - self.candidate.as_ref().map(|c| c.pkgbase.as_str()).unwrap_or("(gone)") + self.candidate.as_ref().map_or("(gone)", |c| c.pkgbase.as_str()) } } @@ -157,7 +166,7 @@ pub enum BulkOutcome { /// only way to reach it, and the alias is what carries the /// feature-conditional visibility. mod bulk_refusal { - use super::*; + use super::{Error, Stale}; #[derive(Debug, thiserror::Error)] pub enum BulkRefusal { |
