-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_changelog_local.sh
executable file
·144 lines (122 loc) · 4.81 KB
/
generate_changelog_local.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
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
#!/bin/sh
# Exit on error
set -e
# Error handling
trap 'echo "An error occurred at line $LINENO. Exiting."' ERR
# Path to the Git repository (current directory)
REPO_DIR="."
CHANGELOG_FILE="$REPO_DIR/CHANGELOG.md"
GITHUB_REPO_URL=$(git remote get-url origin 2>/dev/null | sed 's/\.git$//')
if [ -z "$GITHUB_REPO_URL" ]; then
GITHUB_REPO_URL=0
fi
echo "Starting changelog generation script..."
echo "Repository:"
echo $GITHUB_REPO_URL
# Create or clear the changelog file
> $CHANGELOG_FILE
# Add the introductory text to the changelog
echo "# Changelog" >> $CHANGELOG_FILE
echo "" >> $CHANGELOG_FILE
echo "All notable changes to this project will be documented in this file." >> $CHANGELOG_FILE
echo "" >> $CHANGELOG_FILE
echo "The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html) and to [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/)." >> $CHANGELOG_FILE
echo "" >> $CHANGELOG_FILE
# Go to the repository directory
cd $REPO_DIR
# Fetch the latest changes
git fetch --tags
echo "Fetched latest tags."
# Get the latest tag
LATEST_TAG=$(git describe --tags --abbrev=0)
# Get tags in reverse order
TAGS=$(git tag --sort=-v:refname)
# Check if there are any tags
if [ -z "$TAGS" ]; then
echo "No tags found in the repository."
exit 1
fi
echo "Found tags: $TAGS"
# Placeholder for the previous tag
TAG_TO=HEAD
# Define categories
CATEGORIES="feat fix ci perf docs gitops deploy test demo build chore style refactor"
# Regular expression for matching conventional commits
CONVENTIONAL_COMMIT_REGEX="^.* (feat|fix|ci|perf|docs|gitops|deploy|test|demo|build|chore|style|refactor)(\(.*\))?: "
print_tag() {
echo "Processing tag: $2"
TAG_DATE=$(git log -1 --format=%ai $2 | cut -d ' ' -f 1)
if [ "$2" = "HEAD" ]; then
if [ $(git rev-parse $1) != $(git rev-parse "HEAD") ]; then
echo "## Unreleased changes" >> $CHANGELOG_FILE
echo "" >> $CHANGELOG_FILE
fi
else
echo "## $2 ($TAG_DATE)" >> $CHANGELOG_FILE
echo "" >> $CHANGELOG_FILE
fi
# Collect all commits for this tag range
if [ -z "$1" ]; then
ALL_COMMITS=$(git log $2 --oneline --always)
else
ALL_COMMITS=$(git log $1..$2 --oneline --always)
fi
# Process each category
for KEY in $CATEGORIES; do
CATEGORY_COMMITS=$(echo "$ALL_COMMITS" | grep -E "^.* $KEY(\(.*\))?: " || true)
if [ ! -z "$CATEGORY_COMMITS" ]; then
case $KEY in
"feat") CATEGORY_NAME="Feature" ;;
"fix") CATEGORY_NAME="Bug Fixes" ;;
"ci") CATEGORY_NAME="Continuous Integration" ;;
"perf") CATEGORY_NAME="Performance Improvements" ;;
"docs") CATEGORY_NAME="Documentation" ;;
"gitops") CATEGORY_NAME="GitOps" ;;
"deploy") CATEGORY_NAME="Deployment" ;;
"test") CATEGORY_NAME="Test" ;;
"demo") CATEGORY_NAME="Demo" ;;
"build") CATEGORY_NAME="Build" ;;
"chore") CATEGORY_NAME="Chore" ;;
"style") CATEGORY_NAME="Style" ;;
"refactor") CATEGORY_NAME="Refactor" ;;
esac
echo "### $CATEGORY_NAME" >> $CHANGELOG_FILE
echo "Listing commits for category: $CATEGORY_NAME under tag $2"
echo "$CATEGORY_COMMITS" | while read -r COMMIT; do
HASH=$(echo $COMMIT | awk '{print $1}')
MESSAGE=$(echo $COMMIT | sed -E "s/^$HASH $KEY(\(.*\))?: //")
if [ "$GITHUB_REPO_URL" != "0" ]; then
echo "- $MESSAGE [\`$HASH\`]($GITHUB_REPO_URL/commit/$HASH)" >> $CHANGELOG_FILE
else
echo "- $MESSAGE" >> $CHANGELOG_FILE
fi
done
echo "" >> $CHANGELOG_FILE
fi
done
# Process 'Other' category
OTHER_COMMITS=$(echo "$ALL_COMMITS" | grep -v -E "$CONVENTIONAL_COMMIT_REGEX" || true)
if [ ! -z "$OTHER_COMMITS" ]; then
echo "### Other" >> $CHANGELOG_FILE
echo "Listing commits for category: Other under tag $2"
echo "$OTHER_COMMITS" | while read -r COMMIT; do
HASH=$(echo $COMMIT | awk '{print $1}')
MESSAGE=$(echo $COMMIT | sed -E 's/^[^ ]* //')
if [ "$GITHUB_REPO_URL" != "0" ]; then
echo "- $MESSAGE [\`$HASH\`]($GITHUB_REPO_URL/commit/$HASH)" >> $CHANGELOG_FILE
else
echo "- $MESSAGE" >> $CHANGELOG_FILE
fi
done
echo "" >> $CHANGELOG_FILE
fi
echo "Completed processing tag: $2"
# Update the previous tag
}
# Iterate over tags
for TAG_FROM in $TAGS; do
print_tag $TAG_FROM $TAG_TO
TAG_TO=$TAG_FROM
done
print_tag "" $TAG_FROM
echo "Changelog generation complete."