From bff678bf1ab2904d7a0078e814150b4a70b946c8 Mon Sep 17 00:00:00 2001 From: Emmanuel Adeoye <76078947+Adeolu5678@users.noreply.github.com> Date: Mon, 16 Nov 2026 21:06:03 +0100 Subject: [PATCH 1/2] feat(ci): add rich GitHub Actions job summary ($GITHUB_STEP_SUMMARY) --- src/lib.rs | 74 +++++++ src/summary.rs | 557 +++++++++++++++++++++++++++++++++++++++++++++++++ tests/http.rs | 244 ++++++++++++++++++++++ 3 files changed, 875 insertions(+) create mode 100644 src/summary.rs diff --git a/src/lib.rs b/src/lib.rs index 7e39553..d4b34ec 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,6 +20,7 @@ pub mod render; mod result; mod sarif; mod severity; +mod summary; pub mod text; pub mod update; @@ -174,6 +175,16 @@ struct CiReviewArgs { /// Also write the review as SARIF 2.1.0 to this file. #[arg(long, value_name = "FILE")] sarif: Option, + /// Also write a Markdown job summary of the review to this file. If passed + /// without a path, writes to `$GITHUB_STEP_SUMMARY` when set. Pass `-` for stdout. + #[arg(long, num_args = 0..=1, value_name = "FILE")] + summary: Option>, + /// Explicit file path to write the Markdown job summary to. Pass `-` for stdout. + #[arg(long, value_name = "FILE")] + summary_file: Option, + /// Disable writing a job summary, even if GITHUB_STEP_SUMMARY is set. + #[arg(long)] + no_summary: bool, /// Print JSON instead of text. #[arg(long)] json: bool, @@ -1151,6 +1162,23 @@ async fn ci_review( if let Some(path) = sarif_path { write_sarif(api, review_id, &detail, path).await?; } + let summary_destination = resolve_summary_destination( + args.no_summary, + args.summary.as_ref(), + args.summary_file.as_ref(), + )?; + if let Some(dest) = &summary_destination { + summary::write(&detail, api.review_url(review_id).as_str(), dest) + .await + .with_context(|| match dest { + summary::SummaryDestination::File(path) => { + format!("could not write job summary to {}", path.display()) + } + summary::SummaryDestination::Stdout => { + "could not write job summary to stdout".to_owned() + } + })?; + } print_review( api, review_id, @@ -1169,6 +1197,46 @@ async fn ci_review( Ok(verdict) } +fn resolve_summary_destination( + no_summary: bool, + summary: Option<&Option>, + summary_file: Option<&PathBuf>, +) -> anyhow::Result> { + if no_summary { + return Ok(None); + } + if let Some(path) = summary_file { + return Ok(Some(destination_from_path(path))); + } + if let Some(arg) = summary { + return match arg { + Some(path) => Ok(Some(destination_from_path(path))), + None => { + let env_path = std::env::var_os("GITHUB_STEP_SUMMARY").ok_or_else(|| { + anyhow::anyhow!( + "--summary was passed without a file, and GITHUB_STEP_SUMMARY is not set in the environment" + ) + })?; + Ok(Some(destination_from_path(std::path::Path::new(&env_path)))) + } + }; + } + if let Some(env_path) = std::env::var_os("GITHUB_STEP_SUMMARY") { + if !env_path.is_empty() { + return Ok(Some(destination_from_path(std::path::Path::new(&env_path)))); + } + } + Ok(None) +} + +fn destination_from_path(path: &std::path::Path) -> summary::SummaryDestination { + if path == std::path::Path::new("-") { + summary::SummaryDestination::Stdout + } else { + summary::SummaryDestination::File(path.to_path_buf()) + } +} + /// Polls for the newest `PULL_REQUEST` review whose head is `commit` until /// `deadline`, asking once through `request` from [`REQUEST_AFTER`] past /// `started`. `None` means no review appeared, which it has already said. @@ -1563,6 +1631,9 @@ mod tests { let args = CiReviewArgs { commit: None, sarif: None, + summary: None, + summary_file: None, + no_summary: false, json: false, timeout: 3, fail_on: None, @@ -1630,6 +1701,9 @@ mod tests { let args = CiReviewArgs { commit: None, sarif: None, + summary: None, + summary_file: None, + no_summary: false, json: false, timeout: 3, fail_on: None, diff --git a/src/summary.rs b/src/summary.rs new file mode 100644 index 0000000..caf69ae --- /dev/null +++ b/src/summary.rs @@ -0,0 +1,557 @@ +//! Renders a Markdown job summary of a review for GitHub Actions +//! (`$GITHUB_STEP_SUMMARY`) or a local report file. + +use std::collections::BTreeSet; +use std::fmt::Write as _; +use std::path::{Path, PathBuf}; + +use crate::model::{Finding, ReviewDetail}; +use crate::outcome::{self, Outcome}; +use crate::render; +use crate::severity::Severity; +use crate::text::{one_line, terminal_safe}; + +/// Destination where the summary should be written. +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) enum SummaryDestination { + File(PathBuf), + Stdout, +} + +/// Writes a Markdown job summary of the review to `destination`, appending if writing to a file. +pub(crate) async fn write( + detail: &ReviewDetail, + review_url: &str, + destination: &SummaryDestination, +) -> anyhow::Result<()> { + let markdown = render(detail, review_url); + match destination { + SummaryDestination::Stdout => { + let mut stdout = tokio::io::stdout(); + tokio::io::AsyncWriteExt::write_all(&mut stdout, markdown.as_bytes()).await?; + tokio::io::AsyncWriteExt::flush(&mut stdout).await?; + } + SummaryDestination::File(path) => { + if let Some(parent) = path.parent() { + if !parent.as_os_str().is_empty() { + tokio::fs::create_dir_all(parent).await?; + } + } + let mut file = tokio::fs::OpenOptions::new() + .create(true) + .append(true) + .open(path) + .await?; + tokio::io::AsyncWriteExt::write_all(&mut file, markdown.as_bytes()).await?; + tokio::io::AsyncWriteExt::flush(&mut file).await?; + } + } + Ok(()) +} + +/// Renders the review as a Markdown document suitable for a GitHub Actions job summary. +pub(crate) fn render(detail: &ReviewDetail, review_url: &str) -> String { + let open_findings = collect_open_findings(detail); + let (serious_count, warning_count) = count_severities(&open_findings); + + let (icon, outcome_str) = match outcome::terminal(detail) { + Some(Outcome::Completed) => { + if serious_count > 0 { + ("🔴", "BLOCKING") + } else if warning_count > 0 { + ("🟡", "WARNINGS") + } else { + ("đŸŸĸ", "PASS") + } + } + Some(Outcome::Incomplete) => ("âš ī¸", "INCOMPLETE"), + Some(Outcome::Failed) => ("❌", "FAILED"), + Some(Outcome::Stopped) => ("âšī¸", "STOPPED"), + None => ("âŗ", detail.status.as_str()), + }; + + let mut out = String::new(); + + let _ = writeln!(out, "### {icon} Proofline Review: {outcome_str}\n"); + let _ = writeln!(out, "[View review in Proofline]({review_url})\n"); + + if let Some(repo) = &detail.repository_full_name { + let _ = writeln!(out, "- **Repository:** `{}`", escape_cell(repo)); + } + if let Some(commit) = &detail.head_commit_sha { + let _ = writeln!(out, "- **Commit:** `{}`", escape_cell(commit)); + } + if let Some(pr) = detail.pull_request_number { + if let Some(run) = detail.run_number { + let _ = writeln!(out, "- **Pull Request:** #{pr} (run {run})"); + } else { + let _ = writeln!(out, "- **Pull Request:** #{pr}"); + } + } + let _ = writeln!(out); + + render_metrics_table( + &mut out, + detail, + open_findings.len(), + serious_count, + warning_count, + &open_findings, + ); + render_assessments(&mut out, detail); + render_findings(&mut out, &open_findings); + + out +} + +fn collect_open_findings<'a>(detail: &'a ReviewDetail) -> Vec<(&'a str, &'a Finding)> { + let mut findings = Vec::new(); + if let Some(code) = outcome::code_assessment(detail) { + for item in &code.findings { + if !is_resolved(item) { + findings.push(("Code", item)); + } + } + } + for assessment in &detail.assessments { + for item in &assessment.findings { + if !is_resolved(item) { + findings.push((assessment.environment_display_name.as_str(), item)); + } + } + } + findings +} + +fn count_severities(findings: &[(&str, &Finding)]) -> (usize, usize) { + let mut serious = 0; + let mut warning = 0; + for (_, finding) in findings { + match Severity::parse(&finding.severity) { + Some(Severity::Serious) => serious += 1, + Some(Severity::Warning) => warning += 1, + None => {} + } + } + (serious, warning) +} + +fn render_metrics_table( + out: &mut String, + detail: &ReviewDetail, + total_findings: usize, + serious_count: usize, + warning_count: usize, + open_findings: &[(&str, &Finding)], +) { + let files_count = files_inspected_count(detail, open_findings); + + let _ = writeln!(out, "#### Summary Metrics\n"); + let _ = writeln!( + out, + "| Files Inspected | Open Findings | Serious (🔴) | Warnings (🟡) |" + ); + let _ = writeln!(out, "| :---: | :---: | :---: | :---: |"); + let _ = writeln!( + out, + "| {files_count} | {total_findings} | {serious_count} | {warning_count} |\n" + ); +} + +fn files_inspected_count(detail: &ReviewDetail, open_findings: &[(&str, &Finding)]) -> usize { + let scope_files = detail + .subjects + .iter() + .find(|subject| subject.role == "PRIMARY" && subject.kind == "GIT_REVISION") + .and_then(|subject| subject.file_scope.as_ref()) + .map(|scope| scope.paths.len()); + + if let Some(count) = scope_files { + if count > 0 { + return count; + } + } + + let mut cited_files = BTreeSet::new(); + for (_, finding) in open_findings { + for evidence in &finding.evidence_refs { + if let Some(path) = &evidence.repository_path { + cited_files.insert(path.as_str()); + } + } + } + cited_files.len() +} + +fn render_assessments(out: &mut String, detail: &ReviewDetail) { + let _ = writeln!(out, "#### Assessments\n"); + let _ = writeln!(out, "| Assessment | Status | Open Findings |"); + let _ = writeln!(out, "| :--- | :--- | :--- |"); + + if let Some(code) = outcome::code_assessment(detail) { + let status = code.status.as_str(); + let open_count = code.findings.iter().filter(|f| !is_resolved(f)).count(); + let status_desc = outcome::code_gap(code) + .map_or_else(|| status.to_owned(), |gap| format!("{status} ({gap})")); + let _ = writeln!( + out, + "| Code | {} | {} |", + escape_cell(&status_desc), + open_count + ); + } + + for assessment in &detail.assessments { + let name = &assessment.environment_display_name; + let status = assessment.status.as_str(); + let open_count = assessment + .findings + .iter() + .filter(|f| !is_resolved(f)) + .count(); + let status_desc = outcome::gap(assessment).map_or_else( + || status.to_owned(), + |gaps| format!("{status} ({})", gaps.join(", ")), + ); + let _ = writeln!( + out, + "| {} | {} | {} |", + escape_cell(name), + escape_cell(&status_desc), + open_count + ); + } + let _ = writeln!(out); +} + +fn render_findings(out: &mut String, open_findings: &[(&str, &Finding)]) { + let _ = writeln!(out, "#### Findings\n"); + + if open_findings.is_empty() { + let _ = writeln!(out, "✅ No open infrastructure findings.\n"); + return; + } + + let _ = writeln!( + out, + "| Severity | Environment | Risk Domain | Claim | Location |" + ); + let _ = writeln!(out, "| :--- | :--- | :--- | :--- | :--- |"); + + for (env, finding) in open_findings { + let severity = severity_badge(&finding.severity); + let domain = escape_cell(&one_line(&finding.risk_domain)); + let claim = escape_cell(&one_line(&finding.claim)); + let locs = render::location(finding); + let loc_str = if locs.is_empty() { + "-".to_owned() + } else { + locs.iter() + .map(|l| format!("`{}`", escape_cell(l))) + .collect::>() + .join(", ") + }; + + let _ = writeln!( + out, + "| {} | {} | `{}` | {} | {} |", + severity, + escape_cell(env), + domain, + claim, + loc_str + ); + } + let _ = writeln!(out); + + let _ = writeln!( + out, + "
\n🔍 Finding Details & Remediation\n" + ); + + for (env, finding) in open_findings { + let severity = severity_badge(&finding.severity); + let domain = escape_cell(&one_line(&finding.risk_domain)); + let claim = escape_cell(&one_line(&finding.claim)); + let locs = render::location(finding); + let loc_str = if locs.is_empty() { + "-".to_owned() + } else { + locs.iter() + .map(|l| format!("`{}`", escape_cell(l))) + .collect::>() + .join(", ") + }; + + let _ = writeln!(out, "##### {severity} {claim}"); + let _ = writeln!(out, "- **Environment:** {}", escape_cell(env)); + let _ = writeln!(out, "- **Risk Domain:** `{domain}`"); + let _ = writeln!(out, "- **Location:** {loc_str}"); + if !finding.impact.is_empty() { + let _ = writeln!(out, "- **Impact:** {}", escape_cell(&one_line(&finding.impact))); + } + if let Some(remediation) = &finding.remediation { + if !remediation.is_empty() { + let _ = writeln!(out, "- **Remediation:** {}", escape_cell(&one_line(remediation))); + } + } + let _ = writeln!(out); + } + + let _ = writeln!(out, "
\n"); +} + +fn is_resolved(finding: &Finding) -> bool { + finding.lifecycle_state == "RESOLVED" +} + +fn severity_badge(severity_str: &str) -> String { + match Severity::parse(severity_str) { + Some(Severity::Serious) => "🔴 **SERIOUS**".to_owned(), + Some(Severity::Warning) => "🟡 **WARNING**".to_owned(), + None => format!("â„šī¸ **{}**", escape_cell(severity_str)), + } +} + +pub(crate) fn escape_cell(value: &str) -> String { + terminal_safe(value) + .replace('|', "\\|") + .replace('\n', " ") + .replace('\r', "") +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::model::{Assessment, Checks, EvidenceRef, FileScope, ReviewSubject}; + use uuid::Uuid; + + #[test] + fn renders_clean_review_summary() { + let detail = ReviewDetail { + id: Uuid::nil(), + kind: "PULL_REQUEST".to_owned(), + status: "COMPLETED".to_owned(), + assessment_count: 1, + assessments: vec![Assessment { + id: Uuid::nil(), + environment_display_name: "production".to_owned(), + status: "COMPLETED".to_owned(), + findings: Vec::new(), + checks: Checks { + receipt_recorded: true, + status: "READY".to_owned(), + families: Vec::new(), + }, + incompleteness_kind: None, + reason_codes: None, + release_evidence_gap: None, + }], + code_assessment: None, + base_commit_sha: None, + head_commit_sha: Some("0123456789abcdef0123456789abcdef01234567".to_owned()), + open_items: None, + pull_request_number: Some(42), + repository_full_name: Some("acme/app".to_owned()), + run_number: Some(1), + subjects: vec![ReviewSubject { + kind: "GIT_REVISION".to_owned(), + role: "PRIMARY".to_owned(), + file_scope: Some(FileScope { + paths: vec!["src/main.rs".to_owned(), "src/lib.rs".to_owned()], + paths_digest: "sha256:abc".to_owned(), + }), + }], + local_reverifications: Vec::new(), + }; + + let summary = render(&detail, "https://proofline.sh/reviews/test"); + assert!(summary.contains("### đŸŸĸ Proofline Review: PASS")); + assert!(summary.contains("[View review in Proofline](https://proofline.sh/reviews/test)")); + assert!(summary.contains("- **Repository:** `acme/app`")); + assert!(summary.contains("- **Commit:** `0123456789abcdef0123456789abcdef01234567`")); + assert!(summary.contains("- **Pull Request:** #42 (run 1)")); + assert!(summary.contains("| 2 | 0 | 0 | 0 |")); + assert!(summary.contains("| production | COMPLETED | 0 |")); + assert!(summary.contains("✅ No open infrastructure findings.")); + } + + #[test] + fn renders_summary_with_mixed_serious_and_warning_findings() { + let finding_serious = Finding { + id: Uuid::nil(), + finding_fingerprint: Some("fp-1".to_owned()), + severity: "SERIOUS".to_owned(), + risk_domain: "reliability".to_owned(), + claim: "Orders dropped on retry".to_owned(), + impact: "Data loss in queue".to_owned(), + lifecycle_state: "OPEN".to_owned(), + remediation: Some("Persist transactions before retry".to_owned()), + evidence_refs: vec![EvidenceRef { + kind: "CODE".to_owned(), + label: "order logic".to_owned(), + provenance_status: None, + repository_path: Some("src/orders.rs".to_owned()), + start_line: Some(42), + end_line: Some(50), + file_sha256: None, + commit_sha: None, + companion_repository: None, + }], + }; + + let finding_warning = Finding { + id: Uuid::nil(), + finding_fingerprint: Some("fp-2".to_owned()), + severity: "WARNING".to_owned(), + risk_domain: "performance".to_owned(), + claim: "Missing index on lookup".to_owned(), + impact: "Slow queries under peak load".to_owned(), + lifecycle_state: "ACTIVE".to_owned(), + remediation: Some("Add index on user_id".to_owned()), + evidence_refs: vec![EvidenceRef { + kind: "CODE".to_owned(), + label: "query".to_owned(), + provenance_status: None, + repository_path: Some("db/schema.sql".to_owned()), + start_line: Some(15), + end_line: None, + file_sha256: None, + commit_sha: None, + companion_repository: None, + }], + }; + + let detail = ReviewDetail { + id: Uuid::nil(), + kind: "PULL_REQUEST".to_owned(), + status: "COMPLETED".to_owned(), + assessment_count: 1, + assessments: vec![Assessment { + id: Uuid::nil(), + environment_display_name: "production".to_owned(), + status: "COMPLETED".to_owned(), + findings: vec![finding_serious, finding_warning], + checks: Checks { + receipt_recorded: true, + status: "READY".to_owned(), + families: Vec::new(), + }, + incompleteness_kind: None, + reason_codes: None, + release_evidence_gap: None, + }], + code_assessment: None, + base_commit_sha: None, + head_commit_sha: Some("abcdef1234567890abcdef1234567890abcdef12".to_owned()), + open_items: None, + pull_request_number: Some(99), + repository_full_name: Some("acme/orders".to_owned()), + run_number: None, + subjects: Vec::new(), + local_reverifications: Vec::new(), + }; + + let summary = render(&detail, "https://proofline.sh/reviews/orders-99"); + assert!(summary.contains("### 🔴 Proofline Review: BLOCKING")); + assert!(summary.contains("| 2 | 2 | 1 | 1 |")); + assert!(summary.contains("| 🔴 **SERIOUS** | production | `reliability` | Orders dropped on retry | `src/orders.rs:42-50` |")); + assert!(summary.contains("| 🟡 **WARNING** | production | `performance` | Missing index on lookup | `db/schema.sql:15` |")); + assert!(summary.contains("
")); + assert!(summary.contains("🔍 Finding Details & Remediation")); + assert!(summary.contains("- **Impact:** Data loss in queue")); + assert!(summary.contains("- **Remediation:** Persist transactions before retry")); + } + + #[test] + fn renders_warning_only_findings_as_warnings_banner() { + let finding = Finding { + id: Uuid::nil(), + finding_fingerprint: Some("fp-warn".to_owned()), + severity: "WARNING".to_owned(), + risk_domain: "security".to_owned(), + claim: "Loose CORS header".to_owned(), + impact: "Potential CSRF vulnerability".to_owned(), + lifecycle_state: "OPEN".to_owned(), + remediation: Some("Restrict origin list".to_owned()), + evidence_refs: Vec::new(), + }; + + let detail = ReviewDetail { + id: Uuid::nil(), + kind: "PULL_REQUEST".to_owned(), + status: "COMPLETED".to_owned(), + assessment_count: 1, + assessments: vec![Assessment { + id: Uuid::nil(), + environment_display_name: "staging".to_owned(), + status: "COMPLETED".to_owned(), + findings: vec![finding], + checks: Checks { + receipt_recorded: true, + status: "READY".to_owned(), + families: Vec::new(), + }, + incompleteness_kind: None, + reason_codes: None, + release_evidence_gap: None, + }], + code_assessment: None, + base_commit_sha: None, + head_commit_sha: None, + open_items: None, + pull_request_number: Some(10), + repository_full_name: None, + run_number: None, + subjects: Vec::new(), + local_reverifications: Vec::new(), + }; + + let summary = render(&detail, "https://proofline.sh/reviews/10"); + assert!(summary.contains("### 🟡 Proofline Review: WARNINGS")); + assert!(summary.contains("| 0 | 1 | 0 | 1 |")); + } + + #[test] + fn escapes_pipe_newlines_and_ansi_in_cells() { + assert_eq!(escape_cell("foo|bar\nbaz\rqux"), "foo\\|bar bazqux"); + assert_eq!(escape_cell("\x1b[31mred\x1b[0m|text"), "red\\|text"); + } + + #[tokio::test] + async fn writes_and_appends_to_summary_file() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("sub").join("job_summary.md"); + let dest = SummaryDestination::File(path.clone()); + + let detail = ReviewDetail { + id: Uuid::nil(), + kind: "PULL_REQUEST".to_owned(), + status: "COMPLETED".to_owned(), + assessment_count: 0, + assessments: Vec::new(), + code_assessment: None, + base_commit_sha: None, + head_commit_sha: None, + open_items: None, + pull_request_number: None, + repository_full_name: None, + run_number: None, + subjects: Vec::new(), + local_reverifications: Vec::new(), + }; + + write(&detail, "https://proofline.sh/r/1", &dest) + .await + .unwrap(); + assert!(path.exists()); + let first_len = std::fs::read(&path).unwrap().len(); + + // Second write should append, not truncate + write(&detail, "https://proofline.sh/r/1", &dest) + .await + .unwrap(); + let second_len = std::fs::read(&path).unwrap().len(); + assert_eq!(second_len, first_len * 2); + } +} diff --git a/tests/http.rs b/tests/http.rs index 5ea634b..36cd17e 100644 --- a/tests/http.rs +++ b/tests/http.rs @@ -2916,3 +2916,247 @@ async fn config_proposal_targets_must_match_the_selected_organization() { assert_eq!(server.requests()[0]["arguments"]["organization_id"], ORG); assert_eq!(server.frames.load(Ordering::SeqCst), 3); } + +#[tokio::test] +async fn ci_review_summary_writes_markdown_report_or_uses_github_step_summary() { + let (origin, _) = ci_console( + "PULL_REQUEST", + StatusCode::OK, + "COMPLETED", + Some([1, 1]), + None, + ) + .await; + + let directory = tempfile::tempdir().unwrap(); + let explicit_summary = directory.path().join("summary.md"); + + // Case 1: Explicit path via --summary + let mut command = tokio::process::Command::new(env!("CARGO_BIN_EXE_proofline")); + command + .args(["--base-url", &origin, "--org", ORG, "ci", "review"]) + .args([ + "--repo", + "acme/app", + "--timeout", + "0", + "--json", + "--summary", + ]) + .arg(&explicit_summary) + .env("PROOFLINE_TOKEN", TOKEN) + .env("COMMIT_SHA", CI_HEAD) + .env_remove("GITHUB_SHA") + .env_remove("GITHUB_STEP_SUMMARY") + .env("PATH", "") + .current_dir(directory.path()) + .kill_on_drop(true); + let output = tokio::time::timeout(std::time::Duration::from_secs(15), command.output()) + .await + .unwrap() + .unwrap(); + assert_eq!( + output.status.code(), + Some(0), + "stderr: {}", + String::from_utf8_lossy(&output.stderr) + ); + let content = std::fs::read_to_string(&explicit_summary).unwrap(); + assert!(content.contains("### 🔴 Proofline Review: BLOCKING")); + assert!(content.contains("View review in Proofline")); + assert!(content.contains("#### Summary Metrics")); + assert!(content.contains("| production | COMPLETED | 1 |")); + assert!(content.contains("Orders are dropped on retry.")); + assert!(content.contains("🔴 **SERIOUS**")); + assert!(content.contains("🔍 Finding Details & Remediation")); + + // Case 2: Explicit path via --summary-file + let summary_file_path = directory.path().join("summary_file.md"); + let mut command = tokio::process::Command::new(env!("CARGO_BIN_EXE_proofline")); + command + .args(["--base-url", &origin, "--org", ORG, "ci", "review"]) + .args([ + "--repo", + "acme/app", + "--timeout", + "0", + "--json", + "--summary-file", + ]) + .arg(&summary_file_path) + .env("PROOFLINE_TOKEN", TOKEN) + .env("COMMIT_SHA", CI_HEAD) + .env_remove("GITHUB_SHA") + .env_remove("GITHUB_STEP_SUMMARY") + .env("PATH", "") + .current_dir(directory.path()) + .kill_on_drop(true); + let output = tokio::time::timeout(std::time::Duration::from_secs(15), command.output()) + .await + .unwrap() + .unwrap(); + assert_eq!( + output.status.code(), + Some(0), + "stderr: {}", + String::from_utf8_lossy(&output.stderr) + ); + let sf_content = std::fs::read_to_string(&summary_file_path).unwrap(); + assert!(sf_content.contains("### 🔴 Proofline Review: BLOCKING")); + + // Case 3: Bare --summary uses GITHUB_STEP_SUMMARY environment variable + let gha_summary = directory.path().join("github_step_summary.md"); + let mut command = tokio::process::Command::new(env!("CARGO_BIN_EXE_proofline")); + command + .args(["--base-url", &origin, "--org", ORG, "ci", "review"]) + .args([ + "--repo", + "acme/app", + "--timeout", + "0", + "--json", + "--summary", + ]) + .env("PROOFLINE_TOKEN", TOKEN) + .env("COMMIT_SHA", CI_HEAD) + .env("GITHUB_STEP_SUMMARY", &gha_summary) + .env_remove("GITHUB_SHA") + .env("PATH", "") + .current_dir(directory.path()) + .kill_on_drop(true); + let output = tokio::time::timeout(std::time::Duration::from_secs(15), command.output()) + .await + .unwrap() + .unwrap(); + assert_eq!( + output.status.code(), + Some(0), + "stderr: {}", + String::from_utf8_lossy(&output.stderr) + ); + let gha_content = std::fs::read_to_string(&gha_summary).unwrap(); + assert!(gha_content.contains("### 🔴 Proofline Review: BLOCKING")); + + // Case 4: Automatic detection of GITHUB_STEP_SUMMARY when no summary flags are passed + let auto_summary = directory.path().join("auto_detected_summary.md"); + let mut command = tokio::process::Command::new(env!("CARGO_BIN_EXE_proofline")); + command + .args(["--base-url", &origin, "--org", ORG, "ci", "review"]) + .args([ + "--repo", + "acme/app", + "--timeout", + "0", + "--json", + ]) + .env("PROOFLINE_TOKEN", TOKEN) + .env("COMMIT_SHA", CI_HEAD) + .env("GITHUB_STEP_SUMMARY", &auto_summary) + .env_remove("GITHUB_SHA") + .env("PATH", "") + .current_dir(directory.path()) + .kill_on_drop(true); + let output = tokio::time::timeout(std::time::Duration::from_secs(15), command.output()) + .await + .unwrap() + .unwrap(); + assert_eq!( + output.status.code(), + Some(0), + "stderr: {}", + String::from_utf8_lossy(&output.stderr) + ); + let auto_content = std::fs::read_to_string(&auto_summary).unwrap(); + assert!(auto_content.contains("### 🔴 Proofline Review: BLOCKING")); + + // Case 5: --no-summary suppresses automatic GITHUB_STEP_SUMMARY writing + let no_summary_file = directory.path().join("no_summary.md"); + let mut command = tokio::process::Command::new(env!("CARGO_BIN_EXE_proofline")); + command + .args(["--base-url", &origin, "--org", ORG, "ci", "review"]) + .args([ + "--repo", + "acme/app", + "--timeout", + "0", + "--json", + "--no-summary", + ]) + .env("PROOFLINE_TOKEN", TOKEN) + .env("COMMIT_SHA", CI_HEAD) + .env("GITHUB_STEP_SUMMARY", &no_summary_file) + .env_remove("GITHUB_SHA") + .env("PATH", "") + .current_dir(directory.path()) + .kill_on_drop(true); + let output = tokio::time::timeout(std::time::Duration::from_secs(15), command.output()) + .await + .unwrap() + .unwrap(); + assert_eq!( + output.status.code(), + Some(0), + "stderr: {}", + String::from_utf8_lossy(&output.stderr) + ); + assert!(!no_summary_file.exists()); + + // Case 6: Bare --summary without GITHUB_STEP_SUMMARY fails gracefully + let mut command = tokio::process::Command::new(env!("CARGO_BIN_EXE_proofline")); + command + .args(["--base-url", &origin, "--org", ORG, "ci", "review"]) + .args([ + "--repo", + "acme/app", + "--timeout", + "0", + "--json", + "--summary", + ]) + .env("PROOFLINE_TOKEN", TOKEN) + .env("COMMIT_SHA", CI_HEAD) + .env_remove("GITHUB_STEP_SUMMARY") + .env_remove("GITHUB_SHA") + .env("PATH", "") + .current_dir(directory.path()) + .kill_on_drop(true); + let output = tokio::time::timeout(std::time::Duration::from_secs(15), command.output()) + .await + .unwrap() + .unwrap(); + assert_eq!(output.status.code(), Some(1)); + let stderr = String::from_utf8_lossy(&output.stderr); + assert!(stderr.contains("GITHUB_STEP_SUMMARY")); + + // Case 7: --summary - writes summary to stdout + let mut command = tokio::process::Command::new(env!("CARGO_BIN_EXE_proofline")); + command + .args(["--base-url", &origin, "--org", ORG, "ci", "review"]) + .args([ + "--repo", + "acme/app", + "--timeout", + "0", + "--summary", + "-", + ]) + .env("PROOFLINE_TOKEN", TOKEN) + .env("COMMIT_SHA", CI_HEAD) + .env_remove("GITHUB_STEP_SUMMARY") + .env_remove("GITHUB_SHA") + .env("PATH", "") + .current_dir(directory.path()) + .kill_on_drop(true); + let output = tokio::time::timeout(std::time::Duration::from_secs(15), command.output()) + .await + .unwrap() + .unwrap(); + assert_eq!( + output.status.code(), + Some(0), + "stderr: {}", + String::from_utf8_lossy(&output.stderr) + ); + let stdout = String::from_utf8_lossy(&output.stdout); + assert!(stdout.contains("### 🔴 Proofline Review: BLOCKING")); +} From cba2d1e857a505204d7d105be471d71b5154869c Mon Sep 17 00:00:00 2001 From: Emmanuel Adeoye <76078947+Adeolu5678@users.noreply.github.com> Date: Tue, 17 Nov 2026 00:52:53 +0100 Subject: [PATCH 2/2] style: format summary and test files and resolve clippy lints --- src/lib.rs | 28 +++++++++--------- src/summary.rs | 78 ++++++++++++++++++++++++++++++++++++-------------- tests/http.rs | 17 ++--------- 3 files changed, 72 insertions(+), 51 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d4b34ec..277bc33 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -182,7 +182,7 @@ struct CiReviewArgs { /// Explicit file path to write the Markdown job summary to. Pass `-` for stdout. #[arg(long, value_name = "FILE")] summary_file: Option, - /// Disable writing a job summary, even if GITHUB_STEP_SUMMARY is set. + /// Disable writing a job summary, even if `GITHUB_STEP_SUMMARY` is set. #[arg(long)] no_summary: bool, /// Print JSON instead of text. @@ -1209,22 +1209,20 @@ fn resolve_summary_destination( return Ok(Some(destination_from_path(path))); } if let Some(arg) = summary { - return match arg { - Some(path) => Ok(Some(destination_from_path(path))), - None => { - let env_path = std::env::var_os("GITHUB_STEP_SUMMARY").ok_or_else(|| { - anyhow::anyhow!( - "--summary was passed without a file, and GITHUB_STEP_SUMMARY is not set in the environment" - ) - })?; - Ok(Some(destination_from_path(std::path::Path::new(&env_path)))) - } + let destination = if let Some(path) = arg { + destination_from_path(path) + } else { + let env_path = std::env::var_os("GITHUB_STEP_SUMMARY").ok_or_else(|| { + anyhow::anyhow!( + "--summary was passed without a file, and GITHUB_STEP_SUMMARY is not set in the environment" + ) + })?; + destination_from_path(std::path::Path::new(&env_path)) }; + return Ok(Some(destination)); } - if let Some(env_path) = std::env::var_os("GITHUB_STEP_SUMMARY") { - if !env_path.is_empty() { - return Ok(Some(destination_from_path(std::path::Path::new(&env_path)))); - } + if let Some(env_path) = std::env::var_os("GITHUB_STEP_SUMMARY").filter(|p| !p.is_empty()) { + return Ok(Some(destination_from_path(std::path::Path::new(&env_path)))); } Ok(None) } diff --git a/src/summary.rs b/src/summary.rs index caf69ae..e27e779 100644 --- a/src/summary.rs +++ b/src/summary.rs @@ -3,7 +3,7 @@ use std::collections::BTreeSet; use std::fmt::Write as _; -use std::path::{Path, PathBuf}; +use std::path::PathBuf; use crate::model::{Finding, ReviewDetail}; use crate::outcome::{self, Outcome}; @@ -32,10 +32,8 @@ pub(crate) async fn write( tokio::io::AsyncWriteExt::flush(&mut stdout).await?; } SummaryDestination::File(path) => { - if let Some(parent) = path.parent() { - if !parent.as_os_str().is_empty() { - tokio::fs::create_dir_all(parent).await?; - } + if let Some(parent) = path.parent().filter(|p| !p.as_os_str().is_empty()) { + tokio::fs::create_dir_all(parent).await?; } let mut file = tokio::fs::OpenOptions::new() .create(true) @@ -104,7 +102,7 @@ pub(crate) fn render(detail: &ReviewDetail, review_url: &str) -> String { out } -fn collect_open_findings<'a>(detail: &'a ReviewDetail) -> Vec<(&'a str, &'a Finding)> { +fn collect_open_findings(detail: &ReviewDetail) -> Vec<(&str, &Finding)> { let mut findings = Vec::new(); if let Some(code) = outcome::code_assessment(detail) { for item in &code.findings { @@ -166,10 +164,8 @@ fn files_inspected_count(detail: &ReviewDetail, open_findings: &[(&str, &Finding .and_then(|subject| subject.file_scope.as_ref()) .map(|scope| scope.paths.len()); - if let Some(count) = scope_files { - if count > 0 { - return count; - } + if let Some(count) = scope_files.filter(|&c| c > 0) { + return count; } let mut cited_files = BTreeSet::new(); @@ -191,8 +187,10 @@ fn render_assessments(out: &mut String, detail: &ReviewDetail) { if let Some(code) = outcome::code_assessment(detail) { let status = code.status.as_str(); let open_count = code.findings.iter().filter(|f| !is_resolved(f)).count(); - let status_desc = outcome::code_gap(code) - .map_or_else(|| status.to_owned(), |gap| format!("{status} ({gap})")); + let status_desc = outcome::code_gap(code).map_or_else( + || status.to_owned(), + |gaps| format!("{status} ({})", gaps.join(", ")), + ); let _ = writeln!( out, "| Code | {} | {} |", @@ -288,12 +286,18 @@ fn render_findings(out: &mut String, open_findings: &[(&str, &Finding)]) { let _ = writeln!(out, "- **Risk Domain:** `{domain}`"); let _ = writeln!(out, "- **Location:** {loc_str}"); if !finding.impact.is_empty() { - let _ = writeln!(out, "- **Impact:** {}", escape_cell(&one_line(&finding.impact))); + let _ = writeln!( + out, + "- **Impact:** {}", + escape_cell(&one_line(&finding.impact)) + ); } - if let Some(remediation) = &finding.remediation { - if !remediation.is_empty() { - let _ = writeln!(out, "- **Remediation:** {}", escape_cell(&one_line(remediation))); - } + if let Some(remediation) = finding.remediation.as_deref().filter(|r| !r.is_empty()) { + let _ = writeln!( + out, + "- **Remediation:** {}", + escape_cell(&one_line(remediation)) + ); } let _ = writeln!(out); } @@ -313,11 +317,43 @@ fn severity_badge(severity_str: &str) -> String { } } +fn strip_ansi(input: &str) -> String { + let mut out = String::with_capacity(input.len()); + let mut chars = input.chars().peekable(); + while let Some(ch) = chars.next() { + if ch == '\x1b' { + if chars.peek() == Some(&'[') { + chars.next(); + while let Some(&c) = chars.peek() { + chars.next(); + if ('@'..='~').contains(&c) { + break; + } + } + } else if chars.peek() == Some(&']') { + chars.next(); + while let Some(&c) = chars.peek() { + chars.next(); + if c == '\x07' { + break; + } + if c == '\x1b' && chars.peek() == Some(&'\\') { + chars.next(); + break; + } + } + } + } else { + out.push(ch); + } + } + out +} + pub(crate) fn escape_cell(value: &str) -> String { - terminal_safe(value) - .replace('|', "\\|") - .replace('\n', " ") - .replace('\r', "") + let stripped = strip_ansi(value); + let normalized = stripped.replace('\r', "").replace('\n', " "); + terminal_safe(&normalized).replace('|', "\\|") } #[cfg(test)] diff --git a/tests/http.rs b/tests/http.rs index 36cd17e..30127ac 100644 --- a/tests/http.rs +++ b/tests/http.rs @@ -3042,13 +3042,7 @@ async fn ci_review_summary_writes_markdown_report_or_uses_github_step_summary() let mut command = tokio::process::Command::new(env!("CARGO_BIN_EXE_proofline")); command .args(["--base-url", &origin, "--org", ORG, "ci", "review"]) - .args([ - "--repo", - "acme/app", - "--timeout", - "0", - "--json", - ]) + .args(["--repo", "acme/app", "--timeout", "0", "--json"]) .env("PROOFLINE_TOKEN", TOKEN) .env("COMMIT_SHA", CI_HEAD) .env("GITHUB_STEP_SUMMARY", &auto_summary) @@ -3132,14 +3126,7 @@ async fn ci_review_summary_writes_markdown_report_or_uses_github_step_summary() let mut command = tokio::process::Command::new(env!("CARGO_BIN_EXE_proofline")); command .args(["--base-url", &origin, "--org", ORG, "ci", "review"]) - .args([ - "--repo", - "acme/app", - "--timeout", - "0", - "--summary", - "-", - ]) + .args(["--repo", "acme/app", "--timeout", "0", "--summary", "-"]) .env("PROOFLINE_TOKEN", TOKEN) .env("COMMIT_SHA", CI_HEAD) .env_remove("GITHUB_STEP_SUMMARY")