summaryrefslogtreecommitdiffstats
path: root/src/usecase/reviews.rs
blob: c58e39bdc09b12fca14d3f1e1c340215b4581843 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//! 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.
// Prepared stays `pub`: what `prepare_review` and `view_review` return to the `aur-build` binary, which is a separate crate.
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())
}