Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ For a CodePipeline execution or Amplify job, run `proofline ci deploy --commit S

`proofline ci deploy --until 1h` waits until Proofline has recorded the deploy checks of the job's commit through the hour check, then exits 0 when each recorded `HEALTHY` with no gap, 3 when a deploy failed or regressed, and 2 when the result is not established, including when a deploy reads a state this CLI version does not know. After a read that answered, the wait keeps reading through a read that fails in transport, times out or gets a 5xx answer, and exits 4 after five such failures in a row, naming the last error; the first read and a refusal end it at once. Without `--timeout`, a wait for `--until landing`, `--until 20m` or `--until 1h` lasts until that check is due for the selected deploys, and at least 30 minutes, so a landing check that reads the runtime again, up to 75 minutes after landing, is still read; `--until 1d` needs `--timeout`. `--environment-id` (the Proofline environment's UUID, the `environment_id` that `--json` prints for each deploy), `--environment` (the GitHub Environment name), `--task`, `--deployment` (the ID of the GitHub deployment a pipeline created), `--run` and `--attempt` select the deploy, and on a `workflow_run` event the commit, `--run` and `--attempt` default to the completed run's. When `--run` defaults to a completed run that did not succeed, the command waits at most 5 minutes for a deploy from that run, then exits 2 with a last line that names the run's conclusion and says it recorded no deploy; a deploy the run did record gets the usual wait for its check. `--json` prints the response. Each deploy prints one line per check. A deploy that rolled the environment back to an earlier commit first prints a `rollback` line with the page of the deploy it restores, when Proofline recorded one, and the pages of the deploys it rolled back. A deploy whose newest check found its service serving an earlier deploy again first prints a `rolled back` line, and stops printing it once a later check finds none; with `--json` the response carries it as `rolled_back`. Both lines can appear for one deploy. A rollback's exit code comes from its checks like any other deploy's; neither line changes it or means the rollback fixed anything. A deploy whose deploy checks ended without recording the `--until` check ends the wait at once with exit 2, and the last line names the check and the deploy's page; a check that is overdue while the checks still run keeps the wait going until the timeout.

In GitHub Actions, pass `--annotations` to `proofline ci deploy` to emit workflow annotations on stderr on every GitHub plan: `::error` for each deploy that failed or recorded `REGRESSED`, and `::warning` when a check is not established or no matching deploy is recorded. When no regression is observed (`HEALTHY` with no gap), no annotation is emitted, and `--json` output on stdout stays unchanged.

## Scans

Proofline cannot analyze a scan's files yet. An accepted scan reads the repository's files at its commit but analyzes none of them, so a scan of a repository with files ends `FAILED` or `INCOMPLETE` with no findings, and the command exits 2. That empty finding list says nothing about the code.
Expand Down
110 changes: 90 additions & 20 deletions src/annotations.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
//! Findings as GitHub Actions workflow commands. The runner reads each
//! `::error file=...,line=...::message` line from a step's output and shows it
//! on the pull request's diff, on every GitHub plan. Findings quote reviewer
//! output and repository content, so every value is escaped: a newline in a
//! claim must never start a second command.
//! Findings and deploy checks as GitHub Actions workflow commands. The runner
//! reads each `::error ...::message` or `::warning ...::message` line from a
//! step's output and shows it on the pull request's diff or run summary, on
//! every GitHub plan. Values quote untrusted reviewer, server, and repository
//! content, so every value is escaped: a newline must never start a second
//! command.

use std::io::Write;

use crate::model::{Finding, ReviewDetail};
use crate::severity::Severity;
use crate::{outcome, sarif, text};

/// Severity of a GitHub Actions workflow annotation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Level {
Error,
Warning,
Notice,
}

impl Level {
const fn as_str(self) -> &'static str {
match self {
Self::Error => "error",
Self::Warning => "warning",
Self::Notice => "notice",
}
}
}

/// Writes one annotation per finding that is not resolved. A finding with a
/// verified location at the review's head is placed on that file and line;
/// any other finding is annotated on the job without a location.
Expand All @@ -28,6 +47,18 @@ pub(super) fn write(detail: &ReviewDetail, out: &mut impl Write) -> std::io::Res
Ok(())
}

/// Writes a job-level workflow annotation (`::error`, `::warning`, or
/// `::notice` with `title=...`) with both the title property and message
/// escaped.
pub(crate) fn write_job(
out: &mut impl Write,
level: Level,
title: &str,
message: &str,
) -> std::io::Result<()> {
write_command(out, level, None, title, message)
}

fn annotate(
out: &mut impl Write,
finding: &Finding,
Expand All @@ -38,44 +69,55 @@ fn annotate(
return Ok(());
}
// A severity this CLI does not know yet is still shown, as a notice.
let command = match Severity::parse(&finding.severity) {
Some(Severity::Serious) => "error",
Some(Severity::Warning) => "warning",
None => "notice",
let level = match Severity::parse(&finding.severity) {
Some(Severity::Serious) => Level::Error,
Some(Severity::Warning) => Level::Warning,
None => Level::Notice,
};
let mut properties = Vec::new();
if let Some((path, start, end)) = finding
let location = finding
.evidence_refs
.iter()
.find_map(|reference| sarif::location(reference, head))
{
.find_map(|reference| sarif::location(reference, head));
let title = format!("Proofline {lane}: {}", finding.risk_domain);
let message = format!("{}\n\n{}", finding.claim, finding.impact);
write_command(out, level, location, &title, &message)
}

fn write_command(
out: &mut impl Write,
level: Level,
location: Option<(&str, u32, u32)>,
title: &str,
message: &str,
) -> std::io::Result<()> {
let mut properties = Vec::new();
if let Some((path, start, end)) = location {
properties.push(format!("file={}", escape_property(path)));
properties.push(format!("line={start}"));
properties.push(format!("endLine={end}"));
}
let title = format!("Proofline {lane}: {}", finding.risk_domain);
properties.push(format!("title={}", escape_property(&title)));
let message = format!("{}\n\n{}", finding.claim, finding.impact);
properties.push(format!("title={}", escape_property(title)));
writeln!(
out,
"::{command} {}::{}",
"::{} {}::{}",
level.as_str(),
properties.join(","),
escape_data(&message)
escape_data(message)
)
}

/// The message of a workflow command, escaped as `@actions/core` does.
/// Control characters other than a newline or a tab, `\r` among them, are
/// replaced first, so nothing reaches the log that the runner or a terminal
/// would interpret.
fn escape_data(value: &str) -> String {
pub(crate) fn escape_data(value: &str) -> String {
text::terminal_safe(value)
.replace('%', "%25")
.replace('\n', "%0A")
}

/// A property value of a workflow command, where `:` and `,` also delimit.
fn escape_property(value: &str) -> String {
pub(crate) fn escape_property(value: &str) -> String {
escape_data(value).replace(':', "%3A").replace(',', "%2C")
}

Expand All @@ -93,4 +135,32 @@ mod tests {
assert_eq!(escape_property("a:b,c%\nd"), "a%3Ab%2Cc%25%0Ad");
assert_eq!(escape_data("ok\u{1b}[2J"), "ok\u{FFFD}[2J");
}

#[test]
fn write_job_escapes_title_and_message_onto_one_line() {
let mut out = Vec::new();
write_job(
&mut out,
Level::Error,
"Proofline deploy: prod:web,100%\r\n\u{1b}[31m",
"50% drop\n::add-mask::secret\u{202E}",
)
.unwrap();
write_job(
&mut out,
Level::Warning,
"Proofline deploy: LANDING",
"Not established.",
)
.unwrap();
let written = String::from_utf8(out).unwrap();
let lines: Vec<&str> = written.lines().collect();
assert_eq!(
lines,
[
"::error title=Proofline deploy%3A prod%3Aweb%2C100%25\u{FFFD}%0A\u{FFFD}[31m::50%25 drop%0A::add-mask::secret\u{FFFD}",
"::warning title=Proofline deploy%3A LANDING::Not established.",
]
);
}
}
Loading