From d6381b85ece11bd317f695f8eaf2fdab072a0ca2 Mon Sep 17 00:00:00 2001 From: Max Sergeev Date: Mon, 30 Nov 2026 20:41:14 -0300 Subject: [PATCH] Add --fail-on to proofline rerun --- README.md | 2 +- src/lib.rs | 31 ++++++++++++++++ src/tools.rs | 46 +++++++++++++++++++++-- tests/http.rs | 101 ++++++++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 172 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 6a124c3..b60b5e4 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ Native knowledge, plan, fix and configuration commands send the selected organiz `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 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. +`proofline rerun REVIEW_ID` requests the original immutable subject and waits, while `--no-wait` returns its link. `proofline rerun REVIEW_ID --fail-on serious` exits 3 when the completed rerun has an open finding at or above the threshold, counted from the server's open items as for `review --pr`. It first reads the original review, and exits 4 without printing the rerun when the server reports the new run at another head or repository. `--fail-on` cannot be combined with `--no-wait`. 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. Repository commands use the checkout’s `origin` by default. Use `--remote NAME` to select another remote or `--repo URL` to supply a clone URL. The CLI sends the host and full project path to Proofline, including subgroup names. The CLI recognizes gitlab.com and otherwise looks for a GitHub repository on the exact remote host, including GitHub Enterprise Server. Use `--source-provider gitlab` for private GitLab hosts or `--source-provider git` for other Git servers. Hooks can read the same choice from `PROOFLINE_SOURCE_PROVIDER`. A connected private Git repository can use `proofline review --source-provider git --range BASE..HEAD` to request a review of two pushed commits. diff --git a/src/lib.rs b/src/lib.rs index 8bf7a9e..b2e5996 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1430,6 +1430,28 @@ async fn show( output: &OutputArgs, format: OutputFormat, fail_on: Option, +) -> anyhow::Result { + show_subject(api, review_id, wait, output, format, fail_on, None).await +} + +/// The pull-request head and repository a review must be of, as +/// [`confirm_ci_subject`] checks them. +pub(crate) struct Subject { + pub(crate) commit: String, + pub(crate) repository_id: Uuid, +} + +/// [`show`], refusing to report a review that is not of `subject`. A review +/// of another subject prints nothing and is not established, so it can never +/// read as a clean result. +async fn show_subject( + api: &Api, + review_id: Uuid, + wait: bool, + output: &OutputArgs, + format: OutputFormat, + fail_on: Option, + subject: Option<&Subject>, ) -> anyhow::Result { let deadline = if wait { Some(poll_deadline(output.timeout)?) @@ -1441,6 +1463,15 @@ async fn show( say_not_established(api, review_id, output.timeout); return Ok(Verdict::Unavailable); }; + if let Some(subject) = subject + && let Err(error) = confirm_ci_subject(&detail, &subject.commit, subject.repository_id) + { + say(&format!( + "{error:#}, so the review at {} is not measured. It is not a pass.", + api.review_url(review_id) + )); + return Ok(Verdict::Unavailable); + } // `audit_mode` marks an audit, as on a listed review (`ReviewSummary`). if raw.get("audit_mode").is_some_and(|mode| !mode.is_null()) { print_audit(api, raw, &detail, format)?; diff --git a/src/tools.rs b/src/tools.rs index a3602f7..8dde961 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -5,7 +5,11 @@ use anyhow::{Context as _, bail}; use clap::{Args, Subcommand, ValueEnum}; use uuid::Uuid; -use crate::{OutputArgs, OutputFormat, RepositoryArgs, Verdict, api::Api, say, show}; +use crate::severity::Severity; +use crate::{ + OutputArgs, OutputFormat, RepositoryArgs, Subject, Verdict, api::Api, outcome, say, + show_subject, +}; #[derive(Debug, Args)] pub(crate) struct RerunArgs { @@ -13,6 +17,11 @@ pub(crate) struct RerunArgs { /// Return the new review acknowledgment without polling. #[arg(long)] no_wait: bool, + /// Exit 3 when the completed rerun has an open finding at or above this + /// severity, as `review --fail-on` does. The rerun is measured only when + /// it is of the original review's pull request head and repository. + #[arg(long, value_enum, value_name = "SEVERITY", conflicts_with = "no_wait")] + fail_on: Option, #[command(flatten)] output: OutputArgs, } @@ -93,6 +102,12 @@ pub(crate) fn json(value: &serde_json::Value) -> anyhow::Result { } pub(crate) async fn rerun(api: &Api, args: &RerunArgs) -> anyhow::Result { + // Read before the rerun is requested, so a subject that cannot be + // measured costs no run. + let subject = match args.fail_on { + Some(_) => Some(rerun_subject(api, args.review_id).await?), + None => None, + }; let reply = api.rerun(args.review_id).await?; let id: Uuid = reply .get("review_id") @@ -106,17 +121,42 @@ pub(crate) async fn rerun(api: &Api, args: &RerunArgs) -> anyhow::Result anyhow::Result { + let (_, original) = api.review(review_id).await?; + outcome::same_review(&original, review_id)?; + if original.kind != "PULL_REQUEST" { + bail!( + "--fail-on measures a rerun of a pull-request review, but review {review_id} is a {} review", + crate::text::one_line(&original.kind) + ); + } + let (Some(commit), Some(repository_id)) = (original.head_commit_sha, original.repository_id) + else { + bail!( + "review {review_id} names no head commit or repository, so --fail-on could not measure its rerun" + ); + }; + Ok(Subject { + commit, + repository_id, + }) +} + pub(crate) async fn export(api: &Api, args: &ExportArgs) -> anyhow::Result { if args.group_by.is_some() && !matches!(args.dataset, Dataset::FindingOutcomes) { bail!("--group-by is available only for finding_outcomes"); diff --git a/tests/http.rs b/tests/http.rs index eb15b98..f1e8d3b 100644 --- a/tests/http.rs +++ b/tests/http.rs @@ -1368,6 +1368,11 @@ async fn findings_of_a_departed_repository_still_resolve_through_its_reviews() { const CI_REPOSITORY: &str = "0190f3a0-0000-7000-8000-0000000000f0"; const CI_REVIEW: &str = "0190f3a0-0000-7000-8000-0000000000f1"; const CI_HEAD: &str = "89abcdef0123456789abcdef0123456789abcdef"; +/// The earlier review of `CI_HEAD` that `ci_console` reruns as `CI_REVIEW`. +const CI_RERUN_SOURCE: &str = "0190f3a0-0000-7000-8000-0000000000f6"; +/// An earlier review of another head whose rerun `ci_console` also answers +/// with `CI_REVIEW`, as a server that mixed up subjects would. +const CI_RERUN_OTHER_HEAD: &str = "0190f3a0-0000-7000-8000-0000000000f7"; #[derive(Debug, Clone, Copy)] struct CiCodeFinding { @@ -1387,9 +1392,26 @@ struct CiCodeFinding { /// finding and SERIOUS otherwise, and the review counts `open` as its open /// and serious findings. As on the server, the counts hold both lanes under /// their decisions: `[0, 0]` beside a listed finding means a person dismissed -/// it. It also accepts a review request for pull request 12, which answers -/// with `CI_REVIEW`. Every request path is recorded, so a test can tell that +/// it. It also accepts a review request for pull request 12 and reruns of +/// `CI_RERUN_SOURCE` and `CI_RERUN_OTHER_HEAD`, which all answer with +/// `CI_REVIEW`. Every request path is recorded, so a test can tell that /// nothing was requested. +/// A completed earlier run of pull request 12 at `head`, the review a rerun +/// repeats. +fn rerun_source(id: &str, head: &str) -> Value { + json!({ + "id": id, + "kind": "PULL_REQUEST", + "status": "COMPLETED", + "head_commit_sha": head, + "pull_request_number": 12, + "repository_id": CI_REPOSITORY, + "run_number": 1, + "assessment_count": 0, + "assessments": [], + }) +} + async fn ci_console( kind: &'static str, status: StatusCode, @@ -1490,6 +1512,27 @@ async fn ci_console( })) }), ) + .route( + &format!("/api/v1/organizations/{ORG}/reviews/{CI_RERUN_SOURCE}/rerun"), + post(|| async { Json(json!({ "review_id": CI_REVIEW })) }), + ) + .route( + &format!("/api/v1/organizations/{ORG}/reviews/{CI_RERUN_OTHER_HEAD}/rerun"), + post(|| async { Json(json!({ "review_id": CI_REVIEW })) }), + ) + .route( + &format!("/api/v1/organizations/{ORG}/reviews/{CI_RERUN_SOURCE}"), + get(|| async { Json(rerun_source(CI_RERUN_SOURCE, CI_HEAD)) }), + ) + .route( + &format!("/api/v1/organizations/{ORG}/reviews/{CI_RERUN_OTHER_HEAD}"), + get(|| async { + Json(rerun_source( + CI_RERUN_OTHER_HEAD, + "fedcba9876543210fedcba9876543210fedcba98", + )) + }), + ) .route( &format!("/api/v1/organizations/{ORG}/reviews/{CI_REVIEW}"), get(move || { @@ -1971,10 +2014,12 @@ async fn ci_review_fail_on_exits_three_for_an_open_finding_of_a_complete_review( code_finding, ) .await; - // `review --pr` measures a pull-request review as `ci review` does. - let commands: [&[&str]; 2] = [ + // `review --pr` measures a pull-request review as `ci review` does, + // and `rerun` measures the review it starts the same way. + let commands: [&[&str]; 3] = [ &["ci", "review", "--repo", "acme/app", "--commit", CI_HEAD], &["review", "--repo", "acme/app", "--pr", "12"], + &["rerun", CI_RERUN_SOURCE], ]; for (command, (fail_on, code)) in commands.into_iter().flat_map(|command| { [None, Some("warning"), Some("serious")] @@ -2028,6 +2073,54 @@ async fn ci_review_fail_on_exits_three_for_an_open_finding_of_a_complete_review( assert_eq!(output.status.code(), Some(1)); assert!(String::from_utf8_lossy(&output.stderr).contains("warning, serious")); assert!(requests.lock().unwrap().is_empty()); + // A rerun that returns before its result has nothing to measure, so + // `--fail-on` with `--no-wait` is a usage error and nothing is rerun. + let output = cli( + &origin, + &[ + "rerun", + CI_RERUN_SOURCE, + "--no-wait", + "--fail-on", + "serious", + ], + ) + .await; + assert_eq!(output.status.code(), Some(1)); + assert!(String::from_utf8_lossy(&output.stderr).contains("--no-wait")); + assert!(requests.lock().unwrap().is_empty()); + // A rerun the server reports at another head than the review it repeats + // is not measured: nothing is printed and it exits 4, never 0, whatever + // its findings. + let output = cli( + &origin, + &[ + "rerun", + CI_RERUN_OTHER_HEAD, + "--fail-on", + "serious", + "--json", + ], + ) + .await; + let stderr = String::from_utf8_lossy(&output.stderr); + assert_eq!(output.status.code(), Some(4), "{stderr}"); + assert!(stderr.contains("returned head commit"), "{stderr}"); + assert!(stderr.contains("It is not a pass"), "{stderr}"); + assert!(output.stdout.is_empty()); + // The original is read before the one rerun is requested. + let requests = requests.lock().unwrap(); + let rerun = format!("/reviews/{CI_RERUN_OTHER_HEAD}"); + assert_eq!( + requests + .iter() + .filter(|path| path.contains(&rerun)) + .collect::>(), + [ + &format!("GET /api/v1/organizations/{ORG}{rerun}"), + &format!("POST /api/v1/organizations/{ORG}{rerun}/rerun"), + ], + ); } /// A console whose search page names `CI_REVIEW` as the pull-request review of