class BetweenMeals::Changes::Cookbook

Changeset aware cookbook

Public Class Methods

explode_path(path) click to toggle source
# File lib/between_meals/changes/cookbook.rb, line 25
def self.explode_path(path)
  @cookbook_dirs.each do |dir|
    re = %r{^#{dir}/([^/]+)/.*}
    debug("[cookbook] Matching #{path} against ^#{re}")
    m = path.match(re)
    next unless m

    info("Cookbook is #{m[1]}")
    return {
      :cookbook_dir => dir,
      :name => m[1],
    }
  end
  nil
end
find(list, cookbook_dirs, logger, repo, track_symlinks = false) click to toggle source

Given a list of changed files create a list of Cookbook objects

# File lib/between_meals/changes/cookbook.rb, line 130
def self.find(list, cookbook_dirs, logger, repo, track_symlinks = false)
  # rubocop:disable ClassVars
  @@logger = logger
  # rubocop:enable ClassVars
  return [] if list.nil? || list.empty?

  # rubocop:disable MultilineBlockChain
  @repo_dir = File.realpath(repo.repo_path)
  @cookbook_dirs = cookbook_dirs
  list += map_symlinks(list) if track_symlinks
  list.
    group_by do |x|
    # Group by prefix of cookbok_dir + cookbook_name
    # so that we treat deletes and modifications across
    # two locations separately
    g = self.explode_path(x[:path])
    g[:cookbook_dir] + '/' + g[:name] if g
  end.
    map do |_, change|
    # Confirm we're dealing with a cookbook
    # Changes to OWNERS or other stuff that might end up
    # in [core, other, secure] dirs are ignored
    is_cookbook = change.select do |c|
      self.meaningful_cookbook_file?(c[:path])
    end.any?
    if is_cookbook
      BetweenMeals::Changes::Cookbook.new(change, @cookbook_dirs)
    end
  end.compact
  # rubocop:enable MultilineBlockChain
end
meaningful_cookbook_file?(path) click to toggle source
# File lib/between_meals/changes/cookbook.rb, line 21
def self.meaningful_cookbook_file?(path)
  !explode_path(path).nil?
end
new(files, cookbook_dirs) click to toggle source
# File lib/between_meals/changes/cookbook.rb, line 98
def initialize(files, cookbook_dirs)
  @files = files
  @cookbook_dirs = cookbook_dirs
  @name = self.class.explode_path(files.sample[:path])[:name]
  # if metadata.(json|rb) is being deleted and we aren't also
  # adding/modifying one of those two,
  #   cookbook is marked for deletion
  # otherwise it was modified
  #   and will be re-uploaded
  if files.
     select { |x| x[:status] == :deleted }.
     map do |x|
       x[:path].match(
         %{^(#{cookbook_dirs.join('|')})/[^/]+/metadata\.(rb|json)$},
       )
     end.
     compact.
     any? &&
     files.reject { |x| x[:status] == :deleted }.
     map do |x|
       x[:path].match(
         %{^(#{cookbook_dirs.join('|')})/[^/]+/metadata\.(rb|json)$},
       )
     end.none?
    @status = :deleted
  else
    @status = :modified
  end
end