Class: Lita::Handlers::Jenkins

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

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.jobsObject

Returns the value of attribute jobs.



8
9
10
# File 'lib/lita/handlers/jenkins.rb', line 8

def jobs
  @jobs
end

Class Method Details

.default_config(config) ⇒ Object



11
12
13
14
# File 'lib/lita/handlers/jenkins.rb', line 11

def self.default_config(config)
  config.url = nil
  self.jobs = {}
end

Instance Method Details

#headersObject



58
59
60
61
62
63
64
# File 'lib/lita/handlers/jenkins.rb', line 58

def headers
  headers = {}
  if auth = Lita.config.handlers.jenkins.auth
    headers["Authorization"] = "Basic #{Base64.encode64(auth).chomp}"
  end
  headers
end

#jenkins_build(response) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/lita/handlers/jenkins.rb', line 24

def jenkins_build(response)
  if job = jobs[response.matches.last.last.to_i - 1]
    url    = Lita.config.handlers.jenkins.url
    path   = "#{url}/job/#{job['name']}/build"

    http_resp = http.post(path) do |req|
      req.headers = headers
    end

    if http_resp.status == 201
      response.reply "(#{http_resp.status}) Build started for #{job['name']} #{url}/job/#{job['name']}"
    else
      response.reply http_resp.body
    end
  else
    response.reply "I couldn't find that job. Try `jenkins list` to get a list."
  end
end

#jenkins_list(response) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/lita/handlers/jenkins.rb', line 43

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



66
67
68
69
70
71
72
73
74
75
# File 'lib/lita/handlers/jenkins.rb', line 66

def jobs
  if self.class.jobs.empty?
    path = "#{Lita.config.handlers.jenkins.url}/api/json"
    api_response = http.get(path) do |req|
      req.headers = headers
    end
    self.class.jobs = JSON.parse(api_response.body)["jobs"]
  end
  self.class.jobs
end