Skip to content

Commit 0ed5f8f

Browse files
authored
chore: add script to generate changelog (#786)
Assisted-by: Cursor Signed-off-by: Ruben Romero Montes <[email protected]>
1 parent 4b29631 commit 0ed5f8f

File tree

3 files changed

+162
-0
lines changed

3 files changed

+162
-0
lines changed

.conventional-changelog-config.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module.exports = {
2+
types: [
3+
{ type: 'feat', section: 'enhancement', hidden: false },
4+
{ type: 'fix', section: 'fixes', hidden: false },
5+
{ type: 'docs', section: 'documentation', hidden: false },
6+
{ type: 'style', section: 'style', hidden: false },
7+
{ type: 'refactor', section: 'refactor', hidden: false },
8+
{ type: 'perf', section: 'performance', hidden: false },
9+
{ type: 'test', section: 'tests', hidden: false },
10+
{ type: 'chore', section: 'chore', hidden: false }
11+
],
12+
commitUrlFormat: 'https://github.com/fabric8-analytics/fabric8-analytics-vscode-extension/commit/{{hash}}',
13+
compareUrlFormat: 'https://github.com/fabric8-analytics/fabric8-analytics-vscode-extension/compare/{{previousTag}}...{{currentTag}}',
14+
issueUrlFormat: 'https://github.com/fabric8-analytics/fabric8-analytics-vscode-extension/issues/{{id}}',
15+
userUrlFormat: 'https://github.com/{{user}}'
16+
};

scripts/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Scripts
2+
3+
This directory contains utility scripts for the project.
4+
5+
## Changelog Generation
6+
7+
The `generate-changelog.sh` script helps generate changelog entries for new releases. It automatically:
8+
- Gets the current date with proper ordinal suffix (e.g., "May 30th 2025")
9+
- Finds the previous version tag
10+
- Extracts commit messages between the previous and current version
11+
- Formats them into a changelog entry
12+
- Updates the CHANGELOG.md file
13+
14+
### Usage
15+
16+
```bash
17+
./scripts/generate-changelog.sh <version>
18+
```
19+
20+
Example:
21+
```bash
22+
./scripts/generate-changelog.sh v0.9.25
23+
```
24+
25+
### Requirements
26+
27+
- `conventional-changelog-cli` must be installed globally:
28+
```bash
29+
npm install -g conventional-changelog-cli
30+
```
31+
32+
### Output Format
33+
34+
The script generates changelog entries in the following format:
35+
```markdown
36+
## 0.9.25 (May 30th 2025)
37+
38+
- commit message 1
39+
- commit message 2
40+
```
41+
42+
The entry is automatically added to the top of the CHANGELOG.md file, right after the title.

scripts/generate-changelog.sh

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#!/bin/bash
2+
3+
# Check if version argument is provided
4+
if [ -z "$1" ]; then
5+
echo "Error: Version argument is required"
6+
echo "Usage: ./scripts/generate-changelog.sh <version>"
7+
exit 1
8+
fi
9+
10+
# Get the current date with ordinal suffix
11+
day_suffix=$(date +"%e" | sed 's/^[[:space:]]*//')
12+
if [ "$day_suffix" = "1" ] || [ "$day_suffix" = "21" ] || [ "$day_suffix" = "31" ]; then
13+
suffix="st"
14+
elif [ "$day_suffix" = "2" ] || [ "$day_suffix" = "22" ]; then
15+
suffix="nd"
16+
elif [ "$day_suffix" = "3" ] || [ "$day_suffix" = "23" ]; then
17+
suffix="rd"
18+
else
19+
suffix="th"
20+
fi
21+
today="$(date +"%b %-d")$suffix $(date +"%Y")"
22+
23+
# Check if conventional-changelog is installed
24+
if ! command -v conventional-changelog &> /dev/null; then
25+
echo "Error: conventional-changelog is not installed"
26+
echo "Please install it using: npm install -g conventional-changelog-cli"
27+
exit 1
28+
fi
29+
30+
# Get the previous version from package.json
31+
prev_version=$(node -p "require('./package.json').version")
32+
echo "Previous version: $prev_version"
33+
34+
# Create a temporary file for the new changelog
35+
temp_file=$(mktemp)
36+
37+
# Generate new changelog entries for the specific version
38+
version="$1"
39+
version=${version#v}
40+
echo "Generating changelog for version $version..."
41+
42+
# Get the previous version tag
43+
prev_tag=$(git tag --sort=-v:refname | grep -A 1 "v$version" | tail -n 1)
44+
if [ -z "$prev_tag" ]; then
45+
echo "Error: Could not find previous tag for version $version"
46+
exit 1
47+
fi
48+
echo "Previous tag: $prev_tag"
49+
50+
# Get commit messages between tags
51+
commit_messages=$(git log --pretty=format:"- %s" $prev_tag..v$version)
52+
53+
# Create the new version header and content
54+
version_header="## $version ($today)
55+
56+
$commit_messages
57+
"
58+
59+
# Debug: Show the final header
60+
echo "Final version header:"
61+
echo "$version_header"
62+
63+
# Add the new version at the top of the existing changelog
64+
if [ -f CHANGELOG.md ]; then
65+
# If the file exists, add the new version after the title
66+
# Create a temporary file for the new content
67+
temp_changelog=$(mktemp)
68+
# Add the title
69+
head -n 1 CHANGELOG.md > "$temp_changelog"
70+
# Add the new version header
71+
echo "$version_header" >> "$temp_changelog"
72+
# Add the rest of the existing changelog
73+
tail -n +2 CHANGELOG.md >> "$temp_changelog"
74+
# Replace the original file
75+
mv "$temp_changelog" CHANGELOG.md
76+
else
77+
# If the file doesn't exist, create it with the title and new version
78+
echo "# Change Log
79+
80+
$version_header" > CHANGELOG.md
81+
fi
82+
83+
# Clean up
84+
rm "$temp_file"
85+
86+
# Extract release notes for GitHub release
87+
notes=$(sed -n "/## $version/,/^##/p" ./CHANGELOG.md | grep -v '##')
88+
89+
# If running in GitHub Actions, output the notes
90+
if [ -n "$GITHUB_OUTPUT" ]; then
91+
# Escape special characters for GitHub Actions
92+
notes="${notes//'/'%27}"
93+
notes="${notes//$'\n'/'%0A'}"
94+
notes="${notes//$'\r'/'%0D'}"
95+
delimiter="$(openssl rand -hex 8)"
96+
echo "notes<<${delimiter}" >> "$GITHUB_OUTPUT"
97+
echo "$notes" >> "$GITHUB_OUTPUT"
98+
echo "${delimiter}" >> "$GITHUB_OUTPUT"
99+
else
100+
# If running locally, just print the notes
101+
echo "Generated changelog for version $version"
102+
echo "Release notes:"
103+
echo "$notes"
104+
fi

0 commit comments

Comments
 (0)