class JiraCli::Wrapper

Constants

CLI_METHODS
CSV_RESPONSE_METHODS

Public Class Methods

new() click to toggle source
# File lib/jira_cli/wrapper.rb, line 23
def initialize
  @jira_cmd = configure_jira_cmd
  define_cli_methods
end

Private Instance Methods

check_first_line(output, regex) click to toggle source
# File lib/jira_cli/wrapper.rb, line 30
def check_first_line output, regex
  raise OutputError.new("Expected response to begin with \"#{regex}\"", output) unless output =~ Regexp.new(regex)
end
configure_jira_cmd() click to toggle source
# File lib/jira_cli/wrapper.rb, line 34
def configure_jira_cmd
  cli_opts = {
    server:   :server_url,
    user:     :user,
    password: :password
  }.map do |cli_opt, config_var|
    JiraCli.send(config_var) ? "--#{cli_opt} \"#{JiraCli.send(config_var)}\"" : nil
  end.compact

  [JiraCli.cli_jar_path ? "java -jar \"#{JiraCli.cli_jar_path}\"" : 'jira', *cli_opts].join ' '
end
define_cli_methods() click to toggle source
# File lib/jira_cli/wrapper.rb, line 46
def define_cli_methods
  CLI_METHODS.each do |s_method|
    cli_method = s_method.to_s.camelize(:lower)

    define_singleton_method s_method, -> **jira_args do
      if jira_args.delete(:parse_response) && CSV_RESPONSE_METHODS[s_method]
        get_csv_list **{cmd: cli_method}.merge(**jira_args, **CSV_RESPONSE_METHODS[s_method])
      else
        jira_cmd cli_method, **jira_args
      end
    end
  end
end
get_csv_list(cmd:, label:, id_col:, **jira_args) click to toggle source
# File lib/jira_cli/wrapper.rb, line 60
def get_csv_list cmd:, label:, id_col:, **jira_args
  output = jira_cmd cmd, **jira_args
  check_first_line output, '^\d* '+label
  return {} unless output.index("\n")
  output = output[output.index("\n")+1..-1] # removing first line of output
  arr = parse_csv output
  arr.map{|h| [h[id_col], h.except(id_col)]}.to_h
end
jira_cmd(action, **jira_args) click to toggle source
# File lib/jira_cli/wrapper.rb, line 81
def jira_cmd action, **jira_args
  cmd_args = {action: action}.merge(jira_args).map do |k,v|
    cmd = "--#{k.to_s.camelize(:lower)}"
    unless v.is_a? TrueClass
      cmd += v.to_s.include?('"') && !v.to_s.include?('""') ? " '#{v}'" : " \"#{v}\""
    end
    cmd
  end.join(' ')

  stdout, stderr, status = Open3.capture3 "#{@jira_cmd} #{cmd_args}"

  # ap "jira #{cmd_args}"
  # puts stdout
  raise stderr.strip if stderr != ''
  stdout.strip
end
parse_csv(str) click to toggle source
# File lib/jira_cli/wrapper.rb, line 69
def parse_csv str
  CSV.parse(str, headers: true).map do |row|
    row.map do |k,v|
      if v.blank?
        nil
      else
        [k.gsub(/\s/,'').underscore.to_sym, v =~ /\A\d+\z/ ? v.to_i : v]
      end
    end.compact.to_h
  end
end