From 4c5d3c7855225908cd4fe90f70c96f742c79884d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Ch=C4=99=C4=87?= Date: Mon, 9 Nov 2026 14:20:20 +0000 Subject: [PATCH] Write a JUnit XML report from ci review `proofline ci review --junit FILE` writes the retained pull-request review as JUnit XML, the one report format Jenkins, GitLab CI, Buildkite, CircleCI and Azure Pipelines all render as a test report. Each assessment lane is a test suite. An open finding or a blocked check is a failure, an incomplete lane is an error, and a dismissed, accepted or resolved finding passes with its state in the case output. Like SARIF it is a closed projection: no raw API document, no evidence values, no clock. The file does not change the exit code. --- README.md | 6 + src/hooks.rs | 13 +- src/junit.rs | 479 +++++++++++++++++++++++++++ src/lib.rs | 76 ++++- src/model.rs | 3 + src/render.rs | 1 + src/sarif.rs | 59 +++- tests/junit.rs | 877 +++++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 1482 insertions(+), 32 deletions(-) create mode 100644 src/junit.rs create mode 100644 tests/junit.rs diff --git a/README.md b/README.md index 25b8097..291f073 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,12 @@ only the file name. The JSON body cannot exceed 64 MiB. The command sends the ex An identical upload returns the recorded report. Upload success records evidence; the server evaluates the coverage check separately. +## Other CI systems + +`proofline ci review --junit proofline.xml` also writes the review as JUnit XML. An open finding or a blocked check is a failure and an incomplete assessment is an error; a dismissed, accepted or resolved finding passes. The exit code is unchanged, so `--fail-on` still decides it. +GitLab CI: `artifacts: { when: always, reports: { junit: proofline.xml } }` +Jenkins: `sh 'proofline ci review --junit proofline.xml'`, then `junit 'proofline.xml'` in `post { always { } }`. + Licensed under Apache-2.0. ## Publishing diff --git a/src/hooks.rs b/src/hooks.rs index 3c79631..8141b25 100644 --- a/src/hooks.rs +++ b/src/hooks.rs @@ -22,7 +22,9 @@ use crate::outcome::{self, Outcome}; use crate::render::location; use crate::severity::Severity; use crate::text::one_line; -use crate::{RepositoryArgs, Verdict, connect, connected_repository, preflight, read_review, say}; +use crate::{ + ReportKind, RepositoryArgs, Verdict, connect, connected_repository, preflight, read_review, say, +}; /// Identifies a script this CLI wrote. Uninstall removes a hook only when its /// whole text is the script this version writes, so an edited hook stays. @@ -493,8 +495,13 @@ async fn request( verify_finding_ids: Vec::new(), }; let requested = api.request_patch_review(repository_id, &request).await?; - let (_, detail, state) = - read_review(api, requested.review_id, Some(deadline), false).await?; + let (_, detail, state) = read_review( + api, + requested.review_id, + Some(deadline), + ReportKind::Unchecked, + ) + .await?; anyhow::Ok((requested.review_id, detail, state)) }; match requested.await { diff --git a/src/junit.rs b/src/junit.rs new file mode 100644 index 0000000..8c39fe2 --- /dev/null +++ b/src/junit.rs @@ -0,0 +1,479 @@ +//! Read-only `JUnit` XML for one retained pull-request review, for CI systems +//! that render a test report. Like the SARIF projection it is closed: it never +//! serializes the raw API document or World evidence values, and it reads no +//! clock. + +use std::collections::BTreeSet; + +use uuid::Uuid; + +use crate::model::{Assessment, Check, CodeAssessment, Finding, ReviewDetail}; +use crate::outcome::{self, Outcome}; +use crate::sarif::{self, SubjectError}; + +const MESSAGE_CHARACTERS: usize = 200; + +#[derive(Debug, thiserror::Error)] +pub(super) enum JunitError { + #[error(transparent)] + Subject(#[from] SubjectError), + #[error("finding {finding} has unsupported severity {severity}; JUnit export refused")] + UnsupportedSeverity { finding: Uuid, severity: String }, +} + +/// How one test case ended. Only an open finding or a blocked check fails; a +/// review that could not finish a lane is an error, mirroring exit code 2. +enum Status { + Passed, + Failed { + kind: String, + message: String, + body: String, + }, + Errored { + kind: String, + message: String, + }, + Skipped { + message: &'static str, + }, +} + +struct Case { + classname: String, + name: String, + status: Status, + /// Plain lines for ``: the schema allows no properties here. + output: Vec, +} + +struct Suite { + name: String, + properties: Vec<(&'static str, String)>, + cases: Vec, +} + +impl Suite { + fn count(&self, matching: impl Fn(&Status) -> bool) -> usize { + self.cases + .iter() + .filter(|case| matching(&case.status)) + .count() + } +} + +pub(super) fn review( + detail: &ReviewDetail, + requested: Uuid, + review_url: &str, +) -> Result { + sarif::validate_subject(detail, requested, "JUnit")?; + let head = detail + .head_commit_sha + .as_deref() + .ok_or(SubjectError::MissingHead { + review: detail.id, + report: "JUnit", + })? + .to_ascii_lowercase(); + let state = outcome::terminal(detail); + let mut suites = Vec::new(); + for assessment in &detail.assessments { + suites.push(environment_suite(detail, assessment, &head, review_url)?); + } + if let Some(code) = outcome::code_assessment(detail) { + suites.push(code_suite(detail, code, &head, review_url)?); + } else if outcome::code_unavailable(detail) { + suites.push(Suite { + name: "Code-only".to_owned(), + properties: review_properties(detail, &head, review_url), + cases: vec![errored( + "Code-only", + "completeness", + "CODE_ASSESSMENT_UNAVAILABLE".to_owned(), + )], + }); + } + if state != Some(Outcome::Completed) { + let reason = match state { + None => "REVIEW_IN_PROGRESS", + Some(Outcome::Failed) => "REVIEW_FAILED", + Some(Outcome::Stopped) => "REVIEW_STOPPED", + Some(Outcome::Incomplete | Outcome::Completed) => "REVIEW_RESULT_INCOMPLETE", + }; + suites.push(Suite { + name: "review".to_owned(), + properties: review_properties(detail, &head, review_url), + cases: vec![errored("review", "completion", reason.to_owned())], + }); + } + Ok(render(&suites)) +} + +fn review_properties( + detail: &ReviewDetail, + head: &str, + review_url: &str, +) -> Vec<(&'static str, String)> { + let mut properties = vec![ + ("review_id", detail.id.to_string()), + ("review_url", review_url.to_owned()), + ("head_commit_sha", head.to_owned()), + ("review_status", detail.status.clone()), + ]; + if let Some(repository) = &detail.repository_full_name { + properties.push(("repository", repository.clone())); + } + if let Some(number) = detail.pull_request_number { + properties.push(("pull_request_number", number.to_string())); + } + if let Some(run) = detail.run_number { + properties.push(("run_number", run.to_string())); + } + properties +} + +fn environment_suite( + detail: &ReviewDetail, + assessment: &Assessment, + head: &str, + review_url: &str, +) -> Result { + // Prefixed so free text cannot take the name of a synthetic suite. + let name = format!("environment: {}", assessment.environment_display_name); + let name = name.as_str(); + let mut properties = review_properties(detail, head, review_url); + properties.push(("lane", "ENV".to_owned())); + properties.push(("assessment_id", assessment.id.to_string())); + let mut cases = Vec::new(); + for check in &assessment.checks.families { + cases.push(check_case(name, check)); + } + let reasons: BTreeSet = outcome::gap(assessment) + .unwrap_or_default() + .into_iter() + .collect(); + cases.push(completeness(name, &reasons)); + for finding in &assessment.findings { + cases.push(finding_case(name, finding, head, true)?); + } + Ok(Suite { + name: name.to_owned(), + properties, + cases, + }) +} + +fn code_suite( + detail: &ReviewDetail, + code: &CodeAssessment, + head: &str, + review_url: &str, +) -> Result { + let name = "Code-only"; + let mut properties = review_properties(detail, head, review_url); + properties.push(("lane", "CODE".to_owned())); + properties.push(("assessment_id", code.id.to_string())); + let reasons: BTreeSet = outcome::code_gap(code) + .map(str::to_owned) + .into_iter() + .collect(); + let mut cases = vec![completeness(name, &reasons)]; + for finding in &code.findings { + // The API projects no decisions onto CODE findings. + cases.push(finding_case(name, finding, head, false)?); + } + Ok(Suite { + name: name.to_owned(), + properties, + cases, + }) +} + +fn completeness(suite: &str, reasons: &BTreeSet) -> Case { + if reasons.is_empty() { + Case { + classname: suite.to_owned(), + name: "completeness".to_owned(), + status: Status::Passed, + output: Vec::new(), + } + } else { + errored( + suite, + "completeness", + reasons.iter().cloned().collect::>().join(", "), + ) + } +} + +fn errored(suite: &str, name: &str, message: String) -> Case { + Case { + classname: suite.to_owned(), + name: name.to_owned(), + status: Status::Errored { + kind: "INCOMPLETE".to_owned(), + message, + }, + output: Vec::new(), + } +} + +/// One environment check family. Only its name and outcome are read; the +/// detail text can quote evidence and stays out of the report. +fn check_case(suite: &str, check: &Check) -> Case { + let status = match check.outcome.as_str() { + "READY" => Status::Passed, + "NOT_APPLICABLE" => Status::Skipped { + message: "NOT_APPLICABLE", + }, + "BLOCKED" => Status::Failed { + kind: "BLOCKED".to_owned(), + message: format!("check {} is BLOCKED", check.name), + body: format!("check: {}\noutcome: BLOCKED", check.name), + }, + other => Status::Errored { + kind: "INCOMPLETE".to_owned(), + message: format!("check {} is {other}", check.name), + }, + }; + Case { + classname: suite.to_owned(), + name: format!("check {}", check.name), + status, + output: Vec::new(), + } +} + +/// One finding. An open finding fails. A resolved finding, and one a person +/// dismissed or accepted as a risk, passes and records why in its output. +fn finding_case( + suite: &str, + finding: &Finding, + head: &str, + decisions: bool, +) -> Result { + if !matches!(finding.severity.as_str(), "SERIOUS" | "WARNING") { + return Err(JunitError::UnsupportedSeverity { + finding: finding.id, + severity: crate::text::one_line(&finding.severity), + }); + } + let state = if finding.lifecycle_state == "RESOLVED" { + Some("RESOLVED") + } else if decisions { + finding + .validation + .as_deref() + .filter(|decision| matches!(*decision, "DISMISSED" | "ACCEPTED_RISK")) + } else { + None + }; + // The retained fingerprint keeps the case name stable across runs; an + // older server omits it and the occurrence ID stands in. + let identity = finding + .finding_fingerprint + .clone() + .unwrap_or_else(|| finding.id.to_string()); + let name = format!("{} {identity}", finding.risk_domain); + if let Some(state) = state { + return Ok(Case { + classname: suite.to_owned(), + name, + status: Status::Passed, + output: vec![ + format!("state: {}", state.to_lowercase().replace('_', "-")), + format!("finding: {}", finding.id), + format!("severity: {}", finding.severity), + ], + }); + } + let mut lines = vec![ + format!("finding: {}", finding.id), + format!("domain: {}", finding.risk_domain), + ]; + for reference in &finding.evidence_refs { + if let Ok((path, start, end)) = sarif::code_location(reference, head) { + lines.push(if start == end { + format!("cited: {path}:{start}") + } else { + format!("cited: {path}:{start}-{end}") + }); + } + } + lines.push(format!("claim: {}", finding.claim)); + let body = lines.join("\n"); + Ok(Case { + classname: suite.to_owned(), + name, + status: Status::Failed { + kind: finding.severity.clone(), + message: title(&finding.claim), + body, + }, + output: Vec::new(), + }) +} + +/// The API gives a finding no title: its claim's first line stands in. +fn title(claim: &str) -> String { + claim + .lines() + .find(|line| !line.trim().is_empty()) + .unwrap_or_default() + .trim() + .chars() + .take(MESSAGE_CHARACTERS) + .collect() +} + +fn render(suites: &[Suite]) -> String { + let total = |count: fn(&Suite) -> usize| suites.iter().map(count).sum::(); + let mut out = String::from("\n\n"); + for suite in suites { + out.push_str(" \n"); + properties(&mut out, " ", &suite.properties); + for case in &suite.cases { + render_case(&mut out, case); + } + out.push_str(" \n"); + } + out.push_str("\n"); + out +} + +fn render_case(out: &mut String, case: &Case) { + out.push_str(" \n"); + return; + } + out.push_str(">\n"); + match &case.status { + Status::Passed => {} + Status::Skipped { message } => { + out.push_str(" \n"); + } + Status::Errored { kind, message } => { + out.push_str(" \n"); + } + Status::Failed { + kind, + message, + body, + } => { + out.push_str(" '); + text(out, body); + out.push_str("\n"); + } + } + if !case.output.is_empty() { + out.push_str(" "); + text(out, &case.output.join("\n")); + out.push_str("\n"); + } + out.push_str(" \n"); +} + +fn properties(out: &mut String, indent: &str, properties: &[(&'static str, String)]) { + if properties.is_empty() { + return; + } + out.push_str(indent); + out.push_str("\n"); + for (name, value) in properties { + out.push_str(indent); + out.push_str(" \n"); + } + out.push_str(indent); + out.push_str("\n"); +} + +fn number(out: &mut String, name: &str, value: usize) { + attribute(out, name, &value.to_string()); +} + +fn attribute(out: &mut String, name: &str, value: &str) { + out.push(' '); + out.push_str(name); + out.push_str("=\""); + escape(out, value, true); + out.push('"'); +} + +fn text(out: &mut String, value: &str) { + escape(out, value, false); +} + +/// Characters XML 1.0 allows. Others (most control characters, lone +/// surrogates cannot occur in a `str`, U+FFFE and U+FFFF) are dropped. +const fn allowed(character: char) -> bool { + matches!( + character, + '\t' | '\n' | '\r' | '\u{20}'..='\u{D7FF}' | '\u{E000}'..='\u{FFFD}' | '\u{10000}'..='\u{10FFFF}' + ) +} + +/// Escapes `value` for an attribute or a text node. `>` is always escaped, so +/// `]]>` cannot occur, and no CDATA section is ever written. In an attribute +/// a tab, newline or carriage return becomes a character reference, which an +/// XML parser would otherwise normalize to a space. +fn escape(out: &mut String, value: &str, attribute: bool) { + for character in value.chars().filter(|&character| allowed(character)) { + match character { + '&' => out.push_str("&"), + '<' => out.push_str("<"), + '>' => out.push_str(">"), + '"' if attribute => out.push_str("""), + '\'' if attribute => out.push_str("'"), + '\r' => out.push_str(" "), + '\t' if attribute => out.push_str(" "), + '\n' if attribute => out.push_str(" "), + _ => out.push(character), + } + } +} diff --git a/src/lib.rs b/src/lib.rs index 6096711..688a50c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,6 +10,7 @@ pub mod device; pub mod git; mod github_actions; mod hooks; +mod junit; mod mcp; pub mod model; pub mod origin; @@ -169,6 +170,11 @@ struct CiReviewArgs { /// Also write the review as SARIF 2.1.0 to this file. #[arg(long, value_name = "FILE")] sarif: Option, + /// Also write the review as `JUnit` XML to this file, for CI systems that + /// render a test report. Open findings are failures and an incomplete + /// assessment is an error; the file does not change the exit code. + #[arg(long, value_name = "FILE")] + junit: Option, /// Print JSON instead of text. #[arg(long)] json: bool, @@ -976,19 +982,30 @@ fn poll_deadline(minutes: u64) -> Result, - sarif: bool, + subject: ReportKind, ) -> anyhow::Result<(serde_json::Value, ReviewDetail, Option)> { let mut delay = FIRST_POLL_SECONDS; loop { let (raw, detail) = api.review(review_id).await?; - if sarif { - sarif::validate_subject(&detail, review_id)?; + match subject { + ReportKind::Unchecked => {} + ReportKind::Sarif => sarif::validate_subject(&detail, review_id, "SARIF")?, + ReportKind::Junit => sarif::validate_subject(&detail, review_id, "JUnit")?, } let state = outcome::terminal(&detail); let Some(deadline) = deadline else { @@ -1021,8 +1038,12 @@ async fn show( } else { None }; - let sarif = matches!(format, OutputFormat::Sarif); - let (raw, detail, state) = read_review(api, review_id, deadline, sarif).await?; + let subject = if matches!(format, OutputFormat::Sarif) { + ReportKind::Sarif + } else { + ReportKind::Unchecked + }; + let (raw, detail, state) = read_review(api, review_id, deadline, subject).await?; print_review(api, review_id, raw, &detail, state, format)?; match state { Some(Outcome::Completed) => return Ok(Verdict::Done), @@ -1163,19 +1184,45 @@ async fn ci_review( delay = (delay * 2).min(LONGEST_POLL_SECONDS); }; let sarif_path = args.sarif.as_deref(); - let (raw, detail, state) = - read_review(api, review_id, Some(deadline), sarif_path.is_some()).await?; - if let Some(path) = sarif_path { - let document = sarif::review( - &detail, - review_id, - api.organization_id(), - api.review_url(review_id).as_str(), - )?; + let subject = if sarif_path.is_some() { + ReportKind::Sarif + } else if args.junit.is_some() { + ReportKind::Junit + } else { + ReportKind::Unchecked + }; + let (raw, detail, state) = read_review(api, review_id, Some(deadline), subject).await?; + // Both reports are built before either is written, so a refused review + // leaves no file behind. + let sarif_report = sarif_path + .map(|path| { + sarif::review( + &detail, + review_id, + api.organization_id(), + api.review_url(review_id).as_str(), + ) + .map(|document| (path, document)) + }) + .transpose()?; + let junit_report = args + .junit + .as_deref() + .map(|path| { + junit::review(&detail, review_id, api.review_url(review_id).as_str()) + .map(|document| (path, document)) + }) + .transpose()?; + if let Some((path, document)) = sarif_report { tokio::fs::write(path, serde_json::to_vec_pretty(&document)?) .await .with_context(|| format!("could not write SARIF to {}", path.display()))?; } + if let Some((path, document)) = junit_report { + tokio::fs::write(path, document) + .await + .with_context(|| format!("could not write JUnit XML to {}", path.display()))?; + } print_review( api, review_id, @@ -1451,6 +1498,7 @@ mod tests { let args = CiReviewArgs { commit: None, sarif: None, + junit: None, json: false, timeout: 3, fail_on: None, diff --git a/src/model.rs b/src/model.rs index 2420134..3e95daa 100644 --- a/src/model.rs +++ b/src/model.rs @@ -224,6 +224,9 @@ pub struct Finding { pub claim: String, pub impact: String, pub lifecycle_state: String, + /// The latest decision on the finding. Null when no decision is recorded. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub validation: Option, pub remediation: Option, pub evidence_refs: Vec, } diff --git a/src/render.rs b/src/render.rs index 4c4578b..bd2000a 100644 --- a/src/render.rs +++ b/src/render.rs @@ -227,6 +227,7 @@ mod tests { claim: claim.to_owned(), impact: "orders are lost".to_owned(), lifecycle_state: "ACTIVE".to_owned(), + validation: None, remediation: None, evidence_refs: vec![EvidenceRef { kind: "CODE".to_owned(), diff --git a/src/sarif.rs b/src/sarif.rs index b3c39ee..c9f0f05 100644 --- a/src/sarif.rs +++ b/src/sarif.rs @@ -49,20 +49,35 @@ const CODE_DOMAINS: [&str; 11] = [ "committed_secret", ]; +/// A review that cannot be exported as a report, named by `report`. #[derive(Debug, thiserror::Error)] -pub(super) enum SarifError { +pub(super) enum SubjectError { #[error( - "requested review {requested}, but the server returned review {returned}; SARIF export refused" + "requested review {requested}, but the server returned review {returned}; {report} export refused" )] - WrongReview { requested: Uuid, returned: Uuid }, + WrongReview { + requested: Uuid, + returned: Uuid, + report: &'static str, + }, #[error( - "review {review} has kind {kind}; SARIF export requires a retained pull-request review" + "review {review} has kind {kind}; {report} export requires a retained pull-request review" )] - UnsupportedSubject { review: Uuid, kind: String }, + UnsupportedSubject { + review: Uuid, + kind: String, + report: &'static str, + }, #[error( - "review {0} does not record a full commit SHA; SARIF export requires an immutable pull-request head" + "review {review} does not record a full commit SHA; {report} export requires an immutable pull-request head" )] - MissingHead(Uuid), + MissingHead { review: Uuid, report: &'static str }, +} + +#[derive(Debug, thiserror::Error)] +pub(super) enum SarifError { + #[error(transparent)] + Subject(#[from] SubjectError), #[error("finding {finding} has unsupported severity {severity}; SARIF export refused")] UnsupportedSeverity { finding: Uuid, severity: String }, #[error("finding {finding} has unsupported risk domain {domain}; SARIF export refused")] @@ -211,7 +226,7 @@ struct EvidenceProperties<'a> { #[derive(Debug, Clone, Copy, Serialize)] #[serde(rename_all = "SCREAMING_SNAKE_CASE")] -enum LocationStatus { +pub(super) enum LocationStatus { Located, NoVerifiedReviewedLocation, NonCodeReference, @@ -294,23 +309,34 @@ enum AssessmentSource<'a> { Code(&'a CodeAssessment), } -pub(super) fn validate_subject(detail: &ReviewDetail, requested: Uuid) -> Result<(), SarifError> { +/// Checks the review is the requested retained pull-request head. `report` +/// names the format in the refusal. +pub(super) fn validate_subject( + detail: &ReviewDetail, + requested: Uuid, + report: &'static str, +) -> Result<(), SubjectError> { if detail.id != requested { - return Err(SarifError::WrongReview { + return Err(SubjectError::WrongReview { requested, returned: detail.id, + report, }); } if detail.kind != "PULL_REQUEST" { - return Err(SarifError::UnsupportedSubject { + return Err(SubjectError::UnsupportedSubject { review: detail.id, kind: crate::text::one_line(&detail.kind), + report, }); } if !detail.head_commit_sha.as_deref().is_some_and(|head| { matches!(head.len(), 40 | 64) && head.bytes().all(|byte| byte.is_ascii_hexdigit()) }) { - return Err(SarifError::MissingHead(detail.id)); + return Err(SubjectError::MissingHead { + review: detail.id, + report, + }); } Ok(()) } @@ -321,11 +347,14 @@ pub(super) fn review<'a>( organization: Uuid, review_url: &str, ) -> Result, SarifError> { - validate_subject(detail, requested)?; + validate_subject(detail, requested, "SARIF")?; let head = detail .head_commit_sha .as_deref() - .ok_or(SarifError::MissingHead(detail.id))? + .ok_or(SubjectError::MissingHead { + review: detail.id, + report: "SARIF", + })? .to_ascii_lowercase(); let state = outcome::terminal(detail); let mut results = Vec::new(); @@ -579,7 +608,7 @@ fn finding_result<'a>( }) } -fn code_location<'a>( +pub(super) fn code_location<'a>( reference: &'a EvidenceRef, head: &str, ) -> Result<(&'a str, u32, u32), LocationStatus> { diff --git a/tests/junit.rs b/tests/junit.rs new file mode 100644 index 0000000..3856d18 --- /dev/null +++ b/tests/junit.rs @@ -0,0 +1,877 @@ +//! Actual CLI processes read a retained review over HTTP, write `--junit`, and +//! the file is parsed by a strict XML reader and held to the `JUnit` report +//! structure CI systems read: `testsuites`, `testsuite`, `testcase`, and a +//! `failure` or `error` per case at most. That reader is the sole check. It +//! mirrors the content model of the Jenkins `junit-10.xsd`: a `testcase` holds +//! only `skipped`, `error`, `failure` and `system-out`, never `properties`. + +use std::path::Path; +use std::process::Output; +use std::sync::{Arc, Mutex}; +use std::time::Duration; + +use axum::extract::State; +use axum::http::{HeaderMap, StatusCode, header}; +use axum::response::{IntoResponse, Response}; +use axum::routing::get; +use axum::{Json, Router}; +use serde_json::{Value, json}; +use tokio::process::Command; +use uuid::Uuid; + +const ORG: &str = "0190f3a0-0000-7000-8000-000000000001"; +const REPOSITORY: &str = "0190f3a0-0000-7000-8000-0000000000f0"; +const REVIEW: &str = "0190f3a0-0000-7000-8000-000000000033"; +const OTHER_REVIEW: &str = "0190f3a0-0000-7000-8000-000000000034"; +const ASSESSMENT: &str = "0190f3a0-0000-7000-8000-000000000044"; +const CODE_ASSESSMENT: &str = "0190f3a0-0000-7000-8000-000000000055"; +const HEAD: &str = "0123456789abcdef0123456789abcdef01234567"; +const BASE: &str = "89abcdef0123456789abcdef0123456789abcdef"; +const TOKEN: &str = "pld_junitsynthetictoken"; +const PRIVATE_VALUE: &str = "raw-world-evidence-and-operator-diagnostics"; +// The hostile text of tests/sarif.rs, plus what only XML cares about: markup, +// quotes, a CDATA terminator, a carriage return and characters XML forbids. +const CLAIM: &str = "A retry can replay {0} with {{receipt}} from [label](0), \ + array[nested[0]] and C:\\orders\\receipt. 'q' & ]]> \r\nsecond line \u{1}\u{b}\u{fffe}end"; +const CLEAN_CLAIM: &str = "A retry can replay {0} with {{receipt}} from [label](0), \ + array[nested[0]] and C:\\orders\\receipt. 'q' & ]]> \r\nsecond line end"; +const ENVIRONMENT: &str = "production {0} [overview](0)\\primary \"quoted\" &\u{2}"; +const CLEAN_ENVIRONMENT: &str = + "environment: production {0} [overview](0)\\primary \"quoted\" &"; +const FINGERPRINT: &str = "5f3c9a7e1b2d4c6a8e0f9b1d3a5c7e9f2b4d6a8c0e1f3a5b7d9c2e4f6a8b0c1d"; + +#[derive(Clone)] +struct Console { + document: Value, + requests: Arc>>, +} + +async fn detail(State(console): State, headers: HeaderMap) -> Response { + console.requests.lock().unwrap().push("detail".to_owned()); + if headers + .get(header::AUTHORIZATION) + .and_then(|value| value.to_str().ok()) + != Some(format!("Bearer {TOKEN}").as_str()) + { + return StatusCode::UNAUTHORIZED.into_response(); + } + Json(console.document).into_response() +} + +async fn serve(document: Value) -> (String, Arc>>) { + let console = Console { + document, + requests: Arc::default(), + }; + let requests = console.requests.clone(); + let app = Router::new() + .route( + &format!("/api/v1/organizations/{ORG}/repository-names"), + get(|| async { + Json(json!({ "repositories": [{ "id": REPOSITORY, "full_name": "acme/web" }] })) + }), + ) + .route( + &format!("/api/v1/organizations/{ORG}/reviews"), + get(|| async { + Json(json!({ + "items": [{ + "id": REVIEW, + "kind": "PULL_REQUEST", + "status": "COMPLETED", + "created_at": "2026-10-01T00:00:00Z", + "head_commit_sha": HEAD, + "pull_request_number": 42, + "repository_id": REPOSITORY, + "run_number": 7, + }], + "next_cursor": null, + "truncated": false, + "filters": { "repositories": [] }, + })) + }), + ) + .route( + &format!("/api/v1/organizations/{ORG}/reviews/{REVIEW}"), + get(detail), + ) + .with_state(console); + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let address = listener.local_addr().unwrap(); + tokio::spawn(async move { axum::serve(listener, app).await.unwrap() }); + (format!("http://{address}"), requests) +} + +/// `proofline ci review --junit FILE`, with any extra arguments. +async fn ci(origin: &str, report: &Path, extra: &[&str]) -> Output { + ci_waiting(origin, report, "0", extra).await +} + +/// Like `ci`, with a poll timeout in minutes. +async fn ci_waiting(origin: &str, report: &Path, minutes: &str, extra: &[&str]) -> Output { + tokio::time::timeout( + Duration::from_secs(15), + Command::new(env!("CARGO_BIN_EXE_proofline")) + .args(["--base-url", origin, "--org", ORG, "ci", "review"]) + .args(["--repo", "acme/web", "--timeout", minutes, "--commit", HEAD]) + .arg("--junit") + .arg(report) + .args(extra) + .env("PROOFLINE_TOKEN", TOKEN) + .env_remove("GITHUB_SHA") + .env("PATH", "") + .current_dir(std::env::temp_dir()) + .kill_on_drop(true) + .output(), + ) + .await + .unwrap() + .unwrap() +} + +/// Runs the CLI against `document`, returning its output and the report. +async fn export(document: Value, extra: &[&str]) -> (Output, Option) { + let (origin, _) = serve(document).await; + let directory = tempfile::tempdir().unwrap(); + let path = directory.path().join("report.xml"); + let output = ci(&origin, &path, extra).await; + let report = std::fs::read_to_string(&path).ok(); + if let Some(report) = &report { + assert!(!report.contains(TOKEN)); + assert!(!report.contains(PRIVATE_VALUE), "{report}"); + } + (output, report) +} + +fn code(path: &str, start: u32, end: Option) -> Value { + json!({ + "kind": "repository_file", + "label": "verified file read", + "provenance_status": "VERIFIED_REPOSITORY_READ", + "commit_sha": HEAD, + "repository_path": path, + "start_line": start, + "end_line": end, + }) +} + +fn finding(number: u128, severity: &str, refs: Vec) -> Value { + json!({ + "id": Uuid::from_u128(0x0190_f3a0_0000_7000_8000_0000_0000_0100 + number), + "severity": severity, + "risk_domain": "data_integrity", + "claim": CLAIM, + "impact": "The customer can be charged twice.", + "lifecycle_state": "NEW", + "remediation": "Make the order receipt idempotent.", + "evidence_refs": refs, + "evidence_values": PRIVATE_VALUE, + "operator_diagnostics": PRIVATE_VALUE, + }) +} + +fn decided(number: u128, decision: &str) -> Value { + let mut recorded = finding(number, "SERIOUS", Vec::new()); + recorded["validation"] = json!(decision); + recorded +} + +fn review(findings: Vec) -> Value { + json!({ + "id": REVIEW, + "kind": "PULL_REQUEST", + "status": "COMPLETED", + "head_commit_sha": HEAD, + "repository_full_name": "acme/web", + "pull_request_number": 42, + "run_number": 7, + "assessment_count": 1, + "open_items": { "open_findings": 1, "serious_findings": 1 }, + "assessments": [{ + "id": ASSESSMENT, + "environment_display_name": ENVIRONMENT, + "status": "COMPLETED", + "findings": findings, + "checks": { + "receipt_recorded": true, + "status": "READY", + "families": [ + { "name": "deployment", "outcome": "READY", "lifecycle": "GATING" }, + { "name": "capacity", "outcome": "NOT_APPLICABLE", "lifecycle": "GATING" }, + { + "name": "iam", + "outcome": "BLOCKED", + "lifecycle": "GATING", + "detail": PRIVATE_VALUE, + }, + ], + }, + "reviewer_version": PRIVATE_VALUE, + "world_evidence_values": PRIVATE_VALUE, + }], + "operator_diagnostics": PRIVATE_VALUE, + }) +} + +fn code_review(findings: Vec) -> Value { + let mut document = review(Vec::new()); + document["base_commit_sha"] = json!(BASE); + document["assessment_count"] = json!(0); + document["assessments"] = json!([]); + document["code_assessment"] = json!({ + "id": CODE_ASSESSMENT, + "status": "COMPLETED", + "base_commit_sha": BASE, + "head_commit_sha": HEAD, + "incompleteness_kind": null, + "findings": findings, + }); + document +} + +// A strict XML 1.0 reader for the subset a report uses. It rejects what a +// real parser would normalize or refuse: raw control characters, a raw `<` or +// line break in an attribute, `]]>` in text, CDATA, comments, unknown +// entities and mismatched tags. + +#[derive(Debug)] +struct Node { + name: String, + attributes: Vec<(String, String)>, + children: Vec, + text: String, +} + +impl Node { + fn attribute(&self, name: &str) -> Option<&str> { + self.attributes + .iter() + .find(|(key, _)| key == name) + .map(|(_, value)| value.as_str()) + } + + fn all<'a>(&'a self, name: &'a str) -> impl Iterator { + self.children.iter().filter(move |child| child.name == name) + } + + fn one<'a>(&'a self, name: &'a str) -> &'a Self { + let mut found = self.all(name); + let node = found.next().unwrap_or_else(|| panic!("no <{name}>")); + assert!(found.next().is_none(), "two <{name}>"); + node + } +} + +struct Reader { + characters: Vec, + at: usize, +} + +fn xml_char(character: char) -> bool { + matches!( + character, + '\t' | '\n' | '\r' | '\u{20}'..='\u{D7FF}' | '\u{E000}'..='\u{FFFD}' | '\u{10000}'..='\u{10FFFF}' + ) +} + +impl Reader { + fn peek(&self) -> Option { + self.characters.get(self.at).copied() + } + + fn starts_with(&self, prefix: &str) -> bool { + prefix + .chars() + .enumerate() + .all(|(offset, ch)| self.characters.get(self.at + offset) == Some(&ch)) + } + + fn expect(&mut self, prefix: &str) { + assert!( + self.starts_with(prefix), + "expected {prefix:?} at {}", + self.at + ); + self.at += prefix.chars().count(); + } + + fn name(&mut self) -> String { + let start = self.at; + while self + .peek() + .is_some_and(|ch| ch.is_ascii_alphanumeric() || matches!(ch, '-' | '_' | ':' | '.')) + { + self.at += 1; + } + assert!(self.at > start, "expected a name at {start}"); + self.characters[start..self.at].iter().collect() + } + + fn reference(&mut self) -> char { + self.expect("&"); + let start = self.at; + while self.peek().is_some_and(|ch| ch != ';') { + self.at += 1; + } + let entity: String = self.characters[start..self.at].iter().collect(); + self.expect(";"); + let character = match entity.as_str() { + "amp" => '&', + "lt" => '<', + "gt" => '>', + "quot" => '"', + "apos" => '\'', + other => { + let code = other + .strip_prefix("#x") + .map(|hex| u32::from_str_radix(hex, 16).unwrap()) + .or_else(|| { + other + .strip_prefix('#') + .map(|digits| digits.parse().unwrap()) + }) + .unwrap_or_else(|| panic!("unknown entity &{other};")); + char::from_u32(code).unwrap() + } + }; + assert!(xml_char(character), "reference to a forbidden character"); + character + } + + fn value(&mut self) -> String { + self.expect("\""); + let mut value = String::new(); + loop { + let ch = self.peek().expect("unterminated attribute"); + assert!(xml_char(ch), "forbidden character {ch:?} in an attribute"); + match ch { + '"' => break, + '<' | '\t' | '\n' | '\r' => panic!("raw {ch:?} in an attribute"), + '&' => value.push(self.reference()), + _ => { + value.push(ch); + self.at += 1; + } + } + } + self.expect("\""); + value + } + + fn element(&mut self) -> Node { + self.expect("<"); + let name = self.name(); + let mut attributes: Vec<(String, String)> = Vec::new(); + while self.peek() == Some(' ') { + self.at += 1; + let key = self.name(); + self.expect("="); + assert!( + attributes.iter().all(|(seen, _)| *seen != key), + "duplicate attribute {key}" + ); + attributes.push((key, self.value())); + } + let mut node = Node { + name, + attributes, + children: Vec::new(), + text: String::new(), + }; + if self.starts_with("/>") { + self.expect("/>"); + return node; + } + self.expect(">"); + loop { + assert!( + !self.starts_with(""), "]]> in text"); + match self.peek().expect("unterminated element") { + '<' if self.starts_with(" break, + '<' => node.children.push(self.element()), + '&' => node.text.push(self.reference()), + ch => { + assert!( + xml_char(ch) && ch != '\r', + "forbidden character {ch:?} in text" + ); + node.text.push(ch); + self.at += 1; + } + } + } + self.expect(""); + node + } +} + +fn parse(xml: &str) -> Node { + let mut reader = Reader { + characters: xml.chars().collect(), + at: 0, + }; + reader.expect("\n"); + let root = reader.element(); + while reader.peek().is_some_and(char::is_whitespace) { + reader.at += 1; + } + assert_eq!(reader.at, reader.characters.len(), "content after the root"); + root +} + +fn only_attributes(node: &Node, allowed: &[&str], required: &[&str]) { + for (key, _) in &node.attributes { + assert!(allowed.contains(&key.as_str()), "<{}> has {key}", node.name); + } + for key in required { + assert!(node.attribute(key).is_some(), "<{}> lacks {key}", node.name); + } +} + +fn count(node: &Node, key: &str) -> usize { + node.attribute(key).unwrap().parse().unwrap() +} + +/// The `JUnit` report structure: the elements and attributes the Jenkins `JUnit` +/// schema allows, and counts that match the cases beneath them. +fn validate(root: &Node) { + assert_eq!(root.name, "testsuites"); + only_attributes( + root, + &["name", "time", "tests", "failures", "disabled", "errors"], + &[], + ); + assert_eq!(root.attribute("name"), Some("proofline")); + assert!(root.text.trim().is_empty()); + let (mut tests, mut failures, mut errors) = (0, 0, 0); + for suite in &root.children { + assert_eq!(suite.name, "testsuite"); + only_attributes( + suite, + &[ + "name", + "tests", + "failures", + "errors", + "time", + "disabled", + "skipped", + "timestamp", + "hostname", + "id", + "package", + ], + &["name", "tests"], + ); + let cases: Vec<&Node> = suite.all("testcase").collect(); + assert!(suite.text.trim().is_empty()); + for child in &suite.children { + assert!(matches!(child.name.as_str(), "properties" | "testcase")); + } + assert_eq!(count(suite, "tests"), cases.len()); + let ended = |name: &str| { + cases + .iter() + .filter(|case| case.all(name).count() == 1) + .count() + }; + assert_eq!(count(suite, "failures"), ended("failure")); + assert_eq!(count(suite, "errors"), ended("error")); + assert_eq!(count(suite, "skipped"), ended("skipped")); + for properties in suite.all("properties") { + for property in &properties.children { + assert_eq!(property.name, "property"); + only_attributes(property, &["name", "value"], &["name", "value"]); + } + } + for case in cases { + only_attributes(case, &["name", "classname"], &["name"]); + let outcomes = ["skipped", "error", "failure"] + .iter() + .map(|name| case.all(name).count()) + .sum::(); + assert!(outcomes <= 1, "{case:?}"); + for child in &case.children { + match child.name.as_str() { + "failure" | "error" => only_attributes(child, &["message", "type"], &[]), + "skipped" => only_attributes(child, &["message"], &[]), + "system-out" => only_attributes(child, &[], &[]), + other => panic!(" has <{other}>"), + } + } + } + tests += count(suite, "tests"); + failures += count(suite, "failures"); + errors += count(suite, "errors"); + } + assert_eq!(count(root, "tests"), tests); + assert_eq!(count(root, "failures"), failures); + assert_eq!(count(root, "errors"), errors); +} + +fn report(xml: &str) -> Node { + let root = parse(xml); + validate(&root); + root +} + +fn suite<'a>(root: &'a Node, name: &str) -> &'a Node { + root.children + .iter() + .find(|suite| suite.attribute("name") == Some(name)) + .unwrap_or_else(|| panic!("no suite {name}")) +} + +fn case<'a>(suite: &'a Node, prefix: &str) -> &'a Node { + let mut found = suite + .all("testcase") + .filter(|case| case.attribute("name").unwrap().starts_with(prefix)); + let case = found.next().unwrap_or_else(|| panic!("no case {prefix}")); + assert!(found.next().is_none(), "two cases {prefix}"); + case +} + +fn property<'a>(node: &'a Node, name: &str) -> Option<&'a str> { + node.all("properties") + .flat_map(|properties| properties.children.iter()) + .find(|property| property.attribute("name") == Some(name)) + .and_then(|property| property.attribute("value")) +} + +#[tokio::test] +async fn an_open_finding_is_a_failure_with_its_location_and_claim() { + let mut open = finding( + 1, + "SERIOUS", + vec![ + code("src/orders.rs", 10, Some(14)), + code("src/receipt.rs", 3, None), + // Not a verified read of this head: cited by no line. + json!({ "kind": "companion_repository_file", "label": "other", "companion_repository": "acme/other" }), + ], + ); + open["finding_fingerprint"] = json!(FINGERPRINT); + let (output, xml) = export(review(vec![open]), &[]).await; + // The report does not change the exit code: no --fail-on, so findings pass. + assert_eq!(output.status.code(), Some(0), "{output:?}"); + let xml = xml.unwrap(); + let root = report(&xml); + let environment = suite(&root, CLEAN_ENVIRONMENT); + assert_eq!(property(environment, "review_id"), Some(REVIEW)); + assert_eq!(property(environment, "head_commit_sha"), Some(HEAD)); + assert_eq!(property(environment, "lane"), Some("ENV")); + assert_eq!(property(environment, "assessment_id"), Some(ASSESSMENT)); + let failing = case(environment, &format!("data_integrity {FINGERPRINT}")); + let failure = failing.one("failure"); + assert_eq!(failure.attribute("type"), Some("SERIOUS")); + // The title is the claim's first line, cut at the first line break. + let title = CLEAN_CLAIM.lines().next().unwrap().trim(); + assert_eq!(failure.attribute("message"), Some(title)); + let id = Uuid::from_u128(0x0190_f3a0_0000_7000_8000_0000_0000_0101); + let body = &failure.text; + for expected in [ + format!("finding: {id}"), + "domain: data_integrity".to_owned(), + "cited: src/orders.rs:10-14".to_owned(), + "cited: src/receipt.rs:3".to_owned(), + format!("claim: {CLEAN_CLAIM}"), + ] { + assert!(body.contains(&expected), "{expected:?} not in {body:?}"); + } + assert!(!body.contains("acme/other")); + // Checks: ready passes, not applicable is skipped, blocked fails. + assert!(case(environment, "check deployment").children.is_empty()); + assert_eq!( + case(environment, "check capacity") + .one("skipped") + .attribute("message"), + Some("NOT_APPLICABLE") + ); + assert_eq!( + case(environment, "check iam") + .one("failure") + .attribute("type"), + Some("BLOCKED") + ); + assert!(case(environment, "completeness").children.is_empty()); + assert_eq!(count(&root, "failures"), 2); + assert_eq!(count(&root, "errors"), 0); + // Without a fingerprint the occurrence ID names the case. + let (_, xml) = export(review(vec![finding(1, "WARNING", Vec::new())]), &[]).await; + let root = report(&xml.unwrap()); + let warned = case( + suite(&root, CLEAN_ENVIRONMENT), + &format!("data_integrity {id}"), + ); + assert_eq!(warned.one("failure").attribute("type"), Some("WARNING")); +} + +#[tokio::test] +async fn hostile_text_is_escaped_in_every_attribute_and_text_node() { + let (_, xml) = export(review(vec![finding(1, "SERIOUS", Vec::new())]), &[]).await; + let xml = xml.unwrap(); + // The strict reader above already refuses raw markup, control characters + // and a bare `]]>`; check the bytes too. + assert!(!xml.contains("")); + assert!(!xml.contains('\r')); + assert!(xml.chars().all(|ch| ch == '\n' || ch >= ' ')); + assert!(xml.contains("</failure><x a=\"1\"/>")); + assert!(xml.contains(""quoted" <b>&amp;</b>")); + let root = report(&xml); + let environment = suite(&root, CLEAN_ENVIRONMENT); + // The reader decodes it back to the original text, minus forbidden characters. + assert!( + case(environment, "data_integrity") + .one("failure") + .text + .ends_with(CLEAN_CLAIM) + ); +} + +#[tokio::test] +async fn dismissed_accepted_and_resolved_findings_do_not_fail() { + let mut resolved = finding(3, "SERIOUS", Vec::new()); + resolved["lifecycle_state"] = json!("RESOLVED"); + let mut document = review(vec![ + decided(1, "DISMISSED"), + decided(2, "ACCEPTED_RISK"), + resolved, + decided(4, "CONFIRMED"), + ]); + document["open_items"] = json!({ "open_findings": 1, "serious_findings": 1 }); + let (output, xml) = export(document, &["--fail-on", "warning"]).await; + // The server counts one open finding, so --fail-on still decides the exit. + assert_eq!(output.status.code(), Some(3), "{output:?}"); + let root = report(&xml.unwrap()); + let environment = suite(&root, CLEAN_ENVIRONMENT); + for (number, state) in [(1, "dismissed"), (2, "accepted-risk"), (3, "resolved")] { + let id = Uuid::from_u128(0x0190_f3a0_0000_7000_8000_0000_0000_0100 + number); + let settled = case(environment, &format!("data_integrity {id}")); + assert_eq!(settled.all("properties").count(), 0); + let output = &settled.one("system-out").text; + assert!(output.contains(&format!("state: {state}\n")), "{output}"); + assert!(output.contains(&format!("finding: {id}\n")), "{output}"); + assert_eq!(settled.all("failure").count(), 0); + } + // A confirmed finding is still open. + let id = Uuid::from_u128(0x0190_f3a0_0000_7000_8000_0000_0000_0104); + assert_eq!( + case(environment, &format!("data_integrity {id}")) + .all("failure") + .count(), + 1 + ); + // Three settled findings and the blocked check: two failures in all. + assert_eq!(count(&root, "failures"), 2); +} + +#[tokio::test] +async fn the_report_is_independent_of_fail_on_and_the_exit_code() { + let (origin, _) = serve(review(vec![finding(1, "SERIOUS", Vec::new())])).await; + let directory = tempfile::tempdir().unwrap(); + let mut reports = Vec::new(); + for (extra, exit) in [ + (vec![], 0), + (vec!["--fail-on", "serious"], 3), + (vec!["--fail-on", "warning", "--json"], 3), + ] { + let path = directory + .path() + .join(format!("report-{}.xml", reports.len())); + let output = ci(&origin, &path, &extra).await; + assert_eq!(output.status.code(), Some(exit), "{extra:?}: {output:?}"); + reports.push(std::fs::read_to_string(&path).unwrap()); + } + // No clock and no ordering luck: every run writes the same bytes. + assert_eq!(reports[0], reports[1]); + assert_eq!(reports[1], reports[2]); +} + +#[tokio::test] +async fn code_findings_are_a_lane_of_their_own() { + let mut recorded = finding(1, "SERIOUS", vec![code("src/orders.rs", 10, Some(14))]); + recorded["risk_domain"] = json!("logic"); + // CODE findings carry no projected decisions. + let mut settled = finding(2, "WARNING", Vec::new()); + settled["risk_domain"] = json!("edge_case"); + settled["validation"] = json!("DISMISSED"); + let (output, xml) = export(code_review(vec![recorded, settled]), &[]).await; + assert_eq!(output.status.code(), Some(0), "{output:?}"); + let root = report(&xml.unwrap()); + assert_eq!(root.children.len(), 1); + let lane = suite(&root, "Code-only"); + assert_eq!(property(lane, "lane"), Some("CODE")); + assert_eq!(property(lane, "assessment_id"), Some(CODE_ASSESSMENT)); + assert_eq!( + case(lane, "logic").one("failure").attribute("type"), + Some("SERIOUS") + ); + assert!( + case(lane, "logic") + .one("failure") + .text + .contains("cited: src/orders.rs:10-14") + ); + assert_eq!( + case(lane, "edge_case").one("failure").attribute("type"), + Some("WARNING") + ); + assert!(case(lane, "completeness").children.is_empty()); +} + +#[tokio::test] +async fn an_incomplete_lane_is_an_error_and_exits_2() { + let mut environment = review(vec![finding(1, "WARNING", Vec::new())]); + environment["assessments"][0]["reason_codes"] = + json!(["WORLD_UNAVAILABLE", "WORLD_UNAVAILABLE", "CHECK_{0}_"]); + environment["assessments"][0]["checks"]["receipt_recorded"] = json!(false); + let mut incomplete_code = code_review(Vec::new()); + incomplete_code["code_assessment"]["status"] = json!("INCOMPLETE"); + let mut gap = code_review(Vec::new()); + gap["code_assessment"]["incompleteness_kind"] = json!("EVIDENCE_UNAVAILABLE"); + let mut unavailable = code_review(Vec::new()); + unavailable["code_assessment"]["head_commit_sha"] = json!(BASE); + let mut stopped = review(Vec::new()); + stopped["status"] = json!("STOPPED"); + let mut running = review(Vec::new()); + running["status"] = json!("RUNNING"); + for (label, document, lane, message) in [ + ( + "environment", + environment, + CLEAN_ENVIRONMENT, + "CHECK_RECEIPT_ABSENT, CHECK_{0}_, WORLD_UNAVAILABLE", + ), + ( + "code status", + incomplete_code, + "Code-only", + "CODE_ASSESSMENT_STATE_UNAVAILABLE", + ), + ("code gap", gap, "Code-only", "EVIDENCE_UNAVAILABLE"), + ( + "code unavailable", + unavailable, + "Code-only", + "CODE_ASSESSMENT_UNAVAILABLE", + ), + ("stopped", stopped, "review", "REVIEW_STOPPED"), + ("running", running, "review", "REVIEW_IN_PROGRESS"), + ] { + let (output, xml) = export(document, &[]).await; + assert_ne!(output.status.code(), Some(0), "{label}: {output:?}"); + let root = report(&xml.unwrap_or_else(|| panic!("{label}: no report"))); + let lane = suite(&root, lane); + let ended: Vec<&Node> = lane + .all("testcase") + .filter(|case| case.all("error").count() == 1) + .collect(); + assert!( + ended.iter().any(|case| { + let error = case.one("error"); + error.attribute("type") == Some("INCOMPLETE") + && error.attribute("message") == Some(message) + }), + "{label}: {ended:?}" + ); + assert!(count(&root, "errors") >= 1, "{label}"); + } + // An incomplete environment still reports its findings and exits 2. + let mut environment = review(vec![finding(1, "WARNING", Vec::new())]); + environment["assessments"][0]["checks"]["receipt_recorded"] = json!(false); + let (output, xml) = export(environment, &[]).await; + assert_eq!(output.status.code(), Some(2), "{output:?}"); + let root = report(&xml.unwrap()); + assert_eq!(count(&root, "failures"), 2); +} + +#[tokio::test] +async fn a_review_that_is_not_the_requested_pull_request_head_is_refused() { + let mut wrong_review = review(Vec::new()); + wrong_review["id"] = json!(OTHER_REVIEW); + let mut deployment = review(Vec::new()); + deployment["kind"] = json!("DEPLOYMENT_GATE"); + let mut records = vec![ + (wrong_review, "server returned review"), + (deployment, "requires a retained pull-request review"), + ]; + for head in [Value::Null, json!("main")] { + let mut document = review(Vec::new()); + document["head_commit_sha"] = head; + records.push((document, "does not record a full commit SHA")); + } + let severity = review(vec![finding(1, "CRITICAL", Vec::new())]); + records.push((severity, "unsupported severity")); + for (document, expected) in records { + let (output, xml) = export(document, &[]).await; + assert_eq!(output.status.code(), Some(1), "{expected}: {output:?}"); + assert!(xml.is_none(), "{expected}: a refused review wrote a report"); + assert!(output.stdout.is_empty(), "{expected}: {output:?}"); + let stderr = String::from_utf8(output.stderr).unwrap(); + assert!(stderr.contains(expected), "{expected}: {stderr}"); + assert!(stderr.contains("JUnit export"), "{stderr}"); + assert!(!stderr.contains("SARIF"), "{stderr}"); + assert!(!stderr.contains(PRIVATE_VALUE)); + } +} + +#[tokio::test] +async fn a_wrong_review_is_refused_while_polling_without_waiting_for_the_deadline() { + let mut running = review(Vec::new()); + running["id"] = json!(OTHER_REVIEW); + running["status"] = json!("RUNNING"); + let (origin, _) = serve(running).await; + let directory = tempfile::tempdir().unwrap(); + let path = directory.path().join("report.xml"); + let started = std::time::Instant::now(); + let output = ci_waiting(&origin, &path, "5", &[]).await; + assert!(started.elapsed() < Duration::from_secs(10)); + assert_eq!(output.status.code(), Some(1), "{output:?}"); + assert!(!path.exists()); + let stderr = String::from_utf8(output.stderr).unwrap(); + assert!(stderr.contains("server returned review"), "{stderr}"); +} + +#[tokio::test] +async fn an_unwritable_path_fails_without_hiding_the_review() { + let (origin, requests) = serve(review(Vec::new())).await; + let directory = tempfile::tempdir().unwrap(); + let path = directory.path().join("missing").join("report.xml"); + let output = ci(&origin, &path, &[]).await; + assert_eq!(output.status.code(), Some(1), "{output:?}"); + let stderr = String::from_utf8(output.stderr).unwrap(); + assert!(stderr.contains("could not write JUnit XML to"), "{stderr}"); + assert!(!stderr.contains(TOKEN)); + assert_eq!(requests.lock().unwrap().as_slice(), ["detail"]); +} + +#[tokio::test] +async fn an_environment_named_like_a_synthetic_suite_keeps_its_own_suite() { + let mut document = review(Vec::new()); + document["status"] = json!("RUNNING"); + document["code_assessment"] = code_review(Vec::new())["code_assessment"].clone(); + document["assessments"][0]["environment_display_name"] = json!("review"); + let mut second = document["assessments"][0].clone(); + second["id"] = json!("0190f3a0-0000-7000-8000-000000000066"); + second["environment_display_name"] = json!("Code-only"); + document["assessments"].as_array_mut().unwrap().push(second); + let (_, xml) = export(document, &[]).await; + let root = report(&xml.unwrap()); + let mut names: Vec<&str> = root + .all("testsuite") + .map(|suite| suite.attribute("name").unwrap()) + .collect(); + names.sort_unstable(); + let total = names.len(); + names.dedup(); + assert_eq!(names.len(), total, "duplicate suite names: {names:?}"); + for expected in ["environment: review", "environment: Code-only", "review"] { + assert!(names.contains(&expected), "{expected}: {names:?}"); + } +}