From a0fc6e0334799496318af2e6d1116afeac673b71 Mon Sep 17 00:00:00 2001 From: "Daniel (dB.) Doubrovkine" Date: Fri, 20 Nov 2026 17:16:21 -0500 Subject: [PATCH] Name Intridea subsections with sections: `Intridea` kept empty subsection headings in a release, so a fixes-only release had an empty `#### Features` heading, and it rebuilt the next section from the released headings. With `sections:`, a release drops subsections without notes and every Next section starts with all the configured sections. Without it, behavior is unchanged. The empty category removal is shared with `KeepAChangelog`. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_011Gr21j6KD4oksnhmZ5u3pr --- CHANGELOG.md | 1 + docs/changelog/intridea.md | 2 ++ lib/ruby-gem-release/changelog/base.rb | 24 +++++++++++++++ lib/ruby-gem-release/changelog/intridea.rb | 29 ++++++++++++++----- .../changelog/keep_a_changelog.rb | 20 +------------ .../changelog/intridea_spec.rb | 27 +++++++++++++++++ 6 files changed, 77 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aea5abc..1ab56df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - [#79](https://github.com/dblock/ruby-gem-release/pull/79): Select the next development version part with `next_version: :minor` or `:major` on `Workflow::RubyGem` and `Workflow::SemanticRubyGem` - [@dblock](https://github.com/dblock). - [#80](https://github.com/dblock/ruby-gem-release/pull/80): Skip posting empty Danger report comments - [@dblock](https://github.com/dblock). - [#82](https://github.com/dblock/ruby-gem-release/pull/82): Create the default package and Git temporary directories only when release data is first used, so loading a workflow leaves none behind - [@dblock](https://github.com/dblock). +- [#83](https://github.com/dblock/ruby-gem-release/pull/83): Name Intridea subsections with `sections:`, dropping sections without notes from a release and starting every Next section with all of them - [@dblock](https://github.com/dblock). - Your contribution here. ## 0.7.0 (2026/11/20) diff --git a/docs/changelog/intridea.md b/docs/changelog/intridea.md index 49d5411..42dfe53 100644 --- a/docs/changelog/intridea.md +++ b/docs/changelog/intridea.md @@ -26,6 +26,8 @@ changelog = RubyGemRelease::Changelog::Intridea.new(path: 'CHANGELOG.md') When the released section has headings one level below its release heading, `prepare_next!` carries those headings into the new Next section and adds the configured placeholder under each one. With no subsections, it creates the usual single placeholder. Old subsection notes are not copied. +Pass `sections:`, such as `sections: %w[Features Fixes]`, to name the subsections every Next section starts with. `release!` then removes subsection headings left without notes, so a fixes-only release has no empty `#### Features`, and `prepare_next!` always adds every configured section, including ones the previous release dropped. Names must be unique, nonempty, and on one line, and the release heading must be shallower than level six. Without `sections:`, empty subsections stay in the release and are carried forward as before. + ## Release During `release:validate`, placeholder-only or structural-only notes fail unless the selected release already has a heading. Invalid headings and missing placeholders also fail; multiple matching placeholders are allowed. diff --git a/lib/ruby-gem-release/changelog/base.rb b/lib/ruby-gem-release/changelog/base.rb index e9d5a10..6ee94f5 100644 --- a/lib/ruby-gem-release/changelog/base.rb +++ b/lib/ruby-gem-release/changelog/base.rb @@ -89,6 +89,30 @@ def validate_release(version, date) def check_absent(content, pattern, message) raise ArgumentError, "#{message} in #{@path}" unless heading_positions(content, pattern).empty? end + + def single_line?(value) + value.is_a?(String) && !value.strip.empty? && !value.match?(/[\r\n]/) + end + + def validate_sections(sections) + raise ArgumentError, 'sections must be an array of unique nonempty names' unless sections.is_a?(Array) && sections.all? { |name| single_line?(name) } && sections.uniq == sections + end + + # Removes subsection headings left without notes in a released section. + def remove_empty_categories(section, separator, level) + heading_positions(section, /\A\#{#{level + 1}} /).reverse_each do |offset| + finish = section_end_position(section, offset, level + 1) + start = section.index(separator, offset) || section.length + remove_line(section, offset, separator) unless notes?(document(section[start...finish])) + end + end + + def remove_line(section, offset, separator) + finish = section.index(separator, offset) + length = (finish ? finish + separator.length : section.length) - offset + length += separator.length if section[0...offset].end_with?(separator * 2) && section[offset + length, separator.length] == separator + section.slice!(offset, length) + end end end end diff --git a/lib/ruby-gem-release/changelog/intridea.rb b/lib/ruby-gem-release/changelog/intridea.rb index 3ca5ae4..389e946 100644 --- a/lib/ruby-gem-release/changelog/intridea.rb +++ b/lib/ruby-gem-release/changelog/intridea.rb @@ -7,13 +7,15 @@ class Intridea < Base PLACEHOLDER = '- Your contribution here.' DATE_FORMAT = '%Y/%m/%d' - def initialize(path: 'CHANGELOG.md', placeholder: PLACEHOLDER, date_format: DATE_FORMAT) + def initialize(path: 'CHANGELOG.md', placeholder: PLACEHOLDER, date_format: DATE_FORMAT, sections: []) raise ArgumentError, 'placeholder must be a single nonempty line' unless placeholder.is_a?(String) && !placeholder.empty? && !placeholder.match?(/[\r\n]/) raise ArgumentError, 'date_format must be a nonempty single-line strftime string or a callable' unless valid_date_format?(date_format) + validate_sections(sections) super(path: path) @placeholder = placeholder @date_format = date_format + @sections = sections.dup end private @@ -30,11 +32,14 @@ def update_release(version:, date:) prefix = heading_prefix(content, position) check_release_absent(content, version) section_end = section_end_position(content, position, prefix.length) - content[position...section_end] = release_heading(prefix, version, date) + - release_notes(content[line_end(content, position, separator)...section_end], separator, version) + content[position...section_end] = released_section(content[line_end(content, position, separator)...section_end], prefix, version, date, separator) File.write(@path, content) end + def released_section(notes, prefix, version, date, separator) + release_heading(prefix, version, date) + release_notes(notes, separator, version, prefix.length) + end + # Checked before writing, so a date_format that produces an unrecognizable heading leaves the file unchanged. def release_heading(prefix, version, date) heading = "#{prefix} #{version} (#{format_date(date)})" @@ -56,9 +61,17 @@ def update_next(version:, released_version:) write_next_section(content, position, prefix, version, separator) end + # Configured sections start every development section, including ones the + # previous release dropped because they had no notes. def write_next_section(content, position, prefix, version, separator) - subsections = subsection_headings(content, position, prefix.length) - File.write(@path, content.insert(position, build_next_section(prefix, version, subsections, separator))) + File.write(@path, content.insert(position, build_next_section(prefix, version, next_subsections(content, position, prefix), separator))) + end + + def next_subsections(content, position, prefix) + return subsection_headings(content, position, prefix.length) if @sections.empty? + raise ArgumentError, 'section headings cannot be deeper than level six' if prefix.length == 6 + + @sections.map { |name| "#{prefix}# #{name}" } end def check_release_absent(content, version) @@ -83,8 +96,10 @@ def valid_date_format?(date_format) date_format.is_a?(String) && !date_format.empty? && !date_format.match?(/[\r\n]/) end - def release_notes(section, separator, version) - check_notes(remove_placeholder(section, separator), version) + def release_notes(section, separator, version, level) + remove_placeholder(section, separator) + remove_empty_categories(section, separator, level) if @sections.any? + check_notes(section, version) end def remove_placeholder(section, separator) diff --git a/lib/ruby-gem-release/changelog/keep_a_changelog.rb b/lib/ruby-gem-release/changelog/keep_a_changelog.rb index 7988534..363abe5 100644 --- a/lib/ruby-gem-release/changelog/keep_a_changelog.rb +++ b/lib/ruby-gem-release/changelog/keep_a_changelog.rb @@ -16,11 +16,8 @@ def initialize(path: 'CHANGELOG.md', placeholder: nil, sections: []) def validate_template(placeholder, sections) raise ArgumentError, 'placeholder must be a single nonempty line' unless placeholder.nil? || single_line?(placeholder) - raise ArgumentError, 'sections must be an array of unique nonempty names' unless sections.is_a?(Array) && sections.all? { |name| single_line?(name) } && sections.uniq == sections - end - def single_line?(value) - value.is_a?(String) && !value.strip.empty? && !value.match?(/[\r\n]/) + validate_sections(sections) end def release_pattern(version) @@ -78,21 +75,6 @@ def clean_notes(section, separator, level) section end - def remove_empty_categories(section, separator, level) - heading_positions(section, /\A\#{#{level + 1}} /).reverse_each do |offset| - finish = section_end_position(section, offset, level + 1) - start = section.index(separator, offset) || section.length - remove_line(section, offset, separator) unless notes?(document(section[start...finish])) - end - end - - def remove_line(section, offset, separator) - finish = section.index(separator, offset) - length = (finish ? finish + separator.length : section.length) - offset - length += separator.length if section[0...offset].end_with?(separator * 2) && section[offset + length, separator.length] == separator - section.slice!(offset, length) - end - def next_section(prefix, separator) raise ArgumentError, 'category headings cannot be deeper than level six' if prefix.length == 6 && @sections.any? diff --git a/spec/ruby_gem_release/changelog/intridea_spec.rb b/spec/ruby_gem_release/changelog/intridea_spec.rb index efd9ae7..8950ab9 100644 --- a/spec/ruby_gem_release/changelog/intridea_spec.rb +++ b/spec/ruby_gem_release/changelog/intridea_spec.rb @@ -93,6 +93,33 @@ expect(File.read(path)).to eq("### 1.0.1 (Next)\n\n#### Features\n\n* Your contribution here.\n\n#### Fixes\n\n* Your contribution here.\n\n### 1.0.0 (2026/11/07)\n\n#### Features\n\n* Feature.\n\n#### Fixes\n\n* Fix.\n\n### 0.9.0 (2026/11/06)\n\n* Older change.\n") end + it 'drops configured sections without notes and restores them in the next section', :aggregate_failures do + content = "### 1.0.0 (Next)\n\n#### Features\n\n* Your contribution here.\n\n#### Fixes\n\n* Fix.\n* Your contribution here.\n\n### 0.9.0 (2026/11/06)\n\n* Older change.\n" + File.write(path, content) + updater = described_class.new(path: path, placeholder: '* Your contribution here.', sections: %w[Features Fixes]) + + 2.times { updater.release!(release) } + expect(File.read(path)).to eq("### 1.0.0 (2026/11/07)\n\n#### Fixes\n\n* Fix.\n\n### 0.9.0 (2026/11/06)\n\n* Older change.\n") + + 2.times { updater.prepare_next!(next_release) } + expect(File.read(path)).to eq("### 1.0.1 (Next)\n\n#### Features\n\n* Your contribution here.\n\n#### Fixes\n\n* Your contribution here.\n\n" \ + "### 1.0.0 (2026/11/07)\n\n#### Fixes\n\n* Fix.\n\n### 0.9.0 (2026/11/06)\n\n* Older change.\n") + end + + it 'keeps empty subsections without configured sections' do + File.write(path, "### 1.0.0 (Next)\n\n#### Features\n\n* Your contribution here.\n\n#### Fixes\n\n* Fix.\n* Your contribution here.\n") + described_class.new(path: path, placeholder: '* Your contribution here.').release!(release) + expect(File.read(path)).to eq("### 1.0.0 (2026/11/07)\n\n#### Features\n\n#### Fixes\n\n* Fix.\n") + end + + it 'rejects invalid configured sections and level-seven section headings', :aggregate_failures do + [%w[Fixes Fixes], [''], ["Two\nLines"], 'Fixes'].each do |sections| + expect { described_class.new(path: path, sections: sections) }.to raise_error(ArgumentError, /sections must be an array of unique nonempty names/) + end + File.write(path, "###### 1.0.0 (2026/11/07)\n\n- Change.\n") + expect { described_class.new(path: path, sections: %w[Fixes]).prepare_next!(next_release) }.to raise_error(ArgumentError, /deeper than level six/) + end + it 'ignores fenced headings and placeholders while releasing an Intridea section' do content = "## 1.0.0 (Next)\n\n```\n## Example\n- Your contribution here.\n```\n\n- Your contribution here.\n" File.write(path, content)