Skip to content

Commit

Permalink
Merge pull request #4938 from wasmerio/fix-deploy-named-bump
Browse files Browse the repository at this point in the history
fix(cli/package/tag): Better messages when tagging an existing package
  • Loading branch information
xdoardo authored Jul 29, 2024
2 parents 531d30d + 08f932e commit 803c0be
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 22 deletions.
1 change: 1 addition & 0 deletions lib/cli/src/commands/package/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub use common::wait::PublishWait;
// Allowing missing_docs because the comment would override the doc comment on
// the command struct.
#[allow(missing_docs)]
#[allow(clippy::large_enum_variant)]
pub enum Package {
Download(download::PackageDownload),
Build(build::PackageBuild),
Expand Down
1 change: 1 addition & 0 deletions lib/cli/src/commands/package/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ impl PackagePublish {
package_path: self.package_path.clone(),
package_hash,
package_id: None,
maybe_user_package: None,
}
.tag(
client,
Expand Down
99 changes: 77 additions & 22 deletions lib/cli/src/commands/package/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::{
path::{Path, PathBuf},
str::FromStr,
};
use wasmer_api::WasmerClient;
use wasmer_api::{types::PackageVersionWithPackage, WasmerClient};
use wasmer_config::package::{
Manifest, NamedPackageId, NamedPackageIdent, PackageBuilder, PackageHash, PackageIdent,
};
Expand Down Expand Up @@ -85,6 +85,10 @@ pub struct PackageTag {
value_enum
)]
pub wait: PublishWait,

#[clap(skip)]
/// The package related to the NamedPackageId the user chose to tag.
pub(crate) maybe_user_package: Option<PackageVersionWithPackage>,
}

impl PackageTag {
Expand Down Expand Up @@ -370,7 +374,7 @@ impl PackageTag {
}

async fn get_version(
&self,
&mut self,
client: &WasmerClient,
manifest: Option<&Manifest>,
manifest_path: Option<&Path>,
Expand Down Expand Up @@ -422,6 +426,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());

self.maybe_user_package = maybe_pkg;

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 @@ -430,12 +456,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!("Continue ({user_version} -> {new_version})?"))
.interact()?;
if res {
user_version = new_version.clone();
self.update_manifest_version(manifest_path, manifest, &user_version)
.await?;
} else {
anyhow::bail!(
"Refusing to map two different releases of {full_pkg_name} to the same version."
)
}
} 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 @@ -447,7 +495,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 All @@ -465,7 +515,7 @@ impl PackageTag {
}

async fn synthesize_id(
&self,
&mut self,
client: &WasmerClient,
manifest: Option<&Manifest>,
manifest_path: Option<&Path>,
Expand Down Expand Up @@ -501,7 +551,7 @@ impl PackageTag {
}

pub async fn tag(
&self,
&mut self,
client: &WasmerClient,
manifest: Option<&Manifest>,
manifest_path: Option<&Path>,
Expand Down Expand Up @@ -544,20 +594,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) = self.maybe_user_package.as_ref() {
pkg.distribution_v3.pirita_sha256_hash.clone()
} 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 All @@ -566,7 +621,7 @@ impl PackageTag {
impl AsyncCliCommand for PackageTag {
type Output = ();

async fn run_async(self) -> Result<Self::Output, anyhow::Error> {
async fn run_async(mut self) -> Result<Self::Output, anyhow::Error> {
tracing::info!("Checking if user is logged in");
let client = login_user(&self.env, !self.non_interactive, "tag a package").await?;

Expand Down

0 comments on commit 803c0be

Please sign in to comment.