Class: RightBranch::Commands::ChangePullRequestTarget

Inherits:
Object
  • Object
show all
Defined in:
lib/right_branch/commands/change_pull_request_target.rb

Constant Summary collapse

REQUIRED_OPTIONS =
[:repository, :new_branch, :pull_request]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream = $stdout) ⇒ ChangePullRequestTarget

Returns a new instance of ChangePullRequestTarget.



16
17
18
# File 'lib/right_branch/commands/change_pull_request_target.rb', line 16

def initialize(stream = $stdout)
  @stream = stream
end

Instance Attribute Details

#streamObject

Returns the value of attribute stream.



10
11
12
# File 'lib/right_branch/commands/change_pull_request_target.rb', line 10

def stream
  @stream
end

Class Method Details

.run(stream = $stdout) ⇒ Object



12
13
14
# File 'lib/right_branch/commands/change_pull_request_target.rb', line 12

def self.run(stream = $stdout)
  new(stream).run!
end

Instance Method Details

#build_credentials(options) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/right_branch/commands/change_pull_request_target.rb', line 60

def build_credentials(options)
  if options[:access_token].to_s.empty?
    { login: options[:username], password: options[:password] }
  else
    { access_token: options[:access_token] }
  end
end

#build_opt_parser(options) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/right_branch/commands/change_pull_request_target.rb', line 84

def build_opt_parser(options)
  OptionParser.new do |opts|
    opts.banner = 'Usage: right_branch [options]'

    opts.on("-u", "--username USERNAME", "Username") do |v|
      options[:username] = v
    end

    opts.on("-r", "--repository REPOSITORY", "Repository") do |v|
      options[:repository] = v
    end

    opts.on("-b", "--new-branch NEW_BRANCH", "New branch") do |v|
      options[:new_branch] = v
    end

    opts.on("-p", "--pull-request PULL_REQUEST", "Pull request") do |v|
      options[:pull_request] = v
    end

    opts.on("-t", "--access-token ACCESS_TOKEN", "Access token") do |v|
      options[:access_token] = v
    end

    opts.on("--password PASSWORD", "Password") do |v|
      options[:password] = v
    end
  end
end

#build_optionsObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/right_branch/commands/change_pull_request_target.rb', line 37

def build_options
  options = {}
  opt_parser = build_opt_parser(options)
  opt_parser.parse!
  fallback_to_options_from_env(options)

  missing = missing_opt_keys(options)
  unless missing.empty?
    abort missing_key_abort_message(missing, opt_parser)
  end

  if prompt_for_password?(options)
    options[:password] = ask('Enter your password: ') { |q| q.echo = '*' }
  end

  options
end

#fallback_to_options_from_env(options) ⇒ Object



114
115
116
117
118
# File 'lib/right_branch/commands/change_pull_request_target.rb', line 114

def fallback_to_options_from_env(options)
  options[:username] ||= ENV['RIGHT_BRANCH_USERNAME']
  options[:repository] ||= ENV['RIGHT_BRANCH_REPOSITORY']
  options[:access_token] ||= ENV['RIGHT_BRANCH_ACCESS_TOKEN']
end

#missing_key_abort_message(keys, opt_parser) ⇒ Object



76
77
78
# File 'lib/right_branch/commands/change_pull_request_target.rb', line 76

def missing_key_abort_message(keys, opt_parser)
  "Missing required options: #{keys.join(', ')}\n\n#{opt_parser}"
end

#missing_opt_keys(options) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/right_branch/commands/change_pull_request_target.rb', line 68

def missing_opt_keys(options)
  missing = REQUIRED_OPTIONS - options.keys
  required_blank = options.select do |k, v|
    REQUIRED_OPTIONS.include?(k) && String(v).empty?
  end
  missing += required_blank.keys
end

#prompt_for_password?(options) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
# File 'lib/right_branch/commands/change_pull_request_target.rb', line 55

def prompt_for_password?(options)
  String(options[:access_token]).empty? &&
    String(options[:password]).empty?
end

#run!Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/right_branch/commands/change_pull_request_target.rb', line 20

def run!
  options = build_options

  attrs = {
    credentials: build_credentials(options),
    repository: options[:repository]
  }

  updater(attrs).run!(options[:pull_request], options[:new_branch])
rescue ::Octokit::Unauthorized
  abort 'Invalid credentials to update pull request'
rescue ::Octokit::UnprocessableEntity
  abort 'Invalid branch'
rescue ::Octokit::NotFound
  abort 'Pull request not found'
end

#updater(attrs) ⇒ Object



80
81
82
# File 'lib/right_branch/commands/change_pull_request_target.rb', line 80

def updater(attrs)
  RightBranch::Updater.new(attrs)
end