Skip to content

Commit

Permalink
feat(crud): Add flag to auto-accept retrieved PR information (#54)
Browse files Browse the repository at this point in the history
* add --yes flag to add command

* add changelog entry
  • Loading branch information
MalteHerrmann authored Jul 25, 2024
1 parent c985eab commit b53eeef
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 39 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This changelog was created using the `clu` binary

### Features

- (crud) [#54](https://github.com/MalteHerrmann/changelog-utils/pull/54) Add flag to auto-accept retrieved PR information.
- (lint) [#46](https://github.com/MalteHerrmann/changelog-utils/pull/46) Add support for linter escapes.

### Improvements
Expand Down
99 changes: 64 additions & 35 deletions src/add.rs
Original file line number Diff line number Diff line change
@@ -1,53 +1,82 @@
use crate::errors::AddError;
use crate::{change_type, changelog, config, entry, github::get_open_pr, release};
use crate::{
change_type, changelog, config, entry,
errors::AddError,
github::{get_open_pr, PRInfo},
release,
};
use inquire::{Select, Text};
use std::borrow::BorrowMut;

// Runs the logic to add an entry to the unreleased section of the changelog.
pub async fn run() -> Result<(), AddError> {
pub async fn run(accept: bool) -> Result<(), AddError> {
let config = config::load()?;

let mut selectable_change_types: Vec<String> =
config.change_types.clone().into_keys().collect();
selectable_change_types.sort();

let pr_info = get_open_pr(&config).await.unwrap_or_default();

let ct_idx = selectable_change_types
.iter()
.position(|ct| ct.eq(&pr_info.change_type))
.unwrap_or_default();
let retrieved: bool;
let pr_info = match get_open_pr(&config).await {
Ok(i) => {
retrieved = true;
i
}
Err(_) => {
retrieved = false;
PRInfo::default()
}
};

let selected_change_type =
Select::new("Select change type to add into:", selectable_change_types)
.with_starting_cursor(ct_idx)
.prompt()?;
let mut selected_change_type = pr_info.change_type.clone();
if !accept || !retrieved || !selectable_change_types.contains(&pr_info.change_type) {
let ct_idx = selectable_change_types
.iter()
.position(|ct| ct.eq(&pr_info.change_type))
.unwrap_or_default();

selected_change_type =
Select::new("Select change type to add into:", selectable_change_types)
.with_starting_cursor(ct_idx)
.prompt()?;
}

let pr_number = match Text::new("Please provide the PR number:")
.with_initial_value(&pr_info.number)
.prompt()?
let mut pr_number = pr_info
.number
.parse::<u16>()
{
Ok(pr) => pr,
Err(e) => return Err(AddError::Input(e.into())),
};
.expect("expected valid pr number to be returned");
if !accept || !retrieved {
pr_number = match Text::new("Please provide the PR number:")
.with_initial_value(&pr_info.number)
.prompt()?
.parse::<u16>()
{
Ok(pr) => pr,
Err(e) => return Err(AddError::Input(e.into())),
};
}

let cat_idx = config
.categories
.iter()
.position(|c| c.eq(&pr_info.category))
.unwrap_or_default();

let cat = Select::new(
"Select the category of the made changes:",
config.categories.clone(),
)
.with_starting_cursor(cat_idx)
.prompt()?;

let desc = Text::new("Please provide a short description of the made changes:\n")
.with_initial_value(&pr_info.description)
let mut cat = pr_info.category.clone();
if !accept || !retrieved || !config.categories.contains(&cat) {
let cat_idx = config
.categories
.iter()
.position(|c| c.eq(&pr_info.category))
.unwrap_or_default();

cat = Select::new(
"Select the category of the made changes:",
config.categories.clone(),
)
.with_starting_cursor(cat_idx)
.prompt()?;
}

let mut desc = pr_info.description.clone();
if !accept || !retrieved {
desc = Text::new("Please provide a short description of the made changes:\n")
.with_initial_value(&pr_info.description)
.prompt()?;
}

let mut changelog = changelog::load(config.clone())?;
add_entry(
Expand Down
6 changes: 3 additions & 3 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use clap::{Args, Parser, Subcommand};
#[derive(Parser, Debug)]
pub enum ChangelogCLI {
#[command(about = "Adds a new entry to the unreleased section of the changelog")]
Add,
Add(AddArgs),
#[command(about = "Applies all possible auto-fixes to the changelog")]
Fix,
#[command(about = "Checks if the changelog contents adhere to the defined rules")]
Expand All @@ -24,9 +24,9 @@ It creates an empty changelog skeleton if no existing changelog is found as well
}

#[derive(Args, Debug)]
pub struct LintArgs {
pub struct AddArgs {
#[arg(short, long)]
pub fix: bool,
pub yes: bool,
}

#[derive(Subcommand, Debug)]
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use clu::{add, cli::ChangelogCLI, cli_config, errors::CLIError, init, lint, rele
#[tokio::main]
async fn main() -> Result<(), CLIError> {
match ChangelogCLI::parse() {
ChangelogCLI::Add => Ok(add::run().await?),
ChangelogCLI::Add(add_args) => Ok(add::run(add_args.yes).await?),
ChangelogCLI::Fix => Ok(lint::run(true)?),
ChangelogCLI::Lint => Ok(lint::run(false)?),
ChangelogCLI::Init => Ok(init::run()?),
Expand Down

0 comments on commit b53eeef

Please sign in to comment.