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(cli): Commit and push changelog entry after adding #68

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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: 2 additions & 1 deletion .clconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
"Features": "feat",
"Improvements": "imp"
},
"commit_message": "add changelog entry",
"expected_spellings": {
"CLI": "cli"
},
"legacy_version": null,
"target_repo": "https://github.com/MalteHerrmann/changelog-utils"
}
}
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ This changelog was created using the `clu` binary

### Features

- (cli) [#68](https://github.com/MalteHerrmann/changelog-utils/pull/68) Commit and push changelog entry after adding.
- (cli) [#67](https://github.com/MalteHerrmann/changelog-utils/pull/67) Add option to push branch to remote.
- (cli) [#63](https://github.com/MalteHerrmann/changelog-utils/pull/63) Enable switching between release types when not specifying a version.

Expand Down
12 changes: 10 additions & 2 deletions src/add.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
use crate::{
change_type, changelog, config, entry,
errors::AddError,
github::{extract_pr_info, get_git_info, get_open_pr, PRInfo},
github::{commit, extract_pr_info, get_git_info, get_open_pr, PRInfo},
inputs, release,
};
use std::borrow::BorrowMut;

// Runs the logic to add an entry to the unreleased section of the changelog.
//
// After adding the new entry, the user is queried for a commit message to use
// to commit the changes.
//
// NOTE: the changes are NOT pushed to the origin when running the `add` command.
pub async fn run(accept: bool) -> Result<(), AddError> {
let config = config::load()?;
let git_info = get_git_info(&config)?;
Expand Down Expand Up @@ -68,7 +73,10 @@ pub async fn run(accept: bool) -> Result<(), AddError> {
pr_number,
);

Ok(changelog.write(&changelog.path)?)
changelog.write(&changelog.path)?;

let cm = inputs::get_commit_message(&config)?;
Ok(commit(cm.as_str())?)
}

/// Adds the given contents into a new entry in the unreleased section
Expand Down
6 changes: 6 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ pub struct Config {
/// an abbreviation that is to be used as a short form
/// in pull request titles.
pub change_types: BTreeMap<String, String>,
/// The default commit message to be used when committing
/// the new changelog entry.
pub commit_message: String,
/// The map of expected spellings.
///
/// Note: The key is the correct spelling and the value
Expand Down Expand Up @@ -56,9 +59,12 @@ impl Default for Config {
default_change_types.insert("Features".into(), "feat".into());
default_change_types.insert("Improvements".into(), "imp".into());

let commit_message = "add changelog entry".to_string();

Config {
categories: Vec::default(),
change_types: default_change_types,
commit_message,
expected_spellings: BTreeMap::default(),
legacy_version: None,
target_repo: String::default(),
Expand Down
3 changes: 2 additions & 1 deletion src/create_pr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,6 @@ pub async fn run() -> Result<(), CreateError> {
.expect("received no error creating the PR but html_url was None")
);

Ok(())
let cm = inputs::get_commit_message(&config)?;
Ok(github::commit_and_push(&cm)?)
}
2 changes: 2 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ pub enum EntryError {
pub enum GitHubError {
#[error("failed to get current branch")]
CurrentBranch,
#[error("failed to commit changes")]
FailedToCommit,
#[error("failed to push to origin")]
FailedToPush,
#[error("failed to call GitHub API: {0}")]
Expand Down
55 changes: 55 additions & 0 deletions src/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,61 @@ fn get_current_local_branch() -> Result<String, GitHubError> {
}
}

/// Commits the current changes with the given commit message and pushes to the origin.
pub fn commit_and_push(message: &str) -> Result<(), GitHubError> {
stage_changelog_changes()?;

match Command::new("git")
.args(vec!["commit", "-a", "-m", message])
.status()?
.success()
{
true => Ok(push()?),
false => Err(GitHubError::FailedToCommit),
}
}

/// Commits the current changes with the given commit message and pushes to the origin.
pub fn commit(message: &str) -> Result<(), GitHubError> {
stage_changelog_changes()?;

if !Command::new("git")
.args(vec!["commit", "-m", message])
.status()?
.success()
{
return Err(GitHubError::FailedToCommit);
}

Ok(())
}

/// Adds the changelog to the staged changes in Git.
fn stage_changelog_changes() -> Result<(), GitHubError> {
// TODO: pass the changelog filename / path
if !Command::new("git")
.args(vec!["add", "CHANGELOG.md"])
.status()?
.success()
{
return Err(GitHubError::FailedToCommit)
}

Ok(())
}

/// Tries to push the latest commits on the current branch.
pub fn push() -> Result<(), GitHubError> {
match Command::new("git")
.args(vec!["push"])
.status()?
.success()
{
true => Ok(()),
false => Err(GitHubError::FailedToPush),
}
}

/// Tries to push the current branch to the origin repository.
pub fn push_to_origin(branch_name: &str) -> Result<(), GitHubError> {
match Command::new("git")
Expand Down
8 changes: 8 additions & 0 deletions src/inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ pub fn get_category(config: &Config, default_idx: usize) -> Result<String, Input
.prompt()?)
}

pub fn get_commit_message(config: &Config) -> Result<String, InputError> {
Ok(
Text::new("Please provide the commit message:\n")
.with_initial_value(config.commit_message.as_str())
.prompt()?
)
}

pub fn get_description(default_value: &str) -> Result<String, InputError> {
Ok(
Text::new("Please provide a one-line description of the made changes:\n")
Expand Down
1 change: 1 addition & 0 deletions src/testdata/example_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"Improvements": "imp",
"Features": "feat"
},
"commit_message": "add changelog entry",
"expected_spellings": {
"API": "api",
"CLI": "cli",
Expand Down
3 changes: 2 additions & 1 deletion src/testdata/example_config_without_optionals.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
"Improvements": "imp",
"Features": "feat"
},
"commit_message": "add changelog entry",
"expected_spellings": {
"API": "api",
"CLI": "cli",
"Web-SDK": "web[-\\s]*sdk"
},
"target_repo": "https://github.com/MalteHerrmann/changelog-utils"
}
}
1 change: 1 addition & 0 deletions tests/testdata/evmos_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"State Machine Breaking": "imp",
"API Breaking": "imp"
},
"commit_message": "add changelog entry",
"expected_spellings": {
"ABI": "abi",
"EIP-712": "eip[-\\s]*712"
Expand Down
Loading