class Jira::Command::Checkout

Attributes

options[RW]
ticket[RW]

Public Class Methods

new(ticket, options) click to toggle source
# File lib/jira/commands/checkout.rb, line 17
def initialize(ticket, options)
  self.ticket = ticket
  self.options = options
end

Public Instance Methods

run() click to toggle source
# File lib/jira/commands/checkout.rb, line 22
def run
  return unless Jira::Core.ticket?(ticket)
  return if metadata.empty?
  unless metadata['errorMessages'].nil?
    on_failure
    return
  end
  unless remote?
    on_failure
    return
  end

  create_branch unless branches.include?(ticket)
  checkout_branch
  reset_branch unless branches.include?(ticket)
  on_success
end

Private Instance Methods

branches() click to toggle source
# File lib/jira/commands/checkout.rb, line 50
def branches
  @branches ||= `git branch --list 2> /dev/null`.split(' ')
  @branches.delete("*")
  @branches
end
checkout_branch() click to toggle source
# File lib/jira/commands/checkout.rb, line 60
def checkout_branch
  `git checkout #{ticket_name} 2> /dev/null`
end
create_branch() click to toggle source
# File lib/jira/commands/checkout.rb, line 56
def create_branch
  `git branch #{ticket_name} 2> /dev/null`
end
metadata() click to toggle source
# File lib/jira/commands/checkout.rb, line 64
def metadata
  @metadata ||= api.get("issue/#{ticket}")
end
on_failure() click to toggle source
# File lib/jira/commands/checkout.rb, line 46
def on_failure
  puts "No ticket checked out."
end
on_success() click to toggle source
# File lib/jira/commands/checkout.rb, line 42
def on_success
  puts "Ticket #{ticket} checked out."
end
remote() click to toggle source
# File lib/jira/commands/checkout.rb, line 68
def remote
  @remote ||= options['remote'] || io.select('Remote?', remotes)
end
remote?() click to toggle source
# File lib/jira/commands/checkout.rb, line 84
def remote?
  return true if remotes.include?(remote)
  false
end
remotes() click to toggle source
# File lib/jira/commands/checkout.rb, line 72
def remotes
  @remotes ||= `git remote 2> /dev/null`.split(' ')
end
reset_branch() click to toggle source
# File lib/jira/commands/checkout.rb, line 89
def reset_branch
  `git reset --hard #{remote} 2> /dev/null`
end
summary() click to toggle source
# File lib/jira/commands/checkout.rb, line 76
def summary
  metadata['fields']['summary'].downcase.tr(" ", "_")
end
ticket_name() click to toggle source
# File lib/jira/commands/checkout.rb, line 80
def ticket_name
  "#{ticket}_#{summary}"
end