Repository navigation
145 lines (122 loc) · 5.6 KB
/
Copy pathprepare-release.yml
File metadata and controls
145 lines (122 loc) · 5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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 }}