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 3e7685e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 36 deletions.
35 changes: 20 additions & 15 deletions lib/modulesync/pr/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,28 @@ 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 = @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}"

# 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])
Expand Down
44 changes: 23 additions & 21 deletions lib/modulesync/pr/gitlab.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,37 @@ 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 3e7685e

Please sign in to comment.