-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleanup: move config validation out of processing
This commit moves configuration validation (--pr cannot be used without --branch) into initialization. This catches the error early on, before any actual work is done. Signed-off-by: Manuel Hutter <manuel@vshn.ch>
- Loading branch information
Showing
3 changed files
with
76 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,41 @@ | ||
require 'octokit' | ||
require 'modulesync/util' | ||
|
||
module ModuleSync::PR | ||
class GitHub | ||
def initialize(token, endpoint) | ||
Octokit.configure do |c| | ||
c.api_endpoint = endpoint | ||
module ModuleSync | ||
module PR | ||
# GitHub creates and manages pull requests on github.com or GitHub | ||
# Enterprise installations. | ||
class GitHub | ||
def initialize(token, endpoint) | ||
Octokit.configure do |c| | ||
c.api_endpoint = endpoint | ||
end | ||
@api = Octokit::Client.new(:access_token => token) | ||
end | ||
@api = Octokit::Client.new(:access_token => token) | ||
end | ||
|
||
def manage(namespace, module_name, options) | ||
repo_path = File.join(namespace, module_name) | ||
head = "#{namespace}:#{options[:branch]}" | ||
def manage(namespace, module_name, options) | ||
repo_path = File.join(namespace, module_name) | ||
head = "#{namespace}:#{options[:branch]}" | ||
|
||
pull_requests = @api.pull_requests(repo_path, :state => 'open', :base => 'master', :head => head) | ||
if pull_requests.empty? | ||
pr = @api.create_pull_request(repo_path, 'master', options[:branch], options[:pr_title], options[:message]) | ||
$stdout.puts "Submitted PR '#{options[:pr_title]}' to #{repo_path} - merges #{options[:branch]} into master" | ||
else | ||
# Skip creating the PR if it exists already. | ||
$stdout.puts "Skipped! #{pull_requests.length} PRs found for branch #{options[:branch]}" | ||
end | ||
pull_requests = @api.pull_requests(repo_path, :state => 'open', :base => 'master', :head => head) | ||
if pull_requests.empty? | ||
pr = @api.create_pull_request(repo_path, 'master', options[:branch], options[:pr_title], options[:message]) | ||
$stdout.puts "Submitted PR '#{options[:pr_title]}' to #{repo_path} - merges #{options[:branch]} into master" | ||
else | ||
# Skip creating the PR if it exists already. | ||
$stdout.puts "Skipped! #{pull_requests.length} PRs found for branch #{options[:branch]}" | ||
end | ||
|
||
# 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 = ModuleSync::Util.parse_list(options[:pr_labels]) | ||
# 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 = ModuleSync::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. | ||
return if pr_labels.empty? | ||
$stdout.puts "Attaching the following labels to PR #{pr['number']}: #{pr_labels.join(', ')}" | ||
@api.add_labels_to_an_issue(repo_path, pr['number'], 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. | ||
return if pr_labels.empty? | ||
$stdout.puts "Attaching the following labels to PR #{pr['number']}: #{pr_labels.join(', ')}" | ||
@api.add_labels_to_an_issue(repo_path, pr['number'], pr_labels) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters