From 3e7685e7ff9ea5b334b9ffa00b99ef4f0532a315 Mon Sep 17 00:00:00 2001 From: Peter Bittner Date: Tue, 11 Aug 2020 09:36:26 +0200 Subject: [PATCH] Reduce visual complexity by early exit instead of indentation Align implementation for GitHub PRs and GitLab MRs --- lib/modulesync/pr/github.rb | 35 ++++++++++++++++------------- lib/modulesync/pr/gitlab.rb | 44 +++++++++++++++++++------------------ 2 files changed, 43 insertions(+), 36 deletions(-) diff --git a/lib/modulesync/pr/github.rb b/lib/modulesync/pr/github.rb index 2791c060..2fd64041 100644 --- a/lib/modulesync/pr/github.rb +++ b/lib/modulesync/pr/github.rb @@ -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]) diff --git a/lib/modulesync/pr/gitlab.rb b/lib/modulesync/pr/gitlab.rb index 4a7b067c..597c9ff6 100644 --- a/lib/modulesync/pr/gitlab.rb +++ b/lib/modulesync/pr/gitlab.rb @@ -15,7 +15,6 @@ 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' @@ -23,27 +22,30 @@ def manage(namespace, module_name, options) $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