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

feat: add pre-release option #1228

Merged
merged 12 commits into from
Jan 25, 2024
17 changes: 16 additions & 1 deletion crates/release_plz/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,18 @@ impl From<PackageConfig> for release_plz_core::ReleaseConfig {
let is_publish_enabled = value.publish != Some(false);
let is_git_release_enabled = value.git_release_enable != Some(false);
let is_git_release_draft = value.git_release_draft == Some(true);
let git_release_type: release_plz_core::ReleaseType = value
.git_release_type
.map(|release_type| release_type.into())
.unwrap_or_default();
let is_git_tag_enabled = value.git_tag_enable != Some(false);
let release = value.release != Some(false);
let mut cfg = Self::default()
.with_publish(release_plz_core::PublishConfig::enabled(is_publish_enabled))
.with_git_release(
release_plz_core::GitReleaseConfig::enabled(is_git_release_enabled)
.set_draft(is_git_release_draft),
.set_draft(is_git_release_draft)
.set_release_type(git_release_type),
)
.with_git_tag(release_plz_core::GitTagConfig::enabled(is_git_tag_enabled))
.with_release(release);
Expand Down Expand Up @@ -310,6 +315,16 @@ pub enum ReleaseType {
Auto,
}

impl From<ReleaseType> for release_plz_core::ReleaseType {
fn from(value: ReleaseType) -> Self {
match value {
ReleaseType::Prod => Self::Prod,
ReleaseType::Pre => Self::Pre,
ReleaseType::Auto => Self::Auto,
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
74 changes: 67 additions & 7 deletions crates/release_plz_core/src/command/release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use std::{
};

use anyhow::Context;
use cargo_metadata::{Metadata, Package};
use cargo::util_semver::VersionExt;
use cargo_metadata::{semver::Version, Metadata, Package};
use crates_index::{GitIndex, SparseIndex};
use git_cmd::Repo;
use secrecy::{ExposeSecret, SecretString};
Expand Down Expand Up @@ -275,10 +276,19 @@ impl PublishConfig {
}
}

#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub enum ReleaseType {
#[default]
Prod,
Pre,
Auto,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GitReleaseConfig {
enabled: bool,
draft: bool,
release_type: ReleaseType,
}

impl Default for GitReleaseConfig {
Expand All @@ -292,6 +302,7 @@ impl GitReleaseConfig {
Self {
enabled,
draft: false,
release_type: ReleaseType::default(),
}
}

Expand All @@ -303,6 +314,19 @@ impl GitReleaseConfig {
self.draft = draft;
self
}

pub fn set_release_type(mut self, release_type: ReleaseType) -> Self {
self.release_type = release_type;
self
}

pub fn is_pre_release(&self, version: &Version) -> bool {
match self.release_type {
ReleaseType::Pre => true,
ReleaseType::Auto => version.is_prerelease(),
ReleaseType::Prod => false,
}
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -464,15 +488,13 @@ async fn release_package(
.as_ref()
.context("git release not configured. Did you specify git-token and backend?")?;
let release_body = release_body(input, package);
let is_release_draft = input
.get_package_config(&package.name)
.generic
.git_release
.draft;
let release_config = input.get_package_config(&package.name).generic.git_release;
let is_pre_release = release_config.is_pre_release(&package.version);
let release_info = GitReleaseInfo {
git_tag,
release_body,
draft: is_release_draft,
draft: release_config.draft,
pre_release: is_pre_release,
};
publish_git_release(&release_info, &git_release.backend).await?;
}
Expand All @@ -487,6 +509,7 @@ pub struct GitReleaseInfo {
pub git_tag: String,
pub release_body: String,
pub draft: bool,
pub pre_release: bool,
}

fn run_cargo_publish(
Expand Down Expand Up @@ -557,3 +580,40 @@ async fn publish_git_release(
.context("Failed to create release")?;
Ok(())
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn git_release_config_pre_release_default_works() {
let config = GitReleaseConfig::default();
let version = Version::parse("1.0.0").unwrap();
let rc_version = Version::parse("1.0.0-rc1").unwrap();

assert!(!config.is_pre_release(&version));
assert!(!config.is_pre_release(&rc_version));
}

#[test]
fn git_release_config_pre_release_auto_works() {
let mut config = GitReleaseConfig::default();
config = config.set_release_type(ReleaseType::Auto);
let version = Version::parse("1.0.0").unwrap();
let rc_version = Version::parse("1.0.0-rc1").unwrap();

assert!(!config.is_pre_release(&version));
assert!(config.is_pre_release(&rc_version));
}

#[test]
fn git_release_config_pre_release_pre_works() {
let mut config = GitReleaseConfig::default();
config = config.set_release_type(ReleaseType::Pre);
let version = Version::parse("1.0.0").unwrap();
let rc_version = Version::parse("1.0.0-rc1").unwrap();

assert!(config.is_pre_release(&version));
assert!(config.is_pre_release(&rc_version));
}
}
2 changes: 2 additions & 0 deletions crates/release_plz_core/src/git/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ pub struct CreateReleaseOption<'a> {
body: &'a str,
name: &'a str,
draft: &'a bool,
prerelease: &'a bool,
}

#[derive(Deserialize)]
Expand Down Expand Up @@ -193,6 +194,7 @@ impl GitClient {
body: &release_info.release_body,
name: &release_info.git_tag,
draft: &release_info.draft,
prerelease: &release_info.pre_release,
};
self.client
.post(format!("{}/releases", self.repo_url()))
Expand Down
23 changes: 23 additions & 0 deletions website/docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ the following sections:
- [`changelog_update`](#the-changelog_update-field) — Update changelog.
- [`dependencies_update`](#the-dependencies_update-field) — Update all dependencies.
- [`git_release_enable`](#the-git_release_enable-field) — Enable git release.
- [`git_release_type`](#the-git_release_type-field) — Publish mode for git release.
- [`git_release_draft`](#the-git_release_draft-field) — Publish git release as draft.
- [`git_tag_enable`](#the-git_tag_enable-field) — Enable git tag.
- [`pr_draft`](#the-pr_draft-field) — Open the release Pull Request as a draft.
Expand All @@ -72,6 +73,7 @@ the following sections:
- [`changelog_path`](#the-changelog_path-field-package-section) — Changelog path.
- [`changelog_update`](#the-changelog_update-field-package-section) — Update changelog.
- [`git_release_enable`](#the-git_release_enable-field-package-section) — Enable git release.
- [`git_release_type`](#the-git_release_type-field-package-section) — Git release type.
- [`git_release_draft`](#the-git_release_draft-field-package-section) — Publish git release as draft.
- [`git_tag_enable`](#the-git_tag_enable-field-package-section) — Enable git tag.
- [`publish`](#the-publish-field-package-section) — Publish to cargo registry.
Expand Down Expand Up @@ -142,11 +144,28 @@ The supported git releases are:
- [Gitea](https://docs.gitea.io/en-us/)
- [GitLab](https://docs.gitlab.com/ee/user/project/releases/#releases)

#### The `git_release_type` field

Define whether to label the release as production or non-production ready.
Supported values are:

- `"prod"`: will mark the release as ready for production. *(Default)*.
- `"pre"`: will mark the release as not ready for production (pre-release).
- `"auto"`:
- if there's a SemVer pre-release in the version (e.g. `v1.0.0-rc1`), will mark the release as
not ready for production (pre-release).
- if there isn't a semver pre-release in the version (e.g. `v1.0.0`), will mark the release as
ready for production.

*(GitHub, Gitea only)*.

#### The `git_release_draft` field

- If `true`, release-plz creates the git release as draft (unpublished).
- If `false`, release-plz publishes the created git release. *(Default)*.

*(GitHub, Gitea only)*.

#### The `git_tag_enable` field

- If `true`, release-plz creates a git tag for the new package version. *(Default)*.
Expand Down Expand Up @@ -333,6 +352,10 @@ This field cannot be set in the `[workspace]` section.

Overrides the [`workspace.git_release_enable`](#the-git_release_enable-field) field.

#### The `git_release_type` field (`package` section)

Overrides the [`workspace.git_release_type`](#the-git_release_type-field) field.

#### The `git_release_draft` field (`package` section)

Overrides the [`workspace.git_release_draft`](#the-git_release_draft-field) field.
Expand Down