-
Notifications
You must be signed in to change notification settings - Fork 34
249 lines (214 loc) · 8.83 KB
/
Copy pathrelease-drafter.yml
File metadata and controls
249 lines (214 loc) · 8.83 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
name: Release Drafter
on:
push:
branches:
- main
pull_request:
types: [opened, reopened, synchronize]
permissions:
contents: read
jobs:
update_release_draft:
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
permissions:
contents: write
pull-requests: write
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v7
with:
fetch-depth: 0
- name: Set up Python
uses: actions/setup-python@v7
with:
python-version: '3.14'
- name: Install victron-mqtt library
run: pip install victron-mqtt
- name: Find previous release tag
id: prev_tag
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
prev=$(gh release list --json tagName,isDraft -q '[.[] | select(.isDraft | not)] | .[0].tagName')
echo "Previous release tag: $prev"
echo "tag=$prev" >> $GITHUB_OUTPUT
- name: Get commit messages since previous tag
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
prev_tag="${{ steps.prev_tag.outputs.tag }}"
if [ -z "$prev_tag" ]; then
prev_tag=$(git rev-list --max-parents=0 HEAD)
fi
COMMITS=$(git log "$prev_tag"..HEAD --pretty=format:"- %s" | head -20)
echo "COMMITS<<EOF" >> $GITHUB_ENV
echo "$COMMITS" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
# Collect unique commit authors as GitHub usernames
AUTHORS=$(git log "$prev_tag"..HEAD --pretty=format:"%an" | sort -u | while read -r name; do
email=$(git log --author="$name" --pretty=format:"%ae" -1)
username=$(echo "$email" | sed -n 's/.*+\(.*\)@users.noreply.github.com/\1/p')
if [ -n "$username" ]; then
echo "@$username"
else
username=$(gh api "/search/users?q=$email+in:email" --jq '.items[0].login // empty' 2>&1 || echo "")
# If gh api failed, username will contain error JSON — reject anything that's not a simple word
if [[ "$username" =~ ^[a-zA-Z0-9_-]+$ ]]; then
echo "@$username"
else
echo "@$name"
fi
fi
done | sort -u | paste -sd ',' | sed 's/,/, /g')
echo "AUTHORS=$AUTHORS" >> $GITHUB_ENV
- name: Detect breaking changes
id: breaking
run: |
prev_tag="${{ steps.prev_tag.outputs.tag }}"
if [ -z "$prev_tag" ]; then
echo "found=false" >> $GITHUB_OUTPUT
exit 0
fi
git show "${prev_tag}:victron_mqtt.json" > old.json 2>/dev/null || true
if [ ! -f old.json ] || [ ! -s old.json ]; then
echo "found=false" >> $GITHUB_OUTPUT
exit 0
fi
set +e
breaking_output=$(python -m victron_mqtt.utils.detect_breaking_changes old.json victron_mqtt.json 2>/dev/null)
exit_code=$?
set -e
rm -f old.json
if [ $exit_code -eq 1 ] && [ -n "$breaking_output" ]; then
echo "found=true" >> $GITHUB_OUTPUT
echo "BREAKING_CONTENT<<EOF" >> $GITHUB_ENV
echo "$breaking_output" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
else
echo "found=false" >> $GITHUB_OUTPUT
fi
- name: Collect library release notes
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
prev_tag="${{ steps.prev_tag.outputs.tag }}"
# Get old and new library versions
old_ver=""
if [ -n "$prev_tag" ]; then
old_ver=$(git show "${prev_tag}:custom_components/victron_mqtt/_vendor/__init__.py" 2>/dev/null \
| sed -n 's/.*VICTRON_MQTT_VERSION *= *"\(.*\)".*/\1/p')
fi
new_ver=$(sed -n 's/.*VICTRON_MQTT_VERSION *= *"\(.*\)".*/\1/p' custom_components/victron_mqtt/_vendor/__init__.py)
echo "Old library version: $old_ver"
echo "New library version: $new_ver"
if [ -z "$old_ver" ] || [ "$old_ver" = "$new_ver" ]; then
echo "LIB_NOTES=" >> $GITHUB_ENV
exit 0
fi
# Fetch all library releases between old and new version
lib_notes=""
releases=$(gh release list --repo tomer-w/victron_mqtt --json tagName,isDraft -q '[.[] | select(.isDraft | not)] | .[].tagName')
collecting=false
while IFS= read -r tag; do
ver="${tag#v}"
if [ "$ver" = "$old_ver" ]; then
break
fi
if [ "$ver" = "$new_ver" ]; then
collecting=true
fi
if [ "$collecting" = true ]; then
body=$(gh release view "$tag" --repo tomer-w/victron_mqtt --json body -q .body 2>/dev/null || true)
if [ -n "$body" ]; then
lib_notes="${lib_notes}
### ${tag}
${body}
"
fi
fi
done <<< "$releases"
echo "LIB_NOTES<<EOF" >> $GITHUB_ENV
echo "$lib_notes" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
- uses: release-drafter/release-drafter@v7
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Enhance draft release notes
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
DRAFT_TAG=$(gh release list --limit 50 --json isDraft,tagName -q '[.[] | select(.isDraft)] | .[0].tagName')
if [ -z "$DRAFT_TAG" ] || [ "$DRAFT_TAG" = "null" ]; then
echo "No draft release found"
exit 0
fi
BODY=$(gh release view "$DRAFT_TAG" --json body -q .body)
if [ -n "$COMMITS" ]; then
export BODY
BODY=$(python3 -c "
import os, re
body = os.environ['BODY']
commits = os.environ['COMMITS']
authors = os.environ.get('AUTHORS', '').strip()
lib_notes = os.environ.get('LIB_NOTES', '').strip()
# Clean up library notes: remove Contributors section and Changes headers
if lib_notes:
# Remove ## Contributors section and everything after it per release
lib_notes = re.sub(r'## Contributors[^\n]*\n.*?(?=###|\Z)', '', lib_notes, flags=re.DOTALL)
# Remove ## Changes headers (keep the content)
lib_notes = re.sub(r'^## Changes[^\n]*\n+', '', lib_notes, flags=re.MULTILINE)
# Downgrade any remaining ## headers to ####
lib_notes = re.sub(r'^## ', '#### ', lib_notes, flags=re.MULTILINE)
lib_notes = lib_notes.strip()
# Remove release-drafter's empty placeholders
body = body.replace('No changes\n', '').replace('No changes', '')
body = body.replace('No contributors\n', '').replace('No contributors', '')
# Split body into sections
parts = re.split(r'(## [^\n]+)', body)
# Modify section contents
for i, part in enumerate(parts):
if part.strip() == '## Changes' and i + 1 < len(parts):
existing = parts[i + 1].strip()
# Remove lone asterisk (release-drafter empty placeholder)
if existing == '*':
existing = ''
items = []
if existing:
items.append(existing)
items.append(commits)
parts[i + 1] = '\n\n' + '\n'.join(items) + '\n\n'
# Append library changes section after Changes
if lib_notes:
parts[i + 1] += '## Library Changes\n\n' + lib_notes + '\n\n'
elif part.strip() == '## Contributors' and i + 1 < len(parts):
existing = parts[i + 1].strip()
if not existing and authors:
parts[i + 1] = '\n\n' + authors + '\n'
body = ''.join(parts)
# If no Changes section existed, append library changes at the end
if lib_notes and '## Library Changes' not in body:
body = body.rstrip() + '\n\n## Library Changes\n\n' + lib_notes
if '## Contributors' not in body and authors:
body = body.rstrip() + '\n\n## Contributors\n\n' + authors
print(body.rstrip())
")
fi
# Prepend breaking changes
if [ "${{ steps.breaking.outputs.found }}" = "true" ]; then
BODY="${BREAKING_CONTENT}
${BODY}"
fi
gh release edit "$DRAFT_TAG" --notes "$BODY"
autolabeler:
if: github.event_name == 'pull_request'
permissions:
contents: read
pull-requests: write
runs-on: ubuntu-latest
steps:
- uses: release-drafter/release-drafter@v7
with:
dry-run: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}