patbonecrusher/polarbear

View on GitHub
bin/poohbear-review-update

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[:base_branch] = nil
  opts.on('-b', '--base_branch branch', 'base branch name') do |branch|
    options[:base_branch] = branch
  end

  options[:current_branch] = nil
  opts.on('-c', '--current_branch branch', 'current branch name') do |branch|
    options[:current_branch] = branch
  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

  options[:diffargs] = nil
  opts.on( '-x', '--diffargs ARGS', String, 'Instead of diffing 2 branches, diff using whatever is passed in [GIT DIFF ARGS]') do |diffarguments|
    options[:diffargs] = diffarguments || ''
  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
_base_branch = _gitit.branches.exists_locally?('develop') ? 'develop' : 'master'

# Ask user if we have to.
if options[:auto_answer]
  options[:title] = _current_branch if options[:title].nil?
  options[:base_branch] = _base_branch if options[:base_branch].nil?
  options[:current_branch] = _current_branch if options[:current_branch].nil?
else
  options[:title] = ask('title: ') { |q| q.default = _current_branch } if options[:title].nil?
  if options[:diffargs].nil?
    options[:base_branch] = ask('base branch diffing against: ') { |q| q.default = _base_branch } if options[:base_branch].nil?
    options[:current_branch] = ask('current branch diffed against base_branch: ') { |q| q.default = _current_branch } if options[:current_branch].nil?
  end
end

# Update the review
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?

  if options[:diffargs].nil?
    _review[0].add_git_diff(options[:base_branch], options[:current_branch])
  else
    _review[0].add_git_diff_with_args(options[:diffargs])
  end

  p "Review successfully updated.  Id is: #{_review[0].id}"

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