diff options
| author | dev <dev@localhost> | 2026-11-20 18:34:26 +0000 |
|---|---|---|
| committer | dev <dev@localhost> | 2026-11-20 18:34:26 +0000 |
| commit | 895f3547043def08f0822f936ae34458315f6982 (patch) | |
| tree | 9e7b08db7225786f1d1ec5b312314801e3d09c90 /src/usecase/reviews.rs | |
| parent | bbae6c8e83cf145f9af472f69591bae804d03048 (diff) | |
| download | aur-build-895f3547043def08f0822f936ae34458315f6982.tar.gz aur-build-895f3547043def08f0822f936ae34458315f6982.zip | |
usecase: one implementation of each workflow, run by both front ends
`main` opened the repository inline before its command match. `serve` opened it
again, four calls in an order that matters. The web routes wrote subscriptions,
toggles, notices and build retries straight through `db::`, so the CLI and the
UI were two independent guesses at the same product, and the guesses had already
drifted: `sub_add` fell back to repo id 1 where the CLI reported the repository
as uninitialised, and `build_retry` remembered to write the audit row while
nothing else did.
`src/usecase/` is the workflow layer now — bootstrap, detect, reviews,
subscriptions, builds, maintenance. A function earns a place here by having more
than one step in an order that matters: `approve` records the decision *and*
queues the build it authorises, because an approval that silently fails to queue
is the exact failure the review queue exists to prevent, and both front ends had
to remember to pair them.
Nothing in the layer formats output or knows a status code exists. Where the two
front ends genuinely disagree, the result carries the disagreement instead of
resolving it: `Approved::queued` is a `Result` because a failed enqueue is fatal
in the CLI — an approved candidate with no build is a stall nobody notices — and
a soft failure in the UI, which has already committed the decision and can retry
from the package page.
`src/http/routes/mutations.rs` now contains no `db::` call at all, and
`main.rs` is 314 lines with no multi-step sequence and nothing exported.
Rank map: usecase 9, http 10, composition roots 11. The layer test enforces
that usecase cannot name http, which is why `serve` starts the scheduler and the
build pool but leaves the HTTP server to `main`.
One user-visible message moves: `aur-build reject <missing>` now says
`not found: candidate N` (the review layer's wording) instead of
`no candidate #N` (main's). Both exit 1; the check moved with the workflow.
280 passed / 0 failed, clippy -D warnings clean, architecture 8/8.
Diffstat (limited to 'src/usecase/reviews.rs')
| -rw-r--r-- | src/usecase/reviews.rs | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/src/usecase/reviews.rs b/src/usecase/reviews.rs new file mode 100644 index 0000000..8cbb534 --- /dev/null +++ b/src/usecase/reviews.rs @@ -0,0 +1,122 @@ +//! 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<Option<Prepared>> { + 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<Option<Prepared>> { + 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<String>, +} + +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<Approval> { + 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<String> }, + 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<Vec<Decision>> { + 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()) +} |
