put: one generic question for the upload warnings

The per-guard questions asked about one mistake at a time and, asked in
sequence, would have interrupted an upload several times for what is
really one decision. The guards now all run, every reason found is
shown before anything stops (one rebuild can fix everything at once),
refusals stop the upload, and only warnings ask, once, generically:
'Upload with warnings?'. Verdict::Ask loses its per-guard question
field.
This commit is contained in:
vhaudiquet committed 2026-11-17 12:50:49 +01:00
1 parent 94eeea4842
commit 8bfc9d5677
1 file changed
+43 -40
+43 -40
View File
@@ -5,9 +5,10 @@
//! mistakes for plain `dput` uploads. //! mistakes for plain `dput` uploads.
//! //!
//! Each guard is a pure [`Verdict`], so every decision is testable //! Each guard is a pure [`Verdict`], so every decision is testable
//! without a terminal; [`run`] applies them in order, asking the prompter //! without a terminal; [`run`] evaluates all of them, warns for the
//! for the questionable ones. `--force` skips the whole module (the //! questionable ones, and asks a single generic question about uploading
//! caller decides): it is the explicit "upload anyway" answer. //! with warnings. `--force` skips the whole module (the caller decides):
//! it is the explicit "upload anyway" answer.
use std::path::Path; use std::path::Path;
@@ -21,14 +22,11 @@ use super::changes::ChangesFile;
enum Verdict { enum Verdict {
/// No mistake detected. /// No mistake detected.
Proceed, Proceed,
/// The upload may proceed only if the user confirms; the prompter's /// The upload may proceed, but the reason is warned and counts for
/// default answer is "no", so headless runs refuse. /// the generic "upload with warnings?" question.
Ask { Ask {
/// What looks wrong, logged before the question and reused as the /// What looks wrong.
/// refusal reason.
reason: String, reason: String,
/// The yes/no question.
question: String,
}, },
/// Refuse outright: the mistake is unambiguous. /// Refuse outright: the mistake is unambiguous.
Refuse(String), Refuse(String),
@@ -37,12 +35,18 @@ enum Verdict {
/// Run every guard. `tree` is the source directory the tree-level checks /// Run every guard. `tree` is the source directory the tree-level checks
/// validate, `None` when the upload does not carry one next to it /// validate, `None` when the upload does not carry one next to it
/// (an explicit `--changes` collected away from the tree). /// (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( pub(super) async fn run(
changes: &ChangesFile, changes: &ChangesFile,
tree: Option<&Path>, tree: Option<&Path>,
prompter: &dyn Prompter, prompter: &dyn Prompter,
) -> Result<(), Box<dyn std::error::Error>> { ) -> Result<(), Box<dyn std::error::Error>> {
let target = target_series_release(&changes.distribution).await; let target = target_series_release(&changes.distribution).await;
let mut warnings = Vec::new();
let mut refusals = Vec::new();
for verdict in [ for verdict in [
bad_author(changes), bad_author(changes),
no_bug(changes), no_bug(changes),
@@ -51,34 +55,36 @@ pub(super) async fn run(
update_maintainer(changes), update_maintainer(changes),
patches_in_native(changes, tree), patches_in_native(changes, tree),
] { ] {
apply(verdict, prompter)?; match verdict {
} Verdict::Proceed => {}
Ok(()) Verdict::Ask { reason } => {
} log::warn!("{reason}");
warnings.push(reason);
/// Apply one guard's verdict: questions go to the prompter (whose default }
/// answer refuses), refusals become upload errors. Both end in the same Verdict::Refuse(reason) => {
/// `--force` hint, the single way past the guards. log::error!("{reason}");
fn apply(verdict: Verdict, prompter: &dyn Prompter) -> Result<(), Box<dyn std::error::Error>> { refusals.push(reason);
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())
} }
} }
} }
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. /// The error stopping the upload, naming the single way past the guards.
fn refusal(reason: String) -> Box<dyn std::error::Error> { fn upload_stopped() -> Box<dyn std::error::Error> {
format!("{reason} Pass --force to upload anyway.").into() "Upload stopped. Pass --force to upload anyway.".into()
} }
/// Stop uploads whose `Changed-By` is a `ubuntu@` or `root@` address: /// 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 \ reason: "The upload does not reference any Launchpad bug. Add \
'LP: #NNN' to the changelog entry if it fixes a bug." 'LP: #NNN' to the changelog entry if it fixes a bug."
.to_string(), .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.", "The Launchpad bug references {} look like placeholder numbers.",
flagged.join(", ") flagged.join(", ")
), ),
question: "Upload with placeholder bug references?".to_string(),
} }
} }
@@ -188,7 +192,6 @@ fn release_mismatch(changes: &ChangesFile, target: Option<&(String, String)>) ->
codename, codename,
number 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.", modifications are owned by an Ubuntu maintainer.",
changes.version 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 /// A prompter answering yes to every question.
/// asked.
struct ConfirmYes; struct ConfirmYes;
impl Prompter for ConfirmYes { impl Prompter for ConfirmYes {
@@ -622,12 +623,14 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn run_stops_machine_identities_before_asking() { 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 <root@localhost>"), None); let changes = changes("1.0-1", Some("root <root@localhost>"), None);
let error = run(&changes, None, &ConfirmYes) let error = run(&changes, None, &ConfirmYes)
.await .await
.unwrap_err() .unwrap_err()
.to_string(); .to_string();
assert!(error.contains("root@"), "unexpected: {error}"); assert_eq!(error, "Upload stopped. Pass --force to upload anyway.");
} }
} }