lita-jenkins 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dcffef9c4fb103a261e3c90e6fdfa19274cf6efc
4
- data.tar.gz: 60a9bbe4ef16289c48a21756418a72fec9ea6a6f
3
+ metadata.gz: 2e4bae16d034bae22bcb09463001f0713740b7f4
4
+ data.tar.gz: 827d8a0c091f77c8f44c7b4cfb1c0991d35cc9e1
5
5
  SHA512:
6
- metadata.gz: 7ab1d5548358ab37e304e3470f506c7a9950acdf4883bae95b5af3ba15d7a8c2ecccf8f1151ac2edd375c8da25b907dcf95624824d8c42e2e8d3570f40595fa7
7
- data.tar.gz: e39bd4c67170ba91f49f2668aaff6902e65c380e8713faff672b600876f21d701f26fee977943c8ea545072a456cf0b6badea5080321a06bc196f1d6c2b49422
6
+ metadata.gz: 9ea7092693b33fb132b24db70261bad4e4a581b6e65583cfdffeb7f72cd07aef307a6d017dc4db0abb9002b37244b5275024f3edb2cab75f4aa13dc5073b668a
7
+ data.tar.gz: 548703e81c0ca9ea1ca82a175f811cb0b2336b99a74260fbf39239b2c6617e0f8ea1d2df7f86b0bc4947813153c8c152186af2bc7b340d05673506ef1cdf4efb
data/.gitignore CHANGED
@@ -1,13 +1,15 @@
1
- *.gem
2
- *.rbc
1
+ _yardoc
3
2
  .bundle
4
3
  .config
4
+ .ruby-gemset
5
+ .ruby-version
5
6
  .yardoc
6
- Gemfile.lock
7
- InstalledFiles
8
- _yardoc
7
+ *.gem
8
+ *.rbc
9
9
  coverage
10
10
  doc/
11
+ Gemfile.lock
12
+ InstalledFiles
11
13
  lib/bundler/man
12
14
  pkg
13
15
  rdoc
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.travis.yml CHANGED
@@ -1,8 +1,13 @@
1
+ sudo: false
1
2
  language: ruby
3
+ cache: bundler
2
4
  rvm:
3
- - 2.0.0
5
+ - 2.1
6
+ - 2.2
7
+ - 2.2.3
4
8
  script: bundle exec rake
5
9
  before_install:
6
10
  - gem update --system
11
+ - gem install bundler
7
12
  services:
8
13
  - redis-server
@@ -4,39 +4,47 @@ require 'base64'
4
4
  module Lita
5
5
  module Handlers
6
6
  class Jenkins < Handler
7
- class << self
8
- attr_accessor :jobs
9
- end
10
7
 
11
- def self.default_config(config)
12
- config.url = nil
13
- self.jobs = {}
14
- end
8
+ config :auth
9
+ config :url
15
10
 
16
11
  route /j(?:enkins)? list( (.+))?/i, :jenkins_list, command: true, help: {
17
12
  'jenkins list <filter>' => 'lists Jenkins jobs'
18
13
  }
19
14
 
20
- route /j(?:enkins)? b(?:uild)? (\d+)/i, :jenkins_build, command: true, help: {
21
- 'jenkins b(uild) <job_id>' => 'builds the job specified by job_id. List jobs to get ID.'
15
+ route /j(?:enkins)? b(?:uild)? ([\w\.\-_ ]+)(, (.+))?/i, :jenkins_build, command: true, help: {
16
+ 'jenkins b(uild) <job_id or job_name>' => 'builds the job specified by ID or name. List jobs to get ID.'
22
17
  }
23
18
 
24
- def jenkins_build(response)
25
- if job = jobs[response.matches.last.last.to_i - 1]
26
- url = Lita.config.handlers.jenkins.url
27
- path = "#{url}/job/#{job['name']}/build"
19
+ def jenkins_build(response, empty_params = false)
20
+ job = find_job(response.matches.last.first)
21
+ input_params = response.matches.last.last
28
22
 
29
- http_resp = http.post(path) do |req|
30
- req.headers = headers
31
- end
23
+ unless job
24
+ response.reply "I couldn't find that job. Try `jenkins list` to get a list."
25
+ return
26
+ end
32
27
 
33
- if http_resp.status == 201
34
- response.reply "(#{http_resp.status}) Build started for #{job['name']} #{url}/job/#{job['name']}"
35
- else
36
- response.reply http_resp.body
37
- end
28
+ # Either a Hash of params or True/False
29
+ params = input_params ? parse_params(input_params) : empty_params
30
+
31
+ named_job_url = job_url(job['name'])
32
+ path = job_build_url(named_job_url, params)
33
+
34
+ http_resp = http.post(path) do |req|
35
+ req.headers = headers
36
+ req.params = params if params.is_a? Hash
37
+ end
38
+
39
+ if http_resp.status == 201
40
+ reply_text = "(#{http_resp.status}) Build started for #{job['name']} #{named_job_url}"
41
+ reply_text << ", Params: '#{input_params}'" if input_params
42
+ response.reply reply_text
43
+ elsif http_resp.status == 400
44
+ log.debug 'Issuing rebuild with empty_params'
45
+ jenkins_build(response, true)
38
46
  else
39
- response.reply "I couldn't find that job. Try `jenkins list` to get a list."
47
+ response.reply http_resp.body
40
48
  end
41
49
  end
42
50
 
@@ -56,28 +64,47 @@ module Lita
56
64
  end
57
65
 
58
66
  def headers
59
- headers = {}
60
- if auth = Lita.config.handlers.jenkins.auth
61
- headers["Authorization"] = "Basic #{Base64.encode64(auth).chomp}"
67
+ {}.tap do |headers|
68
+ headers["Authorization"] = "Basic #{Base64.encode64(config.auth).chomp}" if config.auth
62
69
  end
63
- headers
64
70
  end
65
71
 
66
72
  def jobs
67
- if self.class.jobs.empty?
68
- path = "#{Lita.config.handlers.jenkins.url}/api/json"
69
- api_response = http.get(path) do |req|
70
- req.headers = headers
71
- end
72
- self.class.jobs = JSON.parse(api_response.body)["jobs"]
73
+ api_response = http.get(api_url) do |req|
74
+ req.headers = headers
73
75
  end
74
- self.class.jobs
76
+ JSON.parse(api_response.body)["jobs"]
75
77
  end
76
78
 
77
79
  private
78
80
 
81
+ def api_url
82
+ "#{config.url}/api/json"
83
+ end
84
+
85
+ def job_url(job_name)
86
+ "#{config.url}/job/#{job_name}"
87
+ end
88
+
89
+ def job_build_url(named_job_url, params)
90
+ if params
91
+ "#{named_job_url}/buildWithParameters?#{params}"
92
+ else
93
+ "#{named_job_url}/build"
94
+ end
95
+ end
96
+
97
+ def find_job(requested_job)
98
+ # Determine if job is only a number.
99
+ if requested_job.match(/\A[-+]?\d+\z/)
100
+ jobs[requested_job.to_i - 1]
101
+ else
102
+ jobs.select { |j| j['name'] == requested_job }.last
103
+ end
104
+ end
105
+
79
106
  def format_job(i, state, job_name)
80
- "[#{i+1}] #{state} #{job_name}\n"
107
+ "[#{i + 1}] #{state} #{job_name}\n"
81
108
  end
82
109
 
83
110
  def color_to_state(text)
@@ -94,6 +121,16 @@ module Lita
94
121
  def filter_match(filter, text)
95
122
  text.match(/#{filter}/i)
96
123
  end
124
+
125
+ def parse_params(input_params)
126
+ {}.tap do |params|
127
+ input_params.split(',').each do |pair|
128
+ key, value = pair.split(/=/)
129
+ params[key.strip] = value.strip
130
+ end
131
+ log.debug "lita-jenkins#parse_params: #{params}"
132
+ end
133
+ end
97
134
  end
98
135
 
99
136
  Lita.register_handler(Jenkins)
data/lita-jenkins.gemspec CHANGED
@@ -1,8 +1,8 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "lita-jenkins"
3
- spec.version = "0.0.3"
4
- spec.authors = ["Daniel Yoon"]
5
- spec.email = ["daniel.kiros@gmail.com"]
3
+ spec.version = "0.0.4"
4
+ spec.authors = ["Daniel Yoon", "Mike Fiedler"]
5
+ spec.email = ["daniel.kiros@gmail.com", "miketheman@gmail.com"]
6
6
  spec.description = %q{Interact with Jenkins CI server.}
7
7
  spec.summary = %q{Interact with Jenkins CI server.}
8
8
  spec.homepage = "https://github.com/daniely/lita-jenkins.git"
@@ -14,6 +14,8 @@ Gem::Specification.new do |spec|
14
14
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
15
15
  spec.require_paths = ["lib"]
16
16
 
17
+ spec.required_ruby_version = ">= 2.1"
18
+
17
19
  spec.add_runtime_dependency "lita", ">= 3.0"
18
20
 
19
21
  spec.add_development_dependency "bundler", "~> 1.3"
@@ -1,33 +1,51 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Lita::Handlers::Jenkins, lita_handler: true do
4
- it { routes_command('jenkins list').to(:jenkins_list) }
5
- it { routes_command('jenkins list filter').to(:jenkins_list) }
4
+ describe 'lita routes' do
5
+ it { is_expected.to route_command('jenkins list').to(:jenkins_list) }
6
+ it { is_expected.to route_command('jenkins list filter').to(:jenkins_list) }
7
+ it { is_expected.to route_command('jenkins build 2').to(:jenkins_build) }
8
+ it { is_expected.to route_command('jenkins build deploy').to(:jenkins_build) }
9
+ it { is_expected.to route_command('jenkins build deploy, PARAM=value').to(:jenkins_build) }
10
+ end
6
11
 
7
- describe '#jenkins list' do
8
- let(:response) { double("Faraday::Response") }
9
- let(:api_response) { %{
10
- {"assignedLabels":[{}],"mode":"NORMAL","nodeDescription":"the master Jenkins node",
11
- "nodeName":"","numExecutors":4,"description":null,"jobs":[
12
- {"name":"chef_converge", "url":"http://test.com/job/chef_converge/","color":"disabled"},
13
- {"name":"deploy", "url":"http://test.com/job/deploy/","color":"red"},
14
- {"name":"build-all", "url":"http://test.com/job/build-all/","color":"blue"},
15
- {"name":"website", "url":"http://test.com/job/website/","color":"red"}],
16
- "overallLoad":{},
17
- "primaryView":{"name":"All","url":"http://test.com/"},"quietingDown":false,"slaveAgentPort":8090,"unlabeledLoad":{},
18
- "useCrumbs":false,"useSecurity":true,"views":[
19
- {"name":"All","url":"http://test.com/"},
20
- {"name":"Chef","url":"http://test.com/view/Chef/"},
21
- {"name":"Deploy","url":"http://test.com/view/Deploy/"},
22
- {"name":"Status","url":"http://test.com/view/Status/"},
23
- {"name":"deploy-pipeline","url":"http://test.com/view/deploy-pipeline/"}]}
24
- } }
25
-
26
- before do
27
- allow_any_instance_of(Faraday::Connection).to receive(:get).and_return(response)
28
- allow_any_instance_of(Faraday::Connection).to receive(:post).and_return(response)
12
+ let(:response) { double("Faraday::Response") }
13
+ let(:api_response) { %{
14
+ {"assignedLabels":[{}],"mode":"NORMAL","nodeDescription":"the master Jenkins node",
15
+ "nodeName":"","numExecutors":4,"description":null,"jobs":[
16
+ {"name":"chef_converge", "url":"http://test.com/job/chef_converge/","color":"disabled"},
17
+ {"name":"deploy", "url":"http://test.com/job/deploy/","color":"red"},
18
+ {"name":"build-all", "url":"http://test.com/job/build-all/","color":"blue"},
19
+ {"name":"website", "url":"http://test.com/job/website/","color":"red"}],
20
+ "overallLoad":{},
21
+ "primaryView":{"name":"All","url":"http://test.com/"},"quietingDown":false,"slaveAgentPort":8090,"unlabeledLoad":{},
22
+ "useCrumbs":false,"useSecurity":true,"views":[
23
+ {"name":"All","url":"http://test.com/"},
24
+ {"name":"Chef","url":"http://test.com/view/Chef/"},
25
+ {"name":"Deploy","url":"http://test.com/view/Deploy/"},
26
+ {"name":"Status","url":"http://test.com/view/Status/"},
27
+ {"name":"deploy-pipeline","url":"http://test.com/view/deploy-pipeline/"}]}
28
+ } }
29
+
30
+ before do
31
+ allow_any_instance_of(Faraday::Connection).to receive(:get).and_return(response)
32
+ allow_any_instance_of(Faraday::Connection).to receive(:post).and_return(response)
33
+ end
34
+
35
+ describe '#headers' do
36
+ it 'returns empty auth headers correctly by default' do
37
+ return_value = described_class.new(robot).headers
38
+ expect(return_value.inspect).to eq("{}")
39
+ end
40
+
41
+ it 'encodes auth headers correctly' do
42
+ registry.config.handlers.jenkins.auth = "foo:bar"
43
+ return_value = described_class.new(robot).headers
44
+ expect(return_value.inspect).to eq("{\"Authorization\"=>\"Basic Zm9vOmJhcg==\"}")
29
45
  end
46
+ end
30
47
 
48
+ describe '#jenkins list' do
31
49
  it 'lists all jenkins jobs' do
32
50
  allow(response).to receive(:status).and_return(200)
33
51
  allow(response).to receive(:body).and_return(api_response)
@@ -41,7 +59,9 @@ describe Lita::Handlers::Jenkins, lita_handler: true do
41
59
  send_command('jenkins list fail')
42
60
  expect(replies.last).to eq("[2] FAIL deploy\n[4] FAIL website\n")
43
61
  end
62
+ end
44
63
 
64
+ describe '#jenkins build' do
45
65
  it 'build job id' do
46
66
  allow(response).to receive(:status).and_return(201)
47
67
  allow(response).to receive(:body).and_return(api_response)
@@ -49,6 +69,27 @@ describe Lita::Handlers::Jenkins, lita_handler: true do
49
69
  expect(replies.last).to eq("(201) Build started for deploy /job/deploy")
50
70
  end
51
71
 
72
+ it 'build job name' do
73
+ allow(response).to receive(:status).and_return(201)
74
+ allow(response).to receive(:body).and_return(api_response)
75
+ send_command('jenkins b deploy')
76
+ expect(replies.last).to eq("(201) Build started for deploy /job/deploy")
77
+ end
78
+
79
+ it 'build job underscored name' do
80
+ allow(response).to receive(:status).and_return(201)
81
+ allow(response).to receive(:body).and_return(api_response)
82
+ send_command('jenkins b chef_converge')
83
+ expect(replies.last).to eq("(201) Build started for chef_converge /job/chef_converge")
84
+ end
85
+
86
+ it 'build job hyphenated name' do
87
+ allow(response).to receive(:status).and_return(201)
88
+ allow(response).to receive(:body).and_return(api_response)
89
+ send_command('jenkins b build-all')
90
+ expect(replies.last).to eq("(201) Build started for build-all /job/build-all")
91
+ end
92
+
52
93
  it 'build job bad id' do
53
94
  allow(response).to receive(:status).and_return(201)
54
95
  allow(response).to receive(:body).and_return(api_response)
@@ -56,6 +97,13 @@ describe Lita::Handlers::Jenkins, lita_handler: true do
56
97
  expect(replies.last).to eq("I couldn't find that job. Try `jenkins list` to get a list.")
57
98
  end
58
99
 
100
+ it 'build job bad name' do
101
+ allow(response).to receive(:status).and_return(201)
102
+ allow(response).to receive(:body).and_return(api_response)
103
+ send_command('jenkins b sloppyjob')
104
+ expect(replies.last).to eq("I couldn't find that job. Try `jenkins list` to get a list.")
105
+ end
106
+
59
107
  it 'build job error 500 response' do
60
108
  allow(response).to receive(:status).and_return(500)
61
109
  allow(response).to receive(:body).and_return(api_response)
@@ -63,10 +111,22 @@ describe Lita::Handlers::Jenkins, lita_handler: true do
63
111
  expect(replies.last).to eq(api_response)
64
112
  end
65
113
 
66
- it 'build job error 500 response' do
67
- Lita.config.handlers.jenkins.auth = "foo:bar"
68
- return_value = Lita::Handlers::Jenkins.new.headers
69
- expect(return_value.inspect).to eq("{\"Authorization\"=>\"Basic Zm9vOmJhcg==\"}")
114
+ context 'paramterized builds' do
115
+ describe 'job with param' do
116
+ it 'builds job id' do
117
+ allow(response).to receive(:status).and_return(201)
118
+ allow(response).to receive(:body).and_return(api_response)
119
+ send_command('jenkins b 2, PARAM=value')
120
+ expect(replies.last).to eq("(201) Build started for deploy /job/deploy, Params: 'PARAM=value'")
121
+ end
122
+
123
+ it 'builds job name' do
124
+ allow(response).to receive(:status).and_return(201)
125
+ allow(response).to receive(:body).and_return(api_response)
126
+ send_command('jenkins b deploy, PARAM=value')
127
+ expect(replies.last).to eq("(201) Build started for deploy /job/deploy, Params: 'PARAM=value'")
128
+ end
129
+ end
70
130
  end
71
131
  end
72
132
  end
data/spec/spec_helper.rb CHANGED
@@ -8,3 +8,5 @@ SimpleCov.start { add_filter "/spec/" }
8
8
 
9
9
  require "lita-jenkins"
10
10
  require "lita/rspec"
11
+
12
+ Lita.version_3_compatibility_mode = false
metadata CHANGED
@@ -1,108 +1,111 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lita-jenkins
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Yoon
8
+ - Mike Fiedler
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2015-05-01 00:00:00.000000000 Z
12
+ date: 2015-11-14 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: lita
15
16
  requirement: !ruby/object:Gem::Requirement
16
17
  requirements:
17
- - - '>='
18
+ - - ">="
18
19
  - !ruby/object:Gem::Version
19
20
  version: '3.0'
20
21
  type: :runtime
21
22
  prerelease: false
22
23
  version_requirements: !ruby/object:Gem::Requirement
23
24
  requirements:
24
- - - '>='
25
+ - - ">="
25
26
  - !ruby/object:Gem::Version
26
27
  version: '3.0'
27
28
  - !ruby/object:Gem::Dependency
28
29
  name: bundler
29
30
  requirement: !ruby/object:Gem::Requirement
30
31
  requirements:
31
- - - ~>
32
+ - - "~>"
32
33
  - !ruby/object:Gem::Version
33
34
  version: '1.3'
34
35
  type: :development
35
36
  prerelease: false
36
37
  version_requirements: !ruby/object:Gem::Requirement
37
38
  requirements:
38
- - - ~>
39
+ - - "~>"
39
40
  - !ruby/object:Gem::Version
40
41
  version: '1.3'
41
42
  - !ruby/object:Gem::Dependency
42
43
  name: rake
43
44
  requirement: !ruby/object:Gem::Requirement
44
45
  requirements:
45
- - - '>='
46
+ - - ">="
46
47
  - !ruby/object:Gem::Version
47
48
  version: '0'
48
49
  type: :development
49
50
  prerelease: false
50
51
  version_requirements: !ruby/object:Gem::Requirement
51
52
  requirements:
52
- - - '>='
53
+ - - ">="
53
54
  - !ruby/object:Gem::Version
54
55
  version: '0'
55
56
  - !ruby/object:Gem::Dependency
56
57
  name: rspec
57
58
  requirement: !ruby/object:Gem::Requirement
58
59
  requirements:
59
- - - '>='
60
+ - - ">="
60
61
  - !ruby/object:Gem::Version
61
62
  version: 3.0.0.beta2
62
63
  type: :development
63
64
  prerelease: false
64
65
  version_requirements: !ruby/object:Gem::Requirement
65
66
  requirements:
66
- - - '>='
67
+ - - ">="
67
68
  - !ruby/object:Gem::Version
68
69
  version: 3.0.0.beta2
69
70
  - !ruby/object:Gem::Dependency
70
71
  name: simplecov
71
72
  requirement: !ruby/object:Gem::Requirement
72
73
  requirements:
73
- - - '>='
74
+ - - ">="
74
75
  - !ruby/object:Gem::Version
75
76
  version: '0'
76
77
  type: :development
77
78
  prerelease: false
78
79
  version_requirements: !ruby/object:Gem::Requirement
79
80
  requirements:
80
- - - '>='
81
+ - - ">="
81
82
  - !ruby/object:Gem::Version
82
83
  version: '0'
83
84
  - !ruby/object:Gem::Dependency
84
85
  name: coveralls
85
86
  requirement: !ruby/object:Gem::Requirement
86
87
  requirements:
87
- - - '>='
88
+ - - ">="
88
89
  - !ruby/object:Gem::Version
89
90
  version: '0'
90
91
  type: :development
91
92
  prerelease: false
92
93
  version_requirements: !ruby/object:Gem::Requirement
93
94
  requirements:
94
- - - '>='
95
+ - - ">="
95
96
  - !ruby/object:Gem::Version
96
97
  version: '0'
97
98
  description: Interact with Jenkins CI server.
98
99
  email:
99
100
  - daniel.kiros@gmail.com
101
+ - miketheman@gmail.com
100
102
  executables: []
101
103
  extensions: []
102
104
  extra_rdoc_files: []
103
105
  files:
104
- - .gitignore
105
- - .travis.yml
106
+ - ".gitignore"
107
+ - ".rspec"
108
+ - ".travis.yml"
106
109
  - Gemfile
107
110
  - LICENSE
108
111
  - README.md
@@ -124,17 +127,17 @@ require_paths:
124
127
  - lib
125
128
  required_ruby_version: !ruby/object:Gem::Requirement
126
129
  requirements:
127
- - - '>='
130
+ - - ">="
128
131
  - !ruby/object:Gem::Version
129
- version: '0'
132
+ version: '2.1'
130
133
  required_rubygems_version: !ruby/object:Gem::Requirement
131
134
  requirements:
132
- - - '>='
135
+ - - ">="
133
136
  - !ruby/object:Gem::Version
134
137
  version: '0'
135
138
  requirements: []
136
139
  rubyforge_project:
137
- rubygems_version: 2.0.3
140
+ rubygems_version: 2.4.7
138
141
  signing_key:
139
142
  specification_version: 4
140
143
  summary: Interact with Jenkins CI server.