From 3bbb1f1182e04f3e321c136f9d9d9ecc095b080a Mon Sep 17 00:00:00 2001 From: "Daniel (dB.) Doubrovkine" Date: Fri, 20 Nov 2026 20:47:38 -0500 Subject: [PATCH] Publish changelog sections as GitHub Release notes Changelog formats gain notes(version), which reads the released section with the format's own heading, expands reference links, and drops link definitions trailing the section. The standard workflow reads it before any publication and passes it to hosting, so GitHub Releases carry the reviewed changelog instead of generated notes, and a missing section fails before the gem is pushed. Releases that already carry notes and changelogs without notes keep their current behavior. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_011Gr21hZDQo5axDMuFgzL6s --- CHANGELOG.md | 1 + RELEASING.md | 2 +- docs/changelog/base.md | 4 +++ docs/changelog/notes.md | 2 +- docs/hosting/github_release.md | 2 +- docs/usage.md | 2 +- docs/workflow/ruby_gem.md | 2 +- lib/ruby-gem-release/changelog/base.rb | 7 +++++ lib/ruby-gem-release/changelog/notes.rb | 11 ++++++- lib/ruby-gem-release/release.rb | 4 +++ lib/ruby-gem-release/workflow/ruby_gem.rb | 10 +++++- spec/fixtures/github_release_examples.rb | 12 ++++--- .../changelog/intridea_spec.rb | 10 ++++++ .../changelog/keep_a_changelog_spec.rb | 8 +++++ spec/ruby_gem_release/changelog/notes_spec.rb | 7 +++++ .../workflow/ruby_gem_spec.rb | 31 +++++++++++++++++++ 16 files changed, 103 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8350f3f..e03ff0e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## 0.9.1 (Next) +- [#90](https://github.com/dblock/ruby-gem-release/pull/90): Publish the released changelog section as GitHub Release notes in the standard workflow instead of generated notes, read before any publication - [@dblock](https://github.com/dblock). - Your contribution here. ## 0.9.0 (2026/11/20) diff --git a/RELEASING.md b/RELEASING.md index d02fa7c..f545509 100644 --- a/RELEASING.md +++ b/RELEASING.md @@ -72,7 +72,7 @@ Rerun with the same source commit and inputs, including the release date. Each t Builds overwrite local artifacts reproducibly. Capture exports the committer timestamp of the source commit as `SOURCE_DATE_EPOCH`, unless explicitly supplied; it controls the gemspec date and archive timestamps. Generated release commits do not change it. Stable toolchains, gemspecs, generated files, and signing inputs remain the caller's responsibility. RubyGems accepts an existing version only when its SHA256 matches; a differing or unavailable checksum fails. -Git reuses matching release and development trees without moving tags or rewinding remote descendants. Development bumps calculate from the supplied release version, so repetition does not increment twice. GitHub replaces differing release metadata and assets; unspecified platform-generated notes are retained. +Git reuses matching release and development trees without moving tags or rewinding remote descendants. Development bumps calculate from the supplied release version, so repetition does not increment twice. GitHub replaces differing release metadata, notes read from the changelog, and assets; unspecified platform-generated notes are retained. Actions preserves `release.gem`, its name/version/digest manifest `release.json`, and `release.bundle` before publication for 30 days. These are for manual inspection, not automatic restoration. Conflicting checksums or tags and unrelated branch advancement require investigation; do not overwrite immutable releases. diff --git a/docs/changelog/base.md b/docs/changelog/base.md index 2af5876..d73e9d0 100644 --- a/docs/changelog/base.md +++ b/docs/changelog/base.md @@ -18,6 +18,10 @@ Default validation requires an existing selected release or pending notes; other New releases require nonempty notes. Only the current section counts, ending at the next heading of equal or shallower depth. Blank lines, category headings, HTML comments or blocks, reference definitions, and the configured `Intridea` contribution placeholder are not notes. Text, lists, quotes, and code with content count. Errors identify the version and changelog path. +## Notes + +`notes(version)` returns the released section for that version, read without editing the file. It uses the format's own release heading, expands reference links inline, and drops link definitions trailing the section, as [read-only notes](notes.md) do with `expand_references: true`. Unreleased, missing, duplicate, or empty sections raise. The [standard workflow](../workflow/ruby_gem.md#hosting) passes the result to hosting. + ## Prepare Next `prepare_next!(release)` delegates the released version and the version for the next development iteration to the concrete format implementation. diff --git a/docs/changelog/notes.md b/docs/changelog/notes.md index f78732e..b786497 100644 --- a/docs/changelog/notes.md +++ b/docs/changelog/notes.md @@ -14,4 +14,4 @@ notes = RubyGemRelease::Changelog::Notes.new( Parsing requires the optional Commonmarker dependency. Exactly one real Markdown heading must match; fenced examples do not count. Extraction stops at the next heading of the same or higher level and rejects empty notes. Original line endings and subsection formatting are retained; surrounding whitespace is stripped. -With `expand_references: true`, resolved reference links and images become inline links, including definitions elsewhere in the source. Code, unresolved references, and existing inline links remain unchanged. The source file is never written. Pass the resulting string as the release's `notes:` for hosted publication. +With `expand_references: true`, resolved reference links and images become inline links, including definitions elsewhere in the source. Code, unresolved references, and existing inline links remain unchanged. Link reference definitions trailing the section, such as Keep a Changelog comparison links at the end of the file, are dropped. The source file is never written. Pass the resulting string as the release's `notes:` for hosted publication. diff --git a/docs/hosting/github_release.md b/docs/hosting/github_release.md index 73a5f24..1efe7cb 100644 --- a/docs/hosting/github_release.md +++ b/docs/hosting/github_release.md @@ -17,7 +17,7 @@ The optional `repository:` argument identifies the target as `'OWNER/REPO'`. It Pass `latest: true` or `latest: false` to the constructor to set the latest-release policy during creation and metadata updates. Existing releases are compared with GitHub's latest release before changing that status. The default `nil` retains GitHub's automatic selection and does not change matching existing releases. Use `latest: false` for maintenance branches to avoid temporarily promoting an older release. -`publish!(release, package: nil, artifacts: [])` creates a release with tag and title `vVERSION`, using the numeric version from the [Release](../release.md) context. It verifies that the tag exists remotely and never creates a tag automatically. With `release.notes`, it supplies that nonempty body through a private temporary `--notes-file` and removes the file afterward; otherwise it retains GitHub's `--generate-notes`. It returns the release URL printed by `gh`. +`publish!(release, package: nil, artifacts: [])` creates a release with tag and title `vVERSION`, using the numeric version from the [Release](../release.md) context. It verifies that the tag exists remotely and never creates a tag automatically. With `release.notes`, it supplies that nonempty body through a private temporary `--notes-file` and removes the file afterward; otherwise it retains GitHub's `--generate-notes`. The standard workflow supplies the released changelog section when the changelog can read it. It returns the release URL printed by `gh`. ## Package diff --git a/docs/usage.md b/docs/usage.md index 5a7c7c0..0ce1a62 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -195,4 +195,4 @@ Source control objects manage release history independently of the repository's Hosting objects publish release notes and attach package artifacts after the release tag is pushed. Set `hosting: nil` in the standard workflow, or omit the hosting publication call in custom callbacks, to disable hosted releases without disabling package publication. -- [`Hosting::GitHubRelease`](hosting/github_release.md): GitHub Releases with generated notes and the exact gem attached. +- [`Hosting::GitHubRelease`](hosting/github_release.md): GitHub Releases with changelog or generated notes and the exact gem attached. diff --git a/docs/workflow/ruby_gem.md b/docs/workflow/ruby_gem.md index 4325f8a..629d307 100644 --- a/docs/workflow/ruby_gem.md +++ b/docs/workflow/ruby_gem.md @@ -36,7 +36,7 @@ The optional `provider:` defaults to [`ExecutionProvider.current`](../execution_ ## Hosting -The optional `hosting:` defaults to [`Hosting::GitHubRelease`](../hosting/github_release.md). Custom objects implement idempotent `publish!(release, package:)`. Set `hosting: nil` to disable hosting without disabling registry publication or Git. +The optional `hosting:` defaults to [`Hosting::GitHubRelease`](../hosting/github_release.md). Custom objects implement idempotent `publish!(release, package:)`. When the changelog implements `notes(version)`, as the built-in formats do, the hosted release receives the released section as its notes, read before any publication so a missing or empty section fails before the gem is pushed. Releases that already carry notes, such as semantic releases, keep them; changelogs without `notes` and `changelog: nil` keep platform-generated notes. Set `hosting: nil` to disable hosting without disabling registry publication or Git. ## Install Tasks diff --git a/lib/ruby-gem-release/changelog/base.rb b/lib/ruby-gem-release/changelog/base.rb index 6ee94f5..014d8f6 100644 --- a/lib/ruby-gem-release/changelog/base.rb +++ b/lib/ruby-gem-release/changelog/base.rb @@ -2,6 +2,7 @@ require 'date' require_relative '../markdown/source' +require_relative 'notes' module RubyGemRelease module Changelog @@ -29,6 +30,12 @@ def released?(version) matches.one? end + # Released section body with reference links expanded, for hosted release notes. + def notes(version) + validate_version(version) + Notes.new(path: @path, heading: method(:release_pattern), expand_references: true).read(version) + end + def prepare_next!(release) update_next(version: release.next_version, released_version: release.version) end diff --git a/lib/ruby-gem-release/changelog/notes.rb b/lib/ruby-gem-release/changelog/notes.rb index 0129ba4..6036c11 100644 --- a/lib/ruby-gem-release/changelog/notes.rb +++ b/lib/ruby-gem-release/changelog/notes.rb @@ -7,6 +7,8 @@ module Changelog class Notes include Markdown::Source + DEFINITION = /\A {0,3}\[[^\]\r\n]+\]:[ \t]*\S[^\r\n]*\z/ + def initialize(path: 'CHANGELOG.md', heading: nil, expand_references: false) @path = path @heading = heading || ->(version) { /\A\#{1,6} #{Regexp.escape(version)} \((?!Next\))[^\r\n]*\d[^\r\n]*\)\z/ } @@ -21,11 +23,18 @@ def read(version) section = content[first...last] raise ArgumentError, "empty release notes in #{@path}" unless notes?(document(section)) - @expand_references ? expanded_section(content, first, last).strip : section.strip + @expand_references ? without_definitions(expanded_section(content, first, last)) : section.strip end private + # Expanded notes no longer need link reference definitions; drops those trailing the section, as at the end of a file. + def without_definitions(section) + lines = section.strip.lines + lines.pop while lines.any? && (lines.last.strip.empty? || DEFINITION.match?(lines.last.chomp)) + lines.join.strip + end + def expanded_section(content, first, last) edits = section_edits(content, first, last) edits.reverse_each { |range, replacement| content[range] = replacement } diff --git a/lib/ruby-gem-release/release.rb b/lib/ruby-gem-release/release.rb index 7d01d0b..59edeac 100644 --- a/lib/ruby-gem-release/release.rb +++ b/lib/ruby-gem-release/release.rb @@ -26,6 +26,10 @@ def with_next_version(version) self.class.new(version: @version, gemspec_version: @gemspec_version, date: @date, next_version: version, notes: @notes) end + def with_notes(notes) + self.class.new(version: @version, gemspec_version: @gemspec_version, date: @date, next_version: @next_version, notes: notes) + end + private def validate_metadata(date, notes) diff --git a/lib/ruby-gem-release/workflow/ruby_gem.rb b/lib/ruby-gem-release/workflow/ruby_gem.rb index 9091aba..c2e7811 100644 --- a/lib/ruby-gem-release/workflow/ruby_gem.rb +++ b/lib/ruby-gem-release/workflow/ruby_gem.rb @@ -91,10 +91,18 @@ def prepare_publication(ctx) end def publish(ctx) + hosted = hosted_release(ctx.release) if @hosting prepare_publication(ctx) run_stage(:publish_registry, ctx) { @destination.publish!(ctx.release, package: @package) } run_stage(:push_release, ctx) { @git.push!(ctx.release) } - run_stage(:publish_hosted_release, ctx) { @hosting.publish!(ctx.release, package: @package) } if @hosting + run_stage(:publish_hosted_release, ctx) { @hosting.publish!(hosted, package: @package) } if @hosting + end + + # Reads changelog notes before any publication, so a missing section fails before the gem is pushed. + def hosted_release(release) + return release if release.notes || !@changelog.respond_to?(:notes) + + release.with_notes(@changelog.notes(release.version)) end def prepare_next(ctx) diff --git a/spec/fixtures/github_release_examples.rb b/spec/fixtures/github_release_examples.rb index 89e73e9..7ad66ac 100644 --- a/spec/fixtures/github_release_examples.rb +++ b/spec/fixtures/github_release_examples.rb @@ -319,11 +319,13 @@ def validation_command expect(run_git('-C', checkout, 'status', '--porcelain')).to eq('') artifact = File.join(directory, 'example-0.1.0.gem') publications = File.readlines(File.join(directory, 'publication.jsonl')).map { |line| JSON.parse(line) } - expect(publications).to eq([ - [Gem.ruby, '-S', 'gem', 'push', artifact], - ['gh', 'release', 'create', 'v0.1.0', '--repo', 'owner/example', '--verify-tag', - '--title', 'v0.1.0', '--generate-notes', artifact] - ]) + expect(publications).to match([ + [Gem.ruby, '-S', 'gem', 'push', artifact], + ['gh', 'release', 'create', 'v0.1.0', '--repo', 'owner/example', '--verify-tag', + '--title', 'v0.1.0', '--notes-file', a_string_ending_with('.md'), artifact] + ]) + hosted = JSON.parse(File.read(File.join(environment.fetch('RELEASE_TEST_SERVICE_DIRECTORY'), 'hosting.json'))) + expect(hosted.fetch('body')).to eq("### Added\n\n- Initial functionality.") end end diff --git a/spec/ruby_gem_release/changelog/intridea_spec.rb b/spec/ruby_gem_release/changelog/intridea_spec.rb index 8950ab9..4111f8b 100644 --- a/spec/ruby_gem_release/changelog/intridea_spec.rb +++ b/spec/ruby_gem_release/changelog/intridea_spec.rb @@ -220,4 +220,14 @@ File.write(path, "### 1.0.0 (2026/11/07)\n\n- Change.\n\n### 1.0.0 (2026-11-07)\n\n- Change.\n") expect { described_class.new(path: path).released?('1.0.0') }.to raise_error(ArgumentError, /duplicate release headings/) end + + describe '#notes' do + it 'reads a released section with reference links expanded and rejects unreleased or invalid versions', :aggregate_failures do + File.write(path, "# Changelog\n\n## 1.3.0 (Next)\n\n- Your contribution here.\n\n## 1.2.3 (2026/11/07)\n\n- [#1][pr]: Fix.\n\n## 1.2.2 (2026/11/06)\n\n- Older.\n\n[pr]: https://example.com/pull/1\n") + changelog = described_class.new(path: path) + expect(changelog.notes('1.2.3')).to eq('- [#1](): Fix.') + expect { changelog.notes('1.3.0') }.to raise_error(ArgumentError) + expect { changelog.notes('1.2') }.to raise_error(ArgumentError, /invalid version/) + end + end end diff --git a/spec/ruby_gem_release/changelog/keep_a_changelog_spec.rb b/spec/ruby_gem_release/changelog/keep_a_changelog_spec.rb index 13c71cf..7c99223 100644 --- a/spec/ruby_gem_release/changelog/keep_a_changelog_spec.rb +++ b/spec/ruby_gem_release/changelog/keep_a_changelog_spec.rb @@ -227,4 +227,12 @@ expect { described_class.new(path: path).release!(release) }.to raise_error(ArgumentError, /duplicate/) expect(File.read(path)).to eq(content) end + + describe '#notes' do + it 'reads the newest released section without the file-level comparison links' do + File.write(path, "# Changelog\n\n## [Unreleased]\n\n## [1.2.3] - 2026-11-07\n\n### Fixed\n\n- Fix.\n\n" \ + "[Unreleased]: https://example.com/compare/v1.2.3...HEAD\n[1.2.3]: https://example.com/compare/v1.2.2...v1.2.3\n") + expect(described_class.new(path: path).notes('1.2.3')).to eq("### Fixed\n\n- Fix.") + end + end end diff --git a/spec/ruby_gem_release/changelog/notes_spec.rb b/spec/ruby_gem_release/changelog/notes_spec.rb index ce059c2..3dd9f4c 100644 --- a/spec/ruby_gem_release/changelog/notes_spec.rb +++ b/spec/ruby_gem_release/changelog/notes_spec.rb @@ -42,4 +42,11 @@ File.write(path, "## 1.2.3 (2026/11/07)\n\n### Changes\n\n\n") expect { reader.read('1.2.3') }.to raise_error(ArgumentError, /empty release notes/) end + + it 'drops link definitions trailing an expanded section but keeps them otherwise' do + content = "## 1.0.0 (2026/11/07)\n\n- [Fix][1].\n\n[1]: https://example.com/1\n[Unreleased]: https://example.com/compare\n" + File.write(path, content) + expect(described_class.new(path: path, expand_references: true).read('1.0.0')).to eq('- [Fix]().') + expect(described_class.new(path: path).read('1.0.0')).to eq("- [Fix][1].\n\n[1]: https://example.com/1\n[Unreleased]: https://example.com/compare") + end end diff --git a/spec/ruby_gem_release/workflow/ruby_gem_spec.rb b/spec/ruby_gem_release/workflow/ruby_gem_spec.rb index 4ca4f6d..f8c9b3f 100644 --- a/spec/ruby_gem_release/workflow/ruby_gem_spec.rb +++ b/spec/ruby_gem_release/workflow/ruby_gem_spec.rb @@ -69,6 +69,37 @@ def run_callbacks %w[validate prepare publish bump].each { |phase| Rake::Task["release:#{phase}"].invoke } end + it 'publishes the released changelog section as hosted release notes, read before any publication', :aggregate_failures do + allow(objects.fetch(:changelog)).to receive(:notes).with('2.0.0') do + events << %i[changelog notes] + '- Released notes.' + end + install + Rake::Task['release:publish'].invoke + expect(events.index(%i[changelog notes])).to be < events.index(%i[destination publish!]) + expect(objects.fetch(:destination)).to have_received(:publish!).with(have_attributes(notes: nil), package: objects.fetch(:package)) + expect(objects.fetch(:hosting)).to have_received(:publish!).with(have_attributes(version: '2.0.0', notes: '- Released notes.'), package: objects.fetch(:package)) + end + + it 'fails before publishing anything when the released changelog section cannot be read' do + allow(objects.fetch(:changelog)).to receive(:notes).and_raise(ArgumentError, 'empty release notes in CHANGELOG.md') + install + expect { Rake::Task['release:publish'].invoke }.to raise_error(ArgumentError, /empty release notes/) + expect(objects.fetch(:destination)).not_to have_received(:publish!) + expect(objects.fetch(:git)).not_to have_received(:commit!) + end + + it 'keeps platform-generated notes for changelogs without readable notes and skips reading without hosting', :aggregate_failures do + install + Rake::Task['release:publish'].invoke + expect(objects.fetch(:hosting)).to have_received(:publish!).with(have_attributes(notes: nil), package: objects.fetch(:package)) + Rake.application = Rake::Application.new + allow(objects.fetch(:changelog)).to receive(:notes) + install(hosting: nil) + Rake::Task['release:publish'].invoke + expect(objects.fetch(:changelog)).not_to have_received(:notes) + end + it 'rejects empty pending notes without invoking release operations' do allow(objects.fetch(:changelog)).to receive(:pending?).and_return(false) install