diff --git a/src/lib.rs b/src/lib.rs index a134b55..562870e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -219,6 +219,16 @@ struct ReviewArgs { conflicts_with_all = ["pr", "staged", "include_untracked", "allow_path"] )] range: Option, + /// Re-verify these open findings of one pull request against the local + /// changes, and print each one's outcome. The outcome is local: the pull + /// request's findings are unchanged. Needs an API token. + #[arg( + long, + value_name = "FINDING_ID", + num_args = 1.., + conflicts_with_all = ["pr", "commit", "range"] + )] + verify: Vec, /// Request a fresh full review of the pull request after settlement. An /// active review is reused with its recorded scope unchanged. #[arg(long, requires = "pr")] @@ -654,6 +664,12 @@ async fn review_local(api: &Api, args: &ReviewArgs) -> anyhow::Result { ) .await?; let files = changes.files(); + if files == 0 && !args.verify.is_empty() { + bail!( + "nothing to re-verify against: the selected local changes match {}; --verify re-verifies findings against uncommitted changes", + changes.base_commit_sha + ); + } if files == 0 { say(&format!( "Nothing to review: the selected local changes match {}.", @@ -667,7 +683,7 @@ async fn review_local(api: &Api, args: &ReviewArgs) -> anyhow::Result { base_commit_sha: changes.base_commit_sha, patch: changes.patch, trigger_source: None, - verify_finding_ids: Vec::new(), + verify_finding_ids: args.verify.clone(), }; let requested = api.request_patch_review(repository_id, &request).await?; say(&format!( @@ -680,6 +696,12 @@ async fn review_local(api: &Api, args: &ReviewArgs) -> anyhow::Result { if files == 1 { "file" } else { "files" }, request.base_commit_sha )); + for verifying in &requested.verify { + say(&format!( + "Re-verifying finding {} against the local changes (request {} of 3).", + verifying.finding_id, verifying.iteration + )); + } finish(api, requested.review_id, args).await } diff --git a/src/model.rs b/src/model.rs index 41726b5..bfca8d7 100644 --- a/src/model.rs +++ b/src/model.rs @@ -64,6 +64,24 @@ pub struct ReviewDetail { pub run_number: Option, #[serde(default)] pub subjects: Vec, + /// The findings a `--verify` patch review re-verified locally. Absent on + /// every other review. + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub local_reverifications: Vec, +} + +/// One finding a patch review re-verified against the local changes. The +/// outcome is local: the pull request's finding is unchanged. +#[derive(Debug, Deserialize, Serialize)] +pub struct LocalReverification { + pub finding_id: Uuid, + pub iteration: i32, + /// `RESOLVED_VERIFIED`, `STILL_PRESENT`, `NOT_VERIFIED` or `PENDING`. + pub outcome: String, + /// Each check the finding cited and its result on the patched head, kept + /// as the API returned it for `--json`. + #[serde(default)] + pub checks: serde_json::Value, } #[derive(Debug, Deserialize, Serialize)] @@ -246,7 +264,9 @@ pub struct PatchReviewRequest { /// that predates the field still accepts a plain review. #[serde(default, skip_serializing_if = "Option::is_none")] pub trigger_source: Option, - /// Open findings to re-verify against the patch. Omitted for a plain review. + /// Open findings of one pull request to re-verify against the patch. + /// Omitted when empty, so a server that predates the field still accepts + /// a plain review. #[serde(default, skip_serializing_if = "Vec::is_empty")] pub verify_finding_ids: Vec, } @@ -264,6 +284,16 @@ pub struct PatchReviewResponse { pub review_id: Uuid, pub existing: bool, pub patch_digest: String, + #[serde(default)] + pub verify: Vec, +} + +/// One finding a patch review re-verifies, and the request's number among +/// those for its series on this patch. +#[derive(Debug, Deserialize, Serialize)] +pub struct PatchVerification { + pub finding_id: Uuid, + pub iteration: i32, } /// Complete selected files at one exact pushed commit. diff --git a/src/outcome.rs b/src/outcome.rs index bc906ff..c5f87ec 100644 --- a/src/outcome.rs +++ b/src/outcome.rs @@ -204,6 +204,7 @@ mod tests { pull_request_number: Some(7), repository_full_name: Some("acme/web".to_owned()), run_number: Some(1), + local_reverifications: Vec::new(), } } diff --git a/src/render.rs b/src/render.rs index c7a7d18..4c4578b 100644 --- a/src/render.rs +++ b/src/render.rs @@ -161,6 +161,21 @@ pub fn review(review: &ReviewDetail, state: Option, url: &Url) -> Strin out.push(String::new()); assessment(&mut out, item); } + if !review.local_reverifications.is_empty() { + out.push(String::new()); + out.push( + "Re-verified against the local changes (the pull request's findings are unchanged):" + .to_owned(), + ); + for item in &review.local_reverifications { + out.push(format!( + " {} {} (request {} of 3)", + item.finding_id, + one_line(&item.outcome), + item.iteration + )); + } + } out.push(String::new()); out.join("\n") } @@ -199,6 +214,7 @@ mod tests { pull_request_number: Some(42), repository_full_name: Some("acme/web".to_owned()), run_number: Some(2), + local_reverifications: Vec::new(), } } diff --git a/tests/local_patch.rs b/tests/local_patch.rs index 538e023..6704439 100644 --- a/tests/local_patch.rs +++ b/tests/local_patch.rs @@ -18,6 +18,7 @@ use tokio::sync::oneshot; const ORG: &str = "0190f3a0-0000-7000-8000-000000000001"; const REPOSITORY: &str = "0190f3a0-0000-7000-8000-000000000022"; const REVIEW: &str = "0190f3a0-0000-7000-8000-000000000033"; +const FINDING: &str = "0190f3a0-0000-7000-8000-000000000044"; const TOKEN: &str = "local-patch-test-token"; #[derive(Clone)] @@ -60,11 +61,52 @@ async fn upload( ) -> Json { assert!(fixture.authorized); assert_eq!(headers[header::AUTHORIZATION], format!("Bearer {TOKEN}")); + let verify: Vec = body["verify_finding_ids"] + .as_array() + .into_iter() + .flatten() + .map(|id| json!({ "finding_id": id, "iteration": 1 })) + .collect(); fixture.uploads.lock().unwrap().push(body); Json(json!({ "review_id": REVIEW, "existing": false, "patch_digest": "a".repeat(64), + "verify": verify, + })) +} + +/// A settled patch review that re-verified `FINDING` against the patch. +async fn settled_review(headers: HeaderMap) -> Json { + assert_eq!(headers[header::AUTHORIZATION], format!("Bearer {TOKEN}")); + Json(json!({ + "id": REVIEW, + "kind": "PATCH", + "status": "COMPLETED", + "assessment_count": 1, + "assessments": [{ + "id": REVIEW, + "environment_display_name": "production", + "status": "COMPLETED", + "findings": [], + "checks": { "receipt_recorded": true, "status": "READY", "families": [] }, + "incompleteness_kind": null, + "reason_codes": null, + "release_evidence_gap": null, + }], + "code_assessment": null, + "base_commit_sha": "b".repeat(40), + "head_commit_sha": "c".repeat(40), + "open_items": null, + "pull_request_number": null, + "repository_full_name": "acme/web", + "run_number": null, + "local_reverifications": [{ + "finding_id": FINDING, + "iteration": 1, + "outcome": "RESOLVED_VERIFIED", + "checks": [], + }], })) } @@ -122,6 +164,10 @@ impl Server { &format!("/api/v1/organizations/{ORG}/repositories/{REPOSITORY}/file-reviews"), post(file_review), ) + .route( + &format!("/api/v1/organizations/{ORG}/reviews/{REVIEW}"), + get(settled_review), + ) .fallback(|| async { StatusCode::NOT_FOUND }) .with_state(fixture.clone()) .layer(middleware::from_fn_with_state(fixture.clone(), observe)); @@ -239,6 +285,12 @@ impl Checkout { } async fn review(&self, server: &Server, allowed: &[&str], flags: &[&str]) -> Output { + let mut flags = flags.to_vec(); + flags.push("--no-wait"); + self.review_and_wait(server, allowed, &flags).await + } + + async fn review_and_wait(&self, server: &Server, allowed: &[&str], flags: &[&str]) -> Output { let mut command = Command::new(env!("CARGO_BIN_EXE_proofline")); command .current_dir(self.root()) @@ -251,7 +303,6 @@ impl Checkout { "review", "--repo", "acme/web", - "--no-wait", ]) .args(flags); self.git_environment(&mut command); @@ -340,6 +391,35 @@ async fn asynchronous_patch_acknowledgment_respects_json_without_polling() { } } +/// `--verify` sends the named findings with the local patch and, once the +/// review settles, prints each one's local outcome. +#[tokio::test] +async fn verify_sends_the_findings_and_prints_their_local_outcomes() { + let checkout = Checkout::new(&[("kept.txt", "base\n")]).await; + checkout.write("kept.txt", "fixed\n").await; + let server = Server::start(true).await; + let output = checkout + .review_and_wait(&server, &[], &["--verify", FINDING]) + .await; + let stderr = String::from_utf8_lossy(&output.stderr); + assert_eq!(output.status.code(), Some(0), "{stderr}"); + let uploads = server.fixture.uploads.lock().unwrap().clone(); + assert_eq!(uploads.len(), 1); + assert_eq!(uploads[0]["verify_finding_ids"], json!([FINDING])); + assert!( + stderr.contains(&format!( + "Re-verifying finding {FINDING} against the local changes (request 1 of 3)." + )), + "{stderr}" + ); + let stdout = String::from_utf8(output.stdout).unwrap(); + assert!( + stdout.contains(&format!(" {FINDING} RESOLVED_VERIFIED (request 1 of 3)")), + "{stdout}" + ); + server.close().await; +} + #[tokio::test] async fn local_selection_uploads_only_selected_bytes_and_refuses_before_http() { let github = format!("ghp_{}", "S".repeat(36)); @@ -362,6 +442,7 @@ async fn local_selection_uploads_only_selected_bytes_and_refuses_before_http() { vec!["--staged", "--include-untracked"], vec!["--staged", "--pr", "7"], vec!["--include-untracked", "--pr", "7"], + vec!["--verify", FINDING, "--pr", "7"], ] { let output = checkout.review(&server, &[], &flags).await; assert_eq!(output.status.code(), Some(1));