Class: PRWithParams::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/pr_with_params/cli.rb

Instance Method Summary collapse

Instance Method Details

#openObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/pr_with_params/cli.rb', line 23

def open
  home_dir_path = `echo $HOME`.chomp
  config_file_path = "#{home_dir_path}/#{options[:config_path].delete_prefix("/")}"
  config_options = (options[:config_path].empty? ? {} : PRWithParams::ConfigParser.new(config_file_path: config_file_path, scope: options[:scope]).parse!).with_indifferent_access
  options[:validators].delete(:conventional_commits) if options[:ignore_conventional_commits]
  all_options = config_options.merge(options)

  branch_name = `git rev-parse --abbrev-ref HEAD`.chomp
  base_branch = all_options.delete(:base_branch) || `git remote show origin | grep "HEAD branch" | sed 's/.*: //'`.chomp

  default_title = `git show-branch --no-name $(git log #{base_branch}..#{branch_name} --pretty=format:"%h" | tail -1)`.chomp
  all_options[:title] ||= default_title

  PRWithParams::OptionsValidator.validate!(all_options.except(:validators), validators: all_options[:validators])

  remote_git_uri = `git config --get remote.origin.url`.sub('[email protected]:', '').sub('.git', '').chomp
  uri_path = "/#{remote_git_uri}/compare/#{base_branch}...#{branch_name}"

  push_local_to_remote(branch_name, base_branch, remote_git_uri)
  open_pr_in_browser(uri_path, all_options)
rescue StandardError => e
  message = "\e[31mERROR\e[0m: An error occurred while building or opening your custom pull request URL"
  reason = "reason: #{e.message}"
  backtrace = "backtrace: #{e.backtrace&.last(10)&.join("\n")}"
  error_message = [message, reason, backtrace, "\n\n"].join("\n")

  warn error_message
  exit 1
end