From 3f4e82d98d402b45d84a92c6374487a619931ecb Mon Sep 17 00:00:00 2001 From: Michael Sasser Date: Tue, 16 Nov 2021 14:55:59 +0100 Subject: [PATCH 1/3] feat(action): Generate the release body with a script --- .github/workflows/release.yml | 35 +++++---- .gitignore | 5 ++ news/.gitkeep | 0 news/templates/default.rst | 3 - pyproject.toml | 2 +- scripts/get_latest_release.py | 134 ++++++++++++++++++++++++++++++++++ 6 files changed, 157 insertions(+), 22 deletions(-) create mode 100644 news/.gitkeep create mode 100755 scripts/get_latest_release.py diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 9a2e4491..eeb092d0 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -64,24 +64,23 @@ jobs: - name: Zip project run: zip --junk-paths matrixctl.zip dist/* README.md LICENSE.txt - - name: Create Release - id: create_release - uses: actions/create-release@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - name: Generate changelog_latest.rst from CHANGELOG.rst + run: python scripts/get_latest_release.py + + - name: Convert from changelog_latest.rst to changelog_latest.md + uses: docker://pandoc/core:2.9 with: - tag_name: ${{ github.ref }} - release_name: Release ${{ github.ref }} - draft: true - prerelease: true + args: >- + --output=chagelog_latest.md + --from rst + --to markdown + chagelog_latest.rst - - name: Upload Release Asset - id: upload-release-asset - uses: actions/upload-release-asset@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - name: Create Release + uses: softprops/action-gh-release@v1 with: - upload_url: ${{ steps.create_release.outputs.upload_url }} - asset_path: ./matrixctl.zip - asset_name: matrixctl.zip - asset_content_type: application/zip + prerelease: false + draft: false + name: Release ${{ github.ref }} + body_path: chagelog_latest.md + files: matrixctl.zip diff --git a/.gitignore b/.gitignore index 97d82e5f..e4b118bf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,10 @@ /.vim +# Files produced by a script while running the script get_latest_release.py +# The latter file is used for as release message. +/chagelog_latest.rst +/chagelog_latest.md + # Created by https://www.toptal.com/developers/gitignore/api/vim,emacs,python # Edit at https://www.toptal.com/developers/gitignore?templates=vim,emacs,python diff --git a/news/.gitkeep b/news/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/news/templates/default.rst b/news/templates/default.rst index ac551dfa..ddd55a0f 100644 --- a/news/templates/default.rst +++ b/news/templates/default.rst @@ -1,9 +1,6 @@ {% if top_line %} {{ top_line }} {{ top_underline * ((top_line)|length)}} -{% elif versiondata.name %} -{{ versiondata.name }} {{ versiondata.version }} ({{ versiondata.date }}) -{{ top_underline * ((versiondata.name + versiondata.version + versiondata.date)|length + 4)}} {% else %} {{ versiondata.version }} ({{ versiondata.date }}) {{ top_underline * ((versiondata.version + versiondata.date)|length + 3)}} diff --git a/pyproject.toml b/pyproject.toml index f09c2988..68f923b5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -196,7 +196,7 @@ filename = "CHANGELOG.rst" issue_format = "`#{issue} `_" directory = "news/" top_line = false -title_format = "{version} ({project_date})" # {name} +# title_format = "{version} ({project_date})" # {name} all_bullets = true # make all fragments bullet points wrap = true # Wrap text to 79 characters template = "news/templates/default.rst" diff --git a/scripts/get_latest_release.py b/scripts/get_latest_release.py new file mode 100755 index 00000000..676aaa39 --- /dev/null +++ b/scripts/get_latest_release.py @@ -0,0 +1,134 @@ +#!/usr/bin/env python3 +# get_latest_release.py - A support script for the MatrixCtl project +# Copyright (c) 2021 Michael Sasser +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +"""Use this script in GH actions, to get the latest changelog. + +The script splits the CHANGELOG.rst by the headline including +the version (example: ``0.11.2 (2021-09-26)``) and date with a +regular expression. +The first list entry at index 0 is everything before the latest entry. +The second list entry at index 1 contains the headline like in the example +above. +The third list entry contains the body of the latest entry including the +underline of the first headline. + +Notes +----- +- This script is not part of the source code of MatrixCtl. It is a release helper + script. +- After the script finished run the following command: + ``pandoc chagelog_latest.rst --from rst --to markdown -o chagelog_latest.md`` + +""" + + +from __future__ import annotations + +import logging +import re +import sys +import typing as t + +from pathlib import Path + + +__author__: str = "Michael Sasser" +__email__: str = "Michael@MichaelSasser.org" +__version__: str = "0.1.0" + + +logger = logging.getLogger(__name__) + +# Modified SemVer version pattern to account for the rest of the headline +# https://regex101.com/r/pOWOBM/1 # With match groups +# https://regex101.com/r/WiHlCF/1 # without +_VERSION_PATTERN: str = ( + r"^((?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)" + r"(?:-(?:" + r"(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)" + r"(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?" + r"(?:\+(?:[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?" + r"\s*\((?:\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12][0-9]|3[01]))\))$" +) + +_HEADLINE: t.Pattern[str] = re.compile(_VERSION_PATTERN, re.MULTILINE) + + +def main(input_path: Path, output_path: Path) -> int: + """Create a new file which only contains the latest changelog entry. + + Attributes + ---------- + input_path : pathlib.Path + The input file as ``Path()``, which is a news file created by + towncrier. + output_path : pathlib.Path + The output file, which does not need to exist beforehand. The output + will be written to this file. + + Returns + ------- + return_code : int + The return code of the program. + - **0** - The program has successfully generated the outputfile. + - **1** - An I/O error occurred. + - **2** - The news file does not contain enough news entries. + + """ + # Read CHANGELOG.rst + try: + with input_path.open() as fp: + found_headlines = _HEADLINE.split(fp.read()) + except (OSError): + logger.fatal(f"Unable to read from the input file! {input_path =}") + return 1 + + # Check, that there are enough headlines to work with + if len(found_headlines) < 3: + logger.fatal(f"Unable to find enough headlines! {found_headlines =}") + return 2 + + entry: str = f"{found_headlines[1]}{found_headlines[2].rstrip()}" + logger.info("Latest entry:\n%s", entry) + + # Write changelog_latest.rst + try: + with output_path.open("w") as fp: + fp.write(entry) + except (OSError): + logger.fatal(f"Unable to write to the output file! {output_path =}") + return 1 + + return 0 + + +if __name__ == "__main__": + # setup logging + logging.basicConfig( + level=logging.INFO, + format="%(asctime)s %(name)s:%(lineno)d %(levelname)s - %(message)s", + ) + + # The path to the project root + project_root_path: Path = Path(__file__).resolve().parent.parent + + input_path: Path = project_root_path / "CHANGELOG.rst" + output_path: Path = project_root_path / "chagelog_latest.rst" + + sys.exit(main(input_path, output_path)) + +# vim: set ft=python : From afc350bdcdf08c2683f272ae1dfe663b205e8ebc Mon Sep 17 00:00:00 2001 From: Michael Sasser Date: Tue, 16 Nov 2021 15:02:41 +0100 Subject: [PATCH 2/3] feat: add missing newsfragment and reorder release workflow --- .github/workflows/release.yml | 27 +++++++++++++++------------ news/284.misc | 1 + 2 files changed, 16 insertions(+), 12 deletions(-) create mode 100644 news/284.misc diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index eeb092d0..0288455a 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -49,18 +49,6 @@ jobs: - name: Build project run: poetry build - - name: pypi-publish - uses: pypa/gh-action-pypi-publish@v1.4.2 - with: - # PyPI user - # user: # optional, default is __token__ - # Password for your PyPI user or an access token - password: ${{ secrets.pypi_token }} - # The repository URL to use - # repository_url: # optional - # The target directory for distribution - # packages_dir: # optional, default is dist - - name: Zip project run: zip --junk-paths matrixctl.zip dist/* README.md LICENSE.txt @@ -84,3 +72,18 @@ jobs: name: Release ${{ github.ref }} body_path: chagelog_latest.md files: matrixctl.zip + + - name: Delete temp files + run: rm -rf matrixctl.zip chagelog_latest.md chagelog_latest.rst + + - name: pypi-publish + uses: pypa/gh-action-pypi-publish@v1.4.2 + with: + # PyPI user + # user: # optional, default is __token__ + # Password for your PyPI user or an access token + password: ${{ secrets.pypi_token }} + # The repository URL to use + # repository_url: # optional + # The target directory for distribution + # packages_dir: # optional, default is dist diff --git a/news/284.misc b/news/284.misc new file mode 100644 index 00000000..487f69ee --- /dev/null +++ b/news/284.misc @@ -0,0 +1 @@ +Generate the release body with a script while running the release action. From 8c8904ab0c9e66143870e244c70b5987c50b1916 Mon Sep 17 00:00:00 2001 From: Michael Sasser Date: Tue, 16 Nov 2021 15:07:27 +0100 Subject: [PATCH 3/3] fix: wrap long line --- scripts/get_latest_release.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/get_latest_release.py b/scripts/get_latest_release.py index 676aaa39..ad418d3c 100755 --- a/scripts/get_latest_release.py +++ b/scripts/get_latest_release.py @@ -28,8 +28,8 @@ Notes ----- -- This script is not part of the source code of MatrixCtl. It is a release helper - script. +- This script is not part of the source code of MatrixCtl. It is a release + helper script. - After the script finished run the following command: ``pandoc chagelog_latest.rst --from rst --to markdown -o chagelog_latest.md``