-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathaction.yml
More file actions
378 lines (342 loc) · 14.4 KB
/
action.yml
File metadata and controls
378 lines (342 loc) · 14.4 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
name: 'Changelogs'
description: 'Create a PR with version bumps and changelogs, and publish packages when merged'
author: 'wevm'
branding:
icon: 'package'
color: 'orange'
inputs:
ecosystem:
description: 'Ecosystem to use (rust, python). Auto-detected if not specified.'
required: false
crate-token:
description: 'Crates.io API token for publishing (Rust)'
required: false
pypi-token:
description: 'PyPI API token for publishing (Python)'
required: false
python-version:
description: 'Python version for building and publishing (Python ecosystem)'
required: false
default: '3.11'
commit:
description: 'Commit message for version bump (overrides conventional-commit)'
required: false
conventional-commit:
description: 'Use conventional commit format (chore: prefix)'
required: false
default: 'false'
branch:
description: 'Branch name for the version PR'
required: false
default: 'changelog-release/main'
post-version-command:
description: 'Command to run after version bumps but before PR creation (e.g. refreshing lockfiles).'
required: false
github-token:
description: 'GitHub token for creating PRs'
required: false
default: ${{ github.token }}
outputs:
hasChangelogs:
description: 'Whether there are pending changelogs'
value: ${{ steps.check.outputs.hasChangelogs }}
pullRequestNumber:
description: 'The pull request number if created or updated'
value: ${{ steps.pr.outputs.pull-request-number }}
published:
description: 'Whether packages were published'
value: ${{ steps.publish.outputs.published }}
publishedPackages:
description: 'JSON array of published packages'
value: ${{ steps.publish.outputs.publishedPackages }}
runs:
using: 'composite'
steps:
- name: Setup Python (for Python ecosystem)
if: inputs.ecosystem == 'python' || inputs.pypi-token != ''
uses: actions/setup-python@v5
with:
python-version: ${{ inputs.python-version }}
- name: Install Python build tools (for Python ecosystem)
if: inputs.ecosystem == 'python' || inputs.pypi-token != ''
shell: bash
run: pip install build twine
- name: Cache changelogs binary
id: cache
uses: actions/cache@v4
with:
path: ~/.cargo/bin/changelogs
key: changelogs-${{ runner.os }}-${{ runner.arch }}-${{ github.action_ref || hashFiles('Cargo.toml') }}
- name: Install changelogs
if: steps.cache.outputs.cache-hit != 'true'
shell: bash
run: cargo install --path "${{ github.action_path }}"
- name: Check for changelogs
id: check
shell: bash
run: |
if [ -d ".changelog" ] && [ "$(find .changelog -name '*.md' ! -name 'README.md' 2>/dev/null | head -1)" ]; then
echo "hasChangelogs=true" >> $GITHUB_OUTPUT
echo "Found pending changelogs"
else
echo "hasChangelogs=false" >> $GITHUB_OUTPUT
echo "No pending changelogs"
fi
# Version mode: Create PR when changelogs exist
- name: Setup Git user
if: steps.check.outputs.hasChangelogs == 'true'
shell: bash
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
- name: Run version command
if: steps.check.outputs.hasChangelogs == 'true'
id: version
shell: bash
run: |
export PATH="$HOME/.local/bin:$PATH"
ECOSYSTEM_FLAG=""
if [ -n "${{ inputs.ecosystem }}" ]; then
ECOSYSTEM_FLAG="--ecosystem ${{ inputs.ecosystem }}"
fi
output=$(changelogs $ECOSYSTEM_FLAG version 2>&1) && version_exit=0 || version_exit=$?
echo "$output"
if [ $version_exit -ne 0 ]; then
echo "::error::changelogs version failed with exit code $version_exit"
exit $version_exit
fi
# Check if using root changelog format (unified versioning)
changelog_format="per-crate"
if [ -f ".changelog/config.toml" ]; then
fmt=$(grep -E '^\s*format\s*=' .changelog/config.toml | sed 's/.*=\s*"\(.*\)"/\1/' | tr -d '[:space:]')
if [ "$fmt" = "root" ]; then
changelog_format="root"
fi
fi
# Extract versions from output (e.g., "tempo-common 0.1.4 → 0.1.5" -> "tempo-common@0.1.5")
all_versions=$(echo "$output" | grep -oE '[a-zA-Z0-9_-]+ [0-9]+\.[0-9]+\.[0-9]+ → [0-9]+\.[0-9]+\.[0-9]+' | sed 's/ .* → /@/')
count=$(echo "$all_versions" | grep -c . || echo 0)
if [ "$changelog_format" = "root" ]; then
# Root format: all packages share one version, use v{version}
new_version=$(echo "$all_versions" | head -1 | sed 's/.*@//')
if [ -n "$new_version" ]; then
versions="\`v$new_version\`"
else
versions=""
fi
elif [ "$count" -eq 0 ]; then
versions=""
elif [ "$count" -eq 1 ]; then
versions="\`$(echo "$all_versions" | head -1)\`"
elif [ "$count" -eq 2 ]; then
first=$(echo "$all_versions" | head -1)
second=$(echo "$all_versions" | tail -1)
versions="\`$first\` and \`$second\`"
else
first=$(echo "$all_versions" | head -1)
second=$(echo "$all_versions" | sed -n '2p')
remaining=$((count - 2))
versions="\`$first\`, \`$second\` and $remaining more"
fi
echo "versions=$versions" >> $GITHUB_OUTPUT
# Build title and commit message with optional conventional commit prefix
if [ "${{ inputs.conventional-commit }}" = "true" ]; then
echo "title=chore: release $versions" >> $GITHUB_OUTPUT
echo "commit-msg=chore: release $versions" >> $GITHUB_OUTPUT
else
echo "title=Release $versions" >> $GITHUB_OUTPUT
echo "commit-msg=Release $versions" >> $GITHUB_OUTPUT
fi
- name: Run post-version command
if: steps.check.outputs.hasChangelogs == 'true' && inputs.post-version-command != ''
shell: bash
run: ${{ inputs.post-version-command }}
- name: Generate PR body
if: steps.check.outputs.hasChangelogs == 'true'
id: body
shell: bash
run: |
{
echo "This PR was opened by the Changelogs release workflow."
echo ""
echo "When you're ready to release, merge this PR and the packages will be published."
echo ""
echo "---"
echo ""
# Show the new changelog entries (uncommitted changes from changelogs version)
if [ -f "CHANGELOG.md" ]; then
git diff -- CHANGELOG.md | grep '^+' | grep -v '^+++' | sed 's/^+//' | head -100 || true
fi
} > /tmp/pr-body.md
echo "body-file=/tmp/pr-body.md" >> $GITHUB_OUTPUT
- name: Create or update PR
if: steps.check.outputs.hasChangelogs == 'true'
id: pr
uses: peter-evans/create-pull-request@v7
with:
token: ${{ inputs.github-token }}
branch: ${{ inputs.branch }}
title: ${{ steps.version.outputs.title }}
body-path: ${{ steps.body.outputs.body-file }}
base: ${{ github.ref_name }}
commit-message: ${{ inputs.commit || steps.version.outputs.commit-msg }}
delete-branch: true
# Publish mode: Publish when no changelogs (PR was just merged)
# Will publish to registry if tokens provided, otherwise just creates git tags
- name: Setup Git user for tags
if: steps.check.outputs.hasChangelogs == 'false'
shell: bash
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
- name: Publish packages
if: steps.check.outputs.hasChangelogs == 'false'
id: publish
shell: bash
env:
CARGO_REGISTRY_TOKEN: ${{ inputs.crate-token }}
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ inputs.pypi-token }}
run: |
export PATH="$HOME/.local/bin:$PATH"
ECOSYSTEM_FLAG=""
if [ -n "${{ inputs.ecosystem }}" ]; then
ECOSYSTEM_FLAG="--ecosystem ${{ inputs.ecosystem }}"
fi
output=$(changelogs $ECOSYSTEM_FLAG publish 2>&1) && publish_exit=0 || publish_exit=$?
echo "$output"
# Parse published packages from output (✓ = published, ⊘ = skipped/tags-only)
packages=$(echo "$output" | { grep -E "^\s+\S+\s+v[0-9]" || true; } | { grep -E "✓|⊘" || true; } | awk '{print $1}' | jq -R -s -c 'split("\n") | map(select(length > 0))')
if [ "$packages" != "[]" ] && [ -n "$packages" ]; then
echo "published=true" >> $GITHUB_OUTPUT
echo "publishedPackages=$packages" >> $GITHUB_OUTPUT
else
echo "published=false" >> $GITHUB_OUTPUT
echo "publishedPackages=[]" >> $GITHUB_OUTPUT
fi
if [ $publish_exit -ne 0 ]; then
echo "::error::changelogs publish failed with exit code $publish_exit"
exit $publish_exit
fi
- name: Push git tags
if: steps.check.outputs.hasChangelogs == 'false' && steps.publish.outcome == 'success'
shell: bash
run: git push --follow-tags
- name: Create GitHub releases
if: steps.check.outputs.hasChangelogs == 'false' && steps.publish.outcome == 'success'
shell: bash
env:
GH_TOKEN: ${{ inputs.github-token }}
run: |
# Get tags that were just pushed
for tag in $(git tag --points-at HEAD); do
echo "Creating release for $tag"
# Extract changelog section for this tag
changelog_notes=""
# Parse package name from tag (e.g., "tidx@0.1.1" -> "tidx")
if [[ "$tag" == *"@"* ]]; then
pkg_name="${tag%@*}"
else
pkg_name=""
fi
# Find the changelog file (per-crate or root)
changelog_file=""
if [ -n "$pkg_name" ]; then
# Check common locations for per-crate changelogs
for dir in "crates/$pkg_name" "packages/$pkg_name" "$pkg_name"; do
if [ -f "$dir/CHANGELOG.md" ]; then
changelog_file="$dir/CHANGELOG.md"
break
fi
done
# Fallback: try stripping first prefix (e.g., "tempo-alloy" -> "alloy")
if [ -z "$changelog_file" ]; then
stripped="${pkg_name#*-}"
if [ "$stripped" != "$pkg_name" ]; then
for dir in "crates/$stripped" "packages/$stripped" "$stripped"; do
if [ -f "$dir/CHANGELOG.md" ]; then
changelog_file="$dir/CHANGELOG.md"
break
fi
done
fi
fi
fi
# Fall back to root CHANGELOG.md
if [ -z "$changelog_file" ] && [ -f "CHANGELOG.md" ]; then
changelog_file="CHANGELOG.md"
fi
if [ -n "$changelog_file" ]; then
# Extract version from tag: "pkg@1.2.3" -> "1.2.3", "v1.2.3" -> "1.2.3"
if [[ "$tag" == v* ]]; then
version="${tag#v}"
elif [[ "$tag" == *"@"* ]]; then
version="${tag#*@}"
else
version="$tag"
fi
changelog_notes=$(awk -v ver="$version" -v tag="$tag" '
BEGIN {
printing=0
root_prefix="## " ver
tag_prefix="## `" tag "`"
}
/^## / {
if (printing) exit
if (index($0, tag_prefix) == 1) { printing=1; next }
if (index($0, root_prefix) == 1) {
c = substr($0, length(root_prefix) + 1, 1)
if (c == " " || c == "(" || c == "") { printing=1; next }
}
}
printing { print }
' "$changelog_file")
fi
# Find previous tag with the same package prefix for accurate changelog diff
previous_tag=""
if [[ "$tag" == *"@"* ]]; then
prefix="${tag%@*}@"
# Pick the next-lower version after current tag, with grep fallback
previous_tag=$(git tag --list "${prefix}*" --sort=-version:refname | awk -v cur="$tag" '$0==cur{found=1;next} found{print;exit}')
if [ -z "$previous_tag" ]; then
previous_tag=$(git tag --list "${prefix}*" --sort=-version:refname | grep -Fxv -- "$tag" | head -1)
fi
else
previous_tag=$(git tag --list "v*" --sort=-version:refname | awk -v cur="$tag" '$0==cur{found=1;next} found{print;exit}')
if [ -z "$previous_tag" ]; then
previous_tag=$(git tag --list "v*" --sort=-version:refname | grep -Fxv -- "$tag" | head -1)
fi
fi
# Generate GitHub's notes to get "New Contributors" and "Full Changelog"
generate_notes_args=(-f tag_name="$tag")
if [ -n "$previous_tag" ]; then
generate_notes_args+=(-f previous_tag_name="$previous_tag")
fi
github_notes=$(gh api repos/${{ github.repository }}/releases/generate-notes \
"${generate_notes_args[@]}" \
--jq '.body' 2>/dev/null || echo "")
# Remove "What's Changed" section, keep "New Contributors" and "Full Changelog"
github_extras=$(echo "$github_notes" | awk '
BEGIN { skip=0 }
/^## What'\''s Changed/ { skip=1; next }
/^## New Contributors/ { skip=0 }
/^\*\*Full Changelog\*\*/ { skip=0 }
!skip { print }
')
# For first-ever per-crate release, skip GitHub extras (no meaningful diff)
if [[ "$tag" == *"@"* ]] && [ -z "$previous_tag" ]; then
github_extras=""
fi
# Combine: changelog content + GitHub extras (New Contributors, Full Changelog)
if [ -n "$changelog_notes" ]; then
notes="$changelog_notes"
if [ -n "$github_extras" ]; then
notes="$notes"$'\n\n'"$github_extras"
fi
echo "$notes" | gh release create "$tag" --notes-file - || true
elif [ -n "$github_notes" ]; then
echo "$github_notes" | gh release create "$tag" --notes-file - || true
else
gh release create "$tag" --notes "" || true
fi
done