Skip to content

Commit 41a65f8

Browse files
Copilotbrunoborges
andcommitted
fix: extract repository URL from existing links in CHANGELOG
Co-authored-by: brunoborges <129743+brunoborges@users.noreply.github.com>
1 parent 5e58e9d commit 41a65f8

File tree

2 files changed

+221
-9
lines changed

2 files changed

+221
-9
lines changed
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
#!/bin/bash
2+
# Test script for update-changelog.sh
3+
4+
set -e
5+
6+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
7+
UPDATE_SCRIPT="${SCRIPT_DIR}/update-changelog.sh"
8+
TEST_DIR="/tmp/changelog-test-$$"
9+
10+
# Colors for output
11+
GREEN='\033[0;32m'
12+
RED='\033[0;31m'
13+
NC='\033[0m' # No Color
14+
15+
passed=0
16+
failed=0
17+
18+
# Setup test directory
19+
mkdir -p "$TEST_DIR"
20+
21+
# Cleanup on exit
22+
cleanup() {
23+
rm -rf "$TEST_DIR"
24+
}
25+
trap cleanup EXIT
26+
27+
# Helper function to run a test
28+
run_test() {
29+
local test_name="$1"
30+
local test_func="$2"
31+
32+
echo -n "Testing: $test_name ... "
33+
34+
if $test_func; then
35+
echo -e "${GREEN}PASSED${NC}"
36+
((passed++))
37+
else
38+
echo -e "${RED}FAILED${NC}"
39+
((failed++))
40+
fi
41+
}
42+
43+
# Test 1: Basic functionality - Replace Unreleased with version
44+
test_basic_replace() {
45+
local test_file="${TEST_DIR}/test1.md"
46+
cat > "$test_file" << 'EOF'
47+
# Changelog
48+
49+
## [Unreleased]
50+
51+
### Added
52+
- New feature
53+
54+
## [1.0.0] - 2026-01-01
55+
56+
### Added
57+
- Initial release
58+
59+
[1.0.0]: https://github.com/test/repo/releases/tag/1.0.0
60+
EOF
61+
62+
# Run the script
63+
CHANGELOG_FILE="$test_file" bash "$UPDATE_SCRIPT" 1.0.1 > /dev/null 2>&1
64+
65+
# Verify the changes
66+
if grep -q "## \[Unreleased\]" "$test_file" && \
67+
grep -q "## \[1.0.1\] - $(date +%Y-%m-%d)" "$test_file" && \
68+
grep -q "\[Unreleased\]: https://github.com/test/repo/compare/v1.0.1...HEAD" "$test_file" && \
69+
grep -q "\[1.0.1\]: https://github.com/test/repo/compare/v1.0.0...v1.0.1" "$test_file"; then
70+
return 0
71+
else
72+
return 1
73+
fi
74+
}
75+
76+
# Test 2: Handle CHANGELOG without Unreleased link
77+
test_no_unreleased_link() {
78+
local test_file="${TEST_DIR}/test2.md"
79+
cat > "$test_file" << 'EOF'
80+
# Changelog
81+
82+
## [Unreleased]
83+
84+
### Added
85+
- New feature
86+
87+
## [1.0.0] - 2026-01-01
88+
89+
[1.0.0]: https://github.com/test/repo/releases/tag/1.0.0
90+
EOF
91+
92+
CHANGELOG_FILE="$test_file" bash "$UPDATE_SCRIPT" 1.0.1 > /dev/null 2>&1
93+
94+
# Should add both Unreleased and version links
95+
if grep -q "\[Unreleased\]: https://github.com/test/repo/compare/v1.0.1...HEAD" "$test_file" && \
96+
grep -q "\[1.0.1\]: https://github.com/test/repo/compare/v1.0.0...v1.0.1" "$test_file"; then
97+
return 0
98+
else
99+
return 1
100+
fi
101+
}
102+
103+
# Test 3: Preserve content structure
104+
test_preserve_content() {
105+
local test_file="${TEST_DIR}/test3.md"
106+
cat > "$test_file" << 'EOF'
107+
# Changelog
108+
109+
## [Unreleased]
110+
111+
### Added
112+
- Feature A
113+
- Feature B
114+
115+
### Fixed
116+
- Bug fix
117+
118+
## [1.0.0] - 2026-01-01
119+
120+
[1.0.0]: https://github.com/test/repo/releases/tag/1.0.0
121+
EOF
122+
123+
CHANGELOG_FILE="$test_file" bash "$UPDATE_SCRIPT" 1.0.1 > /dev/null 2>&1
124+
125+
# Verify content is preserved under the new version
126+
if grep -A 6 "## \[1.0.1\]" "$test_file" | grep -q "Feature A" && \
127+
grep -A 6 "## \[1.0.1\]" "$test_file" | grep -q "Bug fix"; then
128+
return 0
129+
else
130+
return 1
131+
fi
132+
}
133+
134+
# Test 4: Error handling - no Unreleased section
135+
test_no_unreleased_section() {
136+
local test_file="${TEST_DIR}/test4.md"
137+
cat > "$test_file" << 'EOF'
138+
# Changelog
139+
140+
## [1.0.0] - 2026-01-01
141+
142+
[1.0.0]: https://github.com/test/repo/releases/tag/1.0.0
143+
EOF
144+
145+
# Should fail because there's no Unreleased section
146+
if ! CHANGELOG_FILE="$test_file" bash "$UPDATE_SCRIPT" 1.0.1 > /dev/null 2>&1; then
147+
return 0
148+
else
149+
return 1
150+
fi
151+
}
152+
153+
# Test 5: Multiple version handling
154+
test_multiple_versions() {
155+
local test_file="${TEST_DIR}/test5.md"
156+
cat > "$test_file" << 'EOF'
157+
# Changelog
158+
159+
## [Unreleased]
160+
161+
### Added
162+
- New feature
163+
164+
## [1.0.1] - 2026-02-01
165+
166+
## [1.0.0] - 2026-01-01
167+
168+
[1.0.1]: https://github.com/test/repo/compare/v1.0.0...v1.0.1
169+
[1.0.0]: https://github.com/test/repo/releases/tag/1.0.0
170+
EOF
171+
172+
CHANGELOG_FILE="$test_file" bash "$UPDATE_SCRIPT" 1.0.2 > /dev/null 2>&1
173+
174+
# Verify the new version is added and links are updated
175+
if grep -q "## \[1.0.2\] - $(date +%Y-%m-%d)" "$test_file" && \
176+
grep -q "\[1.0.2\]: https://github.com/test/repo/compare/v1.0.1...v1.0.2" "$test_file"; then
177+
return 0
178+
else
179+
return 1
180+
fi
181+
}
182+
183+
# Run all tests
184+
echo "Running CHANGELOG update script tests..."
185+
echo ""
186+
187+
run_test "Basic functionality - Replace Unreleased with version" test_basic_replace
188+
run_test "Handle CHANGELOG without Unreleased link" test_no_unreleased_link
189+
run_test "Preserve content structure" test_preserve_content
190+
run_test "Error handling - no Unreleased section" test_no_unreleased_section
191+
run_test "Multiple version handling" test_multiple_versions
192+
193+
echo ""
194+
echo "=========================================="
195+
echo -e "Tests passed: ${GREEN}${passed}${NC}"
196+
echo -e "Tests failed: ${RED}${failed}${NC}"
197+
echo "=========================================="
198+
199+
if [ $failed -eq 0 ]; then
200+
exit 0
201+
else
202+
exit 1
203+
fi

.github/scripts/update-changelog.sh

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ if [ -z "$1" ]; then
1212
fi
1313

1414
VERSION="$1"
15-
CHANGELOG_FILE="CHANGELOG.md"
15+
CHANGELOG_FILE="${CHANGELOG_FILE:-CHANGELOG.md}"
1616
RELEASE_DATE=$(date +%Y-%m-%d)
1717

1818
echo "Updating CHANGELOG.md for version ${VERSION} (${RELEASE_DATE})"
@@ -39,13 +39,22 @@ BEGIN {
3939
content_found = 0
4040
links_section = 0
4141
first_version_link = ""
42+
repo_url = ""
4243
}
4344
4445
# Track if we are in the links section at the bottom
4546
/^\[/ {
4647
links_section = 1
4748
}
4849
50+
# Capture the repository URL from the first version link
51+
links_section && repo_url == "" && /^\[[0-9]+\.[0-9]+\.[0-9]+\]:/ {
52+
match($0, /(https:\/\/github\.com\/[^\/]+\/[^\/]+)\//, arr)
53+
if (arr[1] != "") {
54+
repo_url = arr[1]
55+
}
56+
}
57+
4958
# Replace [Unreleased] with the version and date
5059
/^## \[Unreleased\]/ {
5160
if (!unreleased_found) {
@@ -60,21 +69,21 @@ BEGIN {
6069
# Capture the first version link to get the previous version
6170
links_section && first_version_link == "" && /^\[[0-9]+\.[0-9]+\.[0-9]+\]:/ {
6271
match($0, /\[([0-9]+\.[0-9]+\.[0-9]+)\]:/, arr)
63-
if (arr[1] != "") {
72+
if (arr[1] != "" && repo_url != "") {
6473
first_version_link = arr[1]
6574
# Insert Unreleased and new version links before first version link
66-
print "[Unreleased]: https://github.com/copilot-community-sdk/copilot-sdk-java/compare/v" version "...HEAD"
67-
print "[" version "]: https://github.com/copilot-community-sdk/copilot-sdk-java/compare/v" arr[1] "...v" version
75+
print "[Unreleased]: " repo_url "/compare/v" version "...HEAD"
76+
print "[" version "]: " repo_url "/compare/v" arr[1] "...v" version
6877
}
6978
}
7079
7180
# Update existing [Unreleased] link if present
7281
links_section && /^\[Unreleased\]:/ {
73-
# Get the previous version from the existing link
74-
match($0, /v([0-9]+\.[0-9]+\.[0-9]+)\.\.\.HEAD/, arr)
75-
if (arr[1] != "") {
76-
print "[Unreleased]: https://github.com/copilot-community-sdk/copilot-sdk-java/compare/v" version "...HEAD"
77-
print "[" version "]: https://github.com/copilot-community-sdk/copilot-sdk-java/compare/v" arr[1] "...v" version
82+
# Get the previous version and repo URL from the existing link
83+
match($0, /(https:\/\/github\.com\/[^\/]+\/[^\/]+)\/compare\/v([0-9]+\.[0-9]+\.[0-9]+)\.\.\.HEAD/, arr)
84+
if (arr[1] != "" && arr[2] != "") {
85+
print "[Unreleased]: " arr[1] "/compare/v" version "...HEAD"
86+
print "[" version "]: " arr[1] "/compare/v" arr[2] "...v" version
7887
next
7988
}
8089
}

0 commit comments

Comments
 (0)