-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrelease.sh
More file actions
346 lines (297 loc) · 10.3 KB
/
Copy pathrelease.sh
File metadata and controls
346 lines (297 loc) · 10.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
#!/usr/bin/env bash
# ------------------------------------------------------------------------------
# release.sh — Automate changelog generation, version bumping, tagging, and release.
#
# Part of the setup-fortran-conda project: https://github.com/fortran-lang/setup-fortran-conda
#
# This script streamlines the release process by:
# - Analyzing Git commit messages and merged pull requests (PRs) to determine the next semantic version bump (major, minor, or patch).
# - Generating a well-structured CHANGELOG.md section using PR titles, commit messages, and contributor names.
# - Updating the VERSION file with the new semantic version.
# - Committing the above changes (CHANGELOG.md and VERSION) with meaningful commit messages.
# - Optionally pushing changes to the repository and tagging the release (`vX.Y.Z`).
# - Creating a GitHub Release with the generated changelog as release notes.
# - Tagging the latest release with both the new version tag and a floating `latest` tag.
#
# It supports two optional modes:
# --dry-run : Preview all steps without writing files or pushing anything.
# --local : Commit changes locally, but skip pushing, tagging, and publishing a GitHub release.
#
# Requires:
# - GitHub CLI (`gh`) — must be installed and authenticated.
# - `jq` — used for parsing GitHub API responses.
#
# Author: Seyed Ali Ghasemi
# License: MIT
# ------------------------------------------------------------------------------
set -euo pipefail
print_help() {
cat <<EOF
Usage: ./release.sh [OPTIONS]
Automatically generate a semantic version bump, update CHANGELOG.md and VERSION,
create a Git tag, and publish a GitHub Release based on recent commits and merged PRs.
Options:
--dry-run Preview the version bump and generated changelog.
No files will be written or pushed.
--local Perform all steps locally: update CHANGELOG.md and VERSION,
commit changes, but do NOT push, tag, or create a GitHub Release.
--help Show this help message and exit.
Requirements:
- GitHub CLI ('gh') must be authenticated and installed
- 'jq' must be installed
Example:
./release.sh --dry-run
EOF
}
DRY_RUN=false
LOCAL_ONLY=false
while [[ $# -gt 0 ]]; do
case "$1" in
--dry-run) DRY_RUN=true ;;
--local) LOCAL_ONLY=true ;;
--help)
print_help
exit 0
;;
*)
echo "❌ Unknown option: $1"
print_help
exit 1
;;
esac
shift
done
RELEASE_IGNORE_PATTERNS=(
"^docs: update CHANGELOG"
"^chore: update VERSION"
"^Merge pull request"
)
should_ignore() {
local msg="$1"
for pattern in "${RELEASE_IGNORE_PATTERNS[@]}"; do
if [[ "$msg" =~ $pattern ]]; then
return 0
fi
done
return 1
}
echo "🧪 Dry run mode: $DRY_RUN"
echo "🔧 Local-only mode: $LOCAL_ONLY"
cd "$(git rev-parse --show-toplevel)"
if ! command -v gh >/dev/null 2>&1; then
echo "❌ GitHub CLI (gh) not found. Please install it: https://cli.github.com/"
exit 1
fi
if ! gh auth status &>/dev/null; then
echo "❌ GitHub CLI is not authenticated. Run: gh auth login"
exit 1
fi
repo=$(gh repo view --json nameWithOwner -q .nameWithOwner)
previous_tag=$(git tag --sort=-v:refname | grep '^v' | head -n1)
previous_tag=${previous_tag:-v0.0.0}
echo "📌 Previous tag: $previous_tag"
commits=$(git log "$previous_tag"..HEAD --pretty=format:"%s")
if [[ -z "$commits" ]]; then
echo "✅ No commits since last release."
exit 0
fi
bump="patch"
if echo "$commits" | grep -qE "^feat(\(.+\))?!?: "; then bump="minor"; fi
if echo "$commits" | grep -qE "BREAKING CHANGE|!: "; then bump="major"; fi
IFS=. read -r major minor patch <<<"${previous_tag#v}"
case "$bump" in
major)
major=$((major + 1))
minor=0
patch=0
;;
minor)
minor=$((minor + 1))
patch=0
;;
patch) patch=$((patch + 1)) ;;
esac
next_tag="v$major.$minor.$patch"
echo "🆕 Next version: $next_tag"
echo "🔍 Fetching commits between $previous_tag and HEAD..."
compare=$(gh api "/repos/$repo/compare/$previous_tag...HEAD")
commit_shas=$(echo "$compare" | jq -r '.commits[].sha')
all_commit_shas=()
while IFS= read -r sha; do all_commit_shas+=("$sha"); done <<<"$commit_shas"
echo "🔍 Fetching recent closed PRs from GitHub API..."
if ! raw_prs=$(gh api "/repos/$repo/pulls?state=closed&per_page=100" 2>&1); then
echo "❌ Failed to fetch PRs from GitHub:"
echo "$raw_prs"
exit 1
fi
if ! echo "$raw_prs" | jq empty >/dev/null 2>&1; then
echo "❌ GitHub API returned invalid JSON:"
echo "$raw_prs"
exit 1
fi
mapfile -t all_prs < <(echo "$raw_prs" | jq -c '.[]')
echo "ℹ️ Retrieved ${#all_prs[@]} PRs"
matched_prs=()
pr_commit_shas=()
for pr in "${all_prs[@]}"; do
merged_at=$(echo "$pr" | jq -r '.merged_at')
[[ "$merged_at" == "null" ]] && continue
pr_number=$(echo "$pr" | jq -r '.number')
pr_commits=$(gh api "/repos/$repo/pulls/$pr_number/commits" | jq -r '.[].sha')
for sha in $pr_commits; do
if [[ " ${all_commit_shas[*]} " =~ " $sha " ]]; then
matched_prs+=("$pr")
pr_commit_shas+=($pr_commits)
break
fi
done
done
features=()
fixes=()
breaking=()
others=()
contributors=()
classify() {
local msg="$1" line="$2"
if [[ "$msg" =~ (!:|BREAKING CHANGE) ]]; then
breaking+=("$line")
elif [[ "$msg" =~ ^feat(\(.+\))?!?:\ || "$msg" =~ ^feat:\ ]]; then
features+=("$line")
elif [[ "$msg" =~ ^fix(\(.+\))?!?:\ || "$msg" =~ ^fix:\ ]]; then
fixes+=("$line")
else
others+=("$line")
fi
}
for pr_json in "${matched_prs[@]}"; do
title=$(echo "$pr_json" | jq -r '.title')
number=$(echo "$pr_json" | jq -r '.number')
author=$(echo "$pr_json" | jq -r '.user.login')
if should_ignore "$title"; then
echo "⏭️ Ignored PR: $title"
continue
fi
line="* $title ([#${number}](https://github.com/$repo/pull/${number})) by [@$author](https://github.com/$author)"
classify "$title" "$line"
contributors+=("$author")
done
for sha in "${all_commit_shas[@]}"; do
[[ " ${pr_commit_shas[*]} " =~ " $sha " ]] && continue
msg=$(git show -s --format="%s" "$sha")
if should_ignore "$msg"; then
echo "⏭️ Ignored commit: $msg"
continue
fi
commit_author=$(gh api "/repos/$repo/commits/$sha" --jq '.author.login' 2>/dev/null || echo "")
author=${commit_author:-$(git show -s --format="%an" "$sha")}
short_sha=$(git rev-parse --short "$sha")
line="* $msg ([${short_sha}](https://github.com/$repo/commit/$sha)) by [@$author](https://github.com/$author)"
classify "$msg" "$line"
contributors+=("$author")
done
readarray -t unique_contributors < <(printf "%s\n" "${contributors[@]}" | grep -v '^null$' | sort -u)
today=$(date +%F)
url="https://github.com/$repo/compare/$previous_tag...$next_tag"
changelog_header="## [$next_tag]($url) - $today\n\n"
changelog_body=""
changelog_contrib=""
section() {
local title="$1"
shift
local items=("$@")
if [[ ${#items[@]} -gt 0 ]]; then
changelog_body+=$'\n'"### $title"$'\n\n'
for item in "${items[@]}"; do
changelog_body+="$item"$'\n'
done
fi
}
section "Features" "${features[@]}"
section "Fixes" "${fixes[@]}"
section "Breaking Changes" "${breaking[@]}"
section "Others" "${others[@]}"
if [[ ${#unique_contributors[@]} -gt 0 ]]; then
changelog_contrib+="\n\n### Contributors\n"
for c in "${unique_contributors[@]}"; do
changelog_contrib+="- [@$c](https://github.com/$c)\n"
done
changelog_contrib+="\n"
fi
changelog_tail="\n\nFull Changelog: [$previous_tag...$next_tag]($url)\n"
changelog_md="${changelog_header}${changelog_body}${changelog_contrib}${changelog_tail}"
changelog_release="${changelog_header}${changelog_body}${changelog_tail}"
echo -e "\n📝 Generated CHANGELOG:\n"
echo -e "$changelog_md"
# DRY RUN: Skip all changes
if [[ "$DRY_RUN" == true ]]; then
echo "🧪 Dry run complete — no files written, no commits made, no tags created."
exit 0
fi
# ✅ STEP 1: Confirm writing CHANGELOG.md
if [[ -f CHANGELOG.md && $(grep -c "## \[$next_tag\]" CHANGELOG.md) -gt 0 ]]; then
echo "⚠️ Changelog for $next_tag already exists — skipping update."
else
echo
read -rp "📄 Write and commit CHANGELOG.md? [y/N] " confirm
confirm="${confirm,,}"
if [[ "$confirm" == "y" || "$confirm" == "yes" ]]; then
echo -e "$changelog_md\n$(cat CHANGELOG.md 2>/dev/null)" >.CHANGELOG.md && mv .CHANGELOG.md CHANGELOG.md
git add CHANGELOG.md
git commit -m "docs: update CHANGELOG for $next_tag"
else
echo "⏭️ Skipping CHANGELOG.md update."
fi
fi
# ✅ STEP 2: Confirm writing VERSION
echo
read -rp "📝 Write and commit VERSION file? [y/N] " confirm
confirm="${confirm,,}"
if [[ "$confirm" == "y" || "$confirm" == "yes" ]]; then
echo "${next_tag#v}" >VERSION
git add VERSION
git commit -m "chore: update VERSION to ${next_tag}"
else
echo "⏭️ Skipping VERSION file update."
fi
# ✅ STEP 3: Confirm pushing commits
if [[ "$LOCAL_ONLY" == false ]]; then
echo
read -rp "📤 Push commits to GitHub? [y/N] " confirm
confirm="${confirm,,}"
if [[ "$confirm" == "y" || "$confirm" == "yes" ]]; then
git push
else
echo "⏭️ Skipping git push."
fi
fi
# ✅ STEP 4: Confirm tagging release
if [[ "$LOCAL_ONLY" == false ]]; then
echo
read -rp "🏷️ Create and push Git tag $next_tag? [y/N] " confirm
confirm="${confirm,,}"
if [[ "$confirm" == "y" || "$confirm" == "yes" ]]; then
if ! git rev-parse "$next_tag" >/dev/null 2>&1; then
git tag "$next_tag"
git push origin "$next_tag"
else
echo "⚠️ Tag $next_tag already exists — skipping."
fi
git tag -f latest "$next_tag"
git push origin -f latest
else
echo "⏭️ Skipping Git tag and latest update."
fi
fi
# ✅ STEP 5: Confirm creating GitHub Release
if [[ "$LOCAL_ONLY" == false ]]; then
echo
read -rp "🚀 Create GitHub Release $next_tag? [y/N] " confirm
confirm="${confirm,,}"
if [[ "$confirm" == "y" || "$confirm" == "yes" ]]; then
gh release create "$next_tag" --title "$next_tag" --notes-file <(echo -e "$changelog_release")
echo "✅ GitHub Release $next_tag created."
else
echo "⏭️ Skipping GitHub Release."
fi
fi
echo "🎉 Done."