Class: RightBranch::Commands::ChangePullRequestTarget
- Inherits:
-
Object
- Object
- RightBranch::Commands::ChangePullRequestTarget
- 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
-
#stream ⇒ Object
Returns the value of attribute stream.
Class Method Summary collapse
Instance Method Summary collapse
- #build_credentials(options) ⇒ Object
- #build_opt_parser(options) ⇒ Object
- #build_options ⇒ Object
- #fallback_to_options_from_env(options) ⇒ Object
-
#initialize(stream = $stdout) ⇒ ChangePullRequestTarget
constructor
A new instance of ChangePullRequestTarget.
- #missing_key_abort_message(keys, opt_parser) ⇒ Object
- #missing_opt_keys(options) ⇒ Object
- #prompt_for_password?(options) ⇒ Boolean
- #run! ⇒ Object
- #updater(attrs) ⇒ Object
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
#stream ⇒ Object
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() if [:access_token].to_s.empty? { login: [:username], password: [:password] } else { access_token: [: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() OptionParser.new do |opts| opts. = 'Usage: right_branch [options]' opts.on("-u", "--username USERNAME", "Username") do |v| [:username] = v end opts.on("-r", "--repository REPOSITORY", "Repository") do |v| [:repository] = v end opts.on("-b", "--new-branch NEW_BRANCH", "New branch") do |v| [:new_branch] = v end opts.on("-p", "--pull-request PULL_REQUEST", "Pull request") do |v| [:pull_request] = v end opts.on("-t", "--access-token ACCESS_TOKEN", "Access token") do |v| [:access_token] = v end opts.on("--password PASSWORD", "Password") do |v| [:password] = v end end end |
#build_options ⇒ Object
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 = {} opt_parser = build_opt_parser() opt_parser.parse! () missing = missing_opt_keys() unless missing.empty? abort (missing, opt_parser) end if prompt_for_password?() [:password] = ask('Enter your password: ') { |q| q.echo = '*' } end 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 () [:username] ||= ENV['RIGHT_BRANCH_USERNAME'] [:repository] ||= ENV['RIGHT_BRANCH_REPOSITORY'] [: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 (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() missing = REQUIRED_OPTIONS - .keys required_blank = .select do |k, v| REQUIRED_OPTIONS.include?(k) && String(v).empty? end missing += required_blank.keys end |
#prompt_for_password?(options) ⇒ Boolean
55 56 57 58 |
# File 'lib/right_branch/commands/change_pull_request_target.rb', line 55 def prompt_for_password?() String([:access_token]).empty? && String([: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! = attrs = { credentials: build_credentials(), repository: [:repository] } updater(attrs).run!([:pull_request], [: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 |