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
47 changes: 42 additions & 5 deletions .github/scripts/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
file records the commit that it was built from.
- ``github-release`` creates the tag and the GitHub release of each released package. It reads the
notes from the merged PR and attaches the files in ``dist/``.
- ``next-dev`` opens the PR for the next development version of the released packages.

PyGithub makes the GitHub calls and GitPython makes the git calls. Only ``uv`` and git-cliff run as
subprocesses, because they have no Python API. The option ``--dry-run`` does not push and does not
Expand All @@ -32,7 +33,7 @@

from connector_notes import commits, notes as connector_notes, repo
from git import Repo
from github import Auth, Github, UnknownObjectException
from github import Auth, Github, GithubException, UnknownObjectException
from github.Repository import Repository


Expand Down Expand Up @@ -115,10 +116,12 @@ def github() -> Repository:
return Github(auth=Auth.Token(os.environ["GH_TOKEN"])).get_repo(os.environ["GITHUB_REPOSITORY"])


def open_pr(branch: str, title: str, body: str, *, dry_run: bool) -> None:
def open_pr(branch: str, title: str, body: str, *, dry_run: bool, auto_merge: bool = False) -> None:
"""Commit the working tree to ``branch``, push it, and open its PR against main.

Update the PR if it is already open.
Update the PR if it is already open. With ``auto_merge``, the PR merges when it has the
approvals and checks that main needs. If the repository does not allow auto-merge, this only
warns.
"""
if dry_run:
print(f"{title}\n{branch}\n\n{body}")
Expand All @@ -137,7 +140,14 @@ def open_pr(branch: str, title: str, body: str, *, dry_run: bool) -> None:
pull.edit(body=body)
else:
pull = remote.create_pull(title=title, body=body, head=branch, base="main")
append("GITHUB_STEP_SUMMARY", f"### {title}\n\n{pull.html_url}\n")
note = ""
if auto_merge:
try:
pull.enable_automerge(merge_method="SQUASH")
note = "\nIt merges when it has the approvals that main needs.\n"
except GithubException as error:
print(f"::warning title=Auto-merge is off::{error.data}")
append("GITHUB_STEP_SUMMARY", f"### {title}\n\n{pull.html_url}\n{note}")


def next_version(git: Repo, package: str, requested: str) -> str:
Expand Down Expand Up @@ -319,6 +329,28 @@ def smoke() -> None:
print("::endgroup::")


def next_dev(entries: dict[str, str], *, dry_run: bool) -> None:
"""Open the PR that moves each released package to its next development version.

Do this only for packages that main still holds at the released version.
"""
names, tags = [], []
for package, version in entries.items():
if version_of(package) != version:
print(f"main already moved {package} past {version}.")
continue
run("uv", "version", "--package", package, "--bump", "minor", "--bump", "dev=0", "--frozen")
names.append(f"{package} {version_of(package).removesuffix('.dev0')}")
tags.append(f"{package}-v{version}")
if not names:
return
run("uv", "lock")
title = f"chore: start {' and '.join(names)} development"
released = " ".join(f"{package}={version}" for package, version in entries.items())
body = f"This PR moves the released packages to their next development version ({released} released). Then builds from main do not look like a release."
open_pr("chore/start-development-after-" + "-".join(tags), title, body, dry_run=dry_run, auto_merge=True)


def parse(text: str) -> dict[str, str]:
"""Read ``package=version`` pairs.

Expand All @@ -338,12 +370,17 @@ def parse(text: str) -> dict[str, str]:
commands.add_parser("released")
commands.add_parser("smoke")
commands.add_parser("github-release").add_argument("--released", required=True)
next_dev_parser = commands.add_parser("next-dev")
next_dev_parser.add_argument("--released", required=True)
next_dev_parser.add_argument("--dry-run", action="store_true")
args = parser.parse_args()
if args.command == "prepare":
prepare(args.package, args.version, dry_run=args.dry_run)
elif args.command == "released":
released()
elif args.command == "smoke":
smoke()
else:
elif args.command == "github-release":
github_release(parse(args.released))
else:
next_dev(parse(args.released), dry_run=args.dry_run)
28 changes: 28 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ name: Release
# 2. Merge the release PR. The same workflow then does these steps:
# - It builds and tests the wheel and the sdist of each package.
# - It creates the tag and the GitHub release.
# - It opens a PR for the next development version. You can merge this PR at any time.
# - It waits for your approval in the pypi environment, then uploads to TestPyPI.
# - It waits for your approval again, then uploads to PyPI.
# The upload steps send the built files. They do not build.
Expand All @@ -24,6 +25,8 @@ name: Release
# The repository settings block PRs from GITHUB_TOKEN, and those PRs do not start CI.
# - A trusted publisher on PyPI and TestPyPI for each package: workflow release.yml, environment
# pypi.
# - The repository setting "Allow auto-merge". With it, the development version PR merges when it
# has the approvals that main needs.

on:
workflow_dispatch:
Expand Down Expand Up @@ -107,6 +110,8 @@ jobs:
permissions:
contents: write
pull-requests: read
outputs:
released: ${{ steps.released.outputs.released }}
steps:
- uses: actions/checkout@v5
with:
Expand Down Expand Up @@ -171,3 +176,26 @@ jobs:
path: dist/
- name: Publish to PyPI
run: uv publish --check-url https://pypi.org/simple/ dist/*

next-development-version:
# Run after the release job, at the same time as the publish jobs.
needs: release
runs-on: ubuntu-latest
steps:
- id: app-token
uses: actions/create-github-app-token@v2
with:
app-id: ${{ vars.RELEASE_APP_ID }}
private-key: ${{ secrets.RELEASE_APP_PRIVATE_KEY }}
permission-contents: write
permission-pull-requests: write
- uses: actions/checkout@v5
with:
ref: main
token: ${{ steps.app-token.outputs.token }}
- uses: astral-sh/setup-uv@v8.2.0
- name: Open the development version PR
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
RELEASED: ${{ needs.release.outputs.released }}
run: uv run .github/scripts/release.py next-dev --released "$RELEASED"
Loading