Skip to content

Commit

Permalink
Merge pull request #5 from mattlqx/cookbook-metadata
Browse files Browse the repository at this point in the history
More cookbook metadata fixes
  • Loading branch information
mattlqx authored Mar 27, 2019
2 parents 7a1971a + ae53d5e commit c74a6a5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ To check Chef cookbook version bumps, use the `chef-cookbook-version` hook. Each
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.

- repo: https://github.com/mattlqx/pre-commit-ruby
rev: v1.2.3
rev: v1.2.4
hooks:
- id: rubocop
- id: foodcritic
Expand Down
14 changes: 6 additions & 8 deletions bin/cookbook-wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,12 @@ def bump_required?(file)
def higher?(old_version, new_version)
old_version = old_version.split('.')
new_version.split('.').each_with_index do |n, idx|
break true if n > old_version[idx]
break true if n.to_i > old_version[idx].to_i
end == true
end

def file_from_git_history(branch, path)
def file_from_git_history(path)
prefix = path.start_with?('/') ? '' : './'
from_branch = IO.popen("git show origin/#{branch}:#{prefix}#{path}", err: :close, &:read)
return from_branch unless from_branch == ''

IO.popen("git show origin/master:#{prefix}#{path}", err: :close, &:read)
end

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

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

if type == 'rb'
old_metadata = MetadataReader.new(file_from_git_history(branch, "#{cookbook}/metadata.rb"), "#{cookbook}/metadata.rb") # rubocop:disable Metrics/LineLength
old_metadata = MetadataReader.new(file_from_git_history("#{cookbook}/metadata.rb"), "#{cookbook}/metadata.rb") # rubocop:disable Metrics/LineLength
new_metadata = MetadataReader.new(IO.read("#{cookbook}/metadata.rb"), "#{cookbook}/metadata.rb")
else
old_metadata = JSON.parse(file_from_git_history(branch, "#{cookbook}/metadata.json")) rescue {} # rubocop:disable Style/RescueModifier, Metrics/LineLength
old_metadata = JSON.parse(file_from_git_history("#{cookbook}/metadata.json")) rescue {} # rubocop:disable Style/RescueModifier, Metrics/LineLength
new_metadata = JSON.parse(IO.read("#{cookbook}/metadata.json"))
end

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

0 comments on commit c74a6a5

Please sign in to comment.