class Reagan::ChangeSet

determines changed files in the commit

Public Class Methods

changed_files() click to toggle source

return hash of chef objects that have been changed

   # File lib/reagan/changeset.rb
33 def self::changed_files
34   if Config.settings['flags']['override_cookbooks']
35     files_from_override
36   else
37     pull = Config.settings['flags']['pull']
38     puts "Grabbing contents of pull request #{pull}\n"
39     hash_builder(query_gh(pull))
40   end
41 end
empty?() click to toggle source

check if the changeset is empty

   # File lib/reagan/changeset.rb
44 def self::empty?
45   objects_updated = false
46   %w(cookbooks roles environments data_bags).each do |object|
47     objects_updated = true unless files[object].empty?
48   end
49   !objects_updated
50 end
files() click to toggle source
   # File lib/reagan/changeset.rb
28 def self::files
29   @files ||= changed_files
30 end
files_from_override() click to toggle source

build a files hash based on the override cookbooks passed by the user

   # File lib/reagan/changeset.rb
53 def self::files_from_override
54   files = {}
55   %w(environments roles data_bags).each { |object| files[object] = {} }
56 
57   # ensure that the passed cookbooks exist in the workspace first
58   cookbooks = []
59   Config.settings['flags']['override_cookbooks'].each do |cb|
60     if object_still_exists(::File.join('cookbooks/', cb))
61       cookbooks << cb
62     else
63       puts "Skipping override cookbook #{cb} as it does not exist in the workspace"
64     end
65   end
66 
67   files['cookbooks'] = cookbooks
68   files
69 end
files_from_pull(pull_changes) click to toggle source

convert pull request response to array of changed files

   # File lib/reagan/changeset.rb
83 def self::files_from_pull(pull_changes)
84   files = []
85   pull_changes.each do |file|
86     files << file[:filename]
87   end
88   files
89 end
hash_builder(pull_files) click to toggle source

builds a hash of files / cookbooks that changed based on the pull data from GH

    # File lib/reagan/changeset.rb
 97 def self::hash_builder(pull_files)
 98   files = {}
 99   %w(environments roles data_bags cookbooks).each { |object| files[object] = [] }
100   cookbooks = []
101 
102   pull_files.each do |file|
103     files['environments'] << file && next if file.match('^environments') && object_still_exists(file)
104     files['roles'] << file && next if file.match('^roles') && object_still_exists(file)
105     files['data_bags'] << file && next if file.match('^data_bags') && object_still_exists(file)
106     cookbooks << file.split('/')[1] if file.match('^cookbooks') && object_still_exists(::File.join('cookbooks/', file.split('/')[1]))
107   end
108   # set cookbooks array to set to dedupe list of cookbooks
109   files['cookbooks'] = cookbooks.to_set
110   files
111 end
object_still_exists(file) click to toggle source

check to see if the file exists in the workspace so we don't test deleted objects

   # File lib/reagan/changeset.rb
92 def self::object_still_exists(file)
93   ::File.exist?(::File.join(Config.settings['jenkins']['workspace_dir'], file))
94 end
query_gh(pull_num) click to toggle source

queries github for the files that have changed

   # File lib/reagan/changeset.rb
72 def self::query_gh(pull_num)
73   Octokit.auto_paginate = true # avoids issues with large commits
74   Octokit.api_endpoint = Config.settings['github']['api_endpoint'] || 'https://api.github.com'
75   gh = Octokit::Client.new(access_token: Config.settings['github']['auth_token'])
76   files_from_pull(gh.pull_request_files(Config.settings['github']['repo'], pull_num))
77 rescue
78   puts 'Failed to query pull request contents from Github. Check the api_endpoint, repo, and token.'.to_red
79   exit 1
80 end