name: Prepare Release on: workflow_dispatch: inputs: version: description: 'New release version (e.g. 3.17.2)' required: true jobs: prepare-release: runs-on: ubuntu-24.04 permissions: contents: write pull-requests: write steps: - name: Validate version format run: | if ! echo "${{ github.event.inputs.version }}" | grep -Eq '^[0-9]+\.[0-9]+\.[0-9]+$'; then echo "Error: Version '${{ github.event.inputs.version }}' must match X.Y.Z format" exit 1 fi - name: Checkout main branch uses: actions/checkout@v7 with: ref: main - name: Get current version id: current_version run: | VERSION=$(grep -Po "(?<=__version__ = ')[0-9]+\.[0-9]+\.[0-9]+" rest_framework/__init__.py) echo "version=$VERSION" >> "$GITHUB_OUTPUT" - name: Update version in __init__.py run: | sed -i "s/__version__ = '${{ steps.current_version.outputs.version }}'/__version__ = '${{ github.event.inputs.version }}'/" rest_framework/__init__.py - name: Generate release notes from GitHub API env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} NEW_VERSION: ${{ github.event.inputs.version }} OLD_VERSION: ${{ steps.current_version.outputs.version }} run: | set -euo pipefail gh api \ --method POST \ -H "Accept: application/vnd.github+json" \ "/repos/${{ github.repository }}/releases/generate-notes" \ -f tag_name="$NEW_VERSION" \ -f previous_tag_name="$OLD_VERSION" \ -f target_commitish=main \ --jq '.body' > input.md if [ ! -s input.md ]; then echo "::error::GitHub returned empty release notes for $OLD_VERSION..$NEW_VERSION" exit 1 fi - name: Set up uv uses: astral-sh/setup-uv@v7 - name: Linkify release notes run: uv tool run linkify-gh-markdown --heading-level 3 input.md > output.md - name: Insert release notes into docs env: NEW_VERSION: ${{ github.event.inputs.version }} OLD_VERSION: ${{ steps.current_version.outputs.version }} run: | python3 << 'EOF' import os, re from datetime import date new_version = os.environ['NEW_VERSION'] old_version = os.environ['OLD_VERSION'] new_series = '.'.join(new_version.split('.')[:2]) with open('output.md') as f: new_notes = f.read().strip() # GitHub wraps the categories in a "What's Changed" heading, which the # rest of this file doesn't use. Drop it, then demote the remaining top # level headings ("New Contributors") to sit under the version heading. new_notes = re.sub(r"^### What's Changed\n+", '', new_notes, flags=re.MULTILINE) new_notes = re.sub(r'^### ', '#### ', new_notes, flags=re.MULTILINE) # GitHub packs the headings against the surrounding bullets, the rest # of the file leaves a blank line on either side. new_notes = re.sub(r'^(#{4} .*)$', r'\n\1\n', new_notes, flags=re.MULTILINE) new_notes = re.sub(r'\n{3,}', '\n\n', new_notes).strip() today = date.today() suffix = 'th' if 11 <= today.day <= 13 else {1: 'st', 2: 'nd', 3: 'rd'}.get(today.day % 10, 'th') heading = f'### {new_version}\n\n**Date**: {today.day}{suffix} {today:%B %Y}\n\n' new_notes = heading + new_notes with open('docs/community/release-notes.md') as f: content = f.read() series_header = f'## {new_series}.x series' old_version_header = f'### {old_version}' if series_header in content: # Same series: insert before the previous version heading content = content.replace( old_version_header, new_notes + '\n\n' + old_version_header, 1 ) else: # New series: insert a new series block before the first existing one first_series = re.search(r'^## \d+\.\d+\.x series', content, re.MULTILINE) insert_pos = first_series.start() if first_series else len(content) new_block = series_header + '\n\n' + new_notes + '\n\n' content = content[:insert_pos] + new_block + content[insert_pos:] with open('docs/community/release-notes.md', 'w') as f: f.write(content) EOF - name: Install gettext run: sudo apt install -y gettext - name: Ensure translations are up to date run: uv tool run --from django django-admin compilemessages - name: Create branch and commit run: | git config user.name "github-actions[bot]" git config user.email "github-actions[bot]@users.noreply.github.com" git checkout -b feat/release-${{ github.event.inputs.version }} git add rest_framework/__init__.py docs/community/release-notes.md rest_framework/locale git commit -m "Prepare release ${{ github.event.inputs.version }}" git push origin feat/release-${{ github.event.inputs.version }} - name: Create pull request env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | gh pr create \ --title "Prepare release ${{ github.event.inputs.version }}" \ --body "This PR prepares the release of version ${{ github.event.inputs.version }}." \ --base main \ --head feat/release-${{ github.event.inputs.version }}