class CLI

Public Class Methods

new(*args) click to toggle source
Calls superclass method
# File lib/mothra/cli.rb, line 47
def initialize(*args)
  super
  @config = YAML.load_file "#{Dir.home}/.mothra.yml"
  @config['BZ_URL'] = clean_bz_url(@config['BZ_URL'])
  @bz = Rodzilla::Resource::Bug.new(@config['BZ_URL'], @config['BZ_USER'], @config['BZ_PASSWD'], :json)
end

Public Instance Methods

attach(bug_id, file_path) click to toggle source
# File lib/mothra/cli.rb, line 135
def attach(bug_id, file_path)
  attach_id = _attach(bug_id, file_path)

  if not attach_id
    puts 'Fail to add attachments, please try again!'.yellow
  else
    print 'PR'
    print " [#{bug_id}] ".cyan
    print 'had add an attachment, '
    puts " [#{attach_id}] ".yellow
    puts "#{@config['BZ_URL']}/show_bug.cgi?id=#{bug_id}".cyan
  end
end
browse(bug_id) click to toggle source
# File lib/mothra/cli.rb, line 98
def browse(bug_id)
  if not is_numeric?bug_id
    puts 'No such bug id'
    exit
  end

  url = "#{@config['BZ_URL']}/show_bug.cgi?id=#{bug_id}"

  if RbConfig::CONFIG['host_os'] =~ /darwin/
    system "open #{url}"
  elsif RbConfig::CONFIG['host_os'] =~ /linux|bsd/
    if find_executable 'xdg-open'
      system "xdg-open #{url}"
    else
      puts 'You have no xdg-open command, please browse it manually!'
      puts url.cyan
    end
  else
    puts 'Could not open url at your platform, sorry.'
  end
end
create(summary) click to toggle source
# File lib/mothra/cli.rb, line 121
def create(summary)
  bug_id = _create(summary)

  if not bug_id
    puts 'Send-pr fail, please try again!'.yellow
  else
    print 'PR'
    print " [#{bug_id}] ".yellow
    puts "created!"
    puts "#{@config['BZ_URL']}/show_bug.cgi?id=#{bug_id}".cyan
  end
end
get(bug_id) click to toggle source
# File lib/mothra/cli.rb, line 77
def get(bug_id)
  if not is_numeric?bug_id
    puts 'No such bug id'
    exit
  end
  
  ret = @bz.get(ids: bug_id)

  begin
    r = ret['bugs'][0]
    print "[#{r['id']}] ".yellow
    print "[#{r['status']}] ".cyan
    print "#{r['summary']} ".white
    puts "[#{r['last_change_time']}] ".light_black
    puts "#{@config['BZ_URL']}/show_bug.cgi?id=#{bug_id}".cyan
  rescue
    puts 'No such bug_id'
  end
end
submit(summary, file_path) click to toggle source
# File lib/mothra/cli.rb, line 150
def submit(summary, file_path)
  bug_id = _create(summary)

  if not bug_id
    puts 'Send-pr fail, please try again!'.yellow
    exit
  end

  attach_id = _attach(bug_id, file_path)

  if not attach_id
    puts 'Fail to add attachments, please upload attachment manually!'.yellow
  else
    print 'PR'
    print " [#{bug_id}] ".cyan
    print 'had add an attachment, '
    puts " [#{attach_id}] ".yellow
  end
  puts "#{@config['BZ_URL']}/show_bug.cgi?id=#{bug_id}".cyan
end

Private Instance Methods

_attach(bug_id, file_path) click to toggle source
# File lib/mothra/cli.rb, line 193
def _attach(bug_id, file_path)
  file_name = get_file_name(file_path)
  file_base64 = base64(file_path)

  begin
    ret = @bz.add_attachment!(ids: [bug_id],
                              data: file_base64,
                              file_name: file_name,
                              summary: file_name,
                              content_type: 'text/plain',
                             )

    ret['ids'][0]
  rescue
    false
  end
end
_create(summary) click to toggle source
# File lib/mothra/cli.rb, line 173
def _create(summary)
  begin
    ret = @bz.create!(product: @config['PRODUCT'],
                      component: @config['COMPONENT'],
                      #version: '1.0', #bugzilla test
                      #op_sys: 'FreeBSD', #bugzilla test
                      #platform: 'All', #bugzilla test
                      version: 'Latest',
                      severity: 'Affects Only Me',
                      op_sys: 'Any',
                      platform: 'Any',
                      summary: summary
                     )

    ret['id']
  rescue
    false
  end
end