From e25c31201832ef21b8d8efd34284c748b0fb573f Mon Sep 17 00:00:00 2001 From: Jan Medek Date: Fri, 14 Feb 2025 13:01:36 +0100 Subject: [PATCH 1/3] feat(scripts/parse_changelog.py): add script to parse changelog for a specific version --- scripts/parse_changelog.py | 46 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 scripts/parse_changelog.py diff --git a/scripts/parse_changelog.py b/scripts/parse_changelog.py new file mode 100644 index 0000000..9a38628 --- /dev/null +++ b/scripts/parse_changelog.py @@ -0,0 +1,46 @@ +import argparse +import re +import pathlib + +CHANGELOG_PATH = pathlib.Path(__file__).parent.parent.joinpath("CHANGELOG.md") + +# Regular expressions for searched contents +VERSION_RE = re.compile(r'^v?(?P[0-9]+\.[0-9]+\.[0-9]+)$') + +def CURRENT_CHANGELOG_RE(version: str): + return re.compile( + r'(?P##\s+\[' + re.escape(version) + r'\].+?)\s*(?:\n##\s+\[[0-9]+\.[0-9]+\.[0-9]+\]|$)', + re.DOTALL + ) + +# Handling of input argument +def version_type(arg): + res = VERSION_RE.search(arg) + if not res: + raise argparse.ArgumentTypeError("Wrong format of version.") + return res.group("version") + +if __name__ == "__main__": + # Register argument parser, argument and parse + parser = argparse.ArgumentParser( + description = "Parses CHANGELOG.md and extracts changes for a specific version." + ) + + parser.add_argument( + "-v", "--version", + help = "Specify number of the version.", + action = "store", + type = version_type, + required = True + ) + + args = parser.parse_args() + + # Search for given version in changelog + parsed_changelog = CURRENT_CHANGELOG_RE(args.version).search(CHANGELOG_PATH.read_text()) + if not parsed_changelog: + raise ValueError(f"Could not find version '{args.version}' in file '{CHANGELOG_PATH}'.") + + # Write parsed changelog to file + out_path = pathlib.Path(CHANGELOG_PATH.parent.joinpath(f"CHANGELOG_parsed.md")) + out_path.write_text(parsed_changelog.group("current_changelog")) \ No newline at end of file From 9cdb2da973b0e6000ae4c9a9f563dcf4b3570643 Mon Sep 17 00:00:00 2001 From: Jan Medek Date: Fri, 14 Feb 2025 13:02:17 +0100 Subject: [PATCH 2/3] ci(release_new_version.yml): release libtropic version on tag creation --- .github/workflows/release_new_version.yml | 51 +++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 .github/workflows/release_new_version.yml diff --git a/.github/workflows/release_new_version.yml b/.github/workflows/release_new_version.yml new file mode 100644 index 0000000..68d49a2 --- /dev/null +++ b/.github/workflows/release_new_version.yml @@ -0,0 +1,51 @@ +name: Release new libtropic version on tag creation +on: + push: + tags: + - 'v[0-9]+\.[0-9]+\.[0-9]+' + +jobs: + check_tag_branch: + runs-on: ubuntu-22.04 + outputs: + do_release: ${{ steps.check_tag_is_on_master.outputs.DO_RELEASE }} + + steps: + - name: Checkout repository + uses: actions/checkout@v4.1.7 + with: + fetch-depth: 0 # Ensure full history is available + + - name: Check tag is on master + id: check_tag_is_on_master + run: | + TAG_COMMIT=$(git rev-list -n 1 ${{ github.ref_name }}) + if git branch -r --contains "$TAG_COMMIT" | grep -q "origin/master"; then + echo "DO_RELEASE=true" >> $GITHUB_OUTPUT + else + echo "DO_RELEASE=false" >> $GITHUB_OUTPUT + fi + + create_release: + needs: check_tag_branch + runs-on: ubuntu-22.04 + if: ${{ needs.check_tag_branch.outputs.do_release == 'true' }} # Skip release if tag is not on master + + steps: + - name: Get tag + run: echo "TAG_NAME=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV + + - name: Parse changelog for tag version # Run script creates new file with parsed changelog + run: | + python3 scripts/parse_changelog.py $TAG_NAME + + - name: Create release + uses: actions/create-release@v1.1.4 + with: + tag_name: ${{ env.TAG_NAME }} + release_name: "libtropic-${{ env.TAG_NAME }}" + body_path: CHANGELOG_parsed.md + draft: false + prerelease: false + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} From 10646d458f15c366c6596d6867fc37d061816d4a Mon Sep 17 00:00:00 2001 From: Jan Medek Date: Fri, 14 Feb 2025 14:04:16 +0100 Subject: [PATCH 3/3] fix(release_new_version.yml): checkout repo in second job, fix arguments for parse script --- .github/workflows/release_new_version.yml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.github/workflows/release_new_version.yml b/.github/workflows/release_new_version.yml index 68d49a2..aed60eb 100644 --- a/.github/workflows/release_new_version.yml +++ b/.github/workflows/release_new_version.yml @@ -32,12 +32,17 @@ jobs: if: ${{ needs.check_tag_branch.outputs.do_release == 'true' }} # Skip release if tag is not on master steps: + - name: Checkout repository + uses: actions/checkout@v4.1.7 + with: + fetch-depth: 0 # Ensure full history is available + - name: Get tag run: echo "TAG_NAME=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV - name: Parse changelog for tag version # Run script creates new file with parsed changelog run: | - python3 scripts/parse_changelog.py $TAG_NAME + python3 scripts/parse_changelog.py --version $TAG_NAME - name: Create release uses: actions/create-release@v1.1.4