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, 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 ⇒ Octokit::Client
Client The octokit client that has been created.
Class Method Details
.add_staged_files(git_repo) ⇒ Object
280 281 282 283 284 285 286 287 |
# File 'lib/pdksync.rb', line 280 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 |
.checkout_branch(git_repo, branch_suffix) ⇒ Object
260 261 262 |
# File 'lib/pdksync.rb', line 260 def self.checkout_branch(git_repo, branch_suffix) git_repo.branch("pdksync_#{branch_suffix}").checkout end |
.clean_env(output_path) ⇒ Object
191 192 193 194 |
# File 'lib/pdksync.rb', line 191 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
Returns A git object representing the local repository.
206 207 208 209 210 |
# File 'lib/pdksync.rb', line 206 def self.clone_directory(namespace, module_name, output_path) Git.clone("https://github.com/#{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
297 298 299 300 301 302 303 304 |
# File 'lib/pdksync.rb', line 297 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
181 182 183 184 185 |
# File 'lib/pdksync.rb', line 181 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
142 143 144 |
# File 'lib/pdksync.rb', line 142 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
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/pdksync.rb', line 330 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
357 358 359 360 361 |
# File 'lib/pdksync.rb', line 357 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
36 37 38 39 40 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 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 |
# File 'lib/pdksync.rb', line 36 def self.main(steps: [:clone], args: nil) create_filespace client = setup_client module_names = return_modules 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[: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| 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, 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 args = { branch_name: "pdksync_#{ref}", commit_message: "pdksync_#{ref}", pr_title: "pdksync_#{ref}" } 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, args[:branch_name], 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") pr = create_pr(client, repo_name, git_instance.current_branch, pdk_version, args[:pr_title]) pr_list.push(pr.html_url) print 'created pr, ' end if steps.include?(:clean_branches) Dir.chdir(main_path) unless Dir.pwd == main_path delete_branch(client, repo_name, 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
Returns The status code of the pdk update run.
234 235 236 237 238 239 240 |
# File 'lib/pdksync.rb', line 234 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
314 315 316 317 318 |
# File 'lib/pdksync.rb', line 314 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
Returns An array of different module names.
162 163 164 |
# File 'lib/pdksync.rb', line 162 def self.return_modules YAML.safe_load(File.open(@managed_modules)) end |
.return_pdk_path ⇒ Object
Returns String Path to the pdk executable.
170 171 172 173 174 175 176 177 178 179 |
# File 'lib/pdksync.rb', line 170 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
Returns A string value that represents the current pdk version.
270 271 272 273 274 |
# File 'lib/pdksync.rb', line 270 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.
248 249 250 251 252 |
# File 'lib/pdksync.rb', line 248 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
Returns The status code of the command run.
220 221 222 223 224 225 226 |
# File 'lib/pdksync.rb', line 220 def self.run_command(output_path, command) Dir.chdir(output_path) unless Dir.pwd == output_path stdout, stderr, status = Open3.capture3(command) puts "\n#{stdout}\n".yellow puts "(FAILURE) Unable to run command '#{command}': #{stderr}".red unless status.exitstatus.zero? status.exitstatus end |
.setup_client ⇒ Octokit::Client
Returns client The octokit client that has been created.
150 151 152 153 154 155 156 |
# File 'lib/pdksync.rb', line 150 def self.setup_client client = Octokit::Client.new(access_token: @access_token.to_s) client.user.login client rescue ArgumentError, Octokit::Unauthorized raise "Access Token not set up correctly - Use export 'GITHUB_TOKEN=<put your token here>' to set it." end |