use std::path::{Path, PathBuf};
use serde::{ser::SerializeStruct, Serialize, Serializer};
use super::{Badge, NextSteps, Outcome, ReceiptQueryError, Untrusted};
use crate::receipt::{Receipt, ReceiptKind, ReceiptStepStatus};
use crate::RunPlan;
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Status {
pub repos: Vec<RepoRow>,
pub problems: Vec<Problem>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct RepoRow {
pub repo_root: Untrusted,
pub project: Option<Untrusted>,
pub label: Untrusted,
pub path_missing: bool,
pub envs: Vec<EnvCell>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EnvCell {
pub env: Untrusted,
pub approved: bool,
pub approved_target: Option<Untrusted>,
pub code: CodeState,
pub last_attempt: Option<AttemptSummary>,
pub code_run: Option<AttemptSummary>,
pub stale_hint: Option<StaleHint>,
pub target: Option<Untrusted>,
pub newer_elsewhere: Option<NewerElsewhere>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum Mark {
Recorded,
Incomplete,
Observed,
NotKnown,
NoCode,
}
impl EnvCell {
pub fn mark(&self) -> Mark {
match self.code {
CodeState::Recorded { complete: true, .. } => Mark::Recorded,
CodeState::Recorded {
complete: false, ..
} => Mark::Incomplete,
CodeState::Observed { .. } => Mark::Observed,
CodeState::NotKnown { .. } => Mark::NotKnown,
CodeState::NeverDeployed
| CodeState::NoCodeChangeInLast { .. }
| CodeState::TargetChanged { .. } => Mark::NoCode,
}
}
}
impl Serialize for EnvCell {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut cell = serializer.serialize_struct("EnvCell", 11)?;
cell.serialize_field("env", &self.env)?;
cell.serialize_field("approved", &self.approved)?;
cell.serialize_field("approved_target", &self.approved_target)?;
cell.serialize_field("mark", &self.mark())?;
cell.serialize_field("code", &self.code)?;
cell.serialize_field("last_attempt", &self.last_attempt)?;
cell.serialize_field("code_run", &self.code_run)?;
cell.serialize_field("stale_hint", &self.stale_hint)?;
cell.serialize_field("target", &self.target)?;
cell.serialize_field("newer_elsewhere", &self.newer_elsewhere)?;
cell.serialize_field("needs_attention", &self.needs_attention())?;
cell.end()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct NewerElsewhere {
pub repo_root: Untrusted,
pub env: Untrusted,
pub run_id: Untrusted,
pub started_at_ms: u128,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum CodeState {
Recorded {
sha: Untrusted,
complete: bool,
evidence: Evidence,
run_id: Untrusted,
at_ms: u128,
},
Observed {
sha: Untrusted,
dirty: Option<bool>,
expected: Untrusted,
run_id: Untrusted,
outcome: Option<Outcome>,
at_ms: u128,
},
NotKnown {
run_id: Option<Untrusted>,
reason: NotKnownReason,
},
NeverDeployed,
NoCodeChangeInLast {
records: usize,
},
TargetChanged {
run_id: Untrusted,
target: Untrusted,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum Evidence {
FastForward,
CheckedOutAtStart,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(tag = "kind", content = "status", rename_all = "snake_case")]
pub enum NotKnownReason {
StepZero(ReceiptStepStatus),
NoStepZero,
Unreadable,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct AttemptSummary {
pub run_id: Untrusted,
pub started_at_ms: u128,
pub kind: ReceiptKind,
pub run_plan: RunPlan,
#[serde(skip_serializing_if = "Option::is_none")]
pub steps_run: Option<String>,
pub badge: Badge,
pub next_steps: NextSteps,
pub path: PathBuf,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum StaleHint {
NewestUnreadable { path: PathBuf },
UnattributedNewer { path: PathBuf },
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Problem {
pub path: PathBuf,
pub message: Untrusted,
pub kind: ProblemKind,
pub cells: Vec<CellRef>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ProblemKind {
Trust,
Projects,
ProjectDir,
Receipt,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize)]
pub struct CellRef {
pub repo_root: Untrusted,
pub env: Untrusted,
}
impl AttemptSummary {
pub(crate) fn of(receipt: &Receipt, path: &Path) -> Self {
Self {
run_id: receipt.run_id.clone().into(),
started_at_ms: receipt.started_at_ms,
kind: receipt.kind,
run_plan: receipt.run_plan,
steps_run: receipt.steps_run(),
badge: receipt.badge(),
next_steps: receipt.next_steps(),
path: path.to_path_buf(),
}
}
fn needs_attention(&self) -> bool {
self.badge.outcome.needs_attention() || self.badge.flags.iter().any(|flag| flag.is_bad())
}
}
impl EnvCell {
pub fn target_moved(&self) -> Option<&str> {
let approved = self.approved_target.as_ref()?.raw();
(self.target.as_ref()?.raw() != approved).then_some(approved)
}
pub fn needs_attention(&self) -> bool {
let code = match &self.code {
CodeState::Recorded { complete, .. } => !complete,
CodeState::Observed { .. } | CodeState::NotKnown { .. } => true,
CodeState::NeverDeployed
| CodeState::NoCodeChangeInLast { .. }
| CodeState::TargetChanged { .. } => false,
};
code || self.stale_hint.is_some()
|| self.newer_elsewhere.is_some()
|| self.target_moved().is_some()
|| self
.code_run
.iter()
.chain(&self.last_attempt)
.any(AttemptSummary::needs_attention)
}
}
pub fn env_order(a: &str, b: &str) -> std::cmp::Ordering {
let rank = |env: &str| match env {
"production" => 0,
"staging" => 1,
_ => 2,
};
rank(a).cmp(&rank(b)).then_with(|| a.cmp(b))
}
pub fn status(receipts_root: &Path, trust_path: &Path) -> Result<Status, ReceiptQueryError> {
Ok(crate::receipt::overview(receipts_root, trust_path)?)
}
#[cfg(test)]
mod tests;