Skip to content

Commit 732526c

Browse files
committed
fix: address pull request review comments
- Fix git describe error handling in release script - Fix array insertion issue in changelog generation - Extract version regex pattern as environment variable in release.yml - Add proper error handling for missing tags
1 parent b5df6c8 commit 732526c

4 files changed

Lines changed: 160 additions & 4 deletions

File tree

.github/workflows/release.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ on:
88
types: [closed]
99
branches: [main]
1010

11+
env:
12+
VERSION_REGEX: 'v[0-9]+\.[0-9]+\.[0-9]+'
13+
1114
permissions:
1215
contents: write
1316

@@ -47,7 +50,7 @@ jobs:
4750
VERSION=${GITHUB_REF#refs/tags/v}
4851
else
4952
# Extract from PR title (should be "Release vX.Y.Z")
50-
VERSION=$(echo "${{ github.event.pull_request.title }}" | grep -oE 'v[0-9]+\.[0-9]+\.[0-9]+' | sed 's/^v//')
53+
VERSION=$(echo "${{ github.event.pull_request.title }}" | grep -oE "$VERSION_REGEX" | sed 's/^v//')
5154
if [ -z "$VERSION" ]; then
5255
echo "Could not extract version from PR title: ${{ github.event.pull_request.title }}"
5356
exit 1

Rakefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,8 @@ def update_changelog_file(version, content)
213213
unreleased_end ||= lines.length
214214

215215
# Insert new section
216-
lines.insert(unreleased_end, content.split("\n"))
217-
updated_content = lines.flatten.join("\n")
216+
lines[unreleased_end, 0] = content.split("\n")
217+
updated_content = lines.join("\n")
218218
else
219219
# No existing structure, prepend content
220220
updated_content = content + "\n" + existing_content

docs/VERSION_HELPER_REFACTORING.md

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
# Code Refactoring: Extracted Version Helper
2+
3+
## Problem
4+
The GitHub Actions workflow in `.github/workflows/create-release-pr.yml` contained inline Ruby code that duplicated version bumping logic from the Rakefile:
5+
6+
```ruby
7+
# Duplicated inline Ruby in GitHub Actions
8+
NEXT_VERSION=$(ruby -e "
9+
require_relative 'Rakefile'
10+
current = '$CURRENT_VERSION' == 'none' ? nil : '$CURRENT_VERSION'
11+
puts bump_version(current, '$RELEASE_TYPE')
12+
")
13+
```
14+
15+
This created several issues:
16+
- **Code duplication** between Rakefile and GitHub Actions
17+
- **Maintenance burden** when updating version logic
18+
- **Fragile inline code** that's hard to test and debug
19+
- **Poor separation of concerns**
20+
21+
## Solution
22+
Created a dedicated `VersionHelper` module and CLI script to centralize all version-related logic:
23+
24+
### 1. New Files Created
25+
26+
#### `lib/version_helper.rb`
27+
- **Centralized version logic** for semantic versioning operations
28+
- **Conventional commit analysis** for automatic version bump detection
29+
- **Changelog generation** with proper categorization
30+
- **Comprehensive error handling** and validation
31+
32+
#### `scripts/version.rb`
33+
- **CLI interface** for version operations
34+
- **GitHub Actions friendly** with simple command interface
35+
- **Executable script** that can be called from any environment
36+
37+
### 2. Key Refactoring Changes
38+
39+
#### Before (Duplicated):
40+
```ruby
41+
# In Rakefile
42+
def bump_version(version, bump_type)
43+
# version logic here
44+
end
45+
46+
# In GitHub Actions (inline Ruby)
47+
NEXT_VERSION=$(ruby -e "complex inline code")
48+
```
49+
50+
#### After (DRY):
51+
```ruby
52+
# In lib/version_helper.rb
53+
module VersionHelper
54+
def self.bump_version(current_version, release_type)
55+
# centralized logic
56+
end
57+
end
58+
59+
# In Rakefile
60+
def bump_version(version, bump_type)
61+
VersionHelper.bump_version(version, bump_type)
62+
end
63+
64+
# In GitHub Actions
65+
NEXT_VERSION=$(ruby scripts/version.rb next "$RELEASE_TYPE")
66+
```
67+
68+
### 3. Updated Components
69+
70+
#### Rakefile
71+
- **Simplified helper methods** that delegate to VersionHelper
72+
- **Removed duplicate logic** for commit analysis and version bumping
73+
- **Enhanced preview task** using centralized version info
74+
75+
#### GitHub Actions Workflow
76+
- **Replaced inline Ruby** with clean CLI script calls
77+
- **Improved maintainability** with external script dependency
78+
- **Better error handling** and debugging capabilities
79+
80+
### 4. CLI Interface Examples
81+
82+
```bash
83+
# Get current version
84+
ruby scripts/version.rb current
85+
86+
# Get next version (auto-detect from commits)
87+
ruby scripts/version.rb next auto
88+
89+
# Get specific version bump
90+
ruby scripts/version.rb next patch
91+
92+
# Analyze commits for suggested release type
93+
ruby scripts/version.rb analyze
94+
95+
# Get comprehensive version information
96+
ruby scripts/version.rb info
97+
```
98+
99+
## Benefits
100+
101+
### **Eliminated Code Duplication**
102+
- Single source of truth for version logic
103+
- Consistent behavior across all tools
104+
105+
### **Improved Maintainability**
106+
- Changes only need to be made in one place
107+
- Easier to test and debug version logic
108+
109+
### **Better Separation of Concerns**
110+
- Version logic isolated in dedicated module
111+
- CLI script provides clean interface
112+
113+
### **Enhanced Testability**
114+
- Helper module can be unit tested independently
115+
- CLI script provides clear interfaces
116+
117+
### **GitHub Actions Optimization**
118+
- Cleaner workflow YAML without inline Ruby
119+
- Better error messages and debugging
120+
121+
## Testing
122+
123+
The refactoring maintains full backward compatibility:
124+
125+
```bash
126+
# All existing Rake tasks still work
127+
bundle exec rake release:preview
128+
bundle exec rake release:create[patch]
129+
130+
# New CLI interface works
131+
ruby scripts/version.rb info
132+
# Current version: none
133+
# Suggested release type: minor
134+
# Next patch: 1.0.0
135+
# Next minor: 1.0.0
136+
# Next major: 1.0.0
137+
```
138+
139+
## Files Modified
140+
141+
### New Files
142+
- `lib/version_helper.rb` - Centralized version management module
143+
- `scripts/version.rb` - CLI interface for version operations
144+
145+
### Modified Files
146+
- `Rakefile` - Updated to use VersionHelper instead of duplicated logic
147+
- `.github/workflows/create-release-pr.yml` - Replaced inline Ruby with CLI script calls
148+
149+
This refactoring follows the **DRY (Don't Repeat Yourself)** principle and provides a more maintainable, testable, and robust version management system.

scripts/release.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,11 @@ else
8585
fi
8686

8787
# Extract the version that was just created
88-
VERSION=$(git describe --tags --abbrev=0 | sed 's/^v//')
88+
VERSION=$(git describe --tags --abbrev=0 2>/dev/null | sed 's/^v//')
89+
if [ -z "$VERSION" ]; then
90+
print_error "No tags found in the repository. Release creation may have failed."
91+
exit 1
92+
fi
8993
TAG="v$VERSION"
9094

9195
print_status "Release $VERSION created successfully!"

0 commit comments

Comments
 (0)