class FlightPlanCli::Commands::Checkout

Attributes

base_branch[R]
branch_prefix[R]
issue_no[R]

Public Class Methods

new(issue_no, options) click to toggle source
# File lib/flight_plan_cli/commands/checkout.rb, line 7
def initialize(issue_no, options)
  @issue_no = issue_no
  @base_branch = options["base"]
  @branch_prefix = options["prefix"]
  @fetched = false
end

Public Instance Methods

process() click to toggle source
# File lib/flight_plan_cli/commands/checkout.rb, line 14
def process
  local_branch_for_issue || remote_branch_for_issue || new_branch_for_issue
rescue Git::GitExecuteError => e
  puts 'Unable to checkout'.red
  puts e.message.yellow
end

Private Instance Methods

branch_name(branch) click to toggle source
# File lib/flight_plan_cli/commands/checkout.rb, line 60
def branch_name(branch)
  "#{branch_prefix}/##{branch['ticket']['remote_number']}-" +
    branch['ticket']['remote_title']
      .gsub(/\([^)]*\)/, '') # remove everything inside brackets
      .match(/^.{0,60}\b/)[0] # take the first 60 chars (finish on word boundry)
      .gsub(/[^a-z0-9\s]/i, '') # remove everything except alpha-numeric
      .squeeze(' ')
      .strip
      .tr(' ', '-')
      .downcase
end
fetch() click to toggle source
# File lib/flight_plan_cli/commands/checkout.rb, line 81
def fetch
  return if @fetched
  puts 'Fetching...'.green
  git.remote('origin').fetch
  @fetched = true
end
local_branch_for_issue() click to toggle source
# File lib/flight_plan_cli/commands/checkout.rb, line 25
def local_branch_for_issue
  issue_branches = local_branches.map(&:name).grep(/##{issue_no}[^0-9]/)
  return false unless issue_branches.count == 1

  branch = issue_branches.first
  puts "Checking out local branch '#{branch}'".green
  git.checkout(branch)
  true
end
local_branches() click to toggle source
# File lib/flight_plan_cli/commands/checkout.rb, line 72
def local_branches
  @local_branches ||= git.branches.local
end
new_branch_for_issue() click to toggle source
# File lib/flight_plan_cli/commands/checkout.rb, line 47
def new_branch_for_issue
  branches = flight_plan.board_tickets(remote_number: issue_no)
  # TODO: update flight_plan to only return one issue when remote_numer is provided
  branches = branches.select { |b| b['ticket']['remote_number'] == issue_no }
  return false unless branches.count == 1

  branch_name = branch_name(branches.first)
  git.checkout(base_branch)
  git.pull
  puts "Creating new branch #{branch_name} from #{base_branch}".green
  git.branch(branch_name).checkout
end
remote_branch_for_issue() click to toggle source
# File lib/flight_plan_cli/commands/checkout.rb, line 35
def remote_branch_for_issue
  issue_branches = remote_branches.map(&:name).grep(/##{issue_no}[^0-9]/)
  return false unless issue_branches.count == 1

  remote_branch_name = issue_branches.first
  branch = remote_branches.find { |rb| rb.name == remote_branch_name }

  puts "Checking out and tracking remote branch '#{branch.name}'".green
  git.checkout(branch.name)
  true
end
remote_branches() click to toggle source
# File lib/flight_plan_cli/commands/checkout.rb, line 76
def remote_branches
  fetch
  @remote_branches ||= git.branches.remote
end