diff --git a/src/put/checks.rs b/src/put/checks.rs index c0ae606..329484d 100644 --- a/src/put/checks.rs +++ b/src/put/checks.rs @@ -5,9 +5,10 @@ //! mistakes for plain `dput` uploads. //! //! Each guard is a pure [`Verdict`], so every decision is testable -//! without a terminal; [`run`] applies them in order, asking the prompter -//! for the questionable ones. `--force` skips the whole module (the -//! caller decides): it is the explicit "upload anyway" answer. +//! without a terminal; [`run`] evaluates all of them, warns for the +//! questionable ones, and asks a single generic question about uploading +//! with warnings. `--force` skips the whole module (the caller decides): +//! it is the explicit "upload anyway" answer. use std::path::Path; @@ -21,14 +22,11 @@ use super::changes::ChangesFile; enum Verdict { /// No mistake detected. Proceed, - /// The upload may proceed only if the user confirms; the prompter's - /// default answer is "no", so headless runs refuse. + /// The upload may proceed, but the reason is warned and counts for + /// the generic "upload with warnings?" question. Ask { - /// What looks wrong, logged before the question and reused as the - /// refusal reason. + /// What looks wrong. reason: String, - /// The yes/no question. - question: String, }, /// Refuse outright: the mistake is unambiguous. Refuse(String), @@ -37,12 +35,18 @@ enum Verdict { /// Run every guard. `tree` is the source directory the tree-level checks /// validate, `None` when the upload does not carry one next to it /// (an explicit `--changes` collected away from the tree). +/// +/// Nothing stops halfway: every reason is shown (warnings first, then +/// refusals), so one rebuild can fix everything found. A refusal stops +/// the upload; only warnings ask, once, generically. pub(super) async fn run( changes: &ChangesFile, tree: Option<&Path>, prompter: &dyn Prompter, ) -> Result<(), Box> { let target = target_series_release(&changes.distribution).await; + let mut warnings = Vec::new(); + let mut refusals = Vec::new(); for verdict in [ bad_author(changes), no_bug(changes), @@ -51,34 +55,36 @@ pub(super) async fn run( update_maintainer(changes), patches_in_native(changes, tree), ] { - apply(verdict, prompter)?; - } - Ok(()) -} - -/// Apply one guard's verdict: questions go to the prompter (whose default -/// answer refuses), refusals become upload errors. Both end in the same -/// `--force` hint, the single way past the guards. -fn apply(verdict: Verdict, prompter: &dyn Prompter) -> Result<(), Box> { - match verdict { - Verdict::Proceed => Ok(()), - Verdict::Refuse(reason) => Err(refusal(reason)), - Verdict::Ask { reason, question } => { - log::warn!("{reason}"); - if prompter.confirm(&question, false)? { - Ok(()) - } else { - // The reason was warned right above the question; the - // refusal only adds what happened and the way out - Err("Upload stopped. Pass --force to upload anyway.".into()) + match verdict { + Verdict::Proceed => {} + Verdict::Ask { reason } => { + log::warn!("{reason}"); + warnings.push(reason); + } + Verdict::Refuse(reason) => { + log::error!("{reason}"); + refusals.push(reason); } } } + + if !refusals.is_empty() { + return Err(upload_stopped()); + } + if warnings.is_empty() { + return Ok(()); + } + if prompter.confirm("Upload with warnings?", false)? { + Ok(()) + } else { + // The reasons were warned right above the question + Err(upload_stopped()) + } } -/// The error for a refused upload: the reason plus the way out. -fn refusal(reason: String) -> Box { - format!("{reason} Pass --force to upload anyway.").into() +/// The error stopping the upload, naming the single way past the guards. +fn upload_stopped() -> Box { + "Upload stopped. Pass --force to upload anyway.".into() } /// Stop uploads whose `Changed-By` is a `ubuntu@` or `root@` address: @@ -117,7 +123,6 @@ fn no_bug(changes: &ChangesFile) -> Verdict { reason: "The upload does not reference any Launchpad bug. Add \ 'LP: #NNN' to the changelog entry if it fixes a bug." .to_string(), - question: "Upload without Launchpad bug references?".to_string(), } } @@ -141,7 +146,6 @@ fn placeholder_bug(changes: &ChangesFile) -> Verdict { "The Launchpad bug references {} look like placeholder numbers.", flagged.join(", ") ), - question: "Upload with placeholder bug references?".to_string(), } } @@ -188,7 +192,6 @@ fn release_mismatch(changes: &ChangesFile, target: Option<&(String, String)>) -> codename, number ), - question: "Upload with a backport suffix not matching the target series?".to_string(), } } @@ -247,7 +250,6 @@ fn update_maintainer(changes: &ChangesFile) -> Verdict { modifications are owned by an Ubuntu maintainer.", changes.version ), - question: "Upload without an ubuntu.com maintainer?".to_string(), } } @@ -583,8 +585,7 @@ mod tests { ); } - /// A prompter answering yes to every question, recording what it was - /// asked. + /// A prompter answering yes to every question. struct ConfirmYes; impl Prompter for ConfirmYes { @@ -622,12 +623,14 @@ mod tests { #[tokio::test] async fn run_stops_machine_identities_before_asking() { - // A bad author is refused outright, with no question in between + // The root@ author is refused outright: no question is asked, not + // even with a yes-answering prompter (the no-bug warning fires on + // the same fixture, and is only logged) let changes = changes("1.0-1", Some("root "), None); let error = run(&changes, None, &ConfirmYes) .await .unwrap_err() .to_string(); - assert!(error.contains("root@"), "unexpected: {error}"); + assert_eq!(error, "Upload stopped. Pass --force to upload anyway."); } }