lib/modulesync.rb in modulesync-0.9.0 vs lib/modulesync.rb in modulesync-0.10.0

- old
+ new

@@ -1,16 +1,23 @@ require 'fileutils' +require 'octokit' require 'pathname' require 'modulesync/cli' require 'modulesync/constants' require 'modulesync/git' require 'modulesync/hook' require 'modulesync/renderer' require 'modulesync/settings' require 'modulesync/util' require 'monkey_patches' +GITHUB_TOKEN = ENV.fetch('GITHUB_TOKEN', '') + +Octokit.configure do |c| + c.api_endpoint = ENV.fetch('GITHUB_BASE_URL', 'https://api.github.com') +end + module ModuleSync include Constants def self.config_defaults { @@ -87,10 +94,15 @@ end end end def self.manage_module(puppet_module, module_files, module_options, defaults, options) + if options[:pr] && !GITHUB_TOKEN + STDERR.puts 'Environment variable GITHUB_TOKEN must be set to use --pr!' + raise unless options[:skip_broken] + end + puts "Syncing #{puppet_module}" namespace, module_name = module_name(puppet_module, options[:namespace]) unless options[:offline] git_base = options[:git_base] git_uri = "#{git_base}#{namespace}" @@ -112,10 +124,30 @@ files_to_manage.each { |filename| manage_file(filename, settings, options) } if options[:noop] Git.update_noop(module_name, options) elsif !options[:offline] - Git.update(module_name, files_to_manage, options) + # Git.update() returns a boolean: true if files were pushed, false if not. + pushed = Git.update(module_name, files_to_manage, options) + return nil unless pushed && options[:pr] + + # We only do GitHub PR work if the GITHUB_TOKEN variable is set in the environment. + repo_path = "#{namespace}/#{module_name}" + puts "Submitting PR '#{options[:pr_title]}' on GitHub to #{repo_path} - merges #{options[:branch]} into master" + github = Octokit::Client.new(:access_token => GITHUB_TOKEN) + pr = github.create_pull_request(repo_path, 'master', options[:branch], options[:pr_title], options[:message]) + puts "PR created at #{pr['html_url']}" + + # PR labels can either be a list in the YAML file or they can pass in a comma + # separated list via the command line argument. + pr_labels = Util.parse_list(options[:pr_labels]) + + # We only assign labels to the PR if we've discovered a list > 1. The labels MUST + # already exist. We DO NOT create missing labels. + unless pr_labels.empty? + puts "Attaching the following labels to PR #{pr['number']}: #{pr_labels.join(', ')}" + github.add_labels_to_an_issue(repo_path, pr['number'], pr_labels) + end end end def self.update(options) options = config_defaults.merge(options)