summaryrefslogtreecommitdiffstats
path: root/src/review/intake.rs
blob: 78e4eb6ad8411c2865a1738ed45bff8f3a13b650 (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
//! Turning a detected change into a scored candidate is one operation.
//!
//! The watcher used to perform review's write protocol itself: look up the
//! accepted release, upsert the candidate, assess the risk, store the risk,
//! work out the missing dependencies, store those. Six calls that only make
//! sense in that order, issued by a module that has no business knowing the
//! order — and if it got the order wrong, or skipped the scoring because it was
//! in a hurry, nothing would have complained.
//!
//! `record` is the protocol. The watcher calls it once with what it observed,
//! and review decides what a candidate means.

use crate::config::Config;
use crate::db::{
    self,
    model::{Recipe, ReleaseRow, SourceDigest},
    Db, Pkgbase,
};
use crate::error::Result;
use crate::review::candidates::{self, NewCandidate, Upsert};
use crate::review::risk;

/// Record a detected change as a candidate, scored. Returns what the upsert
/// decided so the caller can report it; a change that collapsed or was already
/// rejected is recorded as such and scored only when a candidate row exists.
pub fn record(
    db: &Db,
    cfg: &Config,
    pb: &Pkgbase,
    recipe: &Recipe,
    digests: &[SourceDigest],
    accepted: Option<&ReleaseRow>,
    nc: &NewCandidate,
) -> Result<Upsert> {
    let up = candidates::upsert_candidate(db, pb, accepted, nc)?;
    let id = match &up {
        Upsert::Created { id, .. } | Upsert::Collapsed { id, .. } | Upsert::Superseded { new: id, .. } => *id,
        // Nothing was written, so there is nothing to score.
        Upsert::PreviouslyRejected | Upsert::Unchanged => return Ok(up),
    };
    // Scoring is advisory: a candidate that cannot be scored is still a
    // candidate, and a reviewer sees the diff either way.
    if let Ok(a) = risk::assess(db, cfg, recipe, digests, accepted) {
        let _ = db::candidates::set_risk(db, id, &a.level, &a.signals);
    }
    let _ = db::candidates::set_missing_deps(db, id, &missing_deps(db, pb, recipe));
    Ok(up)
}

/// Dependencies this recipe needs that we build ourselves but have not
/// published yet — the reason a build would fail on a missing dep, said before
/// it runs.
fn missing_deps(db: &Db, pb: &Pkgbase, recipe: &Recipe) -> Vec<String> {
    let Ok(ours) = db::our_package_names(db) else { return Vec::new() };
    let mut out = Vec::new();
    for dep in crate::pkgver::dep_names(&recipe.depends) {
        if ours.contains(&dep) || dep == pb.name || recipe.pkgnames.contains(&dep) {
            continue;
        }
        let ours_to_publish = db::get_subscription(db, &dep).is_ok_and(|s| s.is_some())
            || db::get_pkgbase(db, &dep).is_ok_and(|p| p.is_some());
        if ours_to_publish {
            out.push(dep);
        }
    }
    out.sort();
    out
}