lita-jenkins-client 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e3b68aa187c68b0eea7ff4043bbd7386c75b49b9
4
+ data.tar.gz: 705e15c1b4ffd07774add758d91a56497f565031
5
+ SHA512:
6
+ metadata.gz: d616b316970f6b6f7505eee8c8c4138ac69a1985244267af2828c6102a9425542b69defa620c9546e6429f45277d96de45f2402e84a817890638c307cd236496
7
+ data.tar.gz: 88a010b6513b1428e428310f416c9766fe8119a94a3f1295167a679ea74641c11f77d1541ebccc530eaf5d7c7c5af583cc4bb0d34a2b88de875829800d70319d
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.swp
19
+ spec/config.yml
20
+ .byebug_history
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # lita-jenkins-client
2
+
3
+ Lita jenkins handler that use the [jenkins\_api\_client gem](https://github.com/arangamani/jenkins_api_client)
4
+
5
+ ## About
6
+ Integrate Lita and jenkins api. I've implement some commands which is usable now.
7
+
8
+ ## Installation
9
+
10
+ Add lita-jenkins-client to your Lita instance's Gemfile:
11
+
12
+ ``` ruby
13
+ gem "lita-jenkins-client", github: 'joshua5201/lita-jenkins-client'
14
+ ```
15
+
16
+ ## Current Available Commands
17
+
18
+ see `@lita help jenkins` for more info
19
+
20
+ ```
21
+ @lita jenkins version
22
+ @lita jenkins running?
23
+ @lita jenkins cli [cmd]
24
+ @lita jenkins version
25
+
26
+ @lita jenkins job build [job name] [param_key:param_value]
27
+ @lita jenkins job params [job name]
28
+ @lita jenkins job status [job name]
29
+ @lita jenkins job all
30
+ @lita jenkins job list [filter]
31
+ @lita jenkins job list_by_status [success/failure]
32
+ ```
33
+
34
+ ## Test
35
+ 1. `cp spec/config.yml.example spec/config.yml` and edit your configurations.
36
+ 2. Make sure you don't have project named after `test_with_param` and `test_without_param`
37
+ 3. `rspec`
38
+
39
+ ## Contribute
40
+ 1. feature: I only implement some of api from [jenkins\_api\_client gem](https://github.com/arangamani/jenkins_api_client), you can follow the structure and implement more commands.
41
+ 2. bug: please report issues or just help me fix them.
42
+ 3. please provide RSpec tests
43
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,55 @@
1
+ require_relative 'command'
2
+ require 'byebug'
3
+ class Lita::Handlers::JenkinsClient < Lita::Handler
4
+ class Action < Lita::Handlers::JenkinsClient
5
+ namespace "jenkins_client"
6
+
7
+ class << self
8
+ protected
9
+
10
+ def route_prefix
11
+ ["jenkins"]
12
+ end
13
+
14
+ def route_matcher(actions)
15
+ Regexp.new('^'+(route_prefix + [actions].flatten).join(' ').strip + '\b', 'i')
16
+ end
17
+
18
+ def name_prefix
19
+ route_prefix.join(' ')
20
+ end
21
+
22
+ def commands
23
+ {}
24
+ end
25
+
26
+ def method_added(name)
27
+ if self == Lita::Handlers::JenkinsClient::Action
28
+ return
29
+ end
30
+ if cmd = commands[name]
31
+ route(route_matcher(cmd.matcher), name, :help => {
32
+ "#{name_prefix} #{cmd.name}" => "#{cmd.help} Usage: #{name_prefix} #{cmd.usage}"
33
+ })
34
+ end
35
+ end
36
+
37
+ end
38
+
39
+ protected
40
+
41
+ def api_exec(usage = nil)
42
+ if block_given?
43
+ begin
44
+ yield
45
+ rescue Exception => e
46
+ "Error: #{e.message}" + (usage ? " Usage: #{usage}" : '')
47
+ end
48
+ end
49
+ end
50
+
51
+ def usage(name)
52
+ self.class.commands[name].usage
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,42 @@
1
+ require_relative 'command'
2
+ class Lita::Handlers::JenkinsClient < Lita::Handler
3
+ class BaseAction < Lita::Handlers::JenkinsClient::Action
4
+ # 1. Method defined under JenkinsApi::Client
5
+ # 2. Convenience custom commands
6
+ # @lita jenkins [command]
7
+
8
+ namespace 'jenkins_client'
9
+
10
+ def self.commands
11
+ super.merge ({
12
+ version: Command.new(name: 'version', matcher: 'version', help: 'get jenkins version.'),
13
+ cli: Command.new(name: 'cli', matcher: 'cli', help: 'executes the jenkins cli.', usage: 'cli [command]'),
14
+ running?: Command.new(name: 'running?', matcher: 'running', help: 'Show if jenkins is running.'),
15
+ })
16
+ end
17
+
18
+ def version(res)
19
+ res.reply api_exec {
20
+ client.get_jenkins_version
21
+ }
22
+ end
23
+
24
+ def cli(res)
25
+ if res.args.length < 2
26
+ res.reply 'Please provide at least one command'
27
+ return
28
+ end
29
+ res.reply api_exec(usage(:cli)) {
30
+ client.exec_cli(res.args.slice(1...res.args.length).join(' '))
31
+ }
32
+ end
33
+
34
+ def running?(res)
35
+ res.reply api_exec {
36
+ client.get_root.inspect
37
+ "Running"
38
+ }
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,65 @@
1
+ class Lita::Handlers::JenkinsClient < Lita::Handler
2
+ class BuildParam
3
+ attr_reader :type, :name, :description, :default, :choices, :value
4
+ def self.supported_types
5
+ ['boolean', 'string', 'choice'].freeze
6
+ end
7
+
8
+ def self.print_supported_types
9
+ "Currently available param types: #{self.supported_types.join(' ')}"
10
+ end
11
+
12
+ def initialize(type: , name: , description: , default: '', choices: [])
13
+ @type, @name, @description, @default, @choices = type, name, description, default, choices
14
+ if type == 'choice'
15
+ @default = choices[0]
16
+ end
17
+
18
+ if wrong_type?
19
+ raise ArgumentError, self.class.print_supported_types
20
+ end
21
+
22
+ self.value = @default
23
+ end
24
+
25
+ def value=(v)
26
+ if v.nil?
27
+ @value = nil
28
+ end
29
+
30
+ unless v.is_a? (String)
31
+ return
32
+ end
33
+
34
+ case @type
35
+ when 'string'
36
+ @value = v
37
+ when 'boolean'
38
+ if v == 'true'
39
+ @value = true
40
+ elsif v == 'false'
41
+ @value = false
42
+ else
43
+ raise ArgumentError, "#{@name} should be true or false"
44
+ end
45
+ when 'choice'
46
+ if @choices.include? (v)
47
+ @value = v
48
+ else
49
+ raise ArgumentError, "#{@name} should be one of #{@choices.inspect}"
50
+ end
51
+ else
52
+ raise ArgumentError, self.class.print_supported_types
53
+ end
54
+ end
55
+
56
+ def to_h
57
+ {@name => @value}
58
+ end
59
+
60
+ private
61
+ def wrong_type?
62
+ not self.class.supported_types.include? @type
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,9 @@
1
+ class Lita::Handlers::JenkinsClient < Lita::Handler
2
+ class Command
3
+ attr_reader :name, :matcher, :help, :usage
4
+ def initialize(name: , matcher: , help: , usage: nil)
5
+ @name, @matcher, @help = name, matcher, help
6
+ @usage = usage ? usage : name
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,146 @@
1
+ require_relative 'command'
2
+ require_relative 'build_param'
3
+ class Lita::Handlers::JenkinsClient < Lita::Handler
4
+ class JobAction < Lita::Handlers::JenkinsClient::Action
5
+ # Method defined under JenkinsApi::Client::Job
6
+ # @lita jenkins job [command]
7
+
8
+ namespace "jenkins_client"
9
+
10
+ class << self
11
+ def route_prefix
12
+ super + ["job"]
13
+ end
14
+
15
+ def commands
16
+ super.merge({
17
+ all: Command.new(name: 'all', matcher: 'all', help: 'list all jobs.'),
18
+ list: Command.new(name: 'list', matcher: 'list', help: 'list jobs matching pattern.', usage: 'list [filter]'),
19
+ 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]'),
20
+ status: Command.new(name: 'status', matcher: 'status', help: 'print job status.', usage: 'status [job name]'),
21
+ build: Command.new(name: 'build', matcher: 'build', help: "build job, #{BuildParam.print_supported_types}", usage: 'build [job name] [param_key:param_value]'),
22
+ params: Command.new(name: 'params', matcher: 'params', help: 'obtain the build parameters of a job.', usage: 'params [job_name]'),
23
+ exists?: Command.new(name: 'exists?', matcher: 'exists', help: 'check if a job exists', usage: 'exists [job name]'),
24
+ })
25
+ end
26
+ end
27
+
28
+ def all(res)
29
+ res.reply api_exec {
30
+ client.job.list_all.inspect
31
+ }
32
+ end
33
+
34
+ def list(res)
35
+ if res.args.length < 3
36
+ res.reply 'please provide a search condition'
37
+ return
38
+ end
39
+
40
+ res.reply api_exec { client.job.list(res.args[2]) }
41
+ end
42
+
43
+ def list_by_status(res)
44
+ if res.args.length < 3
45
+ res.reply 'please provide a status ( SUCCESS, FAILURE )'
46
+ return
47
+ end
48
+
49
+ res.reply api_exec { client.job.list_by_status(res.args[2]) }
50
+ end
51
+
52
+ def build(res)
53
+ if res.args.length < 3
54
+ res.reply 'please provide a job name'
55
+ return
56
+ end
57
+
58
+ job_name = res.args[2]
59
+ if client.job.exists?(job_name) == false
60
+ res.reply "job #{job_name} not exists"
61
+ return
62
+ end
63
+ hash_args = key_value_pair(res.args.slice(3...res.args.length))
64
+ if hash_args.empty?
65
+ res.reply api_exec {
66
+ print_build_status(client.job.build(job_name))
67
+ }
68
+ return
69
+ end
70
+
71
+ begin
72
+ params = parse_build_params(hash_args, client.job.get_build_params(job_name))
73
+ res.reply api_exec {
74
+ print_build_status(client.job.build(job_name, params))
75
+ }
76
+ rescue ArgumentError => e
77
+ res.reply "Error: #{e.message}"
78
+ end
79
+ end
80
+
81
+ def delete(res)
82
+ if res.args.length < 3
83
+ res.reply 'please provide a job name'
84
+ return
85
+ end
86
+
87
+ res.reply api_exec { client.job.delete(res.args[2]).inspect }
88
+ end
89
+
90
+ def status(res)
91
+ if res.args.length < 3
92
+ res.reply 'please provide a job name'
93
+ return
94
+ end
95
+
96
+ res.reply api_exec { client.job.get_current_build_status(res.args[2]).inspect }
97
+ end
98
+
99
+ def exists?(res)
100
+ if res.args.length < 3
101
+ res.reply 'please provide a job name'
102
+ return
103
+ end
104
+
105
+ res.reply api_exec { client.job.exists?(res.args[2]).inspect }
106
+ end
107
+
108
+ def params(res)
109
+ if res.args.length < 3
110
+ res.reply 'please provide a job name'
111
+ return
112
+ end
113
+
114
+ res.reply api_exec { client.job.get_build_params(res.args[2]).inspect }
115
+ end
116
+
117
+ private
118
+ def parse_build_params(hash_args, build_params)
119
+ params = build_params.map do |param|
120
+ bp = BuildParam.new(param)
121
+ bp.value = hash_args[bp.name]
122
+ bp
123
+ end
124
+
125
+ return params.reduce({}) {|res, p| res.merge(p.to_h) }
126
+ end
127
+
128
+ def key_value_pair(args)
129
+ args.map { |arg|
130
+ if pair = /([\/\\,\.\w-]+):([\/\\,\.\w-]+)/.match(arg)&.captures
131
+ [pair[0], pair[1]]
132
+ else
133
+ nil
134
+ end
135
+ }.compact.to_h
136
+ end
137
+
138
+ def print_build_status(status)
139
+ if status == '201'
140
+ 'Job created. (http status 201)'
141
+ else
142
+ "Something went wrong. (http status: #{status})"
143
+ end
144
+ end
145
+ end
146
+ end
File without changes
File without changes
File without changes
@@ -0,0 +1,57 @@
1
+ require_relative 'jenkins_client/action'
2
+ require_relative 'jenkins_client/base_action'
3
+ require_relative 'jenkins_client/job_action'
4
+ require_relative 'jenkins_client/command'
5
+
6
+ module Lita
7
+ module Handlers
8
+ class JenkinsClient < Handler
9
+ namespace 'jenkins_client'
10
+
11
+ CONFIGS = {
12
+ server_url: String,
13
+ server_ip: String,
14
+ server_port: String,
15
+ proxy_ip: String,
16
+ proxy_port: String,
17
+ jenkins_path: String,
18
+ username: String,
19
+ password: String,
20
+ password_base64: String,
21
+ log_location: String,
22
+ log_level: Fixnum,
23
+ timeout: Fixnum,
24
+ ssl: Object,
25
+ follow_redirects: Object,
26
+ identity_file: String,
27
+ cookies: String
28
+ }.freeze
29
+
30
+ CONFIGS.each do |config_name, config_type|
31
+ if config_type == Object
32
+ config config_name, type: config_type do
33
+ validate do |value|
34
+ "#{config_name} must be true or false or nil" unless [true, false, nil].include? value
35
+ end
36
+ end
37
+ else
38
+ config config_name, type: config_type
39
+ end
40
+ end
41
+
42
+ protected
43
+ def jenkins_params
44
+ CONFIGS.keys.select{|key| config.respond_to?(key)}.map{|key| [key, config.send(key)] }.to_h
45
+ end
46
+
47
+ def client
48
+ @client ||= JenkinsApi::Client.new(jenkins_params)
49
+ end
50
+ end
51
+
52
+ Lita.register_handler(JenkinsClient)
53
+ Lita.register_handler(JenkinsClient::Action)
54
+ Lita.register_handler(JenkinsClient::BaseAction)
55
+ Lita.register_handler(JenkinsClient::JobAction)
56
+ end
57
+ end
@@ -0,0 +1,13 @@
1
+ require "lita"
2
+ require "jenkins_api_client"
3
+
4
+ Lita.load_locales Dir[File.expand_path(
5
+ File.join("..", "..", "locales", "*.yml"), __FILE__
6
+ )]
7
+
8
+ require "lita/handlers/jenkins_client"
9
+
10
+ Lita::Handlers::JenkinsClient.template_root File.expand_path(
11
+ File.join("..", "..", "templates"),
12
+ __FILE__
13
+ )
@@ -0,0 +1,26 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = "lita-jenkins-client"
3
+ spec.version = "0.1.0"
4
+ spec.authors = ["Tsung-en Hsiao"]
5
+ spec.email = ["joshua841025@gmail.com"]
6
+ spec.summary = 'Lita jenkins handler that use the jenkins_api_client gem'
7
+ spec.description = 'Lita jenkins handler that use the jenkins_api_client gem'
8
+ spec.license = "MIT"
9
+ spec.metadata = { "lita_plugin_type" => "handler" }
10
+
11
+ spec.files = `git ls-files`.split($/)
12
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
13
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
14
+ spec.require_paths = ["lib"]
15
+
16
+ spec.add_runtime_dependency "lita", ">= 4.7"
17
+ spec.add_dependency "byebug"
18
+ spec.add_runtime_dependency "jenkins_api_client", '~> 1.4'
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.3"
21
+ spec.add_development_dependency "byebug"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rack-test"
24
+ spec.add_development_dependency "rspec", ">= 3.0.0"
25
+ spec.add_development_dependency "simplecov"
26
+ end
data/locales/en.yml ADDED
@@ -0,0 +1,4 @@
1
+ en:
2
+ lita:
3
+ handlers:
4
+ jenkins_client:
@@ -0,0 +1,7 @@
1
+ :server_url: "http://ci.example.com"
2
+ :username: "yourusername"
3
+ :password: "thepassword"
4
+ :log_level: 2
5
+
6
+ # pass cli tests if you use https and got 403 error
7
+ :cli_test: true
@@ -0,0 +1,43 @@
1
+ <?xml version='1.0' encoding='UTF-8'?>
2
+ <project>
3
+ <description></description>
4
+ <keepDependencies>false</keepDependencies>
5
+ <properties>
6
+ <hudson.model.ParametersDefinitionProperty>
7
+ <parameterDefinitions>
8
+ <hudson.model.BooleanParameterDefinition>
9
+ <name>bool</name>
10
+ <description></description>
11
+ <defaultValue>true</defaultValue>
12
+ </hudson.model.BooleanParameterDefinition>
13
+ <hudson.model.ChoiceParameterDefinition>
14
+ <name>choice</name>
15
+ <description></description>
16
+ <choices class="java.util.Arrays$ArrayList">
17
+ <a class="string-array">
18
+ <string>a</string>
19
+ <string>b</string>
20
+ <string>c</string>
21
+ <string>d</string>
22
+ </a>
23
+ </choices>
24
+ </hudson.model.ChoiceParameterDefinition>
25
+ <hudson.model.StringParameterDefinition>
26
+ <name>foo</name>
27
+ <description></description>
28
+ <defaultValue>bar</defaultValue>
29
+ </hudson.model.StringParameterDefinition>
30
+ </parameterDefinitions>
31
+ </hudson.model.ParametersDefinitionProperty>
32
+ </properties>
33
+ <scm class="hudson.scm.NullSCM"/>
34
+ <canRoam>true</canRoam>
35
+ <disabled>false</disabled>
36
+ <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
37
+ <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
38
+ <triggers/>
39
+ <concurrentBuild>false</concurrentBuild>
40
+ <builders/>
41
+ <publishers/>
42
+ <buildWrappers/>
43
+ </project>
@@ -0,0 +1,16 @@
1
+ <?xml version='1.0' encoding='UTF-8'?>
2
+ <project>
3
+ <description></description>
4
+ <keepDependencies>false</keepDependencies>
5
+ <properties/>
6
+ <scm class="hudson.scm.NullSCM"/>
7
+ <canRoam>true</canRoam>
8
+ <disabled>false</disabled>
9
+ <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
10
+ <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
11
+ <triggers/>
12
+ <concurrentBuild>false</concurrentBuild>
13
+ <builders/>
14
+ <publishers/>
15
+ <buildWrappers/>
16
+ </project>
@@ -0,0 +1,36 @@
1
+ require "spec_helper"
2
+ require "jenkins_api_client"
3
+
4
+ include Lita::Handlers
5
+ describe JenkinsClient::Action, lita_handler: true, additional_lita_handlers: JenkinsClient do
6
+ let(:subject_class) { Lita::Handlers::JenkinsClient::Action }
7
+
8
+ describe 'class methods' do
9
+ describe '#route_matcher' do
10
+ it 'adds a prefix to action name' do
11
+ expect(subject_class.send(:route_matcher, "foo")).to eq(Regexp.new('^jenkins foo\b', 'i'))
12
+ end
13
+
14
+ it 'also accepts array as parameter' do
15
+ expect(subject_class.send(:route_matcher, ["foo", "bar"])).to eq(Regexp.new('^jenkins foo bar\b', 'i'))
16
+ end
17
+ end
18
+ end
19
+
20
+ describe '#api_exec' do
21
+ it 'returns block output' do
22
+ expect(subject.send(:api_exec) { 'output' }).to eq('output')
23
+ end
24
+
25
+ it 'returns error message when error raised' do
26
+ expect(subject.send(:api_exec) { raise Exception, 'test error.' }).to eq('Error: test error.')
27
+ end
28
+
29
+ it 'appends an usage message after error message if usage given' do
30
+ expect(subject.send(:api_exec, 'test usage') {
31
+ raise Exception, 'test error.'
32
+ }).to eq('Error: test error. Usage: test usage')
33
+ end
34
+ end
35
+
36
+ end
@@ -0,0 +1,46 @@
1
+ require "spec_helper"
2
+ require "jenkins_api_client"
3
+
4
+ include Lita::Handlers
5
+ describe JenkinsClient::BaseAction, lita_handler: true, additional_lita_handlers: JenkinsClient do
6
+ it { is_expected.to route("jenkins version").to(:version) }
7
+ it { is_expected.to route("jenkins running?").to(:running?) }
8
+ it { is_expected.to route("jenkins cli").to(:cli) }
9
+
10
+ let! (:client) { JenkinsApi::Client.new(jenkins_config_hash) }
11
+
12
+ before do
13
+ registry.config.handlers.jenkins_client.tap do |config|
14
+ config.username = jenkins_config_hash[:username]
15
+ config.password = jenkins_config_hash[:password]
16
+ config.server_url = jenkins_config_hash[:server_url]
17
+ config.log_level = jenkins_config_hash[:log_level]
18
+ end
19
+ end
20
+
21
+ describe '#version' do
22
+ it 'replies jenkins version' do
23
+ send_message('jenkins version');
24
+ expect(replies.last).to eq(client.get_jenkins_version)
25
+ end
26
+ end
27
+
28
+ describe '#cli' do
29
+ it 'executes the Jenkins CLI', :cli_test => true do
30
+ send_message('jenkins cli list-plugins git')
31
+ expect(replies.last).to eq(client.exec_cli('list-plugins git'))
32
+ end
33
+ it 'ends when no commands provides' do
34
+ send_message('jenkins cli ')
35
+ expect(replies.last).to eq('Please provide at least one command')
36
+ end
37
+ end
38
+
39
+ describe '#running?' do
40
+ it 'returns Running when jenkins running' do
41
+ send_message('jenkins running?')
42
+ expect(replies.last).to eq("Running")
43
+ end
44
+ end
45
+
46
+ end
@@ -0,0 +1,91 @@
1
+ require "spec_helper"
2
+ require "jenkins_api_client"
3
+
4
+ include Lita::Handlers
5
+ describe JenkinsClient::BuildParam do
6
+ let (:boolean_param) { described_class.new(type: 'boolean', name: 'test', description: 'foobar', default: 'true') }
7
+ let (:string_param) { described_class.new(type: 'string', name: 'test', description: 'foobar', default: 'testdefault') }
8
+ let (:choice_param) { described_class.new(type: 'choice', name: 'test', description: 'foobar', choices: ['a', 'b', 'c']) }
9
+ describe '#initialize' do
10
+ it 'initializes boolean type param' do
11
+ expect(boolean_param.type).to eq('boolean')
12
+ expect(boolean_param.name).to eq('test')
13
+ expect(boolean_param.default).to eq('true')
14
+ expect(boolean_param.description).to eq('foobar')
15
+ end
16
+
17
+ it 'initializes string type param' do
18
+ expect(string_param.type).to eq('string')
19
+ end
20
+
21
+ it 'sets value to default' do
22
+ expect(string_param.value).to eq('testdefault')
23
+ end
24
+
25
+ it 'initializes choice type param' do
26
+ expect(choice_param.type).to eq('choice')
27
+ end
28
+
29
+ it 'set default to first choice when type is choice' do
30
+ expect(choice_param.default).to eq('a')
31
+ end
32
+
33
+ it 'raises error when type is not supported' do
34
+ expect {
35
+ described_class.new(type: 'wrong', name: 'test', description: 'foobar', choices: ['a', 'b', 'c'])
36
+ }.to raise_error(ArgumentError)
37
+ end
38
+ end
39
+
40
+ describe '#value=' do
41
+ it 'sets @value to nil if v nil' do
42
+ string_param.value = nil
43
+ expect(string_param.value).to be(nil)
44
+ end
45
+
46
+ it 'ends if input not a String' do
47
+ expect { string_param.value = 123 }.not_to change{string_param.value}
48
+ end
49
+
50
+ describe 'it sets @value according to type' do
51
+ it 'sets string' do
52
+ string_param.value = "new_string"
53
+ expect(string_param.value).to eq("new_string")
54
+ end
55
+
56
+ it 'sets boolean to true' do
57
+ boolean_param.value = "true"
58
+ expect(boolean_param.value).to be true
59
+ end
60
+
61
+ it 'sets boolean to false' do
62
+ boolean_param.value = "false"
63
+ expect(boolean_param.value).to be false
64
+ end
65
+
66
+ it 'raises error when not a boolean string' do
67
+ expect { boolean_param.value = "something wrong" }.to raise_error(ArgumentError)
68
+ end
69
+
70
+ it 'sets choice' do
71
+ choice_param.value = 'b'
72
+ expect(choice_param.value).to eq('b')
73
+ end
74
+
75
+ it 'raises error when not in choices' do
76
+ expect { choice_param.value = 'e' }.to raise_error(ArgumentError)
77
+ end
78
+
79
+ it 'raises error when type not supported' do
80
+ string_param.instance_variable_set(:@type, 'foo')
81
+ expect { string_param.value = 'raise' }.to raise_error(ArgumentError)
82
+ end
83
+ end
84
+ end
85
+
86
+ describe '#to_h' do
87
+ it 'converts BuildParam to Hash' do
88
+ expect(boolean_param.to_h).to eq({'test' => true})
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,22 @@
1
+ require "spec_helper"
2
+ require "jenkins_api_client"
3
+
4
+ include Lita::Handlers
5
+ describe JenkinsClient::Command do
6
+ describe '#initialize' do
7
+ it 'can be initialized with usage' do
8
+ command = described_class.new(name: 'testname', matcher: 'testmatcher', help: 'testhelp', usage: 'testusage')
9
+ expect(command).to be_a JenkinsClient::Command
10
+ end
11
+
12
+ it 'can be initialized without usage' do
13
+ command = described_class.new(name: 'testname', matcher: 'testmatcher', help: 'testhelp')
14
+ expect(command).to be_a JenkinsClient::Command
15
+ end
16
+
17
+ it 'has @usage equals to @name when no usage param provided' do
18
+ command = described_class.new(name: 'testname', matcher: 'testmatcher', help: 'testhelp')
19
+ expect(command.usage).to eq(command.name)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,36 @@
1
+ require "spec_helper"
2
+ require "jenkins_api_client"
3
+
4
+ include Lita::Handlers
5
+ describe JenkinsClient, lita_handler: true do
6
+ describe '#jenkins_params' do
7
+ before do
8
+ registry.config.handlers.jenkins_client.tap do |config|
9
+ config.username = jenkins_config_hash[:username]
10
+ config.password = jenkins_config_hash[:password]
11
+ config.server_url = jenkins_config_hash[:server_url]
12
+ end
13
+ end
14
+
15
+ it 'maps config to a hash param' do
16
+ expect(subject.send(:jenkins_params)[:username]).to eq(jenkins_config_hash[:username])
17
+ expect(subject.send(:jenkins_params)[:password]).to eq(jenkins_config_hash[:password])
18
+ expect(subject.send(:jenkins_params)[:server_url]).to eq(jenkins_config_hash[:server_url])
19
+ end
20
+
21
+ it 'validates boolean value if config_type is Object' do
22
+ expect {
23
+ registry.config.handlers.jenkins_client.tap do |config|
24
+ config.ssl = 123
25
+ end
26
+ }.to raise_error(SystemExit)
27
+ end
28
+
29
+ describe '#client' do
30
+ it 'creates a client instance from the param hash' do
31
+ expect(subject.send(:client)).to be_a JenkinsApi::Client
32
+ end
33
+ end
34
+ end
35
+
36
+ end
@@ -0,0 +1,110 @@
1
+ require "spec_helper"
2
+ require "jenkins_api_client"
3
+
4
+ include Lita::Handlers
5
+ describe JenkinsClient::JobAction, lita_handler: true, additional_lita_handlers: JenkinsClient do
6
+ it { is_expected.to route('jenkins job all').to(:all) }
7
+ it { is_expected.to route('jenkins job list').to(:list) }
8
+ it { is_expected.to route('jenkins job status').to(:status) }
9
+ it { is_expected.to route('jenkins job build').to(:build) }
10
+ it { is_expected.to route('jenkins job exists?').to(:exists?) }
11
+ it { is_expected.to route('jenkins job params').to(:params) }
12
+ let! (:client) { JenkinsApi::Client.new(jenkins_config_hash) }
13
+
14
+ before do
15
+ registry.config.handlers.jenkins_client.tap do |config|
16
+ config.username = jenkins_config_hash[:username]
17
+ config.password = jenkins_config_hash[:password]
18
+ config.server_url = jenkins_config_hash[:server_url]
19
+ config.log_level = jenkins_config_hash[:log_level]
20
+ end
21
+ end
22
+
23
+ describe '#all' do
24
+ it 'returns list of jobs' do
25
+ send_message('jenkins job all')
26
+ expect(replies.last).to eq(client.job.list_all.inspect)
27
+ end
28
+ end
29
+
30
+
31
+ describe '#build' do
32
+ it 'ends when no input' do
33
+ send_message('jenkins job build')
34
+ expect(replies.last).to eq("please provide a job name")
35
+ end
36
+
37
+ it 'ends when job not exists' do
38
+ send_message('jenkins job build not_exist')
39
+ expect(replies.last).to eq("job not_exist not exists")
40
+ end
41
+
42
+ it 'builds job without parameters' do
43
+ send_message('jenkins job build test_without_params')
44
+ expect(replies.last).to eq("Job created. (http status 201)")
45
+ end
46
+
47
+ it 'builds job with parameters' do
48
+ send_message('jenkins job build test_with_params bool:true choice:c foo:foobar')
49
+ expect(replies.last).to eq("Job created. (http status 201)")
50
+ end
51
+
52
+ it 'reply error message when ArgumentError' do
53
+ send_message('jenkins job build test_with_params bool:abc choice:c foo:foobar')
54
+ expect(replies.last).to eq('Error: bool should be true or false')
55
+ end
56
+ end
57
+
58
+ describe '#parse_build_params' do
59
+ let(:build_params) { build_params = [{:type=>"boolean", :name=>"bool", :description=>"", :default=>"true"}, {:type=>"choice", :name=>"choice", :description=>"", :choices=>["a", "b", "c", "d"]}, {:type=>"string", :name=>"foo", :description=>"", :default=>"bar"}] }
60
+ let(:params) { {'bool' => true, 'choice' => 'c', 'foo' => 'foobar'} }
61
+
62
+ it 'parses valid args to valid build params' do
63
+ hash_args = {'bool' => 'true', 'choice' => 'c', 'foo' => 'foobar'}
64
+ expect(subject.send(:parse_build_params, hash_args, build_params)).to eq(params)
65
+ end
66
+ end
67
+
68
+ describe '#key_value_pair' do
69
+ it 'parses key:value string pairs to hash' do
70
+ input = ['foo:bar', 'test:true', 'ignored']
71
+ expect(subject.send(:key_value_pair, input)).to eq({'foo' => 'bar', 'test' => 'true'})
72
+ end
73
+ end
74
+
75
+ describe '#params' do
76
+ it 'obtains job info' do
77
+ send_message('jenkins job params test_with_params')
78
+ expect(replies.last).to eq(client.job.get_build_params('test_with_params').inspect)
79
+ end
80
+
81
+ it 'ends when no input' do
82
+ send_message('jenkins job params')
83
+ expect(replies.last).to eq('please provide a job name')
84
+ end
85
+ end
86
+
87
+ describe '#exists?' do
88
+ it 'shows if job exists' do
89
+ send_message('jenkins job exists? test_with_params')
90
+ expect(replies.last).to eq(client.job.exists?('test_with_params').inspect)
91
+ end
92
+
93
+ it 'ends when no input' do
94
+ send_message('jenkins job exists?')
95
+ expect(replies.last).to eq('please provide a job name')
96
+ end
97
+ end
98
+
99
+ describe '#status' do
100
+ it 'shows job status' do
101
+ send_message('jenkins job status test_with_params')
102
+ expect(replies.last).to eq(client.job.get_current_build_status('test_with_params').inspect)
103
+ end
104
+
105
+ it 'ends when no input' do
106
+ send_message('jenkins job exists?')
107
+ expect(replies.last).to eq('please provide a job name')
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,46 @@
1
+ require 'simplecov'
2
+ SimpleCov.start do
3
+ add_filter "/spec/"
4
+ end
5
+
6
+ require "lita-jenkins-client"
7
+ require "lita/rspec"
8
+ require "psych"
9
+ require "byebug"
10
+
11
+ # A compatibility mode is provided for older plugins upgrading from Lita 3. Since this plugin
12
+ # was generated with Lita 4, the compatibility mode should be left disabled.
13
+ Lita.version_3_compatibility_mode = false
14
+
15
+ def jenkins_config_hash
16
+ @config ||= Psych.load_file('spec/config.yml')
17
+ end
18
+
19
+ def set_test_jobs
20
+ client = JenkinsApi::Client.new(jenkins_config_hash)
21
+ client.job.create_or_update('test_with_params', File.read('spec/fixtures/test_with_params.xml'))
22
+ client.job.create_or_update('test_without_params', File.read('spec/fixtures/test_without_params.xml'))
23
+ client.job.create_or_update('test_without_params2', File.read('spec/fixtures/test_without_params.xml'))
24
+ end
25
+
26
+ def delete_test_jobs
27
+ client = JenkinsApi::Client.new(jenkins_config_hash)
28
+ client.job.delete('test_with_params')
29
+ client.job.delete('test_without_params')
30
+ client.job.delete('test_without_params2')
31
+ end
32
+
33
+ RSpec.configure do |config|
34
+ unless jenkins_config_hash[:cli_test]
35
+ config.filter_run_excluding :cli_test => true
36
+ end
37
+
38
+ config.before(:suite) do
39
+ set_test_jobs
40
+ end
41
+
42
+ config.after(:suite) do
43
+ delete_test_jobs
44
+ end
45
+ end
46
+
File without changes
metadata ADDED
@@ -0,0 +1,208 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lita-jenkins-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tsung-en Hsiao
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-08-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: lita
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: byebug
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: jenkins_api_client
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.4'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.4'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rack-test
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: 3.0.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: 3.0.0
125
+ - !ruby/object:Gem::Dependency
126
+ name: simplecov
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: Lita jenkins handler that use the jenkins_api_client gem
140
+ email:
141
+ - joshua841025@gmail.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".gitignore"
147
+ - Gemfile
148
+ - README.md
149
+ - Rakefile
150
+ - lib/lita-jenkins-client.rb
151
+ - lib/lita/handlers/jenkins_client.rb
152
+ - lib/lita/handlers/jenkins_client/action.rb
153
+ - lib/lita/handlers/jenkins_client/base_action.rb
154
+ - lib/lita/handlers/jenkins_client/build_param.rb
155
+ - lib/lita/handlers/jenkins_client/command.rb
156
+ - lib/lita/handlers/jenkins_client/job_action.rb
157
+ - lib/lita/handlers/jenkins_client/plugin_action.rb
158
+ - lib/lita/handlers/jenkins_client/queue_action.rb
159
+ - lib/lita/handlers/jenkins_client/system_action.rb
160
+ - lita-jenkins-client.gemspec
161
+ - locales/en.yml
162
+ - spec/config.yml.example
163
+ - spec/fixtures/test_with_params.xml
164
+ - spec/fixtures/test_without_params.xml
165
+ - spec/lita/handlers/action_spec.rb
166
+ - spec/lita/handlers/base_action_spec.rb
167
+ - spec/lita/handlers/build_param_spec.rb
168
+ - spec/lita/handlers/command_spec.rb
169
+ - spec/lita/handlers/jenkins_client_spec.rb
170
+ - spec/lita/handlers/job_action_spec.rb
171
+ - spec/spec_helper.rb
172
+ - templates/.gitkeep
173
+ homepage:
174
+ licenses:
175
+ - MIT
176
+ metadata:
177
+ lita_plugin_type: handler
178
+ post_install_message:
179
+ rdoc_options: []
180
+ require_paths:
181
+ - lib
182
+ required_ruby_version: !ruby/object:Gem::Requirement
183
+ requirements:
184
+ - - ">="
185
+ - !ruby/object:Gem::Version
186
+ version: '0'
187
+ required_rubygems_version: !ruby/object:Gem::Requirement
188
+ requirements:
189
+ - - ">="
190
+ - !ruby/object:Gem::Version
191
+ version: '0'
192
+ requirements: []
193
+ rubyforge_project:
194
+ rubygems_version: 2.5.1
195
+ signing_key:
196
+ specification_version: 4
197
+ summary: Lita jenkins handler that use the jenkins_api_client gem
198
+ test_files:
199
+ - spec/config.yml.example
200
+ - spec/fixtures/test_with_params.xml
201
+ - spec/fixtures/test_without_params.xml
202
+ - spec/lita/handlers/action_spec.rb
203
+ - spec/lita/handlers/base_action_spec.rb
204
+ - spec/lita/handlers/build_param_spec.rb
205
+ - spec/lita/handlers/command_spec.rb
206
+ - spec/lita/handlers/jenkins_client_spec.rb
207
+ - spec/lita/handlers/job_action_spec.rb
208
+ - spec/spec_helper.rb