knife-changelog 1.2.4 → 1.4.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4f1c7e863b74d109a6621879de1419046bbc5a2afb08ce618772f349d6aea0da
4
- data.tar.gz: 5c6014713f79e60f1f517073166e114936d75f57db163f51e8f3aee6880e4614
3
+ metadata.gz: cd73707c23a179823ac967479e0ec8de3780d759fb05689e229854281e33a0a8
4
+ data.tar.gz: 80bc4745a9c0f39426a6f259d81a46ec13b17680a6601ed5cba2543fb01ed45a
5
5
  SHA512:
6
- metadata.gz: bff9ad82bfe2d79b1b0d5094ee5b88e396b61013fb453aed9f57a584b48458428a7ec15dd88e63b6affb2e5b1c6e441112847b269312578d3d6f1505382d5660
7
- data.tar.gz: c8e4d5741503d05cea8eaf23f1b8a120292733b8b7d723ac56c9b9f13977d0a501ffce943b6203f86d40e5f5cf16047d7b5a088b412e5a46c235c5a212419bee
6
+ metadata.gz: f86532a7c3005c3ec488fe090f66d7c779d148b9fed9eccd90a546bb9c465cc09e23df145618771b994171703576235494c81a4b207b4b6de027799b4bd897a1
7
+ data.tar.gz: 0f12940e801842989f715b5b0b5be67a6abaaac1d01abfaf3808af42d750beaa7a726fd5d075871094ebda5f044eb6784dd57d68d12c818093130d6b03bb5ab9
@@ -5,7 +5,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = 'knife-changelog'
8
- spec.version = '1.2.4'
8
+ spec.version = '1.4.2'
9
9
  spec.authors = ['Gregoire Seux']
10
10
  spec.email = ['kamaradclimber@gmail.com']
11
11
  spec.summary = 'Facilitate access to cookbooks changelog'
@@ -42,6 +42,12 @@ class Chef
42
42
  long: '--submodules SUBMODULE[,SUBMODULE]',
43
43
  description: 'Submoduless to check for changes as well (comma separated)'
44
44
 
45
+ option :prevent_downgrade,
46
+ long: '--prevent-downgrade',
47
+ description: 'Fail if knife-changelog detect a cookbook downgrade',
48
+ boolean: true,
49
+ default: false
50
+
45
51
  option :policyfile,
46
52
  long: '--policyfile PATH',
47
53
  description: 'Link to policyfile, defaults to "Policyfile.rb"',
@@ -58,7 +64,7 @@ class Chef
58
64
  description: 'Update Berksfile'
59
65
 
60
66
  def run
61
- Log.info config
67
+ Log.info config.to_s
62
68
  if config[:policyfile] && File.exist?(config[:policyfile])
63
69
  puts PolicyChangelog.new(
64
70
  @name_args,
@@ -239,10 +239,15 @@ class KnifeChangelog
239
239
  c
240
240
  end
241
241
 
242
+ GERRIT_REGEXP = %r{^(.*)/[^/]+/[^/]+(?:\.git)$}
242
243
  def linkify(url, changelog)
243
- changelog.map do |line|
244
- line.gsub(/^([a-f0-9]+) (.*)$/, '\2 (%s/commit/\1) ' % [url.chomp('.git')])
245
- end
244
+ format = case url
245
+ when /gitlab/, /github/
246
+ "\\2 (#{url.chomp('.git')}/commit/\\1)"
247
+ when GERRIT_REGEXP
248
+ "\\2 (#{::Regexp.last_match(1)}/#/q/\\1)"
249
+ end
250
+ format ? changelog.map { |line| line.sub(/^([a-f0-9]+) (.*)$/, format) } : changelog
246
251
  end
247
252
 
248
253
  def https_url(location)
@@ -3,6 +3,7 @@
3
3
  require 'chef'
4
4
  require 'chef/knife'
5
5
  require 'chef-dk/command/update'
6
+ require 'chef-dk/command/install'
6
7
  require 'deep_merge'
7
8
  require 'git'
8
9
  require 'json'
@@ -10,6 +11,9 @@ require 'rest-client'
10
11
 
11
12
  class PolicyChangelog
12
13
  TMP_PREFIX = 'knife-changelog'
14
+ # Regex matching Chef cookbook version syntax
15
+ # See https://docs.chef.io/cookbook_versioning.html#syntax
16
+ VERSION_REGEX = /^[1-9]*[0-9](\.[0-9]+){1,2}$/
13
17
 
14
18
  # Initialzes Helper class
15
19
  #
@@ -28,6 +32,8 @@ class PolicyChangelog
28
32
  def update_policyfile_lock
29
33
  backup_dir = Dir.mktmpdir
30
34
  FileUtils.cp(File.join(@policyfile_dir, 'Policyfile.lock.json'), backup_dir)
35
+ installer = ChefDK::Command::Install.new
36
+ raise "Cannot install Policyfile lock #{@policyfile_path}" unless installer.run([@policyfile_relative_path]).zero?
31
37
  updater = ChefDK::Command::Update.new
32
38
  raise "Error updating Policyfile lock #{@policyfile_path}" unless updater.run([@policyfile_path, @cookbooks_to_update].flatten).zero?
33
39
  updated_policyfile_lock = read_policyfile_lock(@policyfile_dir)
@@ -173,10 +179,24 @@ class PolicyChangelog
173
179
  data['current_version'] == data['target_version'] || data['target_version'].nil?
174
180
  end
175
181
 
182
+ # Search for cookbook downgrade and raise an error if any
183
+ def validate_downgrade!(data)
184
+ downgrade = data.select do |_, ck|
185
+ # Do not try to validate downgrade on non-sementic versions (e.g. git revision)
186
+ ck['target_version'] =~ VERSION_REGEX && ck['current_version'] =~ VERSION_REGEX &&
187
+ ::Gem::Version.new(ck['target_version']) < ::Gem::Version.new(ck['current_version'])
188
+ end
189
+
190
+ return if downgrade.empty?
191
+
192
+ details = downgrade.map { |name, data| "#{name} (#{data['current_version']} -> #{data['target_version']})" }
193
+ raise "Trying to downgrade following cookbooks: #{details.join(', ')}"
194
+ end
195
+
176
196
  # Generates Policyfile changelog
177
197
  #
178
198
  # @return [String] formatted version changelog
179
- def generate_changelog
199
+ def generate_changelog(prevent_downgrade: false)
180
200
  lock_current = read_policyfile_lock(@policyfile_dir)
181
201
  current = versions(lock_current['cookbook_locks'], 'current')
182
202
 
@@ -189,6 +209,9 @@ class PolicyChangelog
189
209
  else
190
210
  updated_cookbooks.select { |name, _data| @cookbooks_to_update.include?(name) }
191
211
  end
212
+
213
+ validate_downgrade!(updated_cookbooks) if prevent_downgrade
214
+
192
215
  generate_changelog_from_versions(changelog_cookbooks)
193
216
  end
194
217
 
@@ -92,6 +92,35 @@ RSpec.describe PolicyChangelog do
92
92
  end
93
93
  end
94
94
 
95
+ describe '#linkify' do
96
+ subject { KnifeChangelog::Changelog.new(config) }
97
+ let(:config) { double('config') }
98
+ let(:changelog) do
99
+ ['9363423 Leverage criteo-flavor 3.11 to benefit from labels']
100
+ end
101
+ context 'when url is gitlab style' do
102
+ let(:url) { 'https://gitlab.com/chef-cookbooks/criteo-rackguru.git' }
103
+
104
+ it 'creates a gitlab style link' do
105
+ expect(subject.linkify(url, changelog).first).to match(%r{https://gitlab.com/chef-cookbooks/criteo-rackguru/commit/9363423})
106
+ end
107
+ end
108
+
109
+ context 'when url is github style' do
110
+ let(:url) { 'https://github.com/chef-cookbooks/criteo-rackguru.git' }
111
+ it 'creates a github style link' do
112
+ expect(subject.linkify(url, changelog).first).to match(%r{https://github.com/chef-cookbooks/criteo-rackguru/commit/9363423})
113
+ end
114
+ end
115
+
116
+ context 'when url has no known style' do
117
+ let(:url) { 'https://review.mycompany.com/chef-cookbooks/criteo-rackguru.git' }
118
+ it 'creates a gerrit style link' do
119
+ expect(subject.linkify(url, changelog).first).to match(%r{https://review.mycompany.com/#/q/9363423})
120
+ end
121
+ end
122
+ end
123
+
95
124
  describe '#versions' do
96
125
  context 'when type is current' do
97
126
  it 'returns correct current versions' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knife-changelog
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.4
4
+ version: 1.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gregoire Seux
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-24 00:00:00.000000000 Z
11
+ date: 2020-11-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -243,8 +243,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
243
243
  - !ruby/object:Gem::Version
244
244
  version: '0'
245
245
  requirements: []
246
- rubyforge_project:
247
- rubygems_version: 2.7.7
246
+ rubygems_version: 3.0.8
248
247
  signing_key:
249
248
  specification_version: 4
250
249
  summary: Facilitate access to cookbooks changelog