use std::io::{self, Write as _};
use anyhow::Result;
use openmoat_core::attribution::{Note, SessionSummary, summarise};
use serde::Serialize;
use super::NOTES_REF;
use super::git::Reader;
use super::notes::decode;
use crate::cli::{DiffFormat, ProvenanceLevel};
use crate::render;
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Section {
pub level: &'static str,
pub noted_commits: usize,
pub commits: usize,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub unreadable: Vec<String>,
pub sessions: Vec<SessionSummary>,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<String>,
}
pub fn section(base: Option<&str>, head: Option<&str>, level: ProvenanceLevel) -> Option<Section> {
let level = match level {
ProvenanceLevel::Off => return None,
ProvenanceLevel::Review => "review",
ProvenanceLevel::Info => "info",
};
let base = base?;
let empty = |error: Option<String>| Section {
level,
noted_commits: 0,
commits: 0,
unreadable: Vec::new(),
sessions: Vec::new(),
error,
};
let read = || -> Result<(usize, Vec<Note>, Vec<String>)> {
let git = Reader::here()?;
let commits = git.change(base, head.unwrap_or("HEAD"))?;
let blobs = git.noted()?;
let mut notes = Vec::new();
let mut unreadable = Vec::new();
for commit in &commits {
let Some(blob) = blobs.get(commit) else {
continue;
};
match git.note_text(blob).and_then(|t| decode(&t)) {
Ok(note) if ¬e.commit == commit => notes.push(note),
_ => unreadable.push(commit.clone()),
}
}
Ok((commits.len(), notes, unreadable))
};
match read() {
Ok((_, notes, unreadable)) if notes.is_empty() && unreadable.is_empty() => None,
Ok((commits, notes, unreadable)) => Some(Section {
noted_commits: notes.len() + unreadable.len(),
commits,
sessions: summarise(¬es),
unreadable,
..empty(None)
}),
Err(error) => Some(empty(Some(format!("{error:#}")))),
}
}
#[derive(Serialize)]
pub struct WithSection<'a, R> {
#[serde(flatten)]
pub report: &'a R,
pub provenance: &'a Section,
}
pub fn write<R: Serialize>(
format: DiffFormat,
report: &R,
section: Option<&Section>,
plain: impl FnOnce(DiffFormat, &R) -> Result<()>,
) -> Result<()> {
let Some(section) = section else {
return plain(format, report);
};
if format == DiffFormat::Json {
return render::json(&WithSection {
report,
provenance: section,
});
}
let mut out = io::stdout().lock();
if format == DiffFormat::Github {
for line in annotations(section) {
writeln!(out, "{line}")?;
}
}
plain(format, report)?;
text(&mut out, section)?;
Ok(())
}
pub fn lines(section: &Section) -> Vec<String> {
section
.sessions
.iter()
.map(|s| {
let files = match s.files {
1 => "1 file".to_owned(),
n => format!("{n} files"),
};
let lines = match s.lines {
0 => String::new(),
1 => " / 1 line".to_owned(),
n => format!(" / {n} lines"),
};
let after = s
.untrusted
.as_ref()
.map(|u| format!(", after reading {} ({})", u.kind.describe(), u.source))
.unwrap_or_default();
format!(
"{files}{lines} written by {} (session {}{after})",
s.host,
short(&s.session)
)
})
.collect()
}
pub fn text(out: &mut impl io::Write, section: &Section) -> io::Result<()> {
let tag = format!("{:<7}", section.level);
if let Some(error) = §ion.error {
return writeln!(
out,
"{tag} provenance: the notes on {NOTES_REF} could not be read: {}",
render::clean(error)
);
}
for line in lines(section) {
writeln!(out, "{tag} provenance: {}", render::clean(&line))?;
}
for commit in §ion.unreadable {
writeln!(
out,
"{tag} provenance: the note on {} is not an OpenMoat provenance note",
short(commit)
)?;
}
writeln!(
out,
" {} of {} commits have a note on {NOTES_REF}; notes are claims from the machine \
that wrote them (`openmoat provenance --verify` there checks them), and a commit \
without one proves nothing",
section.noted_commits, section.commits
)
}
pub fn annotations(section: &Section) -> Vec<String> {
let command = if section.level == "review" {
"warning"
} else {
"notice"
};
let title = "title=OpenMoat provenance";
let mut out: Vec<String> = lines(section)
.iter()
.map(|line| format!("::{command} {title}::{}", data(line)))
.collect();
if let Some(error) = §ion.error {
out.push(format!(
"::{command} {title}::{}",
data(&format!("notes could not be read: {error}"))
));
}
out
}
fn data(text: &str) -> String {
text.replace('%', "%25")
.replace('\r', "%0D")
.replace('\n', "%0A")
}
fn short(id: &str) -> &str {
id.get(..12).unwrap_or(id)
}