Skip to content

Commit

Permalink
Merge pull request #2 from mattlqx/cookbook-metadata
Browse files Browse the repository at this point in the history
fall back to master if branch doesn't exist yet
  • Loading branch information
mattlqx authored Mar 21, 2019
2 parents 2ecf67f + 0af41b1 commit 65a2bb8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 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.0
rev: v1.2.1
hooks:
- id: rubocop
- id: foodcritic
Expand Down
13 changes: 9 additions & 4 deletions bin/cookbook-wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ def higher?(old_version, new_version)
end == true
end

def file_from_git_history(branch, 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

# Simple metadata.rb reader
class MetadataReader
attr_reader :data
Expand Down Expand Up @@ -90,14 +97,12 @@ def method_missing(sym, *args, &_block) # rubocop:disable Metrics/AbcSize, Style
changed_cookbooks.each do |cb_data|
cookbook = cb_data[0]
type = cb_data[1]
prefix = cookbook.start_with?('/') ? '' : './'
git_cmd = "git show origin/#{branch}:#{prefix}#{cookbook}/metadata.#{type}"

if type == 'rb'
old_metadata = MetadataReader.new(IO.popen(git_cmd, err: :close, &:read), "#{cookbook}/metadata.rb")
old_metadata = MetadataReader.new(file_from_git_history(branch, "#{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(IO.popen(git_cmd, err: :close, &:read)) rescue {} # rubocop:disable Style/RescueModifier
old_metadata = JSON.parse(file_from_git_history(branch, "#{cookbook}/metadata.json")) rescue {} # rubocop:disable Style/RescueModifier, Metrics/LineLength
new_metadata = JSON.parse(IO.read("#{cookbook}/metadata.json"))
end

Expand Down

0 comments on commit 65a2bb8

Please sign in to comment.