Skip to content

Commit 1207705

Browse files
committed
feat(release): allow forcing changelog regen for a recreated tag
When a release + its tag are deleted and recreated from scratch, the CHANGELOG.md section for that version still exists on main. Both guards against duplicate sections then kick in — the workflow skips the bumper ("already has a section") and ships the stale changelog as the release body, and the bumper itself refuses to overwrite. Add an opt-in override: - changelog-bump.sh: FORCE=1 replaces an existing '## <version>' section instead of refusing. The old section is stripped by exact-heading match (string, not regex) so a prefix-sharing version like v1.1.0-rc1 can't be cross-deleted when regenerating v1.1.0. - release.yml: new `regenerate_changelog` boolean workflow_dispatch input threads through as FORCE=1 and always regenerates, bypassing the "already exists → skip" branch. Empty on tag-push, so normal releases are unchanged.
1 parent 81d3c7a commit 1207705

2 files changed

Lines changed: 54 additions & 7 deletions

File tree

‎.github/workflows/release.yml‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ on:
99
release_tag:
1010
description: "The desired tag for the release (e.g. v1.1.0)."
1111
required: true
12+
regenerate_changelog:
13+
description: "Force-regenerate the CHANGELOG.md section for this version, replacing any existing one. Use when a tag was deleted and recreated from scratch so the stale section must be rebuilt from the new commit range."
14+
type: boolean
15+
default: false
1216

1317
permissions:
1418
contents: read
@@ -110,9 +114,22 @@ jobs:
110114
# commit via `GET /commits/{sha}/pulls`. Default GITHUB_TOKEN
111115
# has the required read access and isn't rate-limited (5000/hr).
112116
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
117+
# When the regenerate_changelog input is set (workflow_dispatch
118+
# only; empty on tag-push), force the bumper to replace an
119+
# existing section instead of skipping it. Threads through as
120+
# FORCE=1 to scripts/changelog-bump.sh.
121+
REGENERATE_CHANGELOG: ${{ github.event.inputs.regenerate_changelog }}
113122
run: |
114123
v_re=$(printf '%s' "$RELEASE_VERSION" | sed -e 's/[][().*+?^$\|/]/\\&/g')
115-
if grep -qE "^## ${v_re}( |\$)" CHANGELOG.md 2>/dev/null; then
124+
if [ "${REGENERATE_CHANGELOG}" = "true" ]; then
125+
# Override path: a deleted+recreated tag leaves a stale section
126+
# on main. Rebuild it from the new commit range, replacing the
127+
# old one (FORCE=1). Always regenerates, even if the section
128+
# exists — that's the whole point of the override.
129+
echo "regenerate_changelog=true — force-rebuilding the ${RELEASE_VERSION} section"
130+
VERSION="$RELEASE_VERSION" AI=1 EDIT=0 FORCE=1 scripts/changelog-bump.sh
131+
echo "did_generate=true" >> "$GITHUB_OUTPUT"
132+
elif grep -qE "^## ${v_re}( |\$)" CHANGELOG.md 2>/dev/null; then
116133
echo "CHANGELOG.md already has a section for ${RELEASE_VERSION} — skipping bumper"
117134
echo "did_generate=false" >> "$GITHUB_OUTPUT"
118135
else

‎scripts/changelog-bump.sh‎

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
# VERSION required - e.g. v1.0.0
2626
# AI optional - 0 to skip Gemini draft (default 1)
2727
# EDIT optional - 0 to skip $EDITOR (default 1)
28+
# FORCE optional - 1 to REPLACE an existing '## <version>'
29+
# section instead of refusing (default 0).
30+
# Use when a tag was deleted + recreated from
31+
# scratch and the stale section must be
32+
# regenerated from the new commit range.
2833
# EDITOR optional - defaults to vim
2934
# GEMINI_API_KEY optional - passed through to changelog-ai.sh
3035
# REPO_URL optional - default: https://github.com/magma-Devs/smart-router
@@ -53,14 +58,39 @@ fi
5358
cd "$(git rev-parse --show-toplevel)"
5459
script_dir="$(dirname "$(readlink -f "$0")")"
5560

56-
# Refuse to clobber an existing section. The release workflow detects
57-
# this and skips the bumper entirely on re-runs; if the user is hitting
58-
# this locally, they should either bump VERSION or hand-edit CHANGELOG.md.
61+
# An existing '## <version>' section normally means a re-run: refuse to
62+
# add a duplicate. FORCE=1 overrides this — it strips the old section so
63+
# the fresh one (built below) replaces it. That's the path for a tag that
64+
# was deleted and recreated from scratch: the CHANGELOG.md on main still
65+
# carries the stale section, and we want it regenerated from the new
66+
# commit range, not preserved.
5967
v_re_escaped="$(printf '%s' "$VERSION" | sed -e 's/[][().*+?^$\|/]/\\&/g')"
6068
if [ -f CHANGELOG.md ] && grep -qE "^## ${v_re_escaped}( |\$)" CHANGELOG.md; then
61-
echo "changelog-bump: CHANGELOG.md already has a '## ${VERSION}' section - refusing to add a duplicate" >&2
62-
echo "changelog-bump: edit the existing section by hand, or remove it first" >&2
63-
exit 1
69+
if [ "${FORCE:-0}" = "1" ]; then
70+
echo "changelog-bump: FORCE=1 - replacing existing '## ${VERSION}' section" >&2
71+
# Delete the old section: from its '## <version>' heading up to (but
72+
# not including) the next '## ' heading, or EOF if it's the last one.
73+
# We match the heading by exact string, not regex, so a version whose
74+
# name is a prefix of another (v1.1.0 vs v1.1.0-rc1) can't cross-match:
75+
# the line must be exactly "## <version>" OR "## <version> " (space +
76+
# date suffix). awk `index()`/substr on the raw literal avoids any
77+
# regex-escaping and the `\.`/`\$` warnings that came with it.
78+
awk -v hdr="## ${VERSION}" '
79+
# Start skipping at the EXACT target heading (bare, or "## <ver> <date>").
80+
# The exact/space-prefixed test means a longer version that merely
81+
# starts with this one (v1.1.0 vs v1.1.0-rc1) is not matched here.
82+
$0 == hdr || substr($0, 1, length(hdr) + 1) == hdr " " { skip = 1; next }
83+
# Any subsequent "## " heading is, by definition, a different section
84+
# (the target already consumed itself via next), so it ends the skip.
85+
skip && /^## / { skip = 0 }
86+
!skip { print }
87+
' CHANGELOG.md > CHANGELOG.md.tmp
88+
mv CHANGELOG.md.tmp CHANGELOG.md
89+
else
90+
echo "changelog-bump: CHANGELOG.md already has a '## ${VERSION}' section - refusing to add a duplicate" >&2
91+
echo "changelog-bump: edit the existing section by hand, remove it first, or re-run with FORCE=1" >&2
92+
exit 1
93+
fi
6494
fi
6595

6696
# --exclude "$VERSION" is critical when the release workflow runs on a

0 commit comments

Comments
 (0)