👷 Add CI to create draft release after merging a `release` PR (#1807)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sebastián Ramírez · committed by GitHub · parent 2f54c22 · Browse files · 4 files·+234 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+234 added−1 removed
- tests:+124 added−0 removed
- other:+110 added−1 removed
Provenance
- Commit
90f087a- Tree
5e03d2b- Author
Sebastián Ramírez <tiangolo@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
2f54c22
4 files changed+234 added−1 removed
added:.github/workflows/create-draft-release.yml+56 added
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556name: Create Draft Release
on: pull_request: types: - closed
permissions: {}
jobs: create-draft-release: if: github.event.pull_request.merged == true && contains(github.event.pull_request.labels.*.name, 'release') runs-on: ubuntu-latest timeout-minutes: 5 permissions: contents: write env: PREPARE_RELEASE_VERSION_FILE: typer/__init__.py PREPARE_RELEASE_RELEASE_NOTES_FILE: docs/release-notes.md steps: - name: Dump GitHub context env: GITHUB_CONTEXT: ${{ toJson(github) }} run: echo "$GITHUB_CONTEXT" - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 with: ref: ${{ github.event.repository.default_branch }} persist-credentials: true - name: Set up Python uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0 with: python-version-file: ".python-version" - name: Install uv uses: astral-sh/setup-uv@08807647e7069bb48b6ef5acd8ec9567f424441b # v8.1.0 with: # Before upgrading uv version, make sure astral-sh/setup-uv knows its checksum. # See: https://github.com/astral-sh/setup-uv/issues/851#issuecomment-4282017837 version: "0.11.4" - name: Extract release details id: release-details run: | set -euo pipefail version="$(uv run python scripts/prepare_release.py current-version)" uv run python scripts/prepare_release.py release-notes > draft-release-notes.md echo "version=$version" >> "$GITHUB_OUTPUT" - name: Create draft release env: GH_TOKEN: ${{ github.token }} VERSION: ${{ steps.release-details.outputs.version }} run: | set -euo pipefail gh release create "$VERSION" \ --draft \ --title "$VERSION" \ --notes-file draft-release-notes.md \ --target "$(git rev-parse HEAD)"modified:.github/workflows/publish.yml+1 added-1 removed
12345667891029 unmodified linesname: Publish
on: release: types: - created - published
permissions: {}
jobs:29 unmodified linesmodified:scripts/prepare_release.py+53 added
6 unmodified lines7 8 import typer9 10 VERSION_PATTERN = re.compile(r'(?m)^__version__ = "(\d+\.\d+\.\d+)"$')11+VERSION_HEADING_PATTERN = re.compile(r"(?m)^## (\d+\.\d+\.\d+)(?: \([^)]+\))?$")12 RELEASE_NOTES_HEADER = "# Release Notes\n\n"13 LATEST_CHANGES_HEADER = "## Latest Changes"14 BumpType = Literal["major", "minor", "patch"]15 57 unmodified lines73 1,74 )75 76 77+def get_release_notes_body(content: str, version: str, release_notes_file: Path) -> str:78+ version_heading = re.compile(rf"(?m)^## {re.escape(version)}(?: \([^)]+\))?$")79+ match = version_heading.search(content)80+ if not match:81+ raise RuntimeError(82+ f"Could not find release notes section for {version} in {release_notes_file}"83+ )84+85+ next_match = VERSION_HEADING_PATTERN.search(content, match.end())86+ end = next_match.start() if next_match else len(content)87+ body = content[match.end() : end].strip()88+ if not body:89+ raise RuntimeError(90+ f"Release notes section for {version} in {release_notes_file} is empty"91+ )92+ return f"{body}\n"93+94+95 @app.command()96 def prepare(97 bump: Annotated[98 BumpType,71 unmodified lines170 ) -> None:171 typer.echo(get_current_version(version_file.read_text(), version_file))172 173 174+@app.command()175+def release_notes(176+ version_file: Annotated[177+ Path,178+ typer.Option(179+ envvar="PREPARE_RELEASE_VERSION_FILE",180+ exists=True,181+ file_okay=True,182+ dir_okay=False,183+ readable=True,184+ help="Path to the Python file containing the __version__ assignment.",185+ ),186+ ],187+ release_notes_file: Annotated[188+ Path,189+ typer.Option(190+ envvar="PREPARE_RELEASE_RELEASE_NOTES_FILE",191+ exists=True,192+ file_okay=True,193+ dir_okay=False,194+ readable=True,195+ help="Path to the release notes Markdown file.",196+ ),197+ ],198+) -> None:199+ version = get_current_version(version_file.read_text(), version_file)200+ typer.echo(201+ get_release_notes_body(202+ release_notes_file.read_text(), version, release_notes_file203+ ),204+ nl=False,205+ )206+207+208 if __name__ == "__main__":209 app()modified:tests/test_prepare_release.py+124 added
6 unmodified lines7 from scripts.prepare_release import (8 BumpType,9 app,10 bump_version,11+ get_release_notes_body,12 update_release_notes,13 update_version_file,14 )15 81 unmodified lines97 content, "0.26.3", date(2026, 5, 28), Path("docs/release-notes.md")98 )99 100 101+def test_get_release_notes_body_with_dated_heading() -> None:102+ content = """# Release Notes103+104+## Latest Changes105+106+## 0.26.3 (2026-05-28)107+108+### Fixes109+110+* Fix something.111+112+## 0.26.2 (2026-05-27)113+114+### Fixes115+116+* Previous fix.117+"""118+119+ body = get_release_notes_body(content, "0.26.3", Path("docs/release-notes.md"))120+121+ assert (122+ body123+ == """### Fixes124+125+* Fix something.126+"""127+ )128+129+130+def test_get_release_notes_body_with_plain_heading() -> None:131+ content = """# Release Notes132+133+## Latest Changes134+135+## 0.26.3136+137+### Fixes138+139+* Fix something.140+"""141+142+ body = get_release_notes_body(content, "0.26.3", Path("docs/release-notes.md"))143+144+ assert body == "### Fixes\n\n* Fix something.\n"145+146+147+def test_get_release_notes_body_allows_non_version_h2_content() -> None:148+ content = """# Release Notes149+150+## Latest Changes151+152+## 0.26.3153+154+## Highlights155+156+* Fix something.157+158+## 0.26.2159+160+* Previous fix.161+"""162+163+ body = get_release_notes_body(content, "0.26.3", Path("docs/release-notes.md"))164+165+ assert body == "## Highlights\n\n* Fix something.\n"166+167+168+def test_get_release_notes_body_requires_version_section() -> None:169+ content = "# Release Notes\n\n## Latest Changes\n"170+171+ with pytest.raises(RuntimeError, match="Could not find"):172+ get_release_notes_body(content, "0.26.3", Path("docs/release-notes.md"))173+174+175+def test_get_release_notes_body_requires_non_empty_section() -> None:176+ content = """# Release Notes177+178+## Latest Changes179+180+## 0.26.3181+182+## 0.26.2183+184+* Previous fix.185+"""186+187+ with pytest.raises(RuntimeError, match="is empty"):188+ get_release_notes_body(content, "0.26.3", Path("docs/release-notes.md"))189+190+191 def test_cli_updates_configured_files(tmp_path: Path) -> None:192 version_file = tmp_path / "package" / "__init__.py"193 version_file.parent.mkdir()194 version_file.write_text('__version__ = "0.26.2"\n')64 unmodified lines259 )260 261 assert result.exit_code == 0, result.output262 assert result.output == "0.26.2\n"263+264+265+def test_cli_prints_release_notes(tmp_path: Path) -> None:266+ version_file = tmp_path / "package" / "__init__.py"267+ version_file.parent.mkdir()268+ version_file.write_text('__version__ = "0.26.3"\n')269+ release_notes_file = tmp_path / "release-notes.md"270+ release_notes_file.write_text(271+ """# Release Notes272+273+## Latest Changes274+275+## 0.26.3 (2026-05-28)276+277+### Fixes278+279+* Fix something.280+"""281+ )282+283+ result = runner.invoke(284+ app,285+ [286+ "release-notes",287+ "--version-file",288+ str(version_file),289+ "--release-notes-file",290+ str(release_notes_file),291+ ],292+ )293+294+ assert result.exit_code == 0, result.output295+ assert result.output == "### Fixes\n\n* Fix something.\n"