pdksync 0.2.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,59 +1,23 @@
1
- require_relative 'lib/pdksync'
2
- require 'github_changelog_generator/task'
3
-
4
- desc 'Run full pdksync process, clone repository, pdk update, create pr.'
5
- task :pdksync do
6
- args = {:branch_name => "pdksync_{ref}",
7
- :commit_message => "pdksync_{ref}",
8
- :pr_title => "pdksync_{ref}"}
9
- PdkSync::main(steps: [:use_pdk_ref, :clone, :pdk_update, :create_commit, :push_and_create_pr], args: args)
1
+ require "pdksync/rake_tasks"
2
+ require "github_changelog_generator/task"
3
+ require "rubocop/rake_task"
4
+ require "rspec/core/rake_task"
5
+ require "bundler/gem_tasks"
6
+
7
+ RuboCop::RakeTask.new(:rubocop) do |t|
8
+ t.options = ["--display-cop-names"]
10
9
  end
11
10
 
12
- namespace :pdk do
13
- desc 'Runs PDK convert against modules'
14
- task :pdk_convert do
15
- PdkSync::main(steps: [:pdk_convert])
16
- end
17
-
18
- desc 'Runs PDK validate against modules'
19
- task :pdk_validate do
20
- PdkSync::main(steps: [:pdk_validate])
21
- end
22
- end
23
-
24
- namespace :git do
25
- desc 'Clone managed modules'
26
- task :clone_managed_modules do
27
- PdkSync::main(steps: [:clone])
28
- end
29
-
30
- desc "Stage commits for modules, branchname and commit message eg rake 'git:create_commit[flippity, commit messagez]'"
31
- task :create_commit, [:branch_name, :commit_message] do |task, args|
32
- PdkSync::main(steps: [:create_commit], args: args)
33
- end
11
+ RSpec::Core::RakeTask.new(:spec)
34
12
 
35
- desc "Push commit, and create PR for modules eg rake 'git:push_and_create_pr[pr title goes here]'"
36
- task :push_and_create_pr, [:pr_title] do |task, args|
37
- PdkSync::main(steps: [:push_and_create_pr], args: args)
38
- end
39
-
40
- desc "Clean up origin branches, (branches must include pdksync in their name) eg rake 'git:clean[pdksync_origin_branch]'"
41
- task :clean_branches, [:branch_name] do |task, args|
42
- PdkSync::main(steps: [:clean_branches], args: args)
43
- end
44
- end
45
-
46
- desc "Run a command against modules eg rake 'run_a_command[complex command here -f -gx]'"
47
- task :run_a_command, [:command] do |task, args|
48
- PdkSync::main(steps: [:run_a_command], args: args[:command])
49
- end
13
+ task :default => :spec
50
14
 
51
15
  GitHubChangelogGenerator::RakeTask.new :changelog do |config|
52
- config.user = 'puppetlabs'
53
- config.project = 'pdksync'
16
+ config.user = "puppetlabs"
17
+ config.project = "pdksync"
54
18
  # config.since_tag = '1.1.1'
55
- config.future_release = '0.1.0'
56
- config.exclude_labels = ['maintenance']
19
+ config.future_release = "0.6.0"
20
+ config.exclude_labels = ["maintenance"]
57
21
  config.header = "# Change log\n\nAll notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org)."
58
22
  config.add_pr_wo_labels = true
59
23
  config.issues = false
data/lib/pdksync.rb CHANGED
@@ -4,359 +4,376 @@ require 'open3'
4
4
  require 'fileutils'
5
5
  require 'rake'
6
6
  require 'pdk'
7
- require 'octokit'
8
- require 'pdksync/constants'
7
+ require 'pdksync/configuration'
8
+ require 'pdksync/gitplatformclient'
9
+
9
10
  require 'json'
10
11
  require 'yaml'
11
12
  require 'colorize'
13
+ require 'bundler'
14
+ require 'octokit'
15
+ require 'pdk/util/template_uri'
16
+ require 'pdksync/logger'
17
+ require 'pdksync/utils'
18
+ require 'terminal-table'
12
19
 
13
20
  # @summary
14
21
  # This module set's out and controls the pdksync process
15
- # @param [String] @access_token
16
- # The token used to access github, must be exported locally.
17
- # @param [String] @namspace
18
- # The namespace of the repositories we are updating.
19
- # @param [String] @pdksync_dir
20
- # The local directory the repositories are to be copied to.
21
- # @param [String] @push_file_destination
22
- # The remote that the pull requests are to be made against.
23
- # @param [String] @create_pr_against
24
- # The branch the the pull requests are to be made against.
25
- # @param [String] @managed_modules
26
- # The file that the array of managed modules is to be retrieved from.
27
22
  module PdkSync
28
- include Constants
29
- @access_token = Constants::ACCESS_TOKEN
30
- @namespace = Constants::NAMESPACE
31
- @pdksync_dir = Constants::PDKSYNC_DIR
32
- @push_file_destination = Constants::PUSH_FILE_DESTINATION
33
- @create_pr_against = Constants::CREATE_PR_AGAINST
34
- @managed_modules = Constants::MANAGED_MODULES
23
+ def self.configuration
24
+ @configuration ||= PdkSync::Configuration.new
25
+ end
26
+
27
+ @main_path = Dir.pwd
28
+
29
+ def self.client
30
+ @client ||= Utils.setup_client
31
+ end
35
32
 
36
33
  def self.main(steps: [:clone], args: nil)
37
- create_filespace
38
- client = setup_client
39
- module_names = return_modules
34
+ Utils.check_pdk_version if ENV['PDKSYNC_VERSION_CHECK'].eql?('true')
35
+ Utils.create_filespace
36
+ Utils.create_filespace_gem
37
+ module_names = Utils.return_modules
38
+
39
+ unless steps.include?(:clone_gem) || steps.include?(:multi_gem_testing)
40
+ raise "No modules found in '#{Utils.configuration.managed_modules}'" if module_names.nil?
41
+ end
42
+
40
43
  pr_list = []
44
+ report_rows = []
45
+ table = Terminal::Table.new
46
+
41
47
  # The current directory is saved for cleanup purposes
42
48
  main_path = Dir.pwd
43
49
 
44
50
  # validation run_a_command
45
51
  if steps.include?(:run_a_command)
46
52
  raise '"run_a_command" requires an argument to run.' if args.nil?
47
- puts "Command '#{args}'"
53
+ PdkSync::Logger.info "Command '#{args}'"
48
54
  end
49
55
  # validation create_commit
50
56
  if steps.include?(:create_commit)
51
57
  raise 'Needs a branch_name and commit_message' if args.nil? || args[:commit_message].nil? || args[:branch_name].nil?
52
- puts "Commit branch_name=#{args[:branch_name]} commit_message=#{args[:commit_message]}"
58
+ PdkSync::Logger.info "Commit branch_name=#{args[:branch_name]} commit_message=#{args[:commit_message]}"
53
59
  end
54
- # validation push_and_create_pr
55
- if steps.include?(:push_and_create_pr)
60
+ # validation create_pr
61
+ if steps.include?(:create_pr)
56
62
  raise 'Needs a pr_title' if args.nil? || args[:pr_title].nil?
57
- puts "PR title =#{args[:pr_title]}"
63
+ PdkSync::Logger.info "PR title =#{args[:additional_title]} #{args[:pr_title]}"
58
64
  end
59
65
  # validation clean_branches
60
66
  if steps.include?(:clean_branches)
61
67
  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')
62
- puts "Removing branch_name =#{args[:branch_name]}"
68
+ PdkSync::Logger.info "Removing branch_name =#{args[:branch_name]}"
69
+ end
70
+ # validation clone_gem
71
+ if steps.include?(:clone_gem)
72
+ raise 'Needs a gem_name' if args.nil? || args[:gem_name].nil?
73
+ PdkSync::Logger.info "Command '#{args}'"
74
+ end
75
+ # validation multi_gem_testing
76
+ if steps.include?(:multi_gem_testing)
77
+ raise '"multi_gem_testing" requires arguments to run version_file and build_gem.' if args.nil? || args[:version_file].nil? || args[:build_gem].nil?
78
+ puts "Command '#{args}'"
79
+ end
80
+ # validation multi_gem_file_update
81
+ if steps.include?(:multigem_file_update)
82
+ raise 'multigem_file_update requires arguments gem_to_test, gemfury_username to run.' if args[:gem_name].nil? || args[:gemfury_username].nil?
83
+ puts "Command '#{args}'"
84
+ end
85
+ # validation gem_file_update
86
+ if steps.include?(:gem_file_update)
87
+ raise '"gem_file_update" requires arguments (gem_to_test) to run.' if args[:gem_to_test].nil?
88
+ puts "Command '#{args}'"
89
+ end
90
+ # validation run_tests_locally
91
+ if steps.include?(:run_tests_locally)
92
+ puts "Command '#{args}'"
93
+ end
94
+ # validation fetch_test_results_locally
95
+ if steps.include?(:fetch_test_results_locally)
96
+ puts "Command '#{args}'"
97
+ end
98
+ # validation run_tests_jenkins
99
+ if steps.include?(:run_tests_jenkins)
100
+ raise 'run_tests_jenkins requires arguments (jenkins_server_url, github_branch) to run.' if args[:github_branch].nil? || args[:jenkins_server_url].nil?
101
+ puts "Command '#{args}'"
102
+ end
103
+ # validation test_results_jenkins
104
+ if steps.include?(:test_results_jenkins)
105
+ raise 'test_results_jenkins requires argument jenkins_server_url to run.' if args[:jenkins_server_url].nil?
106
+ puts "Command '#{args}'"
107
+ end
108
+
109
+ unless steps.include?(:clone_gem) || steps.include?(:multi_gem_testing)
110
+ abort "No modules listed in #{Utils.configuration.managed_modules}" if module_names.nil?
63
111
  end
64
112
 
65
- abort "No modules listed in #{@managed_modules}" if module_names.nil?
66
- module_names.each do |module_name|
113
+ if steps.include?(:clone_gem) || steps.include?(:multi_gem_testing)
114
+ gem_args = args.clone
67
115
  Dir.chdir(main_path) unless Dir.pwd == main_path
68
- print "#{module_name}, "
69
- repo_name = "#{@namespace}/#{module_name}"
70
- output_path = "#{@pdksync_dir}/#{module_name}"
71
- if steps.include?(:clone)
72
- clean_env(output_path) if Dir.exist?(output_path)
73
- print 'delete module directory, '
74
- @git_repo = clone_directory(@namespace, module_name, output_path)
75
- print 'cloned, '
76
- puts "(WARNING) Unable to clone repo for #{module_name}".red if @git_repo.nil?
116
+ PdkSync::Logger.info "#{gem_args[:gem_name]}, "
117
+ output_path = File.join(Utils.configuration.pdksync_gem_dir, gem_args[:gem_name])
118
+ if steps.include?(:clone_gem)
119
+ Utils.clean_env(output_path) if Dir.exist?(output_path)
120
+ PdkSync::Logger.info 'delete gem directory, '
121
+ @git_repo = Utils.clone_directory(Utils.configuration.namespace, gem_args[:gem_name], output_path)
122
+ PdkSync::Logger.info 'cloned'
123
+ PdkSync::Logger.error "Unable to clone repo for #{gem_args[:gem_name]}".red if @git_repo.nil?
77
124
  Dir.chdir(main_path) unless Dir.pwd == main_path
78
- next if @git_repo.nil?
79
125
  end
80
- puts '(WARNING) @output_path does not exist, skipping module'.red unless File.directory?(output_path)
81
- next unless File.directory?(output_path)
82
- if steps.include?(:pdk_convert)
83
- exit_status = run_command(output_path, "#{return_pdk_path} convert --force --template-url https://github.com/puppetlabs/pdk-templates")
84
- print 'converted, '
85
- next unless exit_status.zero?
86
- end
87
- if steps.include?(:pdk_validate)
126
+ puts '(WARNING) @output_path does not exist, gem'.red unless File.directory?(output_path)
127
+ if steps.include?(:multi_gem_testing)
88
128
  Dir.chdir(main_path) unless Dir.pwd == main_path
89
- exit_status = run_command(output_path, "#{return_pdk_path} validate -a")
90
- print 'validated, '
91
- next unless exit_status.zero?
92
- end
93
- if steps.include?(:run_a_command)
129
+ PdkSync::Logger.info 'Multi Gem Testing, '
130
+ current_gem_version = Utils.check_gem_latest_version(gem_args[:gem_name])
131
+ PdkSync::Logger.info current_gem_version
132
+ new_gem_version = Utils.update_gem_latest_version_by_one(current_gem_version)
133
+ PdkSync::Logger.info new_gem_version
94
134
  Dir.chdir(main_path) unless Dir.pwd == main_path
95
- print 'run command, '
96
- exit_status = run_command(output_path, args)
97
- next unless exit_status.zero?
98
- end
99
- if steps.include?(:pdk_update)
135
+ exit_status = Utils.run_command(output_path, "sed s/#{current_gem_version}/#{new_gem_version}/g #{gem_args[:version_file]} >> test.yml", nil)
136
+ PdkSync::Logger.info 'Updated the version'
100
137
  Dir.chdir(main_path) unless Dir.pwd == main_path
101
- next unless pdk_update(output_path).zero?
102
- if steps.include?(:use_pdk_ref)
103
- ref = return_template_ref
104
- args = { branch_name: "pdksync_#{ref}",
105
- commit_message: "pdksync_#{ref}",
106
- pr_title: "pdksync_#{ref}" }
107
- end
108
- print 'pdk update, '
109
- end
110
- if steps.include?(:create_commit)
138
+ exit_status = Utils.run_command(output_path, "cp test.yml #{gem_args[:version_file]}", nil)
111
139
  Dir.chdir(main_path) unless Dir.pwd == main_path
112
- git_instance = Git.open(output_path)
113
- create_commit(git_instance, args[:branch_name], args[:commit_message])
114
- print 'commit created, '
115
- end
116
- if steps.include?(:push_and_create_pr)
140
+ exit_status = Utils.run_command(output_path, 'rm -rf test.yml', nil)
141
+ PdkSync::Logger.info 'bundle install'
117
142
  Dir.chdir(main_path) unless Dir.pwd == main_path
118
- git_instance = Git.open(output_path)
119
- push_staged_files(git_instance, git_instance.current_branch, repo_name)
120
- print 'push, '
121
- pdk_version = return_pdk_version("#{output_path}/metadata.json")
122
- pr = create_pr(client, repo_name, git_instance.current_branch, pdk_version, args[:pr_title])
123
- pr_list.push(pr.html_url)
124
- print 'created pr, '
125
- end
126
- if steps.include?(:clean_branches)
143
+ exit_status = Utils.run_command(output_path, 'bundle install', nil)
144
+ PdkSync::Logger.info 'building gem'
145
+ Dir.chdir(main_path) unless Dir.pwd == main_path
146
+ exit_status = Utils.run_command(output_path, "bundle exec #{gem_args[:build_gem]}", nil)
147
+ PdkSync::Logger.info 'uploading packages'
127
148
  Dir.chdir(main_path) unless Dir.pwd == main_path
128
- delete_branch(client, repo_name, args[:branch_name])
129
- print 'branch deleted, '
149
+ Dir.chdir("#{output_path}/#{gem_args[:gem_path]}") unless Dir.pwd == output_path
150
+ gemfury_token = Utils.configuration.gemfury_access_settings
151
+ Dir.glob('*.gem') do |filename|
152
+ PdkSync::Logger.info filename
153
+ Dir.chdir(main_path) unless Dir.pwd == main_path
154
+ exit_status = Utils.run_command("#{output_path}/#{gem_args[:gem_path]}", "curl -F package=@#{filename} https://" + gemfury_token + "@push.fury.io/#{gem_args[:gemfury_username]}/", nil)
155
+ end
130
156
  end
131
- puts 'done.'.green
132
- end
133
- return if pr_list.size.zero?
134
- puts "\nPRs created:\n".blue
135
- pr_list.each do |pr|
136
- puts pr
137
- end
138
- end
139
-
140
- # @summary
141
- # This method when called will create a directory identified by the set global variable '@pdksync_dir', on the condition that it does not already exist.
142
- def self.create_filespace
143
- FileUtils.mkdir @pdksync_dir unless Dir.exist?(@pdksync_dir)
144
- end
157
+ else
158
+ module_names.each do |module_name|
159
+ module_args = args.clone
160
+ Dir.chdir(main_path) unless Dir.pwd == main_path
161
+ PdkSync::Logger.info "#{module_name}, "
162
+ repo_name = File.join(Utils.configuration.namespace, module_name)
163
+ output_path = File.join(Utils.configuration.pdksync_dir, module_name)
164
+ if steps.include?(:clone)
165
+ Utils.validate_modules_exist(client, module_names)
166
+ Utils.clean_env(output_path) if Dir.exist?(output_path)
167
+ PdkSync::Logger.info 'delete module directory'
168
+ @git_repo = Utils.clone_directory(Utils.configuration.namespace, module_name, output_path)
169
+ PdkSync::Logger.info 'cloned'
170
+ PdkSync::Logger.error "Unable to clone repo for #{module_name}" if @git_repo.nil?
171
+ Dir.chdir(main_path) unless Dir.pwd == main_path
172
+ next if @git_repo.nil?
173
+ end
174
+ PdkSync::Logger.warn "#{output_path} does not exist, skipping module" unless File.directory?(output_path)
175
+ next unless File.directory?(output_path)
176
+ if steps.include?(:pdk_convert)
177
+ exit_status = Utils.run_command(output_path, "#{Utils.return_pdk_path} convert --force #{configuration.templates}", nil)
178
+ PdkSync::Logger.info 'converted'
179
+ next unless exit_status.zero?
180
+ end
181
+ if steps.include?(:pdk_validate)
182
+ Dir.chdir(main_path) unless Dir.pwd == main_path
183
+ exit_status = Utils.run_command(output_path, "#{Utils.return_pdk_path} validate -a", nil)
184
+ PdkSync::Logger.info 'validated' if exit_status.zero?
185
+ next unless exit_status.zero?
186
+ end
187
+ if steps.include?(:run_a_command)
188
+ Dir.chdir(main_path) unless Dir.pwd == main_path
189
+ PdkSync::Logger.info 'run command'
190
+ if module_args[:option].nil?
191
+ pid = Utils.run_command(output_path, module_args[:command], module_args[:option])
192
+ next unless pid != 0 # rubocop:disable Metrics/BlockNesting
193
+ else
194
+ exit_status = Utils.run_command(output_path, module_args[:command], nil)
195
+ next unless exit_status.zero? # rubocop:disable Metrics/BlockNesting
196
+ end
197
+ end
198
+ if steps.include?(:gem_file_update)
199
+ Dir.chdir(main_path) unless Dir.pwd == main_path
200
+ print 'gem file update, '
201
+ Utils.gem_file_update(output_path, module_args[:gem_to_test], module_args[:gem_line], module_args[:gem_sha_finder], module_args[:gem_sha_replacer], module_args[:gem_version_finder], module_args[:gem_version_replacer], module_args[:gem_branch_finder], module_args[:gem_branch_replacer], main_path) # rubocop:disable Metrics/LineLength
202
+ print 'gem file updated, '
203
+ end
204
+ if steps.include?(:run_tests_locally)
205
+ Dir.chdir(main_path) unless Dir.pwd == main_path
206
+ PdkSync::Logger.info 'Run tests '
207
+ module_type = Utils.module_type(output_path, module_name)
208
+ Utils.run_tests_locally(output_path, module_type, module_args[:provision_type], module_name, module_args[:puppet_collection])
209
+ end
210
+ if steps.include?(:fetch_test_results_locally)
211
+ Dir.chdir(main_path) unless Dir.pwd == main_path
212
+ PdkSync::Logger.info 'Fetch test results for local run '
213
+ module_type = Utils.module_type(output_path, module_name)
214
+ table = Utils.fetch_test_results_locally(output_path, module_type, module_name, report_rows)
215
+ end
216
+ if steps.include?(:pdk_update)
217
+ Dir.chdir(main_path) unless Dir.pwd == main_path
218
+ next unless Utils.pdk_update(output_path).zero?
219
+ if steps.include?(:use_pdk_ref)
220
+ ref = Utils.return_template_ref(File.join(output_path, 'metadata.json'))
221
+ pr_title = module_args[:additional_title] ? "#{module_args[:additional_title]} - pdksync_#{ref}" : "pdksync_#{ref}" # rubocop:disable Metrics/BlockNesting
222
+ module_args = module_args.merge(branch_name: "pdksync_#{ref}",
223
+ commit_message: pr_title,
224
+ pr_title: pr_title,
225
+ pdksync_label: Utils.configuration.default_pdksync_label)
226
+ end
227
+ PdkSync::Logger.info 'pdk update'
228
+ end
229
+ if steps.include?(:use_gem_ref)
230
+ pr_title = module_args[:additional_title] ? "#{module_args[:additional_title]} - pdksync_gem_testing" : 'pdksync_gem_testing'
231
+ module_args = module_args.merge(branch_name: "gem_testing_#{module_args[:gem_to_test]}",
232
+ commit_message: pr_title,
233
+ pr_title: pr_title,
234
+ pdksync_label: Utils.configuration.default_pdksync_label)
235
+ end
236
+ if steps.include?(:create_commit)
237
+ Dir.chdir(main_path) unless Dir.pwd == main_path
238
+ git_instance = Git.open(output_path)
239
+ Utils.create_commit(git_instance, module_args[:branch_name], module_args[:commit_message])
240
+ PdkSync::Logger.info 'commit created'
241
+ end
242
+ if steps.include?(:push)
243
+ Dir.chdir(main_path) unless Dir.pwd == main_path
244
+ git_instance = Git.open(output_path)
245
+ if git_instance.diff(git_instance.current_branch, "#{Utils.configuration.push_file_destination}/#{Utils.configuration.create_pr_against}").size != 0 # Git::Diff doesn't have empty? # rubocop:disable Style/ZeroLengthPredicate
246
+ PdkSync::Logger.info 'push'
247
+ Utils.push_staged_files(git_instance, git_instance.current_branch, repo_name)
248
+ else
249
+ PdkSync::Logger.info 'skipped push'
250
+ end
251
+ end
252
+ if steps.include?(:create_pr)
253
+ Dir.chdir(main_path) unless Dir.pwd == main_path
254
+ git_instance = Git.open(output_path)
255
+ if git_instance.diff(git_instance.current_branch, "#{Utils.configuration.push_file_destination}/#{Utils.configuration.create_pr_against}").size != 0 # Git::Diff doesn't have empty? # rubocop:disable Style/ZeroLengthPredicate
256
+ pdk_version = Utils.return_pdk_version("#{output_path}/metadata.json")
145
257
 
146
- # @summary
147
- # This method when called will create and return an octokit client with access to the upstream git repositories.
148
- # @return [Octokit::Client] client
149
- # The octokit client that has been created.
150
- def self.setup_client
151
- client = Octokit::Client.new(access_token: @access_token.to_s)
152
- client.user.login
153
- client
154
- rescue ArgumentError, Octokit::Unauthorized
155
- raise "Access Token not set up correctly - Use export 'GITHUB_TOKEN=<put your token here>' to set it."
156
- end
258
+ # If a label is supplied, verify that it is available in the repo
259
+ label = module_args[:pdksync_label] ? module_args[:pdksync_label] : module_args[:label] # rubocop:disable Metrics/BlockNesting
260
+ label_valid = (label.is_a?(String) && !label.to_str.empty?) ? Utils.check_for_label(client, repo_name, label) : nil # rubocop:disable Metrics/BlockNesting
157
261
 
158
- # @summary
159
- # This method when called will access a file set by the global variable '@managed_modules' and retrieve the information within as an array.
160
- # @return [Array]
161
- # An array of different module names.
162
- def self.return_modules
163
- YAML.safe_load(File.open(@managed_modules))
164
- end
262
+ # Exit current iteration if an error occured retrieving a label
263
+ if label_valid == false # rubocop:disable Metrics/BlockNesting
264
+ raise 'Ensure label is valid'
265
+ end
165
266
 
166
- # @summary
167
- # Try to use a fully installed pdk, otherwise fall back to the bundled pdk gem.
168
- # @return String
169
- # Path to the pdk executable
170
- def self.return_pdk_path
171
- full_path = '/opt/puppetlabs/pdk/bin/pdk'
172
- path = if File.executable?(full_path)
173
- full_path
174
- else
175
- puts "(WARNING) Using pdk on PATH not '#{full_path}'".red
176
- 'pdk'
177
- end
178
- path
179
- end
267
+ # Create the PR and add link to pr list
268
+ pr = Utils.create_pr(client, repo_name, git_instance.current_branch, pdk_version, module_args[:pr_title])
269
+ break if pr.nil? # rubocop:disable Metrics/BlockNesting
180
270
 
181
- def self.create_commit(git_repo, branch_name, commit_message)
182
- checkout_branch(git_repo, branch_name)
183
- add_staged_files(git_repo)
184
- commit_staged_files(git_repo, branch_name, commit_message)
185
- end
271
+ pr_list.push(pr.html_url)
272
+ PdkSync::Logger.info 'created pr'
186
273
 
187
- # @summary
188
- # This method when called will call the delete function against the given repository if it exists.
189
- # @param [String] output_path
190
- # The repository that is to be deleted.
191
- def self.clean_env(output_path)
192
- # If a local copy already exists it is removed
193
- FileUtils.rm_rf(output_path)
194
- end
274
+ # If a valid label is supplied, add this to the PR
275
+ if label_valid == true # rubocop:disable Metrics/BlockNesting
276
+ Utils.add_label(client, repo_name, pr.number, label)
277
+ PdkSync::Logger.info "added label '#{label}' "
278
+ end
279
+ else
280
+ PdkSync::Logger.info 'skipped pr'
281
+ end
282
+ end
283
+ if steps.include?(:clean_branches)
284
+ Dir.chdir(main_path) unless Dir.pwd == main_path
285
+ Utils.delete_branch(client, repo_name, module_args[:branch_name])
286
+ PdkSync::Logger.info 'branch deleted'
287
+ end
288
+ if steps.include?(:run_tests_jenkins)
289
+ jenkins_client = Utils.setup_jenkins_client(module_args[:jenkins_server_url])
290
+ Dir.chdir(main_path) unless Dir.pwd == main_path
291
+ PdkSync::Logger.info 'Run tests in jenkins '
292
+ module_type = Utils.module_type(output_path, module_name)
293
+ if module_type == 'traditional'
294
+ github_user = 'puppetlabs' if module_args[:test_framework].nil? # rubocop:disable Metrics/BlockNesting
295
+ github_user = module_args[:github_user] unless module_args[:github_user].nil? # rubocop:disable Metrics/BlockNesting
296
+ if module_args[:test_framework] == 'jenkins' || module_args[:test_framework].nil? # rubocop:disable Metrics/BlockNesting
297
+ module_name = "puppetlabs-#{module_name}" if %w[cisco_ios device_manager].include?(module_name) # rubocop:disable Metrics/BlockNesting
298
+ job_name = "forge-module_#{module_name}_init-manual-parameters_adhoc"
299
+ job_name = "forge-windows_#{module_name}_init-manual-parameters_adhoc" if ['puppetlabs-reboot', 'puppetlabs-iis', 'puppetlabs-powershell', 'sqlserver'].include?(module_name) # rubocop:disable Metrics/BlockNesting, Metrics/LineLength
300
+ build_id = Utils.run_tests_jenkins(jenkins_client, module_name, module_args[:github_branch], github_user, job_name)
301
+ next if build_id.nil? # rubocop:disable Metrics/BlockNesting
302
+ PdkSync::Logger.info "New adhoc TEST EXECUTION has started. \nYou can check progress here: #{configuration['jenkins_server_url']}/job/#{job_name}/#{build_id}"
303
+ Utils.test_results_jenkins(module_args[:jenkins_server_url], build_id, job_name, module_name)
304
+ end
305
+ end
306
+ if module_type == 'litmus'
307
+ PdkSync::Logger.info '(Error) Module Type is Litmus please use the rake task run_tests_locally to run'.red
308
+ end
309
+ end
310
+ if steps.include?(:test_results_jenkins)
311
+ Dir.chdir(main_path) unless Dir.pwd == main_path
312
+ PdkSync::Logger.info 'Fetch test results from jenkins, '
313
+ module_type = Utils.module_type(output_path, module_name)
314
+ if module_type == 'litmus'
315
+ PdkSync::Logger.info '(Error) Module Type is Litmus please use the rake task run_tests_locally to run'.red
316
+ next
317
+ end
195
318
 
196
- # @summary
197
- # This method when called will clone a given repository into a local location that has also been set.
198
- # @param [String] namespace
199
- # The namespace the repository is located in.
200
- # @param [String] module_name
201
- # The name of the repository.
202
- # @param [String] output_path
203
- # The location the repository is to be cloned to.
204
- # @return [Git::Base]
205
- # A git object representing the local repository.
206
- def self.clone_directory(namespace, module_name, output_path)
207
- Git.clone("https://github.com/#{namespace}/#{module_name}.git", output_path.to_s) # is returned
208
- rescue Git::GitExecuteError => error
209
- puts "(FAILURE) Cloning #{module_name} has failed. #{error}".red
210
- end
319
+ module_name = "puppetlabs-#{module_name}" if %w[cisco_ios device_manager].include?(module_name)
320
+ File.open("results_#{module_name}.out", 'r') do |f|
321
+ f.each_line do |line|
322
+ if line.include?('BUILD_ID')
323
+ build_id = line.split('=')[1].strip
324
+ elsif line.include?('MODULE_NAME')
325
+ module_name = line.split('=')[1].strip
326
+ end
327
+ end
211
328
 
212
- # @summary
213
- # This method when called will run a command command at the given location, with an error message being thrown if it is not successful.
214
- # @param [String] output_path
215
- # The location that the command is to be run from.
216
- # @param [String] command
217
- # The command to be run.
218
- # @return [Integer]
219
- # The status code of the command run.
220
- def self.run_command(output_path, command)
221
- Dir.chdir(output_path) unless Dir.pwd == output_path
222
- stdout, stderr, status = Open3.capture3(command)
223
- puts "\n#{stdout}\n".yellow
224
- puts "(FAILURE) Unable to run command '#{command}': #{stderr}".red unless status.exitstatus.zero?
225
- status.exitstatus
226
- end
329
+ job_name = "forge-module_#{module_name}_init-manual-parameters_adhoc" if module_args[:job_name].nil?
330
+ job_name = "forge-windows_#{module_name}_init-manual-parameters_adhoc" if ['puppetlabs-reboot', 'puppetlabs-iis', 'puppetlabs-powershell', 'sqlserver'].include?(module_name)
331
+ Utils.test_results_jenkins(module_args[:jenkins_server_url], build_id, job_name, module_name)
332
+ end
333
+ end
334
+ if steps.include?(:multigem_file_update)
335
+ Dir.chdir(main_path) unless Dir.pwd == main_path
336
+ gemfury_readonly_token = Utils.configuration.gemfury_access_settings
337
+ Utils.update_gemfile_multigem(output_path, module_args[:gem_name], gemfury_readonly_token, module_args[:gemfury_username])
338
+ PdkSync::Logger.info 'Updated with multigem, '
339
+ end
227
340
 
228
- # @summary
229
- # This method when called will run the 'pdk update --force' command at the given location, with an error message being thrown if it is not successful.
230
- # @param [String] output_path
231
- # The location that the command is to be run from.
232
- # @return [Integer]
233
- # The status code of the pdk update run.
234
- def self.pdk_update(output_path)
235
- # Runs the pdk update command
236
- Dir.chdir(output_path) unless Dir.pwd == output_path
237
- _stdout, stderr, status = Open3.capture3("#{return_pdk_path} update --force")
238
- puts "(FAILURE) Unable to run `pdk update`: #{stderr}".red unless status.exitstatus.zero?
239
- status.exitstatus
240
- end
341
+ if steps.include?(:add_provision_list)
342
+ result = Utils.add_provision_list(output_path, module_args[:key], module_args[:provisioner], [module_args[:images], module_args.extras].flatten)
343
+ raise "#{output_path}/provision.yaml does not exist" unless result
344
+ end
241
345
 
242
- # @summary
243
- # This method when called will retrieve the template ref of the current module, i.e. the one that was navigated into in the 'pdk_update' method.
244
- # @param [String] metadata_file
245
- # An optional input that can be used to set the location of the metadata file.
246
- # @return [String]
247
- # A string value that represents the current pdk template.
248
- def self.return_template_ref(metadata_file = 'metadata.json')
249
- file = File.read(metadata_file)
250
- data_hash = JSON.parse(file)
251
- data_hash['template-ref']
252
- end
346
+ if steps.include?(:generate_vmpooler_release_checks)
347
+ Utils.generate_vmpooler_release_checks(output_path, module_args[:puppet_version].to_i)
348
+ end
253
349
 
254
- # @summary
255
- # This method when called will checkout a new local branch of the given repository.
256
- # @param [Git::Base] git_repo
257
- # A git object representing the local repository to be branched.
258
- # @param [String] branch_suffix
259
- # The string that is appended on the branch name. eg template_ref or a friendly name
260
- def self.checkout_branch(git_repo, branch_suffix)
261
- git_repo.branch("pdksync_#{branch_suffix}").checkout
262
- end
350
+ if steps.include?(:update_os_support)
351
+ Utils.update_os_support(output_path)
352
+ end
263
353
 
264
- # @summary
265
- # This method when called will retrieve the pdk_version of the current module, i.e. the one that was navigated into in the 'pdk_update' method.
266
- # @param [String] metadata_file
267
- # An optional input that can be used to set the location of the metadata file.
268
- # @return [String]
269
- # A string value that represents the current pdk version.
270
- def self.return_pdk_version(metadata_file = 'metadata.json')
271
- file = File.read(metadata_file)
272
- data_hash = JSON.parse(file)
273
- data_hash['pdk-version']
274
- end
354
+ if steps.include?(:remove_platform_from_metadata)
355
+ Utils.remove_platform_from_metadata(output_path, module_args[:os], module_args[:version])
356
+ end
275
357
 
276
- # @summary
277
- # This method when called will stage all changed files within the given repository, conditional on them being managed via the pdk.
278
- # @param [Git::Base] git_repo
279
- # A git object representing the local repository to be staged.
280
- def self.add_staged_files(git_repo)
281
- if git_repo.status.changed != {}
282
- git_repo.add(all: true)
283
- puts 'All files have been staged.'
284
- else
285
- puts 'Nothing to commit.'
286
- end
287
- end
358
+ if steps.include?(:add_platform_to_metadata)
359
+ Utils.add_platform_to_metadata(output_path, module_args[:os], module_args[:version])
360
+ end
288
361
 
289
- # @summary
290
- # This method when called will create a commit containing all currently staged files, with the name of the commit containing the template ref as a unique identifier.
291
- # @param [Git::Base] git_repo
292
- # A git object representing the local repository against which the commit is to be made.
293
- # @param [String] template_ref
294
- # The unique template_ref that is used as part of the commit name.
295
- # @param [String] commit_message
296
- # If sepecified it will be the message for the commit.
297
- def self.commit_staged_files(git_repo, template_ref, commit_message = nil)
298
- message = if commit_message.nil?
299
- "pdksync_#{template_ref}"
300
- else
301
- commit_message
302
- end
303
- git_repo.commit(message)
304
- end
362
+ if steps.include?(:update_requirements)
363
+ Utils.update_requirements(output_path, module_args[:name], module_args[:key], module_args[:value])
364
+ end
305
365
 
306
- # @summary
307
- # This method when called will push the given local commit to local repository's origin.
308
- # @param [Git::Base] git_repo
309
- # A git object representing the local repository againt which the push is to be made.
310
- # @param [String] template_ref
311
- # The unique reference that that represents the template the update has ran against.
312
- # @param [String] repo_name
313
- # The name of the repository on which the commit is to be made.
314
- def self.push_staged_files(git_repo, current_branch, repo_name)
315
- git_repo.push(@push_file_destination, current_branch)
316
- rescue StandardError => error
317
- puts "(FAILURE) Pushing to #{@push_file_destination} for #{repo_name} has failed. #{error}".red
318
- end
366
+ if steps.include?(:normalize_metadata_supported_platforms)
367
+ Utils.normalize_metadata_supported_platforms(output_path)
368
+ end
319
369
 
320
- # @summary
321
- # This method when called will create a pr on the given repository that will create a pr to merge the given commit into the master with the pdk version as an identifier.
322
- # @param [Octokit::Client] client
323
- # The octokit client used to gain access to and manipulate the repository.
324
- # @param [String] repo_name
325
- # The name of the repository on which the commit is to be made.
326
- # @param [String] template_ref
327
- # The unique reference that that represents the template the update has ran against.
328
- # @param [String] pdk_version
329
- # The current version of the pdk on which the update is run.
330
- def self.create_pr(client, repo_name, template_ref, pdk_version, pr_title = nil)
331
- if pr_title.nil?
332
- title = "pdksync - Update using #{pdk_version}"
333
- message = "pdk version: `#{pdk_version}` \n pdk template ref: `#{template_ref}`"
334
- head = "pdksync_#{template_ref}"
335
- else
336
- title = "pdksync - #{pr_title}"
337
- message = "#{pr_title}\npdk version: `#{pdk_version}` \n"
338
- head = template_ref
370
+ PdkSync::Logger.info 'done'
371
+ end
372
+ table = Terminal::Table.new title: 'Module Test Results', headings: %w[Module Status Result From], rows: report_rows
373
+ puts table if steps.include?(:fetch_test_results_locally)
374
+ return if pr_list.size.zero?
375
+ PdkSync::Logger.info "\nPRs created:\n"
376
+ puts pr_list.join("\n")
339
377
  end
340
- pr = client.create_pull_request(repo_name, @create_pr_against,
341
- head,
342
- title,
343
- message)
344
- pr
345
- rescue StandardError => error
346
- puts "(FAILURE) PR creation for #{repo_name} has failed. #{error}".red
347
- end
348
-
349
- # @summary
350
- # This method when called will delete any preexisting branch on the given repository that matches the given name.
351
- # @param [Octokit::Client] client
352
- # The octokit client used to gain access to and manipulate the repository.
353
- # @param [String] repo_name
354
- # The name of the repository from which the branch is to be deleted.
355
- # @param [String] branch_name
356
- # The name of the branch that is to be deleted.
357
- def self.delete_branch(client, repo_name, branch_name)
358
- client.delete_branch(repo_name, branch_name)
359
- rescue StandardError => error
360
- puts "(FAILURE) Deleting #{branch_name} in #{repo_name} failed. #{error}".red
361
378
  end
362
379
  end