Skip to content

Commit

Permalink
Add --output-format=json-full to include the criteria definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
Gankra committed Oct 31, 2022
1 parent 6ca1282 commit 19d574a
Show file tree
Hide file tree
Showing 132 changed files with 207 additions and 11 deletions.
9 changes: 9 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,15 @@ pub enum OutputFormat {
Human,
/// Print output in a machine-readable form with minimal extra context.
Json,
/// Print output in a machine-readable form with as much additional context
/// as possible to enable another tool to operate on that information.
///
/// Extra information will be stored in a top-level 'context' field and
/// include:
///
/// * criteria: The custom criteria this project defines (and their descriptions)
/// * store: The location of the store (supply-chain dir)
JsonFull,
}

#[derive(Clone, Debug)]
Expand Down
11 changes: 11 additions & 0 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,10 +554,21 @@ pub struct CommandHistory {
/// up as miette-style json errors.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonReport {
/// Additional optional context for automation. Only enabled with `--output-format=json-full`
pub context: Option<JsonReportContext>,
#[serde(flatten)]
pub conclusion: JsonReportConclusion,
}

/// Additional context for automation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonReportContext {
/// Where the store (supply-chain) is located
pub store_path: String,
/// The currently defined criteria (currently excludes builtins criteria like `safe-to-deploy`).
pub criteria: SortedMap<CriteriaName, CriteriaEntry>,
}

/// The conclusion of running `check` or `suggest`
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "conclusion")]
Expand Down
21 changes: 17 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use crate::format::{
SortedMap, StoreInfo,
};
use crate::out::{indeterminate_spinner, Out, StderrLogWriter, MULTIPROGRESS};
use crate::resolver::{CriteriaMapper, CriteriaNamespace, DepGraph, ResolveDepth};
use crate::resolver::{CriteriaMapper, CriteriaNamespace, DepGraph, ReportContext, ResolveDepth};
use crate::storage::{Cache, Store};

mod cli;
Expand Down Expand Up @@ -1281,7 +1281,15 @@ fn cmd_suggest(
OutputFormat::Human => report
.print_suggest_human(out, cfg, suggest.as_ref())
.into_diagnostic()?,
OutputFormat::Json => report.print_json(out, suggest.as_ref())?,
OutputFormat::Json => report.print_json(out, suggest.as_ref(), None)?,
OutputFormat::JsonFull => report.print_json(
out,
suggest.as_ref(),
Some(ReportContext {
store: &suggest_store,
cfg,
}),
)?,
}

Ok(())
Expand Down Expand Up @@ -1514,7 +1522,12 @@ fn cmd_check(out: &Arc<dyn Out>, cfg: &Config, sub_args: &CheckArgs) -> Result<(
OutputFormat::Human => report
.print_human(out, cfg, suggest.as_ref())
.into_diagnostic()?,
OutputFormat::Json => report.print_json(out, suggest.as_ref())?,
OutputFormat::Json => report.print_json(out, suggest.as_ref(), None)?,
OutputFormat::JsonFull => report.print_json(
out,
suggest.as_ref(),
Some(ReportContext { store: &store, cfg }),
)?,
}

// Only save imports if we succeeded, to avoid any modifications on error.
Expand Down Expand Up @@ -1734,7 +1747,7 @@ fn cmd_dump_graph(
let graph = resolver::DepGraph::new(&cfg.metadata, cfg.cli.filter_graph.as_ref(), None);
match cfg.cli.output_format {
OutputFormat::Human => graph.print_mermaid(out, sub_args).into_diagnostic()?,
OutputFormat::Json => {
OutputFormat::Json | OutputFormat::JsonFull => {
serde_json::to_writer_pretty(&**out, &graph.nodes).into_diagnostic()?
}
}
Expand Down
17 changes: 14 additions & 3 deletions src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ use crate::errors::{RegenerateExemptionsError, SuggestError};
use crate::format::{
self, AuditKind, AuditsFile, CriteriaName, CriteriaStr, Delta, DependencyCriteria, DiffStat,
ExemptedDependency, ImportName, JsonPackage, JsonReport, JsonReportConclusion,
JsonReportFailForVet, JsonReportFailForViolationConflict, JsonReportSuccess, JsonSuggest,
JsonSuggestItem, JsonVetFailure, PackageName, PackageStr, PolicyEntry, RemoteImport,
SAFE_TO_DEPLOY, SAFE_TO_RUN,
JsonReportContext, JsonReportFailForVet, JsonReportFailForViolationConflict, JsonReportSuccess,
JsonSuggest, JsonSuggestItem, JsonVetFailure, PackageName, PackageStr, PolicyEntry,
RemoteImport, SAFE_TO_DEPLOY, SAFE_TO_RUN,
};
use crate::format::{FastMap, FastSet, SortedMap, SortedSet};
use crate::network::Network;
Expand Down Expand Up @@ -2523,6 +2523,12 @@ fn visit_failures<'a, T>(
Ok(())
}

/// Additional optional context for printing a json Report for automation.
pub struct ReportContext<'a> {
pub cfg: &'a Config,
pub store: &'a Store,
}

impl<'a> ResolveReport<'a> {
pub fn has_errors(&self) -> bool {
// Just check the conclusion
Expand Down Expand Up @@ -2815,8 +2821,13 @@ impl<'a> ResolveReport<'a> {
&self,
out: &Arc<dyn Out>,
suggest: Option<&Suggest>,
context: Option<ReportContext>,
) -> Result<(), miette::Report> {
let result = JsonReport {
context: context.map(|c| JsonReportContext {
store_path: c.cfg.metacfg.store_path().display().to_string(),
criteria: c.store.audits.criteria.clone(),
}),
conclusion: match &self.conclusion {
Conclusion::Success(success) => {
let json_package = |pkgidx: &PackageIdx| {
Expand Down
2 changes: 1 addition & 1 deletion src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1119,7 +1119,7 @@ fn get_reports(metadata: &Metadata, report: ResolveReport) -> (String, String) {
.unwrap();
let json_output = BasicTestOutput::new();
report
.print_json(&json_output.clone().as_dyn(), suggest.as_ref())
.print_json(&json_output.clone().as_dyn(), suggest.as_ref(), None)
.unwrap();
(human_output.to_string(), json_output.to_string())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source: src/tests/vet.rs
expression: json
---
{
"context": null,
"conclusion": "success",
"vetted_fully": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source: src/tests/vet.rs
expression: json
---
{
"context": null,
"conclusion": "success",
"vetted_fully": [],
"vetted_partially": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source: src/tests/vet.rs
expression: json
---
{
"context": null,
"conclusion": "success",
"vetted_fully": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source: src/tests/vet.rs
expression: json
---
{
"context": null,
"conclusion": "fail (vetting)",
"failures": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source: src/tests/vet.rs
expression: json
---
{
"context": null,
"conclusion": "success",
"vetted_fully": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source: src/tests/vet.rs
expression: json
---
{
"context": null,
"conclusion": "success",
"vetted_fully": [],
"vetted_partially": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source: src/tests/vet.rs
expression: json
---
{
"context": null,
"conclusion": "success",
"vetted_fully": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source: src/tests/vet.rs
expression: json
---
{
"context": null,
"conclusion": "fail (vetting)",
"failures": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source: src/tests/vet.rs
expression: json
---
{
"context": null,
"conclusion": "success",
"vetted_fully": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source: src/tests/vet.rs
expression: json
---
{
"context": null,
"conclusion": "success",
"vetted_fully": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source: src/tests/vet.rs
expression: json
---
{
"context": null,
"conclusion": "fail (vetting)",
"failures": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source: src/tests/vet.rs
expression: json
---
{
"context": null,
"conclusion": "fail (vetting)",
"failures": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source: src/tests/vet.rs
expression: json
---
{
"context": null,
"conclusion": "success",
"vetted_fully": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source: src/tests/vet.rs
expression: json
---
{
"context": null,
"conclusion": "success",
"vetted_fully": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source: src/tests/vet.rs
expression: json
---
{
"context": null,
"conclusion": "success",
"vetted_fully": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source: src/tests/vet.rs
expression: json
---
{
"context": null,
"conclusion": "success",
"vetted_fully": [],
"vetted_partially": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source: src/tests/vet.rs
expression: json
---
{
"context": null,
"conclusion": "success",
"vetted_fully": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source: src/tests/vet.rs
expression: json
---
{
"context": null,
"conclusion": "fail (vetting)",
"failures": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source: src/tests/vet.rs
expression: json
---
{
"context": null,
"conclusion": "fail (vetting)",
"failures": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source: src/tests/vet.rs
expression: json
---
{
"context": null,
"conclusion": "success",
"vetted_fully": [],
"vetted_partially": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source: src/tests/vet.rs
expression: json
---
{
"context": null,
"conclusion": "success",
"vetted_fully": [],
"vetted_partially": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source: src/tests/vet.rs
expression: json
---
{
"context": null,
"conclusion": "fail (vetting)",
"failures": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source: src/tests/vet.rs
expression: json
---
{
"context": null,
"conclusion": "fail (vetting)",
"failures": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source: src/tests/vet.rs
expression: json
---
{
"context": null,
"conclusion": "success",
"vetted_fully": [],
"vetted_partially": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source: src/tests/vet.rs
expression: json
---
{
"context": null,
"conclusion": "success",
"vetted_fully": [],
"vetted_partially": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source: src/tests/vet.rs
expression: json
---
{
"context": null,
"conclusion": "fail (vetting)",
"failures": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source: src/tests/vet.rs
expression: json
---
{
"context": null,
"conclusion": "fail (vetting)",
"failures": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source: src/tests/vet.rs
expression: json
---
{
"context": null,
"conclusion": "success",
"vetted_fully": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source: src/tests/vet.rs
expression: json
---
{
"context": null,
"conclusion": "success",
"vetted_fully": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source: src/tests/vet.rs
expression: json
---
{
"context": null,
"conclusion": "success",
"vetted_fully": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source: src/tests/vet.rs
expression: json
---
{
"context": null,
"conclusion": "success",
"vetted_fully": [],
"vetted_partially": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source: src/tests/vet.rs
expression: json
---
{
"context": null,
"conclusion": "success",
"vetted_fully": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source: src/tests/vet.rs
expression: json
---
{
"context": null,
"conclusion": "fail (vetting)",
"failures": [
{
Expand Down
Loading

0 comments on commit 19d574a

Please sign in to comment.