-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit introduces several helpers: * A script that reads the most recent released version from CHANGELOG.md and can be instructed to guess the next version based on news fragments. * A new workflow that renders the changelog and submits a PR with this changelog. * A new trigger for the release workflow that runs when pull requests are closed and checks if it was a release PR as created by the workflow described above. In conclusion, this patch allows to quickly release new versions instead of having to perform the above steps manually on the CLI.
- Loading branch information
Showing
7 changed files
with
286 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Improved release automation: Added workflow that builds the changelog and creates/updates a PR on pushes to the default branch. Added trigger for release workflow when this PR is merged. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
""" | ||
Very simple heuristic to generate the next version number | ||
based on the current changelog news fragments. | ||
This looks for the most recent version by parsing the | ||
CHANGELOG.md file and increments a specific part, | ||
depending on the fragment types present and their contents. | ||
Major bumps are caused by: | ||
* files named `.removed.md` | ||
* files named `.breaking.md` | ||
* files containing `BREAKING:` | ||
Minor bumps are caused by: | ||
* files named `.added.md` | ||
Otherwise, only the patch version is bumped. | ||
""" | ||
|
||
import re | ||
import sys | ||
from pathlib import Path | ||
|
||
PROJECT_ROOT = Path(__file__).parent.parent.resolve() | ||
CHANGELOG_DIR = PROJECT_ROOT / "changelog" | ||
CHANGELOG_FILE = PROJECT_ROOT / "CHANGELOG.md" | ||
|
||
|
||
class Version: | ||
def __init__(self, version): | ||
match = re.search(r"v?(?P<release>[0-9]+(?:\.[0-9]+)*)", version) | ||
if not match: | ||
raise ValueError(f"Invalid version: '{version}'") | ||
self.release = tuple(int(i) for i in match.group("release").split(".")) | ||
|
||
@property | ||
def major(self): | ||
return self._ret(0) | ||
|
||
@property | ||
def minor(self): | ||
return self._ret(1) | ||
|
||
@property | ||
def patch(self): | ||
return self._ret(2) | ||
|
||
def __str__(self): | ||
return ".".join(str(i) for i in self.release) | ||
|
||
def _ret(self, cnt): | ||
try: | ||
return self.release[cnt] | ||
except IndexError: | ||
return 0 | ||
|
||
|
||
def last_release(): | ||
for line in CHANGELOG_FILE.read_text(encoding="utf-8").splitlines(): | ||
if line.startswith("## "): | ||
return Version(line.split(" ")[1]) | ||
return Version("0.0.0") | ||
|
||
|
||
def get_next_version(last): | ||
major = minor = False | ||
|
||
for fragment in CHANGELOG_DIR.glob("[!.]*"): | ||
name = fragment.name.lower() | ||
if ".added" in name: | ||
minor = True | ||
elif ".breaking" in name or ".removed" in name: | ||
major = True | ||
break | ||
if "breaking:" in fragment.read_text(encoding="utf-8").lower(): | ||
major = True | ||
break | ||
if major: | ||
return Version(f"{last.major + 1}.0.0") | ||
if minor: | ||
return Version(f"{last.major}.{last.minor + 1}.0") | ||
return Version(f"{last.major}.{last.minor}.{last.patch + 1}") | ||
|
||
|
||
if __name__ == "__main__": | ||
try: | ||
if sys.argv[1] == "next": | ||
print(get_next_version(last_release())) | ||
raise SystemExit(0) | ||
except IndexError: | ||
pass | ||
print(last_release()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,5 +22,5 @@ jobs: | |
contents: write | ||
id-token: write | ||
pages: write | ||
pull-requests: read | ||
pull-requests: write | ||
{%- endraw %} |
67 changes: 67 additions & 0 deletions
67
...f 'github.com' in source_url %}.github{% endif %}/workflows/prepare-release-action.yml.j2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
name: Prepare Release PR | ||
|
||
on: | ||
workflow_call: | ||
workflow_dispatch: | ||
inputs: | ||
version: | ||
description: Override the autogenerated version. | ||
required: false | ||
default: '' | ||
type: string | ||
|
||
jobs: | ||
update-release: | ||
name: Render changelog and create/update PR | ||
{%- endraw %} | ||
runs-on: ubuntu-{{ versions["ubuntu"] }} | ||
{%- raw %} | ||
if: github.ref == format('refs/heads/{0}', github.event.repository.default_branch) | ||
permissions: | ||
contents: write | ||
pull-requests: write | ||
|
||
steps: | ||
- name: Checkout code | ||
{%- endraw %} | ||
uses: actions/checkout@{{ versions["actions/checkout"] }} | ||
{%- raw %} | ||
|
||
- name: Set up Python 3.10 | ||
{%- endraw %} | ||
uses: actions/setup-python@{{ versions["actions/setup-python"] }} | ||
{%- raw %} | ||
with: | ||
python-version: '3.10' | ||
|
||
- name: Install project | ||
run: | | ||
python -m pip install --upgrade pip | ||
python -m pip install -e '.[dev,docs]' | ||
|
||
- name: Get next version | ||
if: github.event_name == 'push' || inputs.version == '' | ||
id: next-version | ||
run: echo "version=$(python tools/version.py next)" >> "$GITHUB_OUTPUT" | ||
|
||
- name: Update CHANGELOG.md and push to release PR | ||
env: | ||
NEXT_VERSION: ${{ (github.event_name == 'workflow_dispatch' && inputs.version != '') && inputs.version || steps.next-version.outputs.version }} | ||
run: towncrier build --yes --version "${NEXT_VERSION}" | ||
|
||
- name: Create/update release PR | ||
{%- endraw %} | ||
uses: peter-evans/create-pull-request@{{ versions["peter-evans/create-pull-request"] }} | ||
{%- raw %} | ||
with: | ||
commit-message: Release v${{ (github.event_name == 'workflow_dispatch' && inputs.version != '') && inputs.version || steps.next-version.outputs.version }} | ||
branch: release/auto | ||
sign-commits: true | ||
title: Release v${{ (github.event_name == 'workflow_dispatch' && inputs.version != '') && inputs.version || steps.next-version.outputs.version }} | ||
body: | | ||
This automated PR builds the latest changelog. When merged, a new release is published automatically. | ||
|
||
If you want to rebuild this PR with a custom version or the current date, you can also trigger the corresponding workflow manually in Actions > Prepare Release > Run workflow. | ||
|
||
You can still follow the manual release procedure outlined in: https://salt-extensions.github.io/salt-extension-copier/topics/publishing.html | ||
{%- endraw %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters