Skip to content

Commit

Permalink
Reduce visual complexity by early exit instead of indentation
Browse files Browse the repository at this point in the history
Align implementation for GitHub PRs and GitLab MRs
  • Loading branch information
bittner committed Aug 11, 2020
1 parent d441ca4 commit ebc888f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 44 deletions.
44 changes: 21 additions & 23 deletions lib/modulesync/pr/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,34 @@ def manage(namespace, module_name, options)
$stdout.puts \
"Using no-op. Would submit PR '#{options[:pr_title]}' to #{repo_path} " \
"- merges #{options[:branch]} into #{target_branch}"
else
pull_requests = @api.pull_requests(repo_path, :state => 'open', :base => target_branch, :head => head)
if pull_requests.empty?
pr = @api.create_pull_request(repo_path,
target_branch,
options[:branch],
options[:pr_title],
options[:message])
$stdout.puts \
"Submitted PR '#{options[:pr_title]}' to #{repo_path} "\
"- merges #{options[:branch]} into #{target_branch}"
else
# Skip creating the PR if it exists already.
$stdout.puts "Skipped! #{pull_requests.length} PRs found for branch #{options[:branch]}"
end
return
end

pull_requests = @api.pull_requests(repo_path,
:state => 'open',
:base => target_branch,
:head => head)
unless pull_requests.empty?
# Skip creating the PR if it exists already.
$stdout.puts "Skipped! #{pull_requests.length} PRs found for branch #{options[:branch]}"
return
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 = @api.create_pull_request(repo_path,
target_branch,
options[:branch],
options[:pr_title],
options[:message])
$stdout.puts \
"Submitted PR '#{options[:pr_title]}' to #{repo_path} " \
"- merges #{options[:branch]} into #{target_branch}"

# 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?
if options[:noop]
puts "Using no-op. Would attach the following labels to the PR: #{pr_labels.join(', ')}"
else
$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
$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
Expand Down
45 changes: 24 additions & 21 deletions lib/modulesync/pr/gitlab.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,38 @@ def initialize(token, endpoint)

def manage(namespace, module_name, options)
repo_path = File.join(namespace, module_name)

head = "#{namespace}:#{options[:branch]}"
target_branch = options[:pr_target_branch] || 'master'

if options[:noop]
$stdout.puts \
"Using no-op. Would submit MR '#{options[:pr_title]}' to #{repo_path} " \
"- merges #{options[:branch]} into #{target_branch}"
else
merge_requests = @api.merge_requests(repo_path,
:state => 'opened',
:source_branch => head,
:target_branch => target_branch)
if merge_requests.empty?
mr_labels = ModuleSync::Util.parse_list(options[:pr_labels])
mr = @api.create_merge_request(repo_path, options[:pr_title],
:source_branch => options[:branch],
:target_branch => target_branch,
:labels => mr_labels)
$stdout.puts \
"Submitted MR '#{options[:pr_title]}' to #{repo_path} " \
"- merges #{options[:branch]} into #{target_branch}"
return if mr_labels.empty?
$stdout.puts "Attached the following labels to MR #{mr.iid}: #{mr_labels.join(', ')}"
else
# Skip creating the MR if it exists already.
$stdout.puts "Skipped! #{merge_requests.length} MRs found for branch #{options[:branch]}"
end
return
end

merge_requests = @api.merge_requests(repo_path,
:state => 'opened',
:source_branch => head,
:target_branch => target_branch)
unless merge_requests.empty?
# Skip creating the MR if it exists already.
$stdout.puts "Skipped! #{merge_requests.length} MRs found for branch #{options[:branch]}"
return
end

mr_labels = ModuleSync::Util.parse_list(options[:pr_labels])
mr = @api.create_merge_request(repo_path,
options[:pr_title],
:source_branch => options[:branch],
:target_branch => target_branch,
:labels => mr_labels)
$stdout.puts \
"Submitted MR '#{options[:pr_title]}' to #{repo_path} " \
"- merges #{options[:branch]} into #{target_branch}"

return if mr_labels.empty?
$stdout.puts "Attached the following labels to MR #{mr.iid}: #{mr_labels.join(', ')}"
end
end
end
Expand Down

0 comments on commit ebc888f

Please sign in to comment.