| 1 |
name: Prepare Release |
| 2 |
|
| 3 |
on: |
| 4 |
workflow_dispatch: |
| 5 |
inputs: |
| 6 |
version: |
| 7 |
description: 'New release version (e.g. 3.17.2)' |
| 8 |
required: true |
| 9 |
|
| 10 |
jobs: |
| 11 |
prepare-release: |
| 12 |
runs-on: ubuntu-24.04 |
| 13 |
permissions: |
| 14 |
contents: write |
| 15 |
pull-requests: write |
| 16 |
steps: |
| 17 |
- name: Validate version format |
| 18 |
run: | |
| 19 |
if ! echo "${{ github.event.inputs.version }}" | grep -Eq '^[0-9]+\.[0-9]+\.[0-9]+$'; then |
| 20 |
echo "Error: Version '${{ github.event.inputs.version }}' must match X.Y.Z format" |
| 21 |
exit 1 |
| 22 |
fi |
| 23 |
|
| 24 |
- name: Checkout main branch |
| 25 |
uses: actions/checkout@v7 |
| 26 |
with: |
| 27 |
ref: main |
| 28 |
|
| 29 |
- name: Get current version |
| 30 |
id: current_version |
| 31 |
run: | |
| 32 |
VERSION=$(grep -Po "(?<=__version__ = ')[0-9]+\.[0-9]+\.[0-9]+" rest_framework/__init__.py) |
| 33 |
echo "version=$VERSION" >> "$GITHUB_OUTPUT" |
| 34 |
|
| 35 |
- name: Update version in __init__.py |
| 36 |
run: | |
| 37 |
sed -i "s/__version__ = '${{ steps.current_version.outputs.version }}'/__version__ = '${{ github.event.inputs.version }}'/" rest_framework/__init__.py |
| 38 |
|
| 39 |
- name: Generate release notes from GitHub API |
| 40 |
env: |
| 41 |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 42 |
NEW_VERSION: ${{ github.event.inputs.version }} |
| 43 |
OLD_VERSION: ${{ steps.current_version.outputs.version }} |
| 44 |
run: | |
| 45 |
set -euo pipefail |
| 46 |
gh api \ |
| 47 |
--method POST \ |
| 48 |
-H "Accept: application/vnd.github+json" \ |
| 49 |
"/repos/${{ github.repository }}/releases/generate-notes" \ |
| 50 |
-f tag_name="$NEW_VERSION" \ |
| 51 |
-f previous_tag_name="$OLD_VERSION" \ |
| 52 |
-f target_commitish=main \ |
| 53 |
--jq '.body' > input.md |
| 54 |
if [ ! -s input.md ]; then |
| 55 |
echo "::error::GitHub returned empty release notes for $OLD_VERSION..$NEW_VERSION" |
| 56 |
exit 1 |
| 57 |
fi |
| 58 |
|
| 59 |
- name: Set up uv |
| 60 |
uses: astral-sh/setup-uv@v7 |
| 61 |
|
| 62 |
- name: Linkify release notes |
| 63 |
run: uv tool run linkify-gh-markdown --heading-level 3 input.md > output.md |
| 64 |
|
| 65 |
- name: Insert release notes into docs |
| 66 |
env: |
| 67 |
NEW_VERSION: ${{ github.event.inputs.version }} |
| 68 |
OLD_VERSION: ${{ steps.current_version.outputs.version }} |
| 69 |
run: | |
| 70 |
python3 << 'EOF' |
| 71 |
import os, re |
| 72 |
from datetime import date |
| 73 |
|
| 74 |
new_version = os.environ['NEW_VERSION'] |
| 75 |
old_version = os.environ['OLD_VERSION'] |
| 76 |
|
| 77 |
new_series = '.'.join(new_version.split('.')[:2]) |
| 78 |
|
| 79 |
with open('output.md') as f: |
| 80 |
new_notes = f.read().strip() |
| 81 |
|
| 82 |
# GitHub wraps the categories in a "What's Changed" heading, which the |
| 83 |
# rest of this file doesn't use. Drop it, then demote the remaining top |
| 84 |
# level headings ("New Contributors") to sit under the version heading. |
| 85 |
new_notes = re.sub(r"^### What's Changed\n+", '', new_notes, flags=re.MULTILINE) |
| 86 |
new_notes = re.sub(r'^### ', '#### ', new_notes, flags=re.MULTILINE) |
| 87 |
|
| 88 |
# GitHub packs the headings against the surrounding bullets, the rest |
| 89 |
# of the file leaves a blank line on either side. |
| 90 |
new_notes = re.sub(r'^(#{4} .*)$', r'\n\1\n', new_notes, flags=re.MULTILINE) |
| 91 |
new_notes = re.sub(r'\n{3,}', '\n\n', new_notes).strip() |
| 92 |
|
| 93 |
today = date.today() |
| 94 |
suffix = 'th' if 11 <= today.day <= 13 else {1: 'st', 2: 'nd', 3: 'rd'}.get(today.day % 10, 'th') |
| 95 |
heading = f'### {new_version}\n\n**Date**: {today.day}{suffix} {today:%B %Y}\n\n' |
| 96 |
new_notes = heading + new_notes |
| 97 |
|
| 98 |
with open('docs/community/release-notes.md') as f: |
| 99 |
content = f.read() |
| 100 |
|
| 101 |
series_header = f'## {new_series}.x series' |
| 102 |
old_version_header = f'### {old_version}' |
| 103 |
|
| 104 |
if series_header in content: |
| 105 |
# Same series: insert before the previous version heading |
| 106 |
content = content.replace( |
| 107 |
old_version_header, |
| 108 |
new_notes + '\n\n' + old_version_header, |
| 109 |
1 |
| 110 |
) |
| 111 |
else: |
| 112 |
# New series: insert a new series block before the first existing one |
| 113 |
first_series = re.search(r'^## \d+\.\d+\.x series', content, re.MULTILINE) |
| 114 |
insert_pos = first_series.start() if first_series else len(content) |
| 115 |
new_block = series_header + '\n\n' + new_notes + '\n\n' |
| 116 |
content = content[:insert_pos] + new_block + content[insert_pos:] |
| 117 |
|
| 118 |
with open('docs/community/release-notes.md', 'w') as f: |
| 119 |
f.write(content) |
| 120 |
EOF |
| 121 |
|
| 122 |
- name: Install gettext |
| 123 |
run: sudo apt install -y gettext |
| 124 |
|
| 125 |
- name: Ensure translations are up to date |
| 126 |
run: uv tool run --from django django-admin compilemessages |
| 127 |
|
| 128 |
- name: Create branch and commit |
| 129 |
run: | |
| 130 |
git config user.name "github-actions[bot]" |
| 131 |
git config user.email "github-actions[bot]@users.noreply.github.com" |
| 132 |
git checkout -b feat/release-${{ github.event.inputs.version }} |
| 133 |
git add rest_framework/__init__.py docs/community/release-notes.md rest_framework/locale |
| 134 |
git commit -m "Prepare release ${{ github.event.inputs.version }}" |
| 135 |
git push origin feat/release-${{ github.event.inputs.version }} |
| 136 |
|
| 137 |
- name: Create pull request |
| 138 |
env: |
| 139 |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 140 |
run: | |
| 141 |
gh pr create \ |
| 142 |
--title "Prepare release ${{ github.event.inputs.version }}" \ |
| 143 |
--body "This PR prepares the release of version ${{ github.event.inputs.version }}." \ |
| 144 |
--base main \ |
| 145 |
--head feat/release-${{ github.event.inputs.version }} |