lib/pdk/util/changelog_generator.rb in pdk-2.7.1 vs lib/pdk/util/changelog_generator.rb in pdk-3.0.0
- old
+ new
@@ -2,11 +2,11 @@
module PDK
module Util
module ChangelogGenerator
# Taken from the version regex in https://forgeapi.puppet.com/schemas/module.json
- VERSION_REGEX = %r{^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-(0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(\.(0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*)?(\+[0-9a-zA-Z-]+(\.[0-9a-zA-Z-]+)*)?$}
+ VERSION_REGEX = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-(0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(\.(0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*)?(\+[0-9a-zA-Z-]+(\.[0-9a-zA-Z-]+)*)?$/.freeze
GEM = 'github_changelog_generator'.freeze
# Raises if the github_changelog_generator is not available
def self.github_changelog_generator_available!
check_command = PDK::CLI::Exec::InteractiveCommand.new(PDK::CLI::Exec.bundle_bin, 'info', 'github_changelog_generator')
@@ -14,11 +14,11 @@
result = check_command.execute!
return if result[:exit_code].zero?
- raise PDK::CLI::ExitWithError, 'Unable to generate the changelog as the %{gem} gem is not included in this module\'s Gemfile' % { gem: GEM }
+ raise PDK::CLI::ExitWithError, format('Unable to generate the changelog as the %{gem} gem is not included in this module\'s Gemfile', gem: GEM)
end
# Runs the Changelog Generator gem (in the module's context) to automatically create a CHANGLELOG.MD file
#
# @returns [String] The content of the new Changelog
@@ -27,26 +27,28 @@
changelog_command = PDK::CLI::Exec::InteractiveCommand.new(PDK::CLI::Exec.bundle_bin, 'exec', 'rake', 'changelog')
changelog_command.context = :module
result = changelog_command.execute!
- raise PDK::CLI::ExitWithError, 'Error generating changelog: %{stdout}' % { stdout: result[:stdout] } unless result[:exit_code].zero?
+ raise PDK::CLI::ExitWithError, format('Error generating changelog: %{stdout}', stdout: result[:stdout]) unless result[:exit_code].zero?
output = changelog_content
- raise PDK::CLI::ExitWithError, 'The generated changelog contains uncategorized Pull Requests. Please label them and try again. See %{changelog_file} for more details' % { changelog_file: changelog_file } if output =~ %r{UNCATEGORIZED PRS; GO LABEL THEM} # rubocop:disable Metrics/LineLength
+ raise PDK::CLI::ExitWithError, format('The generated changelog contains uncategorized Pull Requests. Please label them and try again. See %{changelog_file} for more details', changelog_file: changelog_file) if output.include?('UNCATEGORIZED PRS; GO LABEL THEM') # rubocop:disable Metrics/LineLength
+
output
end
# Computes the next version, based on the content of a changelog
#
# @param current_version [String, Gem::Version] The current version of the module
# @return [String] The new version. May be the same as the current version if there are no notable changes
def self.compute_next_version(current_version)
- raise PDK::CLI::ExitWithError, 'Invalid version string %{version}' % { version: current_version } unless current_version =~ VERSION_REGEX
+ raise PDK::CLI::ExitWithError, format('Invalid version string %{version}', version: current_version) unless VERSION_REGEX.match?(current_version)
+
version = Gem::Version.create(current_version).segments
- PDK.logger.info 'Determing the target version from \'%{file}\'' % { file: changelog_file }
+ PDK.logger.info format('Determing the target version from \'%{file}\'', file: changelog_file)
# Grab all lines that start with ## between from the latest changes
# For example given the changelog below
# ```
@@ -81,28 +83,28 @@
changelog_content.each_line do |line|
line.strip!
if line.start_with?('[')
# We're leaving the latest changes so we can break
break if in_changelog_entry
+
in_changelog_entry = true
end
- if in_changelog_entry && line.start_with?('##')
- data += line
- end
+ data += line if in_changelog_entry && line.start_with?('##')
end
# Check for meta headers in first two header line matches
- if data =~ %r{^### Changed}
+ case data
+ when /^### Changed/
# Major Version bump
version[0] += 1
version[1] = 0
version[2] = 0
- elsif data =~ %r{^### Added}
+ when /^### Added/
# Minor Version bump
version[1] += 1
version[2] = 0
- elsif data =~ %r{^### Fixed}
+ when /^### Fixed/
# Patch Version bump
version[2] += 1
end
version.join('.')
@@ -126,9 +128,10 @@
@changelog_file ||= ['CHANGELOG.md', 'CHANGELOG.MD'].map { |file| PDK::Util::Filesystem.expand_path(file) }.find { |path| PDK::Util::Filesystem.file?(path) }
end
def self.changelog_content
return '' if changelog_file.nil?
+
PDK::Util::Filesystem.read_file(changelog_file, open_args: 'rb:utf-8')
end
end
end
end