lib/knife/changelog/policyfile.rb in knife-changelog-1.4.0 vs lib/knife/changelog/policyfile.rb in knife-changelog-1.4.1
- old
+ new
@@ -9,10 +9,13 @@
require 'json'
require 'rest-client'
class PolicyChangelog
TMP_PREFIX = 'knife-changelog'
+ # Regex matching Chef cookbook version syntax
+ # See https://docs.chef.io/cookbook_versioning.html#syntax
+ VERSION_REGEX = /^[1-9]*[0-9](\.[0-9]+){1,2}$/
# Initialzes Helper class
#
# @param cookbooks [Array<String>] cookbooks to update (@name_args)
# @param policyfile [String] policyfile path
@@ -176,10 +179,14 @@
data['current_version'] == data['target_version'] || data['target_version'].nil?
end
# Search for cookbook downgrade and raise an error if any
def validate_downgrade!(data)
- downgrade = data.select { |_, ck| ::Gem::Version.new(ck['target_version']) < ::Gem::Version.new(ck['current_version']) }
+ downgrade = data.select do |_, ck|
+ # Do not try to validate downgrade on non-sementic versions (e.g. git revision)
+ ck['target_version'] =~ VERSION_REGEX && ck['current_version'] =~ VERSION_REGEX &&
+ ::Gem::Version.new(ck['target_version']) < ::Gem::Version.new(ck['current_version'])
+ end
return if downgrade.empty?
details = downgrade.map { |name, data| "#{name} (#{data['current_version']} -> #{data['target_version']})" }
raise "Trying to downgrade following cookbooks: #{details.join(', ')}"