Class: Lita::Handlers::Jenkins

Inherits:
Handler
  • Object
show all
Defined in:
lib/lita/handlers/jenkins.rb

Instance Method Summary collapse

Instance Method Details

#headersObject



67
68
69
70
71
# File 'lib/lita/handlers/jenkins.rb', line 67

def headers
  {}.tap do |headers|
    headers["Authorization"] = "Basic #{Base64.strict_encode64(config.auth).chomp}" if config.auth
  end
end

#jenkins_build(response, empty_params = false) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/lita/handlers/jenkins.rb', line 20

def jenkins_build(response, empty_params = false)
  job = find_job(response.matches.last.first)
  input_params = response.matches.last.last

  unless job
    response.reply "I couldn't find that job. Try `jenkins list` to get a list."
    return
  end

  # Either a Hash of params or True/False
  params = input_params ? parse_params(input_params) : empty_params

  named_job_url = job_url(job['name'])
  path = job_build_url(named_job_url, params)

  http_resp = http(config.http_options).post(path) do |req|
    req.headers = headers
    req.params  = params if params.is_a? Hash
  end

  if http_resp.status == 201
    reply_text = "(#{http_resp.status}) Build started for #{job['name']} #{named_job_url}"
    reply_text << ", Params: '#{input_params}'" if input_params
    response.reply reply_text
  elsif http_resp.status == 400
    log.debug 'Issuing rebuild with empty_params'
    jenkins_build(response, true)
  else
    response.reply http_resp.body
  end
end

#jenkins_list(response) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/lita/handlers/jenkins.rb', line 52

def jenkins_list(response)
  filter = response.matches.first.last
  reply  = ''

  jobs.each_with_index do |job, i|
    job_name      = job['name']
    state         = color_to_state(job['color'])
    text_to_check = state + job_name

    reply << format_job(i, state, job_name) if filter_match(filter, text_to_check)
  end

  response.reply reply
end

#jobsObject



73
74
75
76
77
78
# File 'lib/lita/handlers/jenkins.rb', line 73

def jobs
  api_response = http(config.http_options).get(api_url) do |req|
    req.headers = headers
  end
  JSON.parse(api_response.body)["jobs"]
end