Skip to content

Commit

Permalink
feat(config): Add changelog path to the configuration (#70)
Browse files Browse the repository at this point in the history
* add changelog path to config in case other names are desired

* remove unused variable

* add comment

* add changelog entry
  • Loading branch information
MalteHerrmann authored Jan 26, 2025
1 parent 716e160 commit 7774125
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 17 deletions.
1 change: 1 addition & 0 deletions .clconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"Features": "feat",
"Improvements": "imp"
},
"changelog_path": "CHANGELOG.md",
"commit_message": "add changelog entry",
"expected_spellings": {
"CLI": "cli"
Expand Down
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

- (config) [#70](https://github.com/MalteHerrmann/changelog-utils/pull/70) Add changelog path to the configuration.
- (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
2 changes: 1 addition & 1 deletion src/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub async fn run(accept: bool) -> Result<(), AddError> {
changelog.write(&changelog.path)?;

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

/// Adds the given contents into a new entry in the unreleased section
Expand Down
4 changes: 4 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ pub struct Config {
/// The default commit message to be used when committing
/// the new changelog entry.
pub commit_message: String,
/// The relative path of the changelog file.
pub changelog_path: String,
/// The map of expected spellings.
///
/// Note: The key is the correct spelling and the value
Expand Down Expand Up @@ -60,11 +62,13 @@ impl Default for Config {
default_change_types.insert("Improvements".into(), "imp".into());

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

Config {
categories: Vec::default(),
change_types: default_change_types,
commit_message,
changelog_path,
expected_spellings: BTreeMap::default(),
legacy_version: None,
target_repo: String::default(),
Expand Down
2 changes: 1 addition & 1 deletion src/create_pr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ pub async fn run() -> Result<(), CreateError> {
);

let cm = inputs::get_commit_message(&config)?;
Ok(github::commit_and_push(&cm)?)
Ok(github::commit_and_push(&config, &cm)?)
}
27 changes: 12 additions & 15 deletions src/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ pub fn extract_pr_info(config: &Config, pr: &PullRequest) -> Result<PRInfo, GitH

/// Returns an authenticated Octocrab instance if possible.
pub fn get_authenticated_github_client() -> Result<Octocrab, GitHubError> {
// NOTE: make sure to export the token and not only define using GITHUB_TOKEN=... because Rust executes
// in a child process, that cannot pick it up without using `export`
let token = std::env::var("GITHUB_TOKEN")?;

Ok(octocrab::OctocrabBuilder::new()
Expand Down Expand Up @@ -120,9 +122,9 @@ 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()?;
pub fn commit_and_push(config: &Config, message: &str) -> Result<(), GitHubError> {
stage_changelog_changes(config)?;

match Command::new("git")
.args(vec!["commit", "-a", "-m", message])
.status()?
Expand All @@ -134,9 +136,9 @@ pub fn commit_and_push(message: &str) -> Result<(), GitHubError> {
}

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

if !Command::new("git")
.args(vec!["commit", "-m", message])
.status()?
Expand All @@ -149,26 +151,21 @@ pub fn commit(message: &str) -> Result<(), GitHubError> {
}

/// Adds the changelog to the staged changes in Git.
fn stage_changelog_changes() -> Result<(), GitHubError> {
// TODO: pass the changelog filename / path
fn stage_changelog_changes(config: &Config) -> Result<(), GitHubError> {
if !Command::new("git")
.args(vec!["add", "CHANGELOG.md"])
.args(vec!["add", config.changelog_path.as_str()])
.status()?
.success()
{
return Err(GitHubError::FailedToCommit)
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()
{
match Command::new("git").args(vec!["push"]).status()?.success() {
true => Ok(()),
false => Err(GitHubError::FailedToPush),
}
Expand Down
1 change: 1 addition & 0 deletions src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub fn init_in_folder(target: PathBuf) -> Result<(), InitError> {
config.target_repo.clone_from(&origin);
};

// TODO: check for available changelog file names and adjust config if something else other than the default is found
let changelog_path = target.join("CHANGELOG.md");
match fs::read_to_string(changelog_path.clone()) {
Ok(contents) => {
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"
},
"changelog_path": "CHANGELOG.md",
"commit_message": "add changelog entry",
"expected_spellings": {
"API": "api",
Expand Down
1 change: 1 addition & 0 deletions src/testdata/example_config_without_optionals.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"Improvements": "imp",
"Features": "feat"
},
"changelog_path": "CHANGELOG.md",
"commit_message": "add changelog entry",
"expected_spellings": {
"API": "api",
Expand Down
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"
},
"changelog_path": "CHANGELOG.md",
"commit_message": "add changelog entry",
"expected_spellings": {
"ABI": "abi",
Expand Down

0 comments on commit 7774125

Please sign in to comment.