patbonecrusher/polarbear

View on GitHub
bin/poohbear-review-delete

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby

require 'highline/import'
require 'base64'
require 'polarbear'
require 'gitit'

#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
def usage
  puts 'Commands for updating an existing review.'
  exit 0
end

#-----------------------------------------------------------------------------
# ................ A R G U M E N T S .. P R O C E S S I N G .................
#-----------------------------------------------------------------------------

#-----------------------------------------------------------------------------
# Will try to infer the source control mechanism if possible and call.
#-----------------------------------------------------------------------------
options = {}

optparse = OptionParser.new do |opts|
  # Set a banner, displayed at the top of the help screen.
  opts.banner = 'Usage: poohbear update [options]'

  # Define the options, and what they do
  options[:title] = nil
  opts.on('-t', '--title title', 'review title') do |title|
    options[:title] = title
  end

  options[:id] = nil
  opts.on('-i', '--id reviewid', 'review id') do |reviewid|
    options[:id] = reviewid
  end

  options[:usage] = nil
  opts.on( '-u', '--usage', 'Print one liner about this script' ) do
    options[:usage] = true
  end

  options[:auto_answer] = nil
  opts.on( '-y', '--auto_answer', 'Won\'t prompt the user for anything and rely on args and defaults' ) do
    options[:auto_answer] = true
  end

  opts.on( '-v', '--verbose', 'Verbose ') do
    options[:loglevel] = :info
  end

  # This displays the help screen, all programs are assumed to have this
  # option.
  opts.on( '-h', '--help', 'Display this screen' ) do
    puts opts
    exit
  end
end

#-----------------------------------------------------------------------------
# Parse the command-line. Remember there are two forms of the parse method.
# The 'parse' method simply parses ARGV, while the 'parse!' method parses
# ARGV and removes any options found there, as well as any parameters for the
# the options. What's left is the list of files to resize.
#-----------------------------------------------------------------------------
begin
  optparse.parse!
rescue => ex
  puts ex.to_s
  exit 1
end

usage if options[:usage]

# Do some validation
#-----------------------------------------------------------------------------
_gitit = Gitit::Git.new(Dir.pwd)
unless _gitit.repo.valid?
  p 'poohbear must be run from a folder that is a valid git repo'
  exit 1
end

_current_branch = _gitit.branches.get_current_branch

# Ask user if we have to.
#-----------------------------------------------------------------------------
if options[:auto_answer]
  options[:title] = _current_branch if options[:title].nil?
else
  options[:title] = ask('title: ') { |q| q.default = _current_branch } if options[:title].nil?
end

# Do the operation
#-----------------------------------------------------------------------------
begin
  _codecollab = PolarBear::CodeCollab.new
  if options[:id].nil?
    _review = _codecollab.get_review_with_title(options[:title])
  else
    _review = _codecollab.get_review_with_id(options[:id])
  end
  raise "can't find review with title #{title}" if _review.empty?

  _review[0].delete!
  p 'Review successfully deleted.  '

  exit 0
rescue => ex
  p ex.to_s
  exit 1
end