Module: PdkSync
- Includes:
- Constants
- Defined in:
- lib/pdksync.rb,
lib/pdksync/constants.rb
Defined Under Namespace
Modules: Constants
Constant Summary
Constants included from Constants
Constants::ACCESS_TOKEN, Constants::CREATE_PR_AGAINST, Constants::MANAGED_MODULES, Constants::NAMESPACE, Constants::PDKSYNC_DIR, Constants::PUSH_FILE_DESTINATION
Class Method Summary collapse
- .add_staged_files(git_repo) ⇒ Object
- .checkout_branch(git_repo, template_ref) ⇒ Object
- .clean_branches ⇒ Object
- .clean_env(output_path) ⇒ Object
-
.clone_directory(namespace, module_name, output_path) ⇒ Git::Base
A git object representing the local repository.
- .commit_staged_files(git_repo, template_ref) ⇒ Object
- .create_filespace ⇒ Object
- .create_pr(client, repo_name, template_ref, pdk_version) ⇒ Object
- .delete_branch(client, repo_name, branch_name) ⇒ Object
-
.pdk_convert(output_path) ⇒ Integer
The status code of the pdk converty run.
-
.pdk_update(output_path) ⇒ Integer
The status code of the pdk update run.
- .push_staged_files(git_repo, template_ref, repo_name) ⇒ Object
-
.retrieve_branches(client, repo_name) ⇒ Array
An array containing all existing branches.
-
.return_modules ⇒ Array
An array of different module names.
-
.return_pdk_version(metadata_file = 'metadata.json') ⇒ String
A string value that represents the current pdk version.
-
.return_template_ref(metadata_file = 'metadata.json') ⇒ String
A string value that represents the current pdk template.
- .run_pdksync ⇒ Object
-
.setup_client ⇒ Octokit::Client
Client The octokit client that has been created.
- .sync(module_name, client) ⇒ Object
-
.validate_autofix ⇒ Integer
The status code of the pdk validate run.
Class Method Details
.add_staged_files(git_repo) ⇒ Object
227 228 229 230 |
# File 'lib/pdksync.rb', line 227 def self.add_staged_files(git_repo) git_repo.add(all: true) puts 'All files have been staged.' end |
.checkout_branch(git_repo, template_ref) ⇒ Object
206 207 208 209 |
# File 'lib/pdksync.rb', line 206 def self.checkout_branch(git_repo, template_ref) puts "Creating the following branch: pdksync_#{template_ref}." git_repo.branch("pdksync_#{template_ref}".to_s).checkout end |
.clean_branches ⇒ Object
282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/pdksync.rb', line 282 def self.clean_branches puts 'Beginning pdksync cleanup run' @client = setup_client @module_names = return_modules @module_names.each do |module_name| puts '*************************************' puts "Cleaning #{module_name}" @repo_name = "#{@namespace}/#{module_name}" retrieve_branches(@client, @repo_name).each do |branch| delete_branch(@client, @repo_name, branch.name) if branch.name.include? 'pdksync' end end end |
.clean_env(output_path) ⇒ Object
112 113 114 115 116 |
# File 'lib/pdksync.rb', line 112 def self.clean_env(output_path) puts 'Cleaning your environment.' # If a local copy already exists it is removed FileUtils.rm_rf(output_path) end |
.clone_directory(namespace, module_name, output_path) ⇒ Git::Base
Returns A git object representing the local repository.
128 129 130 131 132 133 |
# File 'lib/pdksync.rb', line 128 def self.clone_directory(namespace, module_name, output_path) puts "Cloning #{module_name} to #{output_path}." Git.clone("https://github.com/#{namespace}/#{module_name}.git", output_path.to_s) # is returned rescue Git::GitExecuteError puts "(FAILURE) Cloning #{module_name} has failed - check the module name and namespace are correct." end |
.commit_staged_files(git_repo, template_ref) ⇒ Object
238 239 240 241 |
# File 'lib/pdksync.rb', line 238 def self.commit_staged_files(git_repo, template_ref) git_repo.commit("pdksync_#{template_ref}") puts "Creating the following commit: pdksync_#{template_ref}." end |
.create_filespace ⇒ Object
57 58 59 |
# File 'lib/pdksync.rb', line 57 def self.create_filespace FileUtils.mkdir @pdksync_dir unless Dir.exist?(@pdksync_dir) end |
.create_pr(client, repo_name, template_ref, pdk_version) ⇒ Object
268 269 270 271 272 273 274 275 276 277 |
# File 'lib/pdksync.rb', line 268 def self.create_pr(client, repo_name, template_ref, pdk_version) pr = client.create_pull_request(repo_name, @create_pr_against, "pdksync_#{template_ref}".to_s, "pdksync - Update using #{pdk_version}", "pdk version: `#{pdk_version}` \n pdk template ref: `#{template_ref}`") puts 'The PR has been created.' pr rescue StandardError puts "(FAILURE) PR creation for #{repo_name} has failed." end |
.delete_branch(client, repo_name, branch_name) ⇒ Object
318 319 320 321 |
# File 'lib/pdksync.rb', line 318 def self.delete_branch(client, repo_name, branch_name) puts "Removing '#{branch_name}' from '#{repo_name}'" client.delete_branch(repo_name, branch_name) end |
.pdk_convert(output_path) ⇒ Integer
Returns The status code of the pdk converty run.
141 142 143 144 145 146 147 148 149 150 |
# File 'lib/pdksync.rb', line 141 def self.pdk_convert(output_path) Dir.chdir(output_path) unless Dir.pwd == output_path _stdout, stderr, status = Open3.capture3('pdk convert --force --template-url https://github.com/puppetlabs/pdk-templates') if status != 0 puts "(FAILURE) Unable to run `pdk convert`: #{stderr}" else puts 'PDK convert has run.' end status end |
.pdk_update(output_path) ⇒ Integer
Returns The status code of the pdk update run.
158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/pdksync.rb', line 158 def self.pdk_update(output_path) # Runs the pdk update command Dir.chdir(output_path) unless Dir.pwd == output_path stdout, stderr, status = Open3.capture3('pdk update --force') if status != 0 puts "(FAILURE) Unable to run `pdk update`: #{stderr}" else puts 'PDK update has run.' end return status unless status == 0 && stdout.include?('No changes required.') # rubocop:disable Style/NumericPredicate puts 'No commits since last run.' end |
.push_staged_files(git_repo, template_ref, repo_name) ⇒ Object
251 252 253 254 255 256 |
# File 'lib/pdksync.rb', line 251 def self.push_staged_files(git_repo, template_ref, repo_name) git_repo.push(@push_file_destination, "pdksync_#{template_ref}") puts 'All staged files have been pushed to the repo, bon voyage!' rescue StandardError puts "(FAILURE) Pushing to #{@push_file_destination} for #{repo_name} has failed." end |
.retrieve_branches(client, repo_name) ⇒ Array
Returns An array containing all existing branches.
305 306 307 308 |
# File 'lib/pdksync.rb', line 305 def self.retrieve_branches(client, repo_name) puts "Retrieving branches from #{repo_name}" client.branches(repo_name) end |
.return_modules ⇒ Array
Returns An array of different module names.
78 79 80 |
# File 'lib/pdksync.rb', line 78 def self.return_modules YAML.safe_load(File.open(@managed_modules)) end |
.return_pdk_version(metadata_file = 'metadata.json') ⇒ String
Returns A string value that represents the current pdk version.
217 218 219 220 221 |
# File 'lib/pdksync.rb', line 217 def self.return_pdk_version( = 'metadata.json') file = File.read() data_hash = JSON.parse(file) data_hash['pdk-version'] end |
.return_template_ref(metadata_file = 'metadata.json') ⇒ String
Returns A string value that represents the current pdk template.
194 195 196 197 198 |
# File 'lib/pdksync.rb', line 194 def self.return_template_ref( = 'metadata.json') file = File.read() data_hash = JSON.parse(file) data_hash['template-ref'] end |
.run_pdksync ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/pdksync.rb', line 37 def self.run_pdksync puts 'Beginning pdksync run' create_filespace @client = setup_client @module_names = return_modules # The current directory is saved for cleanup purposes @main_path = Dir.pwd # Run an iterative loop for each @module_name @module_names.each do |module_name| puts '*************************************' puts "Syncing #{module_name}" sync(module_name, @client) # Cleanup used to ensure that the current directory is reset after each run. Dir.chdir(@main_path) unless Dir.pwd == @main_path end end |
.setup_client ⇒ Octokit::Client
Returns client The octokit client that has been created.
65 66 67 68 69 70 71 72 |
# File 'lib/pdksync.rb', line 65 def self.setup_client client = Octokit::Client.new(access_token: @access_token.to_s) client.user.login puts 'Client login has been successful.' client rescue ArgumentError raise "Access Token not set up correctly - Use export 'GITHUB_TOKEN=<put your token here>' to set it." end |
.sync(module_name, client) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/pdksync.rb', line 90 def self.sync(module_name, client) @repo_name = "#{@namespace}/#{module_name}" @output_path = "#{@pdksync_dir}/#{module_name}" clean_env(@output_path) if Dir.exist?(@output_path) @git_repo = clone_directory(@namespace, module_name, @output_path) return if @git_repo.nil? return unless pdk_update(@output_path) == 0 # rubocop:disable Style/NumericPredicate @template_ref = return_template_ref checkout_branch(@git_repo, @template_ref) @pdk_version = return_pdk_version add_staged_files(@git_repo) commit_staged_files(@git_repo, @template_ref) push_staged_files(@git_repo, @template_ref, @repo_name) create_pr(client, @repo_name, @template_ref, @pdk_version) end |
.validate_autofix ⇒ Integer
Returns The status code of the pdk validate run.
177 178 179 180 181 182 183 184 185 186 |
# File 'lib/pdksync.rb', line 177 def self.validate_autofix # Runs the pdk validate command _stdout, stderr, status = Open3.capture3('pdk validate -a') if status != 0 puts "(FAILURE) Something went wrong with the validate: #{stderr}" else puts 'Validate has run successfully.' end status end |