Skip to content

Commit

Permalink
make summation of reports actually look nice, and implement
Browse files Browse the repository at this point in the history
 it in changelog validate
  • Loading branch information
Emilgardis committed Jan 9, 2024
1 parent 84786e2 commit 0505a66
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 13 deletions.
8 changes: 4 additions & 4 deletions xtask/src/build_docker_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,10 @@ pub fn build_docker_image(
}
if results.iter().any(|r| r.is_err()) {
#[allow(unknown_lints, clippy::manual_try_fold)]
results
.into_iter()
.filter_map(Result::err)
.fold(Err(eyre::eyre!("encountered error(s)")), |_, e| Err(e.1))?;
return Err(crate::util::with_section_reports(
eyre::eyre!("some error(s) encountered"),
results.into_iter().filter_map(Result::err).map(|e| e.1),
));
}
Ok(())
}
Expand Down
45 changes: 37 additions & 8 deletions xtask/src/changelog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ use std::fs;
use std::path::Path;

use crate::util::{project_dir, write_to_string};
use cross::shell::MessageInfo;

use chrono::{Datelike, Utc};
use clap::{Args, Subcommand};
use cross::shell::MessageInfo;
use cross::ToUtf8;
use eyre::Context;
use serde::Deserialize;
Expand Down Expand Up @@ -508,6 +509,7 @@ pub fn build_changelog(
Ok(())
}

#[allow(clippy::disallowed_methods)]
pub fn validate_changelog(
ValidateChangelog { mut files, .. }: ValidateChangelog,
msg_info: &mut MessageInfo,
Expand All @@ -532,19 +534,46 @@ pub fn validate_changelog(
})
.collect();
}
let mut errors = vec![];
for file in files {
let file_name = Path::new(&file);
let path = changes_dir.join(file_name);
let stem = file_stem(&path)?;
let contents =
fs::read_to_string(&path).wrap_err_with(|| eyre::eyre!("cannot find file {file}"))?;
let id = IdType::parse_stem(stem)?;
let value = serde_json::from_str(&contents)
.wrap_err_with(|| format!("unable to parse JSON for \"{file}\""))?;
let _ = ChangelogEntry::from_value(id, value)
.wrap_err_with(|| format!("unable to extract changelog from \"{file}\""))?;
let contents = fs::read_to_string(&path)
.wrap_err_with(|| eyre::eyre!("cannot find file {}", path.display()))?;

let id = match IdType::parse_stem(stem)
.wrap_err_with(|| format!("unable to parse file stem for \"{}\"", path.display()))
{
Ok(id) => id,
Err(e) => {
errors.push(e);
continue;
}
};

let value = match serde_json::from_str(&contents)
.wrap_err_with(|| format!("unable to parse JSON for \"{}\"", path.display()))
{
Ok(value) => value,
Err(e) => {
errors.push(e);
continue;
}
};

let res = ChangelogEntry::from_value(id, value)
.wrap_err_with(|| format!("unable to extract changelog from \"{}\"", path.display()))
.map(|_| ());
errors.extend(res.err());
}

if !errors.is_empty() {
return Err(crate::util::with_section_reports(
eyre::eyre!("some files were not validated"),
errors,
));
}
// also need to validate the existing changelog
let _ = read_changelog(&root)?;

Expand Down
12 changes: 11 additions & 1 deletion xtask/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::env;
use std::fs;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::{env, fmt::Write as _};

Check warning on line 5 in xtask/src/util.rs

View workflow job for this annotation

GitHub Actions / docker-in-docker

unused import: `fmt::Write`

use cross::shell::MessageInfo;
use cross::{docker, CommandExt, ToUtf8};
Expand Down Expand Up @@ -118,6 +118,16 @@ pub fn get_matrix() -> &'static Vec<CiTarget> {
.unwrap()
}

pub fn with_section_reports(
origin: eyre::Report,
iter: impl IntoIterator<Item = eyre::Report>,
) -> eyre::Report {
use color_eyre::{Section as _, SectionExt as _};
iter.into_iter().fold(origin, |report, e| {
report.section(format!("{e:?}").header("Error:"))
})
}

pub fn format_repo(registry: &str, repository: &str) -> String {
let mut output = String::new();
if !repository.is_empty() {
Expand Down

0 comments on commit 0505a66

Please sign in to comment.