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

Class Method Details

.add_staged_files(git_repo) ⇒ Object

Parameters:

  • git_repo (Git::Base)

    A git object representing the local repository to be staged.



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

Parameters:

  • git_repo (Git::Base)

    A git object representing the local repository to be branched.

  • template_ref (String)

    The unique template_ref that is used as part of the branch name.



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_branchesObject



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

Parameters:

  • output_path (String)

    The repository that is to be deleted.



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.

Parameters:

  • namespace (String)

    The namespace the repository is located in.

  • module_name (String)

    The name of the repository.

  • output_path (String)

    The location the repository is to be cloned to.

Returns:

  • (Git::Base)

    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

Parameters:

  • git_repo (Git::Base)

    A git object representing the local repository against which the commit is to be made.

  • template_ref (String)

    The unique template_ref that is used as part of the commit name.



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_filespaceObject



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

Parameters:

  • client (Octokit::Client)

    The octokit client used to gain access to and manipulate the repository.

  • repo_name (String)

    The name of the repository on which the commit is to be made.

  • template_ref (String)

    The unique reference that that represents the template the update has ran against.

  • pdk_version (String)

    The current version of the pdk on which the update is run.



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

Parameters:

  • client (Octokit::Client)

    The octokit client used to gain access to and manipulate the repository.

  • repo_name (String)

    The name of the repository from which the branch is to be deleted.

  • branch_name (String)

    The name of the branch that is to be deleted.



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.

Parameters:

  • output_path (String)

    The location that the command is to be run from.

Returns:

  • (Integer)

    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.

Parameters:

  • output_path (String)

    The location that the command is to be run from.

Returns:

  • (Integer)

    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

Parameters:

  • git_repo (Git::Base)

    A git object representing the local repository againt which the push is to be made.

  • template_ref (String)

    The unique reference that that represents the template the update has ran against.

  • repo_name (String)

    The name of the repository on which the commit is to be made.



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.

Parameters:

  • client (Octokit::Client)

    The octokit client used to gain access to and manipulate the repository.

  • repo_name (String)

    The name of the repository from which the branches are to be retrieved.

Returns:

  • (Array)

    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_modulesArray

Returns An array of different module names.

Returns:

  • (Array)

    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.

Parameters:

  • metadata_file (String) (defaults to: 'metadata.json')

    An optional input that can be used to set the location of the metadata file.

Returns:

  • (String)

    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.

Parameters:

  • metadata_file (String) (defaults to: 'metadata.json')

    An optional input that can be used to set the location of the metadata file.

Returns:

  • (String)

    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_pdksyncObject



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_clientOctokit::Client

Returns client The octokit client that has been created.

Returns:

  • (Octokit::Client)

    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.
  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

Parameters:

  • module_name (String)

    The name of the module to be put through the process

  • client (Octokit::Client)

    The client used to access github.



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_autofixInteger

Returns The status code of the pdk validate run.

Parameters:

  • output_path (String)

    The location that the command is to be run from.

Returns:

  • (Integer)

    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