class Git::Whistles::Youtrack::Branch

Public Class Methods

new(current_dir: ENV['PWD']) click to toggle source
Calls superclass method Git::Whistles::App::new
# File lib/git-whistles/youtrack/branch.rb, line 9
def initialize(current_dir: ENV['PWD'])
  @current_dir = current_dir
  super()
end

Public Instance Methods

main(args) click to toggle source
Calls superclass method Git::Whistles::App#main
# File lib/git-whistles/youtrack/branch.rb, line 14
def main(args)
  super

  parse_args!(args)
  if args.count < 1
    puts Term::ANSIColor.yellow usage
    return false
  end

  ticket = get_ticket_from args[0]
  if ticket.nil?
    die "Could not find ticket #{args[0]}"
    return false
  end

  suggested_branch_name = suggested_branch_name_from(ticket)
  print_suggested_branch suggested_branch_name

  user_branch_name = read_user_answer
  show_and_exit "Cancelled by user" if user_branch_name.nil?

  final_branch_name = user_branch_name.empty? ? suggested_branch_name : user_branch_name
  create_branch final_branch_name

  puts Term::ANSIColor.green 'Created branch'
  true
end

Private Instance Methods

create_branch(branch_name) click to toggle source
# File lib/git-whistles/youtrack/branch.rb, line 73
def create_branch branch_name
  `cd #{@current_dir} && git checkout -b #{branch_name}`
end
get_ticket_from(ticket_id) click to toggle source
# File lib/git-whistles/youtrack/branch.rb, line 48
def get_ticket_from ticket_id
  youtrack_client.find_ticket ticket_id
end
print_suggested_branch(branch_name) click to toggle source
read_user_answer() click to toggle source
# File lib/git-whistles/youtrack/branch.rb, line 69
def read_user_answer
  Readline.readline '  > ', false
end
suggested_branch_name_from(ticket) click to toggle source
# File lib/git-whistles/youtrack/branch.rb, line 56
def suggested_branch_name_from ticket
  "#{ticket.project}/#{ticket.title}-#{ticket.id}".
    downcase.
    gsub(/[^\w\d\/]/, '-').gsub!(/-+/, '-')
end
usage() click to toggle source
# File lib/git-whistles/youtrack/branch.rb, line 44
def usage
  'Usage: git youtrack-branch YOUTRACK_STORY_ID'
end
youtrack_client() click to toggle source
# File lib/git-whistles/youtrack/branch.rb, line 52
def youtrack_client
  @youtrack_client ||= Git::Whistles::Youtrack::Api.new
end