Class: BetweenMeals::Changes::Cookbook

Inherits:
Change
  • Object
show all
Defined in:
lib/between_meals/changes/cookbook.rb

Overview

Changeset aware cookbook

Instance Attribute Summary

Attributes inherited from Change

#name, #status

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Change

#debug, debug, #info, info, #logger=, #to_s

Constructor Details

#initialize(files, cookbook_dirs) ⇒ Cookbook

Returns a new instance of Cookbook.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/between_meals/changes/cookbook.rb', line 85

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

Class Method Details

.explode_path(path) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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) ⇒ Object

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



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/between_meals/changes/cookbook.rb', line 117

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


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/between_meals/changes/cookbook.rb', line 41

def self.map_symlinks(files)
  # For each symlink get the source path, if any files have changed under
  # the source path, fake them as coming from the symlink path. This
  # allows the normal cookbook logic to just work.
  symlinks = {}
  @cookbook_dirs.each do |dir|
    dir = File.join(@repo_dir, dir)
    # Find symlinks in each cookbook_dir
    links = Dir.foreach(dir).select do |d|
      File.symlink?(File.join(dir, d))
    end
    links.each do |link|
      link = File.join(dir, link)
      next if symlinks[link]

      source = File.realpath(link)
      repo = File.join(@repo_dir, '/')
      # maps absolute symlink path to relative source and link paths
      symlinks[link] = {
        'source' => source.gsub(repo, ''),
        'link' => link.gsub(repo, ''),
      }
    end
  end

  # Create the file hash expected for each file that is a link or coming
  # from a linked directory but fake the source path as a symlink path.
  # Hacky but works :)
  links_to_append = []
  symlinks.each_value do |lrp| # link_abs_path, link_relative_path
    files.each do |f|
      # a symlink will never have trailing '/', add one.
      f[:path] += '/' if f[:path] == lrp['link']
      next unless f[:path].start_with?(lrp['source'])

      # This make a deep dup of the file hash
      l = Marshal.load(Marshal.dump(f))
      l[:path].gsub!(lrp['source'], lrp['link'])
      links_to_append << l
    end
  end
  links_to_append
end

.meaningful_cookbook_file?(path) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/between_meals/changes/cookbook.rb', line 21

def self.meaningful_cookbook_file?(path)
  !explode_path(path).nil?
end