Skip to content

Commit c74a6a5

Browse files
authored
Merge pull request #5 from mattlqx/cookbook-metadata
More cookbook metadata fixes
2 parents 7a1971a + ae53d5e commit c74a6a5

2 files changed

Lines changed: 7 additions & 9 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ To check Chef cookbook version bumps, use the `chef-cookbook-version` hook. Each
1515
To unit test Ruby changes in your repo, use the `rspec` hook. Each path in your repo with a `spec` directory should have a `Gemfile` that includes your desired version of rspec (or a derivative library). It will be installed via Bundler prior to testing. Rspec will only be run against the closest directory in a changed file's path with a spec dir.
1616

1717
- repo: https://github.com/mattlqx/pre-commit-ruby
18-
rev: v1.2.3
18+
rev: v1.2.4
1919
hooks:
2020
- id: rubocop
2121
- id: foodcritic

bin/cookbook-wrapper.rb

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,12 @@ def bump_required?(file)
3535
def higher?(old_version, new_version)
3636
old_version = old_version.split('.')
3737
new_version.split('.').each_with_index do |n, idx|
38-
break true if n > old_version[idx]
38+
break true if n.to_i > old_version[idx].to_i
3939
end == true
4040
end
4141

42-
def file_from_git_history(branch, path)
42+
def file_from_git_history(path)
4343
prefix = path.start_with?('/') ? '' : './'
44-
from_branch = IO.popen("git show origin/#{branch}:#{prefix}#{path}", err: :close, &:read)
45-
return from_branch unless from_branch == ''
46-
4744
IO.popen("git show origin/master:#{prefix}#{path}", err: :close, &:read)
4845
end
4946

@@ -96,21 +93,22 @@ def method_missing(sym, *args, &_block) # rubocop:disable Metrics/AbcSize, Style
9693
exit(success) if changed_cookbooks.empty?
9794

9895
IO.popen('git fetch origin') { |_f| true }
99-
branch = IO.popen('git symbolic-ref --short HEAD', &:read).chomp
10096
changed_cookbooks.each do |cb_data|
10197
cookbook = cb_data[0]
10298
type = cb_data[1]
10399

104100
if type == 'rb'
105-
old_metadata = MetadataReader.new(file_from_git_history(branch, "#{cookbook}/metadata.rb"), "#{cookbook}/metadata.rb") # rubocop:disable Metrics/LineLength
101+
old_metadata = MetadataReader.new(file_from_git_history("#{cookbook}/metadata.rb"), "#{cookbook}/metadata.rb") # rubocop:disable Metrics/LineLength
106102
new_metadata = MetadataReader.new(IO.read("#{cookbook}/metadata.rb"), "#{cookbook}/metadata.rb")
107103
else
108-
old_metadata = JSON.parse(file_from_git_history(branch, "#{cookbook}/metadata.json")) rescue {} # rubocop:disable Style/RescueModifier, Metrics/LineLength
104+
old_metadata = JSON.parse(file_from_git_history("#{cookbook}/metadata.json")) rescue {} # rubocop:disable Style/RescueModifier, Metrics/LineLength
109105
new_metadata = JSON.parse(IO.read("#{cookbook}/metadata.json"))
110106
end
111107

112108
if old_metadata.empty?
113109
puts "#{cookbook} does not have a metadata file in git history. Skipping."
110+
elsif !old_metadata.data.key?(:version)
111+
puts "#{cookbook} did not previously have a version. Skipping."
114112
elsif new_metadata['version'] == old_metadata['version']
115113
puts "#{cookbook} has changed and has not been version bumped."
116114
success = false

0 commit comments

Comments
 (0)