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: prerelease_label can be set at runtime with --prerelease-label or KNOPE_PRERELEASE_LABEL. #247

Merged
merged 2 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 1 addition & 2 deletions .github/workflows/prerelease.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ jobs:
git_user_signingkey: true
git_commit_gpgsign: true
git_push_gpgsign: false
- run: |
cargo run -- prerelease
- run: cargo run -- release --prerelease-label=rc
env:
GITHUB_TOKEN: ${{ secrets.PAT }}
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ platform-dirs = "0.3.0"
git-conventional = "0.12.0"
ureq = { version = "2.5.0", features = ["json"] }
http = "0.2.8"
clap = { version = "3.2.17", features = ["cargo", "derive"] }
clap = { version = "3.2.17", features = ["cargo", "derive", "env"] }
itertools = "0.10.3"
miette = { version = "5.3.0", features = ["fancy"] }
thiserror = "1.0.32"
Expand Down
2 changes: 2 additions & 0 deletions docs/src/config/step/PrepareRelease.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type = "PrepareRelease"
prerelease_label = "rc"
```

If your prerelease workflow is exactly like your release workflow, you can instead temporarily add a prerelease label by passing the `--prerelease-label` option to `knope` or by setting the `KNOPE_PRERELEASE_LABEL` environment variable. This option overrides any set `prerelease_label` for any workflow run.

### Going from Pre-release to Full Release

Let's say that in addition to the configuration from the above example, you also have a section like this:
Expand Down
10 changes: 10 additions & 0 deletions docs/src/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ There are a few options you can pass to `knope` to control how it behaves.
3. `--generate` will generate a `knope.toml` file in the current directory.
4. `--validate` will check your `knope.toml` to make sure every workflow in it is valid, then exit. This could be useful to run in CI to make sure that your config is always valid. The exit code of this command will be 0 only if the config is valid.
5. `--dry-run` will pretend to run the selected workflow (either via arg or prompt), but will not actually perform any work (e.g., external commands, file I/O, API calls). Detects the same errors as `--validate` but also outputs info about what _would_ happen to stdout.
6. `--prerelease-label` will override the `prerelease_label` for any [`PrepareRelease`] step run.

### Environment Variables

These are all the environment variables that Knope will look for when running workflows.

1. `KNOPE_PRERELEASE_LABEL` works just like the `--prerelease-label` option. Note that the option takes precedence over the environment variable.
2. `GITHUB_TOKEN` will be used to load credentials from GitHub for [GitHub config].

## Features

Expand All @@ -39,3 +47,5 @@ You define a [config] file named `knope.toml` which has some metadata (e.g. Jira
[config]: config/config.md
[workflow]: config/workflow.md
[step]: config/step/step.md
[`preparerelease`]: config/step/PrepareRelease.md
[github config]: config/github.md
31 changes: 0 additions & 31 deletions knope.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,37 +48,6 @@ command = "git push"
[[workflows.steps]]
type = "Release"

[[workflows]]
name = "prerelease"

[[workflows.steps]]
type = "PrepareRelease"
prerelease_label = "rc"

[[workflow.steps]]
type = "Command"
command = "cargo update -w"

[[workflows.steps]]
type = "Command"
command = "npx -y prettier **/*.md --write"

[[workflows.steps]]
type = "Command"
command = "git add Cargo.toml Cargo.lock CHANGELOG.md"

[[workflows.steps]]
type = "Command"
command = "git commit -m \"chore: Bump to version\""
variables = { "version" = "Version" }

[[workflows.steps]]
type = "Command"
command = "git push"

[[workflows.steps]]
type = "Release"

[github]
owner = "knope-dev"
repo = "knope"
7 changes: 7 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ impl Config {
.into_diagnostic()
.wrap_err("Invalid TOML when parsing config")
}

/// Set the prerelease label for all `PrepareRelease` steps in all workflows in `self`.
pub(crate) fn set_prerelease_label(&mut self, label: &str) {
for workflow in &mut self.workflows {
workflow.set_prerelease_label(label);
}
}
}

/// Generate a brand new config file for the project in the current directory.
Expand Down
9 changes: 8 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ pub fn run(cli: Cli) -> Result<()> {

let preselected_workflow = cli.workflow;

let config = Config::load()?;
let mut config = Config::load()?;
if let Some(prerelease_label) = cli.prerelease_label {
config.set_prerelease_label(&prerelease_label);
}
let state = State::new(config.jira, config.github, config.packages);

if cli.validate {
Expand Down Expand Up @@ -99,6 +102,10 @@ pub struct Cli {
#[clap(long)]
/// Generate a new `knope.toml` file.
generate: bool,

#[clap(long, env = "KNOPE_PRERELEASE_LABEL")]
/// Set the `prerelease_label` attribute of any `PrepareRelease` steps at runtime.
prerelease_label: Option<String>,
}

#[cfg(test)]
Expand Down
7 changes: 7 additions & 0 deletions src/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ impl Step {
Step::Release => releases::release(run_type),
}
}

/// Set `prerelease_label` if `self` is `PrepareRelease`.
pub(crate) fn set_prerelease_label(&mut self, prerelease_label: &str) {
if let Step::PrepareRelease(prepare_release) = self {
prepare_release.prerelease_label = Some(String::from(prerelease_label));
}
}
}

#[derive(Debug, Error, Diagnostic)]
Expand Down
9 changes: 9 additions & 0 deletions src/workflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ pub(crate) struct Workflow {
pub(crate) steps: Vec<Step>,
}

impl Workflow {
/// Set `prerelease_label` for any steps that are `PrepareRelease` steps.
pub(crate) fn set_prerelease_label(&mut self, prerelease_label: &str) {
for step in &mut self.steps {
step.set_prerelease_label(prerelease_label);
}
}
}

/// A collection of errors from running with the `--validate` option.
#[derive(Debug, Error, Diagnostic)]
#[error("There are problems with the defined workflows")]
Expand Down
Loading