Skip to content

Commit

Permalink
fix: 🐛 return error when changelog content is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
M-Adoo committed Jan 8, 2024
1 parent d78a31f commit 548f8f6
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 14 deletions.
17 changes: 15 additions & 2 deletions .github/workflows/release-version.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,31 @@ jobs:
# todo: use cargo-release directly merge the changelog
# https://github.com/crate-ci/cargo-release/issues/741
- name: config merge changelog env
run: MERGE_CHANGELOG=${{ inputs.merge_changelog }}
- name: Extract changelog
run: export MERGE_CHANGELOG=${{ inputs.merge_changelog }}
- name: version & tag name
if: ${{ env.NEW_COMMIT_COUNT > 0 }}
run: |
echo "TAG_NAME=$(git describe --tags --abbrev=0)" >> $GITHUB_ENV
version=$(git describe --tags --abbrev=0 | grep -oP '(?<=v)\S*')
echo "VERSION=$version" >> $GITHUB_ENV
- name: Extract changelog
if: ${{ env.NEW_COMMIT_COUNT > 0 && inputs.merge_changelog == false}}
run: |
{
echo 'CHANGELOG<<delimiter-for-changelog'
rclog -t ${version} -p ./CHANGELOG.md extract
echo 'delimiter-for-changelog'
} >> $GITHUB_ENV
- name: Merge changelog
if: ${{ env.NEW_COMMIT_COUNT > 0 && inputs.merge_changelog == true}}
run: |
{
echo 'CHANGELOG<<delimiter-for-changelog'
echo $RCLOG_MERGE
echo 'delimiter-for-changelog'
} >> $GITHUB_ENV
echo "CHANGELOG=$(cat CHANGELOG.md)" >> $GITHUB_ENV
- name: Github release notes
if: ${{ env.NEW_COMMIT_COUNT > 0 }}
uses: ncipollo/release-action@v1
Expand Down
9 changes: 6 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,24 @@ Please only add new entries below the [Unreleased](#unreleased---releasedate) he

## [@Unreleased] - @ReleaseDate

## Infrastructure
### Infrastructure

- Add github actions to manage rclog release(#1 @M-Adoo)

## Features
### Features

- Add a reusable workflow to help Rust project in Github to release version. (#1 @M-Adoo)
- Optionally, merge changelogs from all pre-release versions into the release version
- Publish a new version to crates.io.
- Extract the changelog of a specific version and create a new release note on GitHub.

### Fixed

- Return error when the changelog content is empty. (#1 @M-Adoo)

## 0.1.1-alpha.1 - 2024-01-04

## Features
### Features

The initial version of the changelog management tool of [Ribir](ribir.org). Run ``rclog -h`` to see the usage.

Expand Down
5 changes: 5 additions & 0 deletions rclog_hook.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

if $MERGE_CHANGELOG; then
export $RCLOG_MERGE = $(rclog -t $NEW_VERSION -p ./CHANGELOG.md merge)
fi
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
};
match args.command {
Commands::Extract => {
let content = extract_content(&version, &changelog);
let content = extract_content(&version, &changelog)?;
println!("{}", content);
}
Commands::Merge => {
Expand Down
24 changes: 16 additions & 8 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ use comrak::{
use semver::{Version, VersionReq};

/// Pick the changelog content of the given version
pub fn extract_content(version: &Version, changelog: &str) -> String {
pub fn extract_content(
version: &Version,
changelog: &str,
) -> Result<String, Box<dyn std::error::Error + 'static>> {
let arena = Arena::new();
let doc = parse_document(&arena, changelog, &Options::default());

Expand All @@ -26,11 +29,13 @@ pub fn extract_content(version: &Version, changelog: &str) -> String {
) {
break;
}

format_commonmark(node, &<_>::default(), &mut content).unwrap();
format_commonmark(node, &<_>::default(), &mut content)?;
}
if content.is_empty() {
Err("No changelog content found".into())
} else {
Ok(String::from_utf8(content)?)
}

String::from_utf8(content).unwrap()
}

/// Merge the pre-release version changelog to the more stable version
Expand Down Expand Up @@ -134,8 +139,11 @@ pub fn merge_pre_release_changelogs(

// pop the last '\n'
content.pop();

Ok(String::from_utf8(content)?)
if content.is_empty() {
Err("No changelog content found".into())
} else {
Ok(String::from_utf8(content)?)
}
}

fn pick_version<'a>(node: &'a AstNode<'a>) -> Option<Version> {
Expand Down Expand Up @@ -184,7 +192,7 @@ version 0.1.1
[0.1.2]: ribir.org
";
let v0_1_2 = Version::parse("0.1.2").unwrap();
let content = extract_content(&v0_1_2, content);
let content = extract_content(&v0_1_2, content).unwrap();
assert_eq!(content, "### Features\n- version [0.1.2](ribir.org)\n");
}

Expand Down

0 comments on commit 548f8f6

Please sign in to comment.