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
//! Which program build reviewed each module's approved configuration.
//!
//! A module's `config.review` answer is valid only for the build that gave it.
//! Approving a new program for a module is a launch-only approval: the new
//! build cannot run before that approval, and the build still running is not
//! the one being approved. Every plan therefore records, per module, the
//! program identity of the build its approved configuration was reviewed by.
//! Once a newly approved program is active, the next plan re-reviews the
//! module's approved layers with that build and records it. Until then the new
//! build runs with the configuration approved last, and status names the
//! module as waiting for re-review.
use std::collections::BTreeMap;
use super::{program::approved_base, GateError, GateSnapshot, Result};
use crate::config_store::{CommitKind, StoreBody};
/// Key in a generation's `apply_plan`: module id to the program identity (see
/// [`approved_base`]) of the build that reviewed that module's configuration.
pub(super) const REVIEWED_PROGRAMS: &str = "reviewed_programs";
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Rereview {
/// The approved program has not yet become active through a child started
/// from it, so no running process is the build to review with.
WaitingForProgram,
/// The approved program is active; the next plan reviews with it.
Ready,
}
pub(super) fn identity(snapshot: &GateSnapshot, module: &str) -> String {
approved_base(snapshot, module).as_str().to_owned()
}
/// Program identities recorded by the newest approved generation. A generation
/// without the record vouches for no review: every module it approved counts
/// as needing re-review rather than as reviewed by the programs it approved.
pub(super) fn recorded(body: &StoreBody) -> Result<BTreeMap<String, String>> {
let Some(record) = body
.generations
.values()
.rev()
.find(|entry| entry.kind != CommitKind::Development)
.and_then(|entry| entry.generation.changeset.apply_plan.get(REVIEWED_PROGRAMS))
else {
return Ok(BTreeMap::new());
};
serde_json::from_value(record.clone()).map_err(GateError::invalid)
}
/// Program identity of the module's active generation. Program changes become
/// active only through a child started from the new program, so this is the
/// build of the process that acknowledged the module's current configuration.
pub(super) fn active(body: &StoreBody, module: &str) -> Result<Option<String>> {
let Some(entry) = body
.versions
.iter()
.find(|(tuple, _)| tuple.module == module && tuple.project.is_none())
.and_then(|(_, versions)| versions.active)
.and_then(|id| body.generations.get(&id))
else {
return Ok(None);
};
let snapshot = GateSnapshot::from_store(&entry.configuration)?;
Ok(Some(identity(&snapshot, module)))
}
/// Modules among `modules` whose approved program is not the build their
/// configuration was last reviewed by.
pub(super) fn pending<'a>(
body: &StoreBody,
modules: impl IntoIterator<Item = &'a String>,
) -> Result<BTreeMap<String, Rereview>> {
let approved = GateSnapshot::from_store(&body.approved)?;
let recorded = recorded(body)?;
let mut pending = BTreeMap::new();
for module in modules {
let program = identity(&approved, module);
if recorded.get(module) == Some(&program) {
continue;
}
let state = if active(body, module)?.as_ref() == Some(&program) {
Rereview::Ready
} else {
Rereview::WaitingForProgram
};
pending.insert(module.clone(), state);
}
Ok(pending)
}