Module: PdkSync
- Includes:
- Constants
- Defined in:
- lib/pdksync.rb,
lib/pdksync/constants.rb
Overview
Configuration is loaded from ‘$HOME/.pdksync.yml`. If $HOME is not set, the config_path will use the current directory. Set PDKSYNC_LABEL to ” to disable adding a label during pdksync runs.
Defined Under Namespace
Modules: Constants Classes: GitPlatformClient, GithubClient, GitlabClient, PullRequest
Constant Summary
Constants included from Constants
Constants::ACCESS_TOKEN, Constants::CREATE_PR_AGAINST, Constants::GITLAB_API_ENDPOINT, Constants::GIT_BASE_URI, Constants::GIT_PLATFORM, Constants::MANAGED_MODULES, Constants::NAMESPACE, Constants::PDKSYNC_DIR, Constants::PDKSYNC_LABEL, Constants::PUSH_FILE_DESTINATION
Class Method Summary collapse
- .add_label(client, repo_name, issue_number, label) ⇒ Object
- .add_staged_files(git_repo) ⇒ Object
-
.check_for_label(client, repo_name, label) ⇒ Boolean
A boolean stating whether the label was found.
- .checkout_branch(git_repo, branch_suffix) ⇒ 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, commit_message = nil) ⇒ Object
- .create_commit(git_repo, branch_name, commit_message) ⇒ Object
- .create_filespace ⇒ Object
- .create_pr(client, repo_name, template_ref, pdk_version, pr_title = nil) ⇒ Object
- .delete_branch(client, repo_name, branch_name) ⇒ Object
- .main(steps: [:clone], args: nil) ⇒ Object
-
.pdk_update(output_path) ⇒ Integer
The status code of the pdk update run.
- .push_staged_files(git_repo, current_branch, repo_name) ⇒ Object
-
.return_modules ⇒ Array
An array of different module names.
-
.return_pdk_path ⇒ Object
String Path to the pdk executable.
-
.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_command(output_path, command) ⇒ Integer
The status code of the command run.
-
.setup_client ⇒ PdkSync::GitPlatformClient
Client The Git platform client that has been created.
- .validate_modules_exist(client, module_names) ⇒ Object
Class Method Details
.add_label(client, repo_name, issue_number, label) ⇒ Object
465 466 467 468 469 470 |
# File 'lib/pdksync.rb', line 465 def self.add_label(client, repo_name, issue_number, label) client.update_issue(repo_name, issue_number, labels: [label]) rescue StandardError => error puts "(FAILURE) Adding label to #{repo_name} issue #{issue_number} has failed. #{error}".red return false end |
.add_staged_files(git_repo) ⇒ Object
356 357 358 359 360 361 362 363 |
# File 'lib/pdksync.rb', line 356 def self.add_staged_files(git_repo) if git_repo.status.changed != {} git_repo.add(all: true) puts 'All files have been staged.' else puts 'Nothing to commit.' end end |
.check_for_label(client, repo_name, label) ⇒ Boolean
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 |
# File 'lib/pdksync.rb', line 435 def self.check_for_label(client, repo_name, label) # Get labels from repository repo_labels = client.labels(repo_name) # Look for label in the repository's labels match = false repo_labels.each do |repo_label| if repo_label.name == label match = true break end end # Raise error if a match was not found else return true (match == false) ? (raise StandardError, "Label '#{label}' not found in #{repo_name}") : (return true) rescue StandardError => error puts "(FAILURE) Retrieving labels for #{repo_name} has failed. #{error}".red return false end |
.checkout_branch(git_repo, branch_suffix) ⇒ Object
336 337 338 |
# File 'lib/pdksync.rb', line 336 def self.checkout_branch(git_repo, branch_suffix) git_repo.branch("pdksync_#{branch_suffix}").checkout end |
.clean_env(output_path) ⇒ Object
254 255 256 257 |
# File 'lib/pdksync.rb', line 254 def self.clean_env(output_path) # If a local copy already exists it is removed FileUtils.rm_rf(output_path) end |
.clone_directory(namespace, module_name, output_path) ⇒ Git::Base
269 270 271 272 273 |
# File 'lib/pdksync.rb', line 269 def self.clone_directory(namespace, module_name, output_path) Git.clone("#{@git_base_uri}/#{namespace}/#{module_name}.git", output_path.to_s) # is returned rescue Git::GitExecuteError => error puts "(FAILURE) Cloning #{module_name} has failed. #{error}".red end |
.commit_staged_files(git_repo, template_ref, commit_message = nil) ⇒ Object
373 374 375 376 377 378 379 380 |
# File 'lib/pdksync.rb', line 373 def self.commit_staged_files(git_repo, template_ref, = nil) = if .nil? "pdksync_#{template_ref}" else end git_repo.commit() end |
.create_commit(git_repo, branch_name, commit_message) ⇒ Object
244 245 246 247 248 |
# File 'lib/pdksync.rb', line 244 def self.create_commit(git_repo, branch_name, ) checkout_branch(git_repo, branch_name) add_staged_files(git_repo) commit_staged_files(git_repo, branch_name, ) end |
.create_filespace ⇒ Object
184 185 186 |
# File 'lib/pdksync.rb', line 184 def self.create_filespace FileUtils.mkdir @pdksync_dir unless Dir.exist?(@pdksync_dir) end |
.create_pr(client, repo_name, template_ref, pdk_version, pr_title = nil) ⇒ Object
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
# File 'lib/pdksync.rb', line 406 def self.create_pr(client, repo_name, template_ref, pdk_version, pr_title = nil) if pr_title.nil? title = "pdksync - Update using #{pdk_version}" = "pdk version: `#{pdk_version}` \n pdk template ref: `#{template_ref}`" head = "pdksync_#{template_ref}" else title = "pdksync - #{pr_title}" = "#{pr_title}\npdk version: `#{pdk_version}` \n" head = template_ref end pr = client.create_pull_request(repo_name, @create_pr_against, head, title, ) pr rescue StandardError => error puts "(FAILURE) PR creation for #{repo_name} has failed. #{error}".red end |
.delete_branch(client, repo_name, branch_name) ⇒ Object
480 481 482 483 484 |
# File 'lib/pdksync.rb', line 480 def self.delete_branch(client, repo_name, branch_name) client.delete_branch(repo_name, branch_name) rescue StandardError => error puts "(FAILURE) Deleting #{branch_name} in #{repo_name} failed. #{error}".red end |
.main(steps: [:clone], args: nil) ⇒ Object
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 84 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 114 115 116 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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/pdksync.rb', line 51 def self.main(steps: [:clone], args: nil) create_filespace client = setup_client module_names = return_modules raise "No modules found in '#{@managed_modules}'" if module_names.nil? validate_modules_exist(client, module_names) pr_list = [] # The current directory is saved for cleanup purposes main_path = Dir.pwd # validation run_a_command if steps.include?(:run_a_command) raise '"run_a_command" requires an argument to run.' if args.nil? puts "Command '#{args}'" end # validation create_commit if steps.include?(:create_commit) raise 'Needs a branch_name and commit_message' if args.nil? || args[:commit_message].nil? || args[:branch_name].nil? puts "Commit branch_name=#{args[:branch_name]} commit_message=#{args[:commit_message]}" end # validation push_and_create_pr if steps.include?(:push_and_create_pr) raise 'Needs a pr_title' if args.nil? || args[:pr_title].nil? puts "PR title =#{args[:additional_title]} #{args[:pr_title]}" end # validation clean_branches if steps.include?(:clean_branches) raise 'Needs a branch_name, and the branch name contains the string pdksync' if args.nil? || args[:branch_name].nil? || !args[:branch_name].include?('pdksync') puts "Removing branch_name =#{args[:branch_name]}" end abort "No modules listed in #{@managed_modules}" if module_names.nil? module_names.each do |module_name| module_args = args.clone Dir.chdir(main_path) unless Dir.pwd == main_path print "#{module_name}, " repo_name = "#{@namespace}/#{module_name}" output_path = "#{@pdksync_dir}/#{module_name}" if steps.include?(:clone) clean_env(output_path) if Dir.exist?(output_path) print 'delete module directory, ' @git_repo = clone_directory(@namespace, module_name, output_path) print 'cloned, ' puts "(WARNING) Unable to clone repo for #{module_name}".red if @git_repo.nil? Dir.chdir(main_path) unless Dir.pwd == main_path next if @git_repo.nil? end puts '(WARNING) @output_path does not exist, skipping module'.red unless File.directory?(output_path) next unless File.directory?(output_path) if steps.include?(:pdk_convert) exit_status = run_command(output_path, "#{return_pdk_path} convert --force --template-url https://github.com/puppetlabs/pdk-templates") print 'converted, ' next unless exit_status.zero? end if steps.include?(:pdk_validate) Dir.chdir(main_path) unless Dir.pwd == main_path exit_status = run_command(output_path, "#{return_pdk_path} validate -a") print 'validated, ' next unless exit_status.zero? end if steps.include?(:run_a_command) Dir.chdir(main_path) unless Dir.pwd == main_path print 'run command, ' exit_status = run_command(output_path, module_args) next unless exit_status.zero? end if steps.include?(:pdk_update) Dir.chdir(main_path) unless Dir.pwd == main_path next unless pdk_update(output_path).zero? if steps.include?(:use_pdk_ref) ref = return_template_ref pr_title = module_args[:additional_title] ? "#{module_args[:additional_title]} - pdksync_#{ref}" : "pdksync_#{ref}" module_args = module_args.merge(branch_name: "pdksync_#{ref}", commit_message: pr_title, pr_title: pr_title, pdksync_label: @default_pdksync_label) end print 'pdk update, ' end if steps.include?(:create_commit) Dir.chdir(main_path) unless Dir.pwd == main_path git_instance = Git.open(output_path) create_commit(git_instance, module_args[:branch_name], module_args[:commit_message]) print 'commit created, ' end if steps.include?(:push_and_create_pr) Dir.chdir(main_path) unless Dir.pwd == main_path git_instance = Git.open(output_path) push_staged_files(git_instance, git_instance.current_branch, repo_name) print 'push, ' pdk_version = return_pdk_version("#{output_path}/metadata.json") # If a label is supplied, verify that it is available in the repo label = module_args[:pdksync_label] ? module_args[:pdksync_label] : module_args[:label] label_valid = (label.is_a?(String) && !label.to_str.empty?) ? check_for_label(client, repo_name, label) : nil # Exit current iteration if an error occured retrieving a label if label_valid == false raise 'Ensure label is valid' end # Create the PR and add link to pr list pr = create_pr(client, repo_name, git_instance.current_branch, pdk_version, module_args[:pr_title]) if pr.nil? break end pr_list.push(pr.html_url) print 'created pr, ' # If a valid label is supplied, add this to the PR if label_valid == true add_label(client, repo_name, pr.number, label) print "added label '#{label}' " end end if steps.include?(:clean_branches) Dir.chdir(main_path) unless Dir.pwd == main_path delete_branch(client, repo_name, module_args[:branch_name]) print 'branch deleted, ' end puts 'done.'.green end return if pr_list.size.zero? puts "\nPRs created:\n".blue pr_list.each do |pr| puts pr end end |
.pdk_update(output_path) ⇒ Integer
310 311 312 313 314 315 316 |
# File 'lib/pdksync.rb', line 310 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("#{return_pdk_path} update --force") puts "(FAILURE) Unable to run `pdk update`: #{stderr}".red unless status.exitstatus.zero? status.exitstatus end |
.push_staged_files(git_repo, current_branch, repo_name) ⇒ Object
390 391 392 393 394 |
# File 'lib/pdksync.rb', line 390 def self.push_staged_files(git_repo, current_branch, repo_name) git_repo.push(@push_file_destination, current_branch) rescue StandardError => error puts "(FAILURE) Pushing to #{@push_file_destination} for #{repo_name} has failed. #{error}".red end |
.return_modules ⇒ Array
202 203 204 205 |
# File 'lib/pdksync.rb', line 202 def self.return_modules raise "File '#{@managed_modules}' is empty/does not exist" if File.size?(@managed_modules).nil? YAML.safe_load(File.open(@managed_modules)) end |
.return_pdk_path ⇒ Object
233 234 235 236 237 238 239 240 241 242 |
# File 'lib/pdksync.rb', line 233 def self.return_pdk_path full_path = '/opt/puppetlabs/pdk/bin/pdk' path = if File.executable?(full_path) full_path else puts "(WARNING) Using pdk on PATH not '#{full_path}'".red 'pdk' end path end |
.return_pdk_version(metadata_file = 'metadata.json') ⇒ String
346 347 348 349 350 |
# File 'lib/pdksync.rb', line 346 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
324 325 326 327 328 |
# File 'lib/pdksync.rb', line 324 def self.return_template_ref( = 'metadata.json') file = File.read() data_hash = JSON.parse(file) data_hash['template-ref'] end |
.run_command(output_path, command) ⇒ Integer
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/pdksync.rb', line 283 def self.run_command(output_path, command) stdout = '' stderr = '' status = Process::Status Dir.chdir(output_path) unless Dir.pwd == output_path # Environment cleanup required due to Ruby subshells using current Bundler environment if command =~ %r{^bundle} Bundler.with_clean_env do stdout, stderr, status = Open3.capture3(command) end else stdout, stderr, status = Open3.capture3(command) end puts "\n#{stdout}\n".yellow puts "(FAILURE) Unable to run command '#{command}': #{stderr}".red unless status.exitstatus.zero? status.exitstatus end |
.setup_client ⇒ PdkSync::GitPlatformClient
192 193 194 195 196 |
# File 'lib/pdksync.rb', line 192 def self.setup_client PdkSync::GitPlatformClient.new(@git_platform, @git_platform_access_settings) rescue StandardError => error raise "Git platform access not set up correctly: #{error}" end |
.validate_modules_exist(client, module_names) ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/pdksync.rb', line 215 def self.validate_modules_exist(client, module_names) invalid_names = [] raise "Error reading in modules. Check syntax of '#{@managed_modules}'." unless !module_names.nil? && module_names.is_a?(Array) module_names.each do |module_name| # If module name is invalid, push it to invalid names array unless client.repository?("#{@namespace}/#{module_name}") invalid_names.push(module_name) next end end # Raise error if any invalid matches were found raise "Could not find the following repositories: #{invalid_names}" unless invalid_names.empty? end |