-
Notifications
You must be signed in to change notification settings - Fork 0
207 lines (190 loc) · 8.29 KB
/
Copy pathchangelog-sync.yml
File metadata and controls
207 lines (190 loc) · 8.29 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
name: VS Code Extension — Metadata Sync
on:
pull_request:
types: [opened, synchronize, reopened]
paths:
- "CHANGELOG.md"
- "README.md"
concurrency:
group: changelog-sync-${{ github.event.pull_request.number }}
cancel-in-progress: true
permissions:
contents: write
pull-requests: read
jobs:
sync:
runs-on: ubuntu-latest
# Don't run on forks — GITHUB_TOKEN can't push to fork PR branches
if: github.event.pull_request.head.repo.full_name == github.repository
steps:
- name: Checkout PR head
uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
with:
ref: ${{ github.event.pull_request.head.ref }}
token: ${{ secrets.GITHUB_TOKEN }}
persist-credentials: false
fetch-depth: 0
- name: Configure git credentials for push
run: git config --global url."https://x-access-token:$GITHUB_TOKEN@github.com/".insteadOf "https://github.com/"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Extract top CHANGELOG version
id: changelog
run: |
set -euo pipefail
FILE=CHANGELOG.md
# Match lines like: "## 0.1.1 — 2026-04-22" (em-dash, en-dash, or ASCII hyphen)
TOP=$(awk '/^## [0-9]+\.[0-9]+\.[0-9]+/ { print; exit }' "$FILE")
if [ -z "$TOP" ]; then
echo "::error file=$FILE::No valid '## X.Y.Z — DATE' heading found"
exit 1
fi
VERSION=$(echo "$TOP" | grep -oE '^## [0-9]+\.[0-9]+\.[0-9]+' | sed 's/^## //')
if [ -z "$VERSION" ]; then
echo "::error file=$FILE::Could not parse semver from top heading: $TOP"
exit 1
fi
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
echo "Top CHANGELOG version: $VERSION"
- name: Compare with package.json
id: compare
run: |
set -euo pipefail
PJ=package.json
CURRENT=$(jq -r '.version' "$PJ")
TARGET=${{ steps.changelog.outputs.version }}
echo "package.json: $CURRENT"
echo "changelog: $TARGET"
# Compare via sort -V (semver-ish)
newest=$(printf '%s\n%s\n' "$CURRENT" "$TARGET" | sort -V | tail -1)
if [ "$CURRENT" = "$TARGET" ]; then
echo "status=in-sync" >> "$GITHUB_OUTPUT"
elif [ "$newest" = "$TARGET" ] && [ "$CURRENT" != "$TARGET" ]; then
echo "status=bump-needed" >> "$GITHUB_OUTPUT"
else
echo "::error file=$PJ::CHANGELOG top version ($TARGET) is lower than package.json ($CURRENT). Did you edit CHANGELOG history instead of prepending?"
exit 1
fi
- name: Extract README description
id: readme
run: |
set -euo pipefail
README=README.md
if [ ! -f "$README" ]; then
echo "README not found; skipping description extraction"
echo "status=missing" >> "$GITHUB_OUTPUT"
exit 0
fi
SCRIPT=$(mktemp)
cat > "$SCRIPT" <<'PY'
import re, sys
path = sys.argv[1]
with open(path) as f:
md = f.read()
lines = md.splitlines()
i = 0
while i < len(lines) and not re.match(r'^#\s+\S', lines[i]):
i += 1
if i >= len(lines):
sys.stderr.write("ERROR: no H1 title found in README\n")
sys.exit(2)
i += 1
while i < len(lines) and lines[i].strip() == "":
i += 1
if i >= len(lines) or lines[i].lstrip().startswith("#"):
sys.stderr.write("ERROR: no description paragraph after H1 title\n")
sys.exit(2)
para = []
while i < len(lines) and lines[i].strip() != "" and not lines[i].lstrip().startswith("#"):
para.append(lines[i].strip())
i += 1
p = " ".join(para)
p = re.sub(r'!\[([^\]]*)\]\([^)]*\)', r'\1', p)
p = re.sub(r'\[([^\]]+)\]\([^)]*\)', r'\1', p)
p = re.sub(r'`([^`]+)`', r'\1', p)
p = re.sub(r'\*\*([^*]+)\*\*', r'\1', p)
p = re.sub(r'\*([^*]+)\*', r'\1', p)
p = re.sub(r'__([^_]+)__', r'\1', p)
p = re.sub(r'_([^_]+)_', r'\1', p)
p = re.sub(r'\s+', ' ', p).strip()
if not p:
sys.stderr.write("ERROR: empty description paragraph after stripping markdown\n")
sys.exit(2)
if len(p) > 200:
sys.stderr.write(f"ERROR: description too long ({len(p)} chars > 200). Shorten the first paragraph of README.md.\n")
sys.exit(2)
sys.stdout.write(p)
PY
DESC=$(python3 "$SCRIPT" "$README")
echo "description<<__README_EOF__" >> "$GITHUB_OUTPUT"
echo "$DESC" >> "$GITHUB_OUTPUT"
echo "__README_EOF__" >> "$GITHUB_OUTPUT"
echo "status=extracted" >> "$GITHUB_OUTPUT"
echo "Extracted description (${#DESC} chars): $DESC"
- name: Compare with package.json description
id: compare_desc
if: steps.readme.outputs.status == 'extracted'
env:
TARGET_DESC: ${{ steps.readme.outputs.description }}
run: |
set -euo pipefail
PJ=package.json
CURRENT=$(jq -r '.description' "$PJ")
if [ "$CURRENT" = "$TARGET_DESC" ]; then
echo "status=description-ok" >> "$GITHUB_OUTPUT"
echo "Description already in sync"
else
echo "status=description-sync-needed" >> "$GITHUB_OUTPUT"
echo "Description drift detected:"
echo " package.json: $CURRENT"
echo " README: $TARGET_DESC"
fi
- name: Bump package.json and commit
if: steps.compare.outputs.status == 'bump-needed' || steps.compare_desc.outputs.status == 'description-sync-needed'
env:
TARGET: ${{ steps.changelog.outputs.version }}
TARGET_DESC: ${{ steps.readme.outputs.description }}
VERSION_STATUS: ${{ steps.compare.outputs.status }}
DESC_STATUS: ${{ steps.compare_desc.outputs.status }}
PR_HEAD_REF: ${{ github.event.pull_request.head.ref }}
run: |
set -euo pipefail
PJ=package.json
# Apply version bump if needed
if [ "$VERSION_STATUS" = "bump-needed" ]; then
jq --arg v "$TARGET" '.version = $v' "$PJ" > "$PJ.tmp"
mv "$PJ.tmp" "$PJ"
fi
# Apply description sync if needed
if [ "$DESC_STATUS" = "description-sync-needed" ]; then
jq --arg d "$TARGET_DESC" '.description = $d' "$PJ" > "$PJ.tmp"
mv "$PJ.tmp" "$PJ"
fi
# Priority rule: version bump wins the commit message
if [ "$VERSION_STATUS" = "bump-needed" ]; then
MSG="chore: release v$TARGET"
elif [ "$DESC_STATUS" = "description-sync-needed" ]; then
MSG="chore(vscode-extension): sync description from README"
else
echo "Nothing to commit"
exit 0
fi
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add "$PJ"
git commit -m "$MSG"
git push origin HEAD:"$PR_HEAD_REF"
- name: Summary
run: |
echo "### Metadata sync result" >> $GITHUB_STEP_SUMMARY
echo "- CHANGELOG top version: \`${{ steps.changelog.outputs.version }}\`" >> $GITHUB_STEP_SUMMARY
echo "- Version status: \`${{ steps.compare.outputs.status }}\`" >> $GITHUB_STEP_SUMMARY
echo "- README status: \`${{ steps.readme.outputs.status }}\`" >> $GITHUB_STEP_SUMMARY
if [ "${{ steps.readme.outputs.status }}" = "extracted" ]; then
echo "- Description status: \`${{ steps.compare_desc.outputs.status }}\`" >> $GITHUB_STEP_SUMMARY
fi
if [ "${{ steps.compare.outputs.status }}" = "bump-needed" ]; then
echo "- Auto-committed \`chore: release v${{ steps.changelog.outputs.version }}\` to this PR branch." >> $GITHUB_STEP_SUMMARY
elif [ "${{ steps.compare_desc.outputs.status }}" = "description-sync-needed" ]; then
echo "- Auto-committed \`chore(vscode-extension): sync description from README\` to this PR branch." >> $GITHUB_STEP_SUMMARY
fi