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: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Native knowledge, fix and configuration commands send the selected organization

`proofline review --path src/main.rs` reviews only that file's local changes against the normal merge-base. The path is literal and repository-relative; the command refuses directories, globs, and `..`. Combine it with `--staged` to read the index, or `--include-untracked` to select a new, non-ignored file. The command preserves the real index and applies the usual secret preflight. You cannot combine `--path` with pushed-commit, range, or pull-request review.

`proofline reviews --repository-id UUID --cursor CURSOR` returns one page with its continuation metadata, and all flags are optional. `proofline routing REVIEW_ID` reads recorded routing. `proofline run --repo owner/name --pr 7 --run 2` reads one retained run, and `proofline history --repo owner/name --pr 7` reads all recorded runs. These commands can find the reviews your token can still read after a repository disconnects. With a clone URL or Git remote, they resolve the project on its registered host and read its runs by repository ID. A repository with no reviews in the organization exits nonzero. `proofline findings --pr 7` finds the pull request's newest review through the same route and counts only runs of the repository whose full name matches. None of these commands reads the organization's review list.
`proofline reviews --repository-id UUID --cursor CURSOR` returns one page with its continuation metadata, and all flags are optional. `proofline routing REVIEW_ID` reads recorded routing. `proofline run --repo owner/name --pr 7 --run 2` reads one retained run, and `proofline history --repo owner/name --pr 7` reads all recorded runs. These commands can find the reviews your token can still read after a repository disconnects. With a clone URL or Git remote, they resolve the project on its registered host and read its runs by repository ID. A repository with no reviews in the organization exits nonzero. `proofline findings --pr 7` finds the pull request's newest review through the same route and counts only runs of the repository whose full name matches. `proofline findings --review REVIEW_ID --head FULL_SHA` reads that review directly, with no checkout, and exits 1 without printing it unless the review's head is that commit. A search by `--head`, or by the checkout's HEAD, makes the same check on the review it finds. Letter case in a SHA does not matter. None of these commands reads the organization's review list.

`proofline rerun REVIEW_ID` requests the original immutable subject and waits, while `--no-wait` returns its link. Reruns require `reviews:rerun`. `proofline guidance --repo owner/name` requires `guidance:read`, and `proofline fix-prompt FINDING_ID` requires `findings:read`. History, guidance, and fix prompts print JSON; prompt text remains untrusted.

Expand Down
2 changes: 1 addition & 1 deletion src/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ 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)
let (_, detail, state) = read_review(api, requested.review_id, Some(deadline), false, None)
.await?
.context("Proofline did not answer a read of the review before the deadline")?;
anyhow::Ok((requested.review_id, detail, state))
Expand Down
107 changes: 77 additions & 30 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,14 +456,17 @@ struct ReviewArgs {
output: OutputArgs,
}

/// Which review to read. A pull request, a head commit and a review ID are
/// three ways to name one; only a review ID may be combined with a head, which
/// then states the commit that review must be of.
#[derive(Debug, Args)]
#[group(multiple = false)]
struct Selector {
/// Latest review of this pull request.
#[arg(long, value_name = "NUMBER")]
#[arg(long, value_name = "NUMBER", conflicts_with_all = ["head", "review"])]
pr: Option<u64>,
/// Latest review whose head is exactly this pushed commit. Defaults to
/// the checkout's HEAD.
/// Latest review whose head is exactly this pushed commit, in either
/// letter case. Defaults to the checkout's HEAD. With --review, the
/// commit that review must be of; nothing is printed for another head.
#[arg(long, value_name = "SHA")]
head: Option<String>,
/// This review.
Expand All @@ -479,7 +482,7 @@ struct FindingsArgs {
#[arg(long)]
wait: bool,
/// Export this retained pull-request review as SARIF 2.1.0.
#[arg(long, requires = "review", conflicts_with_all = ["json", "pr", "head"])]
#[arg(long, requires = "review", conflicts_with_all = ["json", "pr"])]
sarif: bool,
#[command(flatten)]
repository: RepositoryArgs,
Expand Down Expand Up @@ -1576,13 +1579,27 @@ async fn finish(
&args.output,
OutputFormat::from_json(args.output.json),
args.fail_on,
None,
)
.await
}

async fn findings(api: &Api, args: &FindingsArgs) -> anyhow::Result<Verdict> {
// The commit the review read must be of, when the caller named or implied
// one. A search by head is only a summary row; the detail read below must
// agree with it before anything is printed. A review ID or a pull request
// alone names no commit.
let mut head = None;
let review_id = match (&args.selector.review, args.selector.pr) {
(Some(id), _) => *id,
(Some(id), _) => {
head = args
.selector
.head
.as_deref()
.map(git::commit_sha)
.transpose()?;
*id
}
(None, Some(number)) => pull_request_review(api, &args.repository, number).await?,
(None, None) => {
let sha = match &args.selector.head {
Expand All @@ -1593,19 +1610,34 @@ async fn findings(api: &Api, args: &FindingsArgs) -> anyhow::Result<Verdict> {
resolve_repository(api, &args.repository, RepositoryRead::History).await?;
// An audit is a revision scan of this commit too, but its
// findings are in its report, so it never answers for the head.
latest(api, repository_id, &sha, |item| {
item.audit_mode.is_none() && item.head_commit_sha.as_deref() == Some(sha.as_str())
let found = latest(api, repository_id, &sha, |item| {
item.audit_mode.is_none()
&& item
.head_commit_sha
.as_deref()
.is_some_and(|head| head.eq_ignore_ascii_case(&sha))
})
.await?
.with_context(|| format!("no review of {name} has head commit {sha}; push it and open a pull request, or name one with --pr"))?
.with_context(|| format!("no review of {name} has head commit {sha}; push it and open a pull request, or name one with --pr"))?;
head = Some(sha);
found
}
};
let format = if args.sarif {
OutputFormat::Sarif
} else {
OutputFormat::from_json(args.output.json)
};
show(api, review_id, args.wait, &args.output, format, None).await
show(
api,
review_id,
args.wait,
&args.output,
format,
None,
head.as_deref(),
)
.await
}

#[derive(Debug, thiserror::Error)]
Expand Down Expand Up @@ -1633,20 +1665,26 @@ type ReviewRead = (serde_json::Value, ReviewDetail, Option<Outcome>);
/// last answer before the deadline decides. `None` means no read answered
/// before the deadline. Every read confirms the server's answer is of the
/// review requested, not a cache or routing mix-up that would otherwise let
/// a result for a different review decide this one's outcome.
/// a result for a different review decide this one's outcome. With `head`,
/// it also confirms the review is of that commit.
async fn read_review(
api: &Api,
review_id: Uuid,
deadline: Option<tokio::time::Instant>,
sarif: bool,
head: Option<&str>,
) -> anyhow::Result<Option<ReviewRead>> {
let deadline = match deadline {
Some(deadline) if tokio::time::Instant::now() < deadline => deadline,
_ => return read_review_once(api, review_id, sarif).await.map(Some),
_ => {
return read_review_once(api, review_id, sarif, head)
.await
.map(Some);
}
};
poll_until(
deadline,
|| read_review_once(api, review_id, sarif),
|| read_review_once(api, review_id, sarif, head),
|read: &ReviewRead| read.2.is_some(),
|read: &ReviewRead, delay| {
say(&format!(
Expand Down Expand Up @@ -1694,9 +1732,19 @@ where
}
}

async fn read_review_once(api: &Api, review_id: Uuid, sarif: bool) -> anyhow::Result<ReviewRead> {
async fn read_review_once(
api: &Api,
review_id: Uuid,
sarif: bool,
head: Option<&str>,
) -> anyhow::Result<ReviewRead> {
let (raw, detail) = api.review(review_id).await?;
outcome::same_review(&detail, review_id)?;
// Before the SARIF check and before anything is printed: a review of
// another head never reaches the output.
if let Some(head) = head {
outcome::same_head(&detail, head)?;
}
if sarif {
sarif::validate_subject(&detail, review_id)?;
}
Expand All @@ -1713,21 +1761,25 @@ fn say_not_established(api: &Api, review_id: Uuid, timeout_minutes: u64) {
}

/// Prints the review, after waiting for it to finish when `wait` is set.
/// With `head`, a review of any other head, or of none, is refused before
/// anything is printed.
async fn show(
api: &Api,
review_id: Uuid,
wait: bool,
output: &OutputArgs,
format: OutputFormat,
fail_on: Option<Severity>,
head: Option<&str>,
) -> anyhow::Result<Verdict> {
let deadline = if wait {
Some(poll_deadline(output.timeout)?)
} else {
None
};
let sarif = matches!(format, OutputFormat::Sarif);
let Some((raw, detail, state)) = read_review(api, review_id, deadline, sarif).await? else {
let Some((raw, detail, state)) = read_review(api, review_id, deadline, sarif, head).await?
else {
say_not_established(api, review_id, output.timeout);
return Ok(Verdict::Unavailable);
};
Expand Down Expand Up @@ -1863,19 +1915,7 @@ fn confirm_ci_subject(
text::one_line(&detail.kind)
);
}
if !detail
.head_commit_sha
.as_deref()
.is_some_and(|head| head.eq_ignore_ascii_case(commit))
{
bail!(
"proofline asked for the review of {commit}, but the server returned head commit {}",
detail
.head_commit_sha
.as_deref()
.map_or_else(|| "none".to_owned(), text::one_line)
);
}
outcome::same_head(detail, commit)?;
if let Some(detail_repository) = detail.repository_id
&& detail_repository != repository_id
{
Expand Down Expand Up @@ -1916,7 +1956,7 @@ async fn ci_review(
};
let sarif_path = args.sarif.as_deref();
let Some((raw, detail, state)) =
read_review(api, review_id, Some(deadline), sarif_path.is_some()).await?
read_review(api, review_id, Some(deadline), sarif_path.is_some(), None).await?
else {
say_not_established(api, review_id, args.timeout);
return Ok(Verdict::Unavailable);
Expand Down Expand Up @@ -2641,7 +2681,14 @@ mod tests {
// Reads start at 0, 5 and 15 seconds; the client's own request
// timeout is 30 seconds, so the deadline is what ends the third.
let deadline = tokio::time::Instant::now() + Duration::from_secs(40);
let read = read_review(&api, REQUESTED.parse().unwrap(), Some(deadline), false).await;
let read = read_review(
&api,
REQUESTED.parse().unwrap(),
Some(deadline),
false,
None,
)
.await;
ticker.abort();
let (raw, detail, state) = read.unwrap().expect("an answered read stands");
assert_eq!(*reads.lock().unwrap(), 3);
Expand Down
51 changes: 51 additions & 0 deletions src/outcome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,31 @@ pub(crate) fn same_review(detail: &ReviewDetail, requested: Uuid) -> Result<(),
}
}

/// A review was asked for by head commit, or read with `--head`, but the
/// server's own account of it names a different head or none. `returned` is
/// already safe to print on one line.
#[derive(Debug, thiserror::Error)]
#[error(
"proofline asked for the review of {requested}, but the server returned head commit {returned}"
)]
pub(crate) struct WrongHead {
pub(crate) requested: String,
pub(crate) returned: String,
}

/// Confirms `detail` is of the `requested` head commit. A hexadecimal SHA
/// names the same commit in either letter case, so case is not compared. A
/// review that records no head is not of any requested head.
pub(crate) fn same_head(detail: &ReviewDetail, requested: &str) -> Result<(), WrongHead> {
match detail.head_commit_sha.as_deref() {
Some(head) if head.eq_ignore_ascii_case(requested) => Ok(()),
returned => Err(WrongHead {
requested: requested.to_owned(),
returned: returned.map_or_else(|| "none".to_owned(), crate::text::one_line),
}),
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Outcome {
Completed,
Expand Down Expand Up @@ -343,6 +368,32 @@ mod tests {
}
}

#[test]
fn same_head_ignores_letter_case_and_refuses_another_or_no_head() {
let sha = "0123456789abcdef0123456789abcdef01234567";
let mut detail = review("COMPLETED", Vec::new(), 0);
detail.head_commit_sha = Some(sha.to_ascii_uppercase());
assert!(same_head(&detail, sha).is_ok());
detail.head_commit_sha = Some(sha.to_owned());
assert!(same_head(&detail, &sha.to_ascii_uppercase()).is_ok());

let other = "89abcdef0123456789abcdef0123456789abcdef";
detail.head_commit_sha = Some(format!("{other}\u{1b}[2J"));
let refused = same_head(&detail, sha).unwrap_err();
assert_eq!(refused.requested, sha);
assert_eq!(refused.returned, format!("{other}\u{FFFD}[2J"));

detail.head_commit_sha = None;
let refused = same_head(&detail, sha).unwrap_err();
assert_eq!(refused.returned, "none");
assert_eq!(
refused.to_string(),
format!(
"proofline asked for the review of {sha}, but the server returned head commit none"
)
);
}

fn finding(severity: &str, lifecycle_state: &str) -> Finding {
Finding {
id: Uuid::nil(),
Expand Down
1 change: 1 addition & 0 deletions src/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ pub(crate) async fn run(
&args.output,
OutputFormat::from_json(args.output.json),
None,
None,
)
.await
{
Expand Down
1 change: 1 addition & 0 deletions src/tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ pub(crate) async fn rerun(api: &Api, args: &RerunArgs) -> anyhow::Result<Verdict
&args.output,
OutputFormat::from_json(args.output.json),
None,
None,
)
.await
}
Expand Down
Loading