-
Notifications
You must be signed in to change notification settings - Fork 328
Expand file tree
/
Copy pathrelease.sh
More file actions
511 lines (431 loc) · 14.3 KB
/
Copy pathrelease.sh
File metadata and controls
511 lines (431 loc) · 14.3 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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
#!/bin/bash
set -e
# Check if gh CLI is available
if ! command -v gh >/dev/null 2>&1; then
echo "❌ gh CLI is required. Install with: brew install gh"
exit 1
fi
# Get repository info
REPO_URL=$(git remote get-url origin 2>/dev/null | sed 's|git@github.com:|https://github.com/|' | sed 's|\.git$||')
REPO_OWNER=$(echo "$REPO_URL" | cut -d'/' -f4)
REPO_NAME=$(echo "$REPO_URL" | cut -d'/' -f5)
if ! git diff --quiet || ! git diff --cached --quiet; then
echo "❌ Working tree is not clean. Commit or stash changes before releasing."
exit 1
fi
CURRENT_VERSION=$(node -p "require('./package.json').version")
echo "Current version: $CURRENT_VERSION"
git fetch --tags origin >/dev/null 2>&1 || true
tag_exists() {
git rev-parse -q --verify "refs/tags/$1" >/dev/null
}
sync_registry_metadata_version() {
local version="$1"
if [ ! -f server.json ]; then
return
fi
node - "$version" <<'NODE'
const fs = require("fs");
const version = process.argv[2];
const packageJson = JSON.parse(fs.readFileSync("package.json", "utf8"));
const serverJson = JSON.parse(fs.readFileSync("server.json", "utf8"));
serverJson.version = version;
for (const pkg of serverJson.packages || []) {
if (!pkg.identifier || pkg.identifier === packageJson.name) {
pkg.version = version;
}
}
fs.writeFileSync("server.json", `${JSON.stringify(serverJson, null, 2)}\n`);
NODE
}
# Pin README/docs npx examples to the PREVIOUS stable release, not the version
# being cut right now: a freshly released version has not been proven in the
# wild yet, so new users get the last known-good release by default. Users who
# always want the newest can use @latest (documented in the READMEs).
sync_pinned_npx_docs_version() {
local version="$1"
if [ -z "$version" ]; then
echo "⚠️ No previous release tag found; leaving pinned docs version unchanged."
return
fi
# Git tags can outpace npm when publish fails; pin must exist on the registry.
if ! npm view "@zereight/mcp-gitlab@${version}" version >/dev/null 2>&1; then
local npm_pin
npm_pin=$(
npm view @zereight/mcp-gitlab versions --json 2>/dev/null | node -e '
const wanted = process.argv[1];
let raw = "";
process.stdin.on("data", (c) => (raw += c));
process.stdin.on("end", () => {
const versions = JSON.parse(raw);
const pin = [...versions].reverse().find(
(v) => v.localeCompare(wanted, undefined, { numeric: true }) < 0
);
if (!pin) process.exit(1);
process.stdout.write(pin);
});
' "$version"
) || true
if [ -n "$npm_pin" ]; then
echo "⚠️ @$version is not on npm; pinning docs to @$npm_pin instead."
version="$npm_pin"
else
echo "⚠️ Could not resolve an npm-published pin for @$version; leaving docs unchanged."
return
fi
fi
node - "$version" <<'NODE'
const fs = require("fs");
const path = require("path");
const version = process.argv[2];
const roots = ["README.md", "README.ko.md", "README.zh-CN.md", "docs"];
function files(root) {
if (!fs.existsSync(root)) return [];
const stat = fs.statSync(root);
if (stat.isFile()) return root.endsWith(".md") ? [root] : [];
return fs.readdirSync(root, { withFileTypes: true }).flatMap((entry) => {
const next = path.join(root, entry.name);
if (entry.isDirectory()) return files(next);
return entry.isFile() && next.endsWith(".md") ? [next] : [];
});
}
for (const file of roots.flatMap(files)) {
const before = fs.readFileSync(file, "utf8");
const after = before.replace(/@zereight\/mcp-gitlab@\d+\.\d+\.\d+/g, `@zereight/mcp-gitlab@${version}`);
if (after !== before) fs.writeFileSync(file, after);
}
NODE
}
preflight_registry_metadata() {
if [ ! -f server.json ]; then
return
fi
npm run release:mcp-registry -- --check
}
build_docs() {
make docs
}
# Determine the previous tag for changelog
get_previous_tag() {
local current_tag="$1"
if [ -n "$current_tag" ]; then
# Find the tag before the current one
git describe --tags --abbrev=0 "${current_tag}^" 2>/dev/null || echo ""
else
git describe --tags --abbrev=0 2>/dev/null || echo ""
fi
}
# Fetch PR details from GitHub API
fetch_pr_details() {
local pr_number="$1"
local response
response=$(gh api "repos/$REPO_OWNER/$REPO_NAME/pulls/$pr_number" 2>/dev/null)
if [ -n "$response" ]; then
echo "$response"
fi
}
# Get merged PRs since a specific tag
get_merged_prs() {
local since_tag="$1"
local prs
if [ -n "$since_tag" ]; then
# Get commits since the tag
local commits
commits=$(git log "$since_tag"..HEAD --oneline --format="%s" 2>/dev/null || echo "")
else
local commits
commits=$(git log --oneline -50 --format="%s" 2>/dev/null || echo "")
fi
# Extract PR numbers from commit messages (e.g., "feat: add something (#123)")
echo "$commits" | grep -oE '#[0-9]+' | sed 's/#//' | sort -u
}
format_contributors() {
local range="$1"
local limit="$2"
git shortlog -sne "$range" | head -n "$limit" | awk '
{
count = $1
sub(/^[[:space:]]*[0-9]+[[:space:]]+/, "", $0)
name = $0
email = ""
if (match(name, /<[^>]+>$/)) {
email = substr(name, RSTART + 1, RLENGTH - 2)
}
sub(/[[:space:]]*<[^>]+>[[:space:]]*$/, "", name)
mention = name
if (email ~ /^[0-9]+\+[^@]+@users\.noreply\.github\.com$/) {
mention = email
sub(/^[0-9]+\+/, "", mention)
sub(/@users\.noreply\.github\.com$/, "", mention)
mention = "@" mention
} else if (email ~ /^[^@]+@users\.noreply\.github\.com$/) {
mention = email
sub(/@users\.noreply\.github\.com$/, "", mention)
mention = "@" mention
} else {
gsub(/^[[:space:]]+|[[:space:]]+$/, "", mention)
gsub(/[^A-Za-z0-9-]/, "-", mention)
gsub(/-+/, "-", mention)
gsub(/^-|-$/, "", mention)
if (mention != "") {
mention = "@" mention
}
}
if (mention != "") {
suffix = count == 1 ? "commit" : "commits"
printf "- %s (%s %s)\n", mention, count, suffix
}
}
'
}
format_pr_contributors() {
local pr_numbers="$1"
local fallback_range="$2"
local limit="$3"
if [ -z "$pr_numbers" ]; then
format_contributors "$fallback_range" "$limit"
return
fi
local contributors
contributors=$(
while IFS= read -r pr_num; do
if [ -z "$pr_num" ]; then
continue
fi
local pr_data
pr_data=$(gh api "repos/$REPO_OWNER/$REPO_NAME/pulls/$pr_num" 2>/dev/null || true)
if [ -z "$pr_data" ]; then
continue
fi
local login
login=$(echo "$pr_data" | jq -r '.user.login // empty' 2>/dev/null || true)
if [ -z "$login" ]; then
continue
fi
printf "%s\t#%s\n" "$login" "$pr_num"
done <<< "$pr_numbers" |
awk -F '\t' '
{
count[$1]++
prs[$1] = prs[$1] ? prs[$1] ", " $2 : $2
}
END {
for (login in count) {
printf "%s\t%s\t%s\n", count[login], login, prs[login]
}
}
' |
sort -k1,1nr -k2,2 |
head -n "$limit" |
awk -F '\t' '
{
suffix = $1 == 1 ? "PR" : "PRs"
printf "- @%s (%s %s: %s)\n", $2, $1, suffix, $3
}
'
)
if [ -n "$contributors" ]; then
echo "$contributors"
else
format_contributors "$fallback_range" "$limit"
fi
}
# Generate CHANGELOG-style release notes
generate_changelog_notes() {
local version="$1"
local previous_tag="$2"
local notes="## Changes in v$version\n\n"
# Get all commits since previous tag
local commits
if [ -n "$previous_tag" ]; then
if ! tag_exists "$previous_tag"; then
echo "❌ Release notes base tag $previous_tag does not exist locally." >&2
echo "Run git fetch --tags origin and retry." >&2
exit 1
fi
commits=$(git log "$previous_tag"..HEAD --oneline)
else
commits=$(git log --oneline -50)
fi
# Categorize commits by conventional commit prefix
local features fixes docs refactor chore ci_test build perf other
# Get PR numbers from commits
local pr_numbers
pr_numbers=$(echo "$commits" | grep -oE '#[0-9]+' | sed 's/#//' | sort -u | head -20)
# Fetch PR details and categorize
while IFS= read -r pr_num; do
if [ -z "$pr_num" ]; then
continue
fi
local pr_data
pr_data=$(gh api "repos/$REPO_OWNER/$REPO_NAME/pulls/$pr_num" 2>/dev/null || echo "")
if [ -n "$pr_data" ]; then
local pr_title pr_url pr_merged_at
pr_title=$(echo "$pr_data" | jq -r '.title // empty' 2>/dev/null || echo "")
pr_url=$(echo "$pr_data" | jq -r '.html_url // empty' 2>/dev/null || echo "")
if [ -n "$pr_title" ] && [ -n "$pr_url" ]; then
local pr_line="- $pr_title [#$pr_num]($pr_url)"
# Categorize by PR title prefix
case "$pr_title" in
feat*|Feat*|FEAT*)
features+=" $pr_line\n"
;;
fix*|Fix*|FIX*)
fixes+=" $pr_line\n"
;;
docs*|Docs*|DOCS*)
docs+=" $pr_line\n"
;;
refactor*|Refactor*|REFACTOR*)
refactor+=" $pr_line\n"
;;
chore*|Chore*|CHORE*)
chore+=" $pr_line\n"
;;
ci*|CI*|test*|Test*|TEST*)
ci_test+=" $pr_line\n"
;;
build*|Build*|BUILD*)
build+=" $pr_line\n"
;;
perf*|Perf*|PERF*)
perf+=" $pr_line\n"
;;
*)
other+=" $pr_line\n"
;;
esac
fi
fi
done <<< "$pr_numbers"
# Add categorized sections if they have content
if [ -n "$features" ]; then
notes+="### ✨ Features\n$features\n"
fi
if [ -n "$fixes" ]; then
notes+="### 🐛 Bug Fixes\n$fixes\n"
fi
if [ -n "$perf" ]; then
notes+="### ⚡ Performance\n$perf\n"
fi
if [ -n "$refactor" ]; then
notes+="### ♻️ Refactor\n$refactor\n"
fi
if [ -n "$docs" ]; then
notes+="### 📝 Documentation\n$docs\n"
fi
if [ -n "$build" ]; then
notes+="### 🔧 Build\n$build\n"
fi
if [ -n "$ci_test" ]; then
notes+="### 🔬 CI/Test\n$ci_test\n"
fi
if [ -n "$chore" ]; then
notes+="### 🔨 Chore\n$chore\n"
fi
if [ -n "$other" ]; then
notes+="### Other Changes\n$other\n"
fi
# Add contributors section (scoped to this release range)
local contributors
local contributor_range
local contributor_limit
if [ -n "$previous_tag" ]; then
contributor_range="$previous_tag..HEAD"
contributor_limit=10
else
contributor_range="HEAD"
contributor_limit=20
fi
contributors=$(format_pr_contributors "$pr_numbers" "$contributor_range" "$contributor_limit")
if [ -n "$contributors" ]; then
notes+="\n### Contributors\n$contributors\n"
fi
echo -e "$notes"
}
# True when commits after $1 mention a PR (#123). Empty base → recent history.
commits_mention_prs() {
local since_tag="$1"
local commits
if [ -n "$since_tag" ]; then
commits=$(git log "$since_tag"..HEAD --format="%s" 2>/dev/null || echo "")
else
commits=$(git log --oneline -50 --format="%s" 2>/dev/null || echo "")
fi
echo "$commits" | grep -qE '#[0-9]+'
}
# Check if the current version tag already exists locally
LOCAL_TAG_EXISTS=$(git tag -l "v$CURRENT_VERSION" 2>/dev/null || echo "")
REMOTE_TAG_EXISTS=$(git ls-remote --tags origin "v$CURRENT_VERSION" 2>/dev/null | grep -v "^{}" || echo "")
PREV_TAG=""
if [ -n "$LOCAL_TAG_EXISTS" ] && [ -z "$REMOTE_TAG_EXISTS" ]; then
# Local tag exists but not on remote - push it first
echo "⚠️ Tag v$CURRENT_VERSION exists locally but not on remote. Pushing..."
git push origin "v$CURRENT_VERSION"
NEW_VERSION="$CURRENT_VERSION"
PREV_TAG=$(get_previous_tag "v$CURRENT_VERSION")
elif [ -n "$REMOTE_TAG_EXISTS" ]; then
# Tag already exists on remote. Only bump when HEAD actually moved past it —
# otherwise a second release.sh run creates an empty patch (blank notes).
COMMITS_SINCE_TAG=$(git rev-list --count "v${CURRENT_VERSION}..HEAD" 2>/dev/null || echo 0)
if [ "$COMMITS_SINCE_TAG" = "0" ]; then
echo "✅ Tag v$CURRENT_VERSION already exists on remote and HEAD has no new commits."
echo " Refusing empty patch bump (would produce blank release notes)."
exit 0
fi
if ! commits_mention_prs "v${CURRENT_VERSION}"; then
echo "❌ Commits since v$CURRENT_VERSION have no PR references (#N)."
echo " Refusing release that would produce blank categorized notes."
exit 1
fi
echo "⚠️ Tag v$CURRENT_VERSION already exists on remote. Bumping patch version..."
PREV_TAG="v$CURRENT_VERSION"
npm version patch --no-git-tag-version
NEW_VERSION=$(node -p "require('./package.json').version")
echo "New version: $NEW_VERSION"
sync_registry_metadata_version "$NEW_VERSION"
sync_pinned_npx_docs_version "${PREV_TAG#v}"
preflight_registry_metadata
build_docs
git add -u package.json package-lock.json README.md README.ko.md README.zh-CN.md docs
if [ -f server.json ]; then
git add server.json
fi
git commit -m "chore(release): v$NEW_VERSION"
git tag "v$NEW_VERSION"
else
# No existing tag - create new version bump
PREV_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
if ! commits_mention_prs "$PREV_TAG"; then
echo "❌ Commits since ${PREV_TAG:-start} have no PR references (#N)."
echo " Refusing release that would produce blank categorized notes."
exit 1
fi
npm version patch --no-git-tag-version
NEW_VERSION=$(node -p "require('./package.json').version")
echo "New version: $NEW_VERSION"
sync_registry_metadata_version "$NEW_VERSION"
sync_pinned_npx_docs_version "${PREV_TAG#v}"
preflight_registry_metadata
build_docs
git add -u package.json package-lock.json README.md README.ko.md README.zh-CN.md docs
if [ -f server.json ]; then
git add server.json
fi
git commit -m "chore(release): v$NEW_VERSION"
git tag "v$NEW_VERSION"
fi
echo "Using version: $NEW_VERSION"
# Generate CHANGELOG-style release notes
RELEASE_NOTES=$(generate_changelog_notes "$NEW_VERSION" "$PREV_TAG")
echo "Generated release notes:"
echo "---"
echo "$RELEASE_NOTES"
echo "---"
# Push commit and tag before creating release
git push origin HEAD
git push origin "v$NEW_VERSION"
# Create GitHub release with the notes
gh release create "v$NEW_VERSION" --title "v$NEW_VERSION" --notes "$RELEASE_NOTES"
echo "✅ Release v$NEW_VERSION created and pushed!"
echo "GitHub Actions will now publish to npm and Docker Hub automatically."