class Lita::Handlers::JenkinsClient::JobAction

Public Class Methods

commands() click to toggle source
# File lib/lita/handlers/jenkins_client/job_action.rb, line 15
def commands
  super.merge({
    all: Command.new(name: 'all', matcher: 'all', help: 'list all jobs.'),
    list: Command.new(name: 'list', matcher: 'list', help: 'list jobs matching pattern.', usage: 'list [filter]'),
    list_by_status: Command.new(name: 'list_by_status', matcher: 'list_by_status', help: 'list jobs with certain status.', usage: 'list_by_status [success/failure]'),
    status: Command.new(name: 'status', matcher: 'status', help: 'print job status.', usage: 'status [job name]'),
    build: Command.new(name: 'build', matcher: 'build', help: "build job, #{BuildParam.print_supported_types}", usage: 'build [job name] [param_key:param_value]'),
    params: Command.new(name: 'params', matcher: 'params', help: 'obtain the build parameters of a job.', usage: 'params [job_name]'),
    exists?: Command.new(name: 'exists?', matcher: 'exists', help: 'check if a job exists', usage: 'exists [job name]'),
  })
end
route_prefix() click to toggle source
# File lib/lita/handlers/jenkins_client/job_action.rb, line 11
def route_prefix
  super + ["job"]
end

Public Instance Methods

all(res) click to toggle source
# File lib/lita/handlers/jenkins_client/job_action.rb, line 28
def all(res)
  res.reply api_exec {
    client.job.list_all.inspect
  }
end
build(res) click to toggle source
# File lib/lita/handlers/jenkins_client/job_action.rb, line 52
def build(res)
  if res.args.length < 3
    res.reply 'please provide a job name'
    return
  end

  job_name = res.args[2]
  if client.job.exists?(job_name) == false
    res.reply "job #{job_name} not exists" 
    return 
  end
  hash_args = key_value_pair(res.args.slice(3...res.args.length))
  if hash_args.empty? 
    res.reply api_exec { 
      print_build_status(client.job.build(job_name))
    }
    return 
  end

  begin
    params = parse_build_params(hash_args, client.job.get_build_params(job_name))
    res.reply api_exec { 
      print_build_status(client.job.build(job_name, params))
    }
  rescue ArgumentError => e
    res.reply "Error: #{e.message}"
  end
end
delete(res) click to toggle source
# File lib/lita/handlers/jenkins_client/job_action.rb, line 81
def delete(res)
  if res.args.length < 3 
    res.reply 'please provide a job name'
    return
  end

  res.reply api_exec { client.job.delete(res.args[2]).inspect }
end
exists?(res) click to toggle source
# File lib/lita/handlers/jenkins_client/job_action.rb, line 99
def exists?(res)
  if res.args.length < 3
    res.reply 'please provide a job name'
    return
  end

  res.reply api_exec { client.job.exists?(res.args[2]).inspect }
end
list(res) click to toggle source
# File lib/lita/handlers/jenkins_client/job_action.rb, line 34
def list(res)
  if res.args.length < 3
    res.reply 'please provide a search condition'
    return
  end

  res.reply api_exec { client.job.list(res.args[2]) }
end
list_by_status(res) click to toggle source
# File lib/lita/handlers/jenkins_client/job_action.rb, line 43
def list_by_status(res)
  if res.args.length < 3
    res.reply 'please provide a status ( SUCCESS, FAILURE )'
    return
  end
  
  res.reply api_exec { client.job.list_by_status(res.args[2]) }
end
params(res) click to toggle source
# File lib/lita/handlers/jenkins_client/job_action.rb, line 108
def params(res)
  if res.args.length < 3 
    res.reply 'please provide a job name'
    return
  end

  res.reply api_exec { client.job.get_build_params(res.args[2]).inspect }
end
status(res) click to toggle source
# File lib/lita/handlers/jenkins_client/job_action.rb, line 90
def status(res)
  if res.args.length < 3 
    res.reply 'please provide a job name'
    return
  end

  res.reply api_exec { client.job.get_current_build_status(res.args[2]).inspect }
end

Private Instance Methods

key_value_pair(args) click to toggle source
# File lib/lita/handlers/jenkins_client/job_action.rb, line 128
def key_value_pair(args)
  args.map { |arg|
    if pair = /([\/\\,\.\w-]+):([\/\\,\.\w-]+)/.match(arg)&.captures
      [pair[0], pair[1]]
    else
      nil
    end
  }.compact.to_h
end
parse_build_params(hash_args, build_params) click to toggle source
# File lib/lita/handlers/jenkins_client/job_action.rb, line 118
def parse_build_params(hash_args, build_params)
  params = build_params.map do |param|
    bp = BuildParam.new(param)
    bp.value = hash_args[bp.name]
    bp
  end

  return params.reduce({}) {|res, p| res.merge(p.to_h) }
end
print_build_status(status) click to toggle source