Skip to content

Commit a02bf40

Browse files
committed
Release scripts: extract CHANGELOG entries for the latest version to display in GitHub release notes
1 parent ee2d949 commit a02bf40

File tree

2 files changed

+134
-4
lines changed

2 files changed

+134
-4
lines changed

spec/tasks/build_helpers/gem_publisher_spec.rb

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,105 @@
148148
)
149149
end
150150
end
151+
152+
describe 'extracting a CHANGELOG part for the release notes' do
153+
let(:changelog_content) do
154+
<<~CHANGELOG
155+
## [Unreleased]
156+
157+
- Upcoming feature that is not yet released
158+
- Another upcoming feature
159+
160+
## [0.3.0] - 2026-01-29
161+
162+
- New `rake bun:install:package` task: automatically migrates `package.json` scripts
163+
- New `rake bun:install:procfile` task: automatically migrates `Procfile` files
164+
- Both tasks are automatically invoked by `rake bun:install`
165+
166+
## [0.2.0] - 2025-01-30
167+
168+
- Major update with new features
169+
- Breaking changes included
170+
171+
## [0.1.0] - 2024-12-15
172+
173+
- Initial release
174+
CHANGELOG
175+
end
176+
177+
let(:tmpfile) do
178+
file = Tempfile.new(['CHANGELOG', '.md'])
179+
file.write(changelog_content)
180+
file.close
181+
file
182+
end
183+
184+
after { tmpfile.unlink }
185+
186+
before do
187+
allow(File).to receive(:expand_path)
188+
.with('../../CHANGELOG.md', anything)
189+
.and_return(tmpfile.path)
190+
end
191+
192+
it 'extracts changelog content for a base version' do
193+
result = publisher.send(:extract_changelog_for_version, '0.3.0')
194+
195+
expect(result).to include('rake bun:install:package')
196+
expect(result).to include('rake bun:install:procfile')
197+
expect(result).to include('Both tasks are automatically invoked')
198+
end
199+
200+
it 'extracts changelog content for a composite version' do
201+
result = publisher.send(:extract_changelog_for_version, '0.3.0.1.1.38')
202+
203+
expect(result).to include('rake bun:install:package')
204+
expect(result).not_to include('Unreleased')
205+
expect(result).not_to include('Major update')
206+
end
207+
208+
it 'extracts the last version in changelog' do
209+
result = publisher.send(:extract_changelog_for_version, '0.1.0')
210+
211+
expect(result).to eq('- Initial release')
212+
end
213+
214+
it 'returns nil for non-existent version' do
215+
result = publisher.send(:extract_changelog_for_version, '9.9.9')
216+
217+
expect(result).to be_nil
218+
end
219+
220+
it 'does not include content from other versions' do
221+
result = publisher.send(:extract_changelog_for_version, '0.2.0')
222+
223+
expect(result).to include('Major update')
224+
expect(result).not_to include('Initial release')
225+
expect(result).not_to include('rake bun:install:package')
226+
end
227+
228+
context 'when changelog file does not exist' do
229+
before do
230+
allow(File).to receive(:expand_path)
231+
.with('../../CHANGELOG.md', anything)
232+
.and_return('/nonexistent/path/CHANGELOG.md')
233+
end
234+
235+
it 'returns nil' do
236+
result = publisher.send(:extract_changelog_for_version, '0.3.0')
237+
238+
expect(result).to be_nil
239+
end
240+
end
241+
242+
context 'when changelog has malformed content' do
243+
let(:changelog_content) { 'This is not a valid changelog format' }
244+
245+
it 'returns nil' do
246+
result = publisher.send(:extract_changelog_for_version, '0.3.0')
247+
248+
expect(result).to be_nil
249+
end
250+
end
251+
end
151252
end

tasks/build_helpers/gem_publisher.rb

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,24 @@ def upload_gem_files(release)
6767

6868
def generate_release_notes
6969
bun_version = @version_checker.latest_bun_repo_version
70-
<<~NOTES
70+
changelog_content = extract_changelog_for_version(@version)
71+
72+
notes = <<~NOTES
7173
bundlebun #{@version}, includes a binary distribution of [Bun #{bun_version}](https://github.com/#{BuildHelpers::BUN_REPO}/releases/tag/bun-v#{bun_version}). Built for: #{PlatformManager::PLATFORM_MAPPING.keys.join(", ")}.
7274
73-
Changelog: [CHANGELOG](https://github.com/#{BuildHelpers::GEM_REPO}/blob/main/CHANGELOG.md).
75+
NOTES
76+
77+
if changelog_content && !changelog_content.empty?
78+
notes += <<~CHANGELOG
79+
### What's new
80+
81+
#{changelog_content}
82+
83+
CHANGELOG
84+
end
7485

75-
See [README](https://github.com/#{BuildHelpers::GEM_REPO}) for installation instructions and documentation. See [LICENSE](https://github.com/#{BuildHelpers::GEM_REPO}/blob/main/LICENSE.txt) for licensing information.
86+
notes += <<~FOOTER
87+
See [README](https://github.com/#{BuildHelpers::GEM_REPO}) for installation instructions and documentation. See [CHANGELOG](https://github.com/#{BuildHelpers::GEM_REPO}/blob/main/CHANGELOG.md) for full history. See [LICENSE](https://github.com/#{BuildHelpers::GEM_REPO}/blob/main/LICENSE.txt) for licensing information.
7688
7789
### Quick installation
7890
@@ -81,7 +93,24 @@ def generate_release_notes
8193
8294
rake bun:install
8395
```
84-
NOTES
96+
FOOTER
97+
98+
notes
99+
end
100+
101+
def extract_changelog_for_version(version)
102+
changelog_path = File.expand_path('../../CHANGELOG.md', __dir__)
103+
base_version = version.to_s.split('.')[0..2].join('.')
104+
content = File.read(changelog_path)
105+
106+
# Match the section for this version until the next version header or end of file
107+
pattern = /^## \[#{Regexp.escape(base_version)}\][^\n]*\n(.*?)(?=^## \[|\z)/m
108+
match = content.match(pattern)
109+
return nil unless match
110+
111+
match[1].strip
112+
rescue
113+
nil
85114
end
86115

87116
def publish_to_rubygems

0 commit comments

Comments
 (0)