Skip to content

Commit

Permalink
Add check to disable publishing unstable apis by default
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Bottriell <ryan@bottriell.ca>
  • Loading branch information
rydrman committed Feb 23, 2023
1 parent 36c5262 commit 16f012f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 11 deletions.
20 changes: 20 additions & 0 deletions crates/spk-cli/common/src/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct Publisher {
from: Arc<storage::RepositoryHandle>,
to: Arc<storage::RepositoryHandle>,
skip_source_packages: bool,
allow_unstable_api: bool,
force: bool,
}

Expand All @@ -42,6 +43,7 @@ impl Publisher {
from: source,
to: destination,
skip_source_packages: false,
allow_unstable_api: false,
force: false,
}
}
Expand All @@ -64,6 +66,12 @@ impl Publisher {
self
}

/// Allow publishing packages defined with an unstable api version
pub fn allow_unstable_api(mut self, allow_unstable_api: bool) -> Self {
self.allow_unstable_api = allow_unstable_api;
self

Check warning on line 72 in crates/spk-cli/common/src/publish.rs

View check run for this annotation

Codecov / codecov/patch

crates/spk-cli/common/src/publish.rs#L70-L72

Added lines #L70 - L72 were not covered by tests
}

/// Forcefully publishing a package will overwrite an existing publish if it exists.
pub fn force(mut self, force: bool) -> Self {
self.force = force;
Expand Down Expand Up @@ -99,6 +107,12 @@ impl Publisher {
}
Err(err) => return Err(err.into()),
Ok(recipe) => {
if !recipe.api_version().is_stable() && !self.allow_unstable_api {
return Err(Error::String(format!(
"Recipe has unstable api version, and allow unstable is not set: {:?}",
recipe.api_version()

Check warning on line 113 in crates/spk-cli/common/src/publish.rs

View check run for this annotation

Codecov / codecov/patch

crates/spk-cli/common/src/publish.rs#L111-L113

Added lines #L111 - L113 were not covered by tests
)));
}
tracing::info!("publishing recipe: {}", recipe.ident().format_ident());
if self.force {
self.to.force_publish_recipe(&recipe).await?;
Expand Down Expand Up @@ -148,6 +162,12 @@ impl Publisher {

tracing::debug!(" loading package: {}", build.format_ident());
let spec = self.from.read_package(build).await?;
if !spec.api_version().is_stable() && !self.allow_unstable_api {
return Err(Error::String(format!(
"Package build has unstable api version, and allow unstable is not set: {:?}",
spec.api_version()

Check warning on line 168 in crates/spk-cli/common/src/publish.rs

View check run for this annotation

Codecov / codecov/patch

crates/spk-cli/common/src/publish.rs#L166-L168

Added lines #L166 - L168 were not covered by tests
)));
}
let components = self.from.read_components(build).await?;
tracing::info!("publishing package: {}", spec.ident().format_ident());
let env_spec = components.values().cloned().collect();
Expand Down
7 changes: 7 additions & 0 deletions crates/spk-cli/group2/src/cmd_publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ pub struct Publish {
#[clap(long, short)]
force: bool,

/// Allow publishing package specs written in unstable api versions
///
/// The api version of a spec is the value of the `api` field within it.
#[clap(long)]
allow_unstable_api: bool,

/// The local packages to publish
///
/// This can be an entire package version with all builds or a
Expand All @@ -51,6 +57,7 @@ impl Run for Publish {

let publisher = Publisher::new(Arc::new(source.into()), Arc::new(target.into()))
.skip_source_packages(self.no_source)
.allow_unstable_api(self.allow_unstable_api)

Check warning on line 60 in crates/spk-cli/group2/src/cmd_publish.rs

View check run for this annotation

Codecov / codecov/patch

crates/spk-cli/group2/src/cmd_publish.rs#L60

Added line #L60 was not covered by tests
.force(self.force);

let mut published = Vec::new();
Expand Down
52 changes: 41 additions & 11 deletions crates/spk-schema/src/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,14 @@ pub enum SpecRecipe {
V0Package(super::v0::Spec<VersionIdent>),
}

impl SpecRecipe {
pub fn api_version(&self) -> RecipeApiVersion {
match self {
Self::V0Package(_) => RecipeApiVersion::V0Package,
}
}
}

impl Recipe for SpecRecipe {
type Output = Spec;

Expand Down Expand Up @@ -279,8 +287,8 @@ impl FromYaml for SpecRecipe {
// fairly generic, eg: 'expected struct YamlMapping'
#[derive(Deserialize)]
struct YamlMapping {
#[serde(default = "ApiVersion::default")]
api: ApiVersion,
#[serde(default = "RecipeApiVersion::default")]
api: RecipeApiVersion,
}

let with_version = match serde_yaml::from_str::<YamlMapping>(&yaml) {
Expand All @@ -292,7 +300,7 @@ impl FromYaml for SpecRecipe {
};

match with_version.api {
ApiVersion::V0Package => {
RecipeApiVersion::V0Package => {

Check warning on line 303 in crates/spk-schema/src/spec.rs

View check run for this annotation

Codecov / codecov/patch

crates/spk-schema/src/spec.rs#L303

Added line #L303 was not covered by tests
let inner =
serde_yaml::from_str(&yaml).map_err(|err| SerdeError::new(yaml, err))?;
Ok(Self::V0Package(inner))
Expand All @@ -313,6 +321,14 @@ pub enum Spec {
V0Package(super::v0::Spec<BuildIdent>),
}

impl Spec {
pub fn api_version(&self) -> PackageApiVersion {
match self {
Self::V0Package(_) => PackageApiVersion::V0Package,
}
}
}

impl Satisfy<PkgRequest> for Spec {
fn check_satisfies_request(&self, request: &PkgRequest) -> Compatibility {
match self {
Expand Down Expand Up @@ -452,8 +468,8 @@ impl FromYaml for Spec {
// fairly generic, eg: 'expected struct YamlMapping'
#[derive(Deserialize)]
struct YamlMapping {
#[serde(default = "ApiVersion::default")]
api: ApiVersion,
#[serde(default = "PackageApiVersion::default")]
api: PackageApiVersion,
}

let with_version = match serde_yaml::from_str::<YamlMapping>(&yaml) {
Expand All @@ -465,7 +481,7 @@ impl FromYaml for Spec {
};

match with_version.api {
ApiVersion::V0Package => {
PackageApiVersion::V0Package => {

Check warning on line 484 in crates/spk-schema/src/spec.rs

View check run for this annotation

Codecov / codecov/patch

crates/spk-schema/src/spec.rs#L484

Added line #L484 was not covered by tests
let inner =
serde_yaml::from_str(&yaml).map_err(|err| SerdeError::new(yaml, err))?;
Ok(Self::V0Package(inner))
Expand All @@ -480,14 +496,28 @@ impl AsRef<Spec> for Spec {
}
}

#[derive(Deserialize, Serialize, Copy, Clone)]
pub enum ApiVersion {
#[derive(Default, Debug, Deserialize, Serialize, Copy, Clone)]
pub enum RecipeApiVersion {
#[default]
#[serde(rename = "v0/package")]
V0Package,
}

impl RecipeApiVersion {
pub fn is_stable(&self) -> bool {
matches!(self, Self::V0Package)
}
}

#[derive(Default, Debug, Deserialize, Serialize, Copy, Clone)]
pub enum PackageApiVersion {
#[default]
#[serde(rename = "v0/package")]
V0Package,
}

impl Default for ApiVersion {
fn default() -> Self {
Self::V0Package
impl PackageApiVersion {
pub fn is_stable(&self) -> bool {
matches!(self, Self::V0Package)
}
}

0 comments on commit 16f012f

Please sign in to comment.