Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 7 additions & 45 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,58 +40,20 @@ jobs:
- name: 💎 Install
run: |
python -m pip install --no-cache-dir -r requirements-dev.txt
- name: 📝 Extract release notes from changelog
# Before building and publishing, so a missing or malformed changelog
# section fails the release before anything is uploaded to PyPI
env:
TAG: ${{ github.ref_name }}
run: |
invoke release-notes --tag "$TAG" --output release-notes.md
- name: 💃 Build release
run: |
python setup.py clean --all sdist bdist_wheel
- name: 📦 Publish release to PyPI
uses: pypa/gh-action-pypi-publish@dc37677b2e1c63e2034f94d8a5b11f265b73ba33 # v1.14.2
with:
password: ${{ secrets.pypi_password }}
- name: 📝 Extract release notes from changelog
env:
VERSION: ${{ github.ref_name }}
run: |
python - <<'PY'
import os
import re

version = os.environ["VERSION"].lstrip("v")
lines = open("CHANGELOG.rst", encoding="utf-8").read().splitlines()

# Collect the lines of the "<version>" section (heading underlined with dashes)
section, inside = [], False
for i, line in enumerate(lines):
is_heading = i + 1 < len(lines) and re.fullmatch(r"-{3,}", lines[i + 1])
if is_heading:
if inside:
break
inside = line.strip() == version
continue
if inside and not re.fullmatch(r"-{3,}", line):
section.append(line)
if not section:
raise SystemExit(f"No section for {version} in CHANGELOG.rst")

# Convert to Markdown: **Group** -> ## Group (skipping empty groups),
# join wrapped bullet lines, ``code`` -> `code`
groups, current = [], None
for line in section:
m = re.fullmatch(r"\*\*(.+)\*\*", line.strip())
if m:
current = (m.group(1), [])
groups.append(current)
elif current and line.startswith("* "):
current[1].append(line)
elif current and current[1] and line.startswith(" ") and line.strip():
current[1][-1] += " " + line.strip()

out = []
for title, items in groups:
if items:
out += [f"## {title}", ""] + [i.replace("``", "`") for i in items] + [""]
open("release-notes.md", "w", encoding="utf-8").write("\n".join(out))
print("\n".join(out))
PY
- name: 🚀 Create GitHub Release
env:
GH_TOKEN: ${{ github.token }}
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ Unreleased

**Added**

* Added ``invoke release-notes`` task to preview the GitHub Release notes generated
from this changelog.

**Changed**

* The release workflow now also creates the GitHub Release for each version tag,
Expand Down
73 changes: 73 additions & 0 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import contextlib
import glob
import os
import re
import sys
from shutil import rmtree

Expand Down Expand Up @@ -157,6 +158,78 @@ def prepare_changelog(ctx):
ctx.run('git add CHANGELOG.rst && git commit -m "Prepare changelog for next release"')


def changelog_to_markdown(changelog, version):
"""Convert the section of ``version`` in the CHANGELOG.rst text to GitHub Markdown.

``**Group**`` lines become ``## Group`` headings (empty groups are skipped),
wrapped bullet lines are joined and ``code`` spans use single backticks.

Raises ``ValueError`` if the section is missing, has no entries, or contains
a line that cannot be converted, instead of silently dropping content.
"""
lines = changelog.splitlines()

def is_underline(line):
return re.match(r'-{3,}$', line) is not None

# Collect the lines of the "<version>" section (heading underlined with dashes)
section, found, inside = [], False, False
for i, line in enumerate(lines):
if i + 1 < len(lines) and is_underline(lines[i + 1]):
if inside:
break
inside = line.strip() == version
found = found or inside
continue
if inside and not is_underline(line):
section.append((i + 1, line))
if not found:
raise ValueError('No section for {} in CHANGELOG.rst'.format(version))

groups, current = [], None
for lineno, line in section:
if not line.strip():
continue
group = re.match(r'\*\*(.+)\*\*$', line.strip())
if group:
current = (group.group(1), [])
groups.append(current)
elif current is not None and re.match(r' *[*-] ', line):
# Nested bullets keep their indentation, so Markdown nests them too
current[1].append(line.rstrip())
elif current is not None and current[1] and line.startswith(' '):
current[1][-1] += ' ' + line.strip()
else:
raise ValueError('CHANGELOG.rst line {}: cannot convert {!r} to release notes; '
'expected a **Group** line, a bullet or an indented continuation'.format(lineno, line))

out = []
for title, items in groups:
if items:
out += ['## ' + title, ''] + [item.replace('``', '`') for item in items] + ['']
if not out:
raise ValueError('The {} section of CHANGELOG.rst has no entries'.format(version))
return '\n'.join(out)


@task(help={
'tag': 'Version (tag) to get the notes for, e.g. v2.1.0 or 2.1.0.',
'output': 'File to also write the notes to.'})
def release_notes(ctx, tag, output=None):
"""Prints the GitHub release notes of a version, taken from CHANGELOG.rst."""
version = tag[1:] if tag.startswith('v') else tag
with open(os.path.join(BASE_FOLDER, 'CHANGELOG.rst'), encoding='utf-8') as changelog:
try:
notes = changelog_to_markdown(changelog.read(), version)
except ValueError as e:
raise Exit(str(e))

if output:
with open(output, 'w', encoding='utf-8') as f:
f.write(notes)
print(notes)


@task(help={
'release_type': 'Type of release follows semver rules. Must be one of: major, minor, patch.'})
def release(ctx, release_type):
Expand Down