Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli/package/tag): Better messages when tagging an existing package #4938

Merged
merged 5 commits into from
Jul 29, 2024
Merged
Changes from 3 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
91 changes: 73 additions & 18 deletions lib/cli/src/commands/package/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@ use is_terminal::IsTerminal;
use std::{
path::{Path, PathBuf},
str::FromStr,
sync::OnceLock,
};
use wasmer_api::WasmerClient;
use wasmer_api::{types::PackageVersionWithPackage, WasmerClient};
use wasmer_config::package::{
Manifest, NamedPackageId, NamedPackageIdent, PackageBuilder, PackageHash, PackageIdent,
};

use super::PublishWait;

/// The package related to the NamedPackageId the user chose to tag.
static MAYBE_USER_PACKAGE: OnceLock<Option<PackageVersionWithPackage>> = OnceLock::new();
xdoardo marked this conversation as resolved.
Show resolved Hide resolved

/// Tag an existing package.
#[derive(Debug, clap::Parser)]
pub struct PackageTag {
Expand Down Expand Up @@ -425,6 +429,28 @@ impl PackageTag {
registry_version.clone()
};

// Must necessarily bump if there's a package with the chosen version with a different hash.
let must_bump = {
let maybe_pkg = wasmer_api::query::get_package_version(
client,
full_pkg_name.to_string(),
user_version.to_string(),
)
.await?;
let maybe_hash = maybe_pkg
.as_ref()
.and_then(|p| p.distribution_v3.pirita_sha256_hash.clone());

MAYBE_USER_PACKAGE.set(maybe_pkg).unwrap();

if let Some(hash) = maybe_hash {
let registry_package_hash = PackageHash::from_str(&format!("sha256:{hash}"))?;
registry_package_hash != self.package_hash
} else {
false
}
};

if user_version <= registry_version {
if self.bump {
user_version = registry_version.clone();
Expand All @@ -433,12 +459,34 @@ impl PackageTag {
let theme = ColorfulTheme::default();
let mut new_version = registry_version.clone();
new_version.patch += 1;
if Confirm::with_theme(&theme)
if must_bump {
eprintln!("{}: Registry already has version {user_version} of {full_pkg_name}, but with different contents.", "Warn".bold().yellow());
eprintln!(
"{}: Not bumping the version will make this action fail.",
"Warn".bold().yellow()
);
let res = Confirm::with_theme(&theme)
.with_prompt(format!(
"Update the version from {user_version} to {new_version}?"
))
.interact()?;
if res {
user_version = new_version.clone();
self.update_manifest_version(manifest_path, manifest, &user_version)
.await?;
} else {
anyhow::bail!("Aborted by the user")
}
} else {
let res = Confirm::with_theme(&theme)
.with_prompt(format!("Do you want to bump the package's version ({user_version} -> {new_version})?"))
.interact()? {
.interact()?;
if res {
user_version = new_version.clone();
self.update_manifest_version(manifest_path, manifest, &user_version).await?;
}
self.update_manifest_version(manifest_path, manifest, &user_version)
.await?;
}
}
}
}

Expand All @@ -450,7 +498,9 @@ impl PackageTag {
Some(v) => Ok(v),
None => {
if self.non_interactive {
anyhow::bail!("No package name specified: use --version <package_version>")
anyhow::bail!(
"No package version specified: use --version <package_version>"
)
} else {
let version = crate::utils::prompts::prompt_for_package_version(
"Enter the package version",
Expand Down Expand Up @@ -547,20 +597,25 @@ impl PackageTag {
return Ok(false);
}

if let Some(pkg) = wasmer_api::query::get_package_version(
client,
id.full_name.clone(),
id.version.to_string(),
)
.await?
{
if let Some(hash) = pkg.distribution_v3.pirita_sha256_hash {
let registry_package_hash = PackageHash::from_str(&format!("sha256:{hash}"))?;
if registry_package_hash == self.package_hash {
return Ok(false);
}
let hash = if let Some(pkg) = MAYBE_USER_PACKAGE.get().and_then(|p| p.clone()) {
pkg.distribution_v3.pirita_sha256_hash
} else {
wasmer_api::query::get_package_version(
client,
id.full_name.clone(),
id.version.to_string(),
)
.await?
.and_then(|p| p.distribution_v3.pirita_sha256_hash)
};

if let Some(hash) = hash {
let registry_package_hash = PackageHash::from_str(&format!("sha256:{hash}"))?;
if registry_package_hash == self.package_hash {
return Ok(false);
}
}

Ok(true)
}
}
Expand Down
Loading