-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate_changelog.sh
executable file
·88 lines (74 loc) · 2.52 KB
/
generate_changelog.sh
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
#!/bin/bash
version=$( git describe --tags --always )
tag=$( git describe --tags --always --abbrev=0 )
if [ "$version" = "$tag" ]; then # on a tag
current="$tag"
previous=$( git describe --tags --abbrev=0 HEAD~ )
if [[ $previous == *beta* ]]; then
if [[ $tag == *beta* ]]; then
previous=$( git describe --tags --abbrev=0 HEAD~ )
else
previous=$( git describe --tags --abbrev=0 --exclude="*beta*" HEAD~ )
fi
else
previous=$( git describe --tags --abbrev=0 HEAD~ )
fi
else
current=$( git log -1 --format="%H" )
previous="$tag"
fi
date=$( git log -1 --date=short --format="%ad" )
url=$( git remote get-url origin | sed -e 's/^git@\(.*\):/https:\/\/\1\//' -e 's/\.git$//' )
echo -ne "# [${version}](${url}/tree/${current}) ($date)\n\n[Full Changelog](${url}/compare/${previous}...${current})\n\n" > "CHANGELOG.md"
if [ "$version" = "$tag" ]; then # on a tag
# Fetch release notes using GitHub CLI
releaseNotes=$(gh release view "$tag" --json body -q ".body")
echo -ne "## Release Notes\n${releaseNotes}\n" >> "CHANGELOG.md"
fi
# Create temporary file to store shortlog output
tempFile=$(mktemp)
# Append commits to temporary file
git shortlog --no-merges --reverse "$previous..$current" > "$tempFile"
# Initialize arrays to hold categorized commits
featureCommits=()
bugfixCommits=()
misCommits=()
# Read the temporary file line by line
while IFS= read -r line; do
# Strip leading whitespace and extract commit message
commitMessage=$(echo "$line" | sed -e 's/^\s*[^ ]\+ //')
# Check for commit suffixes
if echo "$line" | grep -q "\[Feature\]"; then
# Remove suffix and add to features array
featureCommits+=("- ${commitMessage//\[Feature\]/}")
elif echo "$line" | grep -q "\[Bugfix\]"; then
# Remove suffix and add to bugfixes array
bugfixCommits+=("- ${commitMessage//\[Bugfix\]/}")
elif echo "$line" | grep -q "\[Misc\]"; then
# Remove suffix and add to bugfixes array
misCommits+=("- ${commitMessage//\[Misc\]/}")
fi
done < "$tempFile"
# Clean up temporary file
rm "$tempFile"
# Write results to changelog
{
if [ ${#featureCommits[@]} -ne 0 ]; then
echo -ne "### Features\n"
for commit in "${featureCommits[@]}"; do
echo "$commit"
done
fi
if [ ${#bugfixCommits[@]} -ne 0 ]; then
echo -ne "### Bug Fixes\n"
for commit in "${bugfixCommits[@]}"; do
echo "$commit"
done
fi
if [ ${#misCommits[@]} -ne 0 ]; then
echo -ne "### Miscellaneous\n"
for commit in "${misCommits[@]}"; do
echo "$commit"
done
fi
} >> "CHANGELOG.md"