jenkins2-api 1.0.3 → 1.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: f548e9981de4f48fd4bc914b6679f0432c0e1261
4
- data.tar.gz: 843887cb04a375b2ae75eb09f9ac5f8d8e586c99
3
+ metadata.gz: e066522b8efd20bd5b1f6a70d0d9b9f3e2ac48c4
4
+ data.tar.gz: 2481dbf92e587438d66f036210234966d4969c13
5
5
  SHA512:
6
- metadata.gz: 6b342762cced5d396f367b628d0a418fdc4bdc5a0a2dfe4785f3a99440b1084de0109cacf6d6771f736c1ff403f6bc0c77aa93999b9005c834dafb7422e6ba61
7
- data.tar.gz: 2414d7813cd1bf923388b0aed30615b417a7161950705f844164a952cfd99d9c77b7917b13a4105ca3948b1bea7bcba8730a3518932ff171f028d7f4465b91aa
6
+ metadata.gz: 74638d02bab0f644794e90004860ec502e40050dbd81af413d9642b0e9b052f14bdbfb45ee0eb161f5a56ef2322803c51611bc7013c52e748fabf4c3f8999b2d
7
+ data.tar.gz: 05afbcf3189101deb7922d252d6113a2a13aecba2ebe097faad651b4c1b2418cbb725df16d3b55454713c2634eaec296e812a43221be3f4da205f4eb9e195f98
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  [![Gem Version](https://badge.fury.io/rb/jenkins2-api.svg)](https://badge.fury.io/rb/jenkins2-api)
2
+ [![Build Status](https://travis-ci.org/Yitsushi/jenkins2-api.svg?branch=master)](https://travis-ci.org/Yitsushi/jenkins2-api)
2
3
 
3
4
  # API Client for Jenkins2
4
5
 
data/bin/jenkins2api CHANGED
@@ -10,15 +10,30 @@ module Jenkins2API
10
10
  class Jenkins2 < Thor
11
11
  map %w[--version -v] => :__print_version
12
12
 
13
- desc "--version, -v", "print the version"
13
+ desc '--version, -v', 'print the version'
14
14
  # Prints the current version number
15
15
  def __print_version
16
16
  puts Jenkins2API::VERSION
17
17
  end
18
18
 
19
- register(Jenkins2API::Command::Job, 'job', 'job <command>', 'Job related commands')
20
- register(Jenkins2API::Command::Build, 'build', 'build <command>', 'Build related commands')
21
- register(Jenkins2API::Command::Node, 'node', 'node <command>', 'Node related commands')
19
+ register(
20
+ Jenkins2API::Command::Job,
21
+ 'job',
22
+ 'job <command>',
23
+ 'Job related commands'
24
+ )
25
+ register(
26
+ Jenkins2API::Command::Build,
27
+ 'build',
28
+ 'build <command>',
29
+ 'Build related commands'
30
+ )
31
+ register(
32
+ Jenkins2API::Command::Node,
33
+ 'node',
34
+ 'node <command>',
35
+ 'Node related commands'
36
+ )
22
37
  end
23
38
  end
24
39
  end
data/lib/client.rb CHANGED
@@ -19,31 +19,35 @@ module Jenkins2API
19
19
  # Throws an +ArgumentError+ if username is specified
20
20
  # but password is empty
21
21
  def initialize(**options)
22
- @server = options[:server] || 'http://127.0.0.1/'
22
+ @server = options[:server] || 'http://127.0.0.1/'
23
23
  @username = options[:username]
24
24
  @password = options[:password]
25
25
 
26
- if @username && !@password
27
- raise ArgumentError, "If username is provided, password is required"
28
- end
26
+ return if @username && @password
27
+
28
+ raise ArgumentError, 'If username is provided, password is required'
29
29
  end
30
30
 
31
- # Job related endpoints. Creates new +Jenkins2API::Endpoint::Job+ instance
31
+ # Job related endpoints.
32
+ # Creates new +Jenkins2API::Endpoint::Job+ instance
32
33
  def job
33
34
  @job ||= Endpoint::Job.new(self)
34
35
  end
35
36
 
36
- # Build related endpoints. Creates new +Jenkins2API::Endpoint::Build+ instance
37
+ # Build related endpoints.
38
+ # Creates new +Jenkins2API::Endpoint::Build+ instance
37
39
  def build
38
40
  @build ||= Endpoint::Build.new(self)
39
41
  end
40
42
 
41
- # Artifact related endpoints. Creates new +Jenkins2API::Endpoint::Artifact+ instance
43
+ # Artifact related endpoints.
44
+ # Creates new +Jenkins2API::Endpoint::Artifact+ instance
42
45
  def artifact
43
46
  @artifact ||= Endpoint::Artifact.new(self)
44
47
  end
45
48
 
46
- # Node/Computer related endpoints. Creates new +Jenkins2API::Endpoint::Node+ instance
49
+ # Node/Computer related endpoints.
50
+ # Creates new +Jenkins2API::Endpoint::Node+ instance
47
51
  def node
48
52
  @node ||= Endpoint::Node.new(self)
49
53
  end
@@ -56,36 +60,45 @@ module Jenkins2API
56
60
  # +response_type+:: +:json+ or +:raw+
57
61
  # +opts+:: sym options to pass to the endpoint. Applicable only if +:post+
58
62
  def api_request(method, path, response_type = :json, **opts)
59
- response_type = :json unless [:json, :raw].include?(response_type)
63
+ req = new_request(method, path, response_type, opts)
64
+ req.basic_auth @username, @password
65
+
66
+ yield req if block_given?
67
+
68
+ req.content_type ||= 'application/x-www-form-urlencoded'
69
+ response = Net::HTTP.start(req.uri.hostname, req.uri.port) do |http|
70
+ http.request(req)
71
+ end
72
+
73
+ handle_response(response, response_type)
74
+ end
75
+
76
+ # Creates a new request for the API call
77
+ # with default and required values
78
+ def new_request(method, path, response_type, **opts)
79
+ response_type = :json unless %i[json raw].include?(response_type)
60
80
 
61
81
  parts = [@server, URI.escape(path)]
62
82
  parts << 'api/json' if response_type == :json
63
83
  uri = URI(File.join(parts))
64
- uri.query = URI.encode_www_form(opts)
84
+ uri.query = URI.encode_www_form(opts) if method == :post
65
85
 
66
- req = case method
67
- when :get then Net::HTTP::Get
68
- when :post then Net::HTTP::Post
86
+ case method
87
+ when :get then Net::HTTP::Get
88
+ when :post then Net::HTTP::Post
69
89
  end.new(uri)
90
+ end
70
91
 
71
- req.basic_auth @username, @password
72
- yield req if block_given?
73
- req.content_type ||= 'application/x-www-form-urlencoded'
74
- response = Net::HTTP.start(req.uri.hostname, req.uri.port) { |http| http.request req }
75
-
76
- case response
77
- when Net::HTTPSuccess
78
- if response_type == :json
79
- JSON.parse(response.body)
80
- else
81
- response.body
82
- end
83
- when Net::HTTPRedirection
84
- puts "Redirect: #{response['location']}"
85
- response['location']
86
- else
87
- puts "Response: #{response.code}, #{response.body}"
88
- response.value
92
+ # Handles response based on response_type
93
+ def handle_response(response, response_type)
94
+ return response['location'] if response.is_a?(Net::HTTPRedirection)
95
+
96
+ response.value unless response.is_a?(Net::HTTPSuccess)
97
+
98
+ if response_type == :json
99
+ JSON.parse(response.body)
100
+ else
101
+ response.body
89
102
  end
90
103
  end
91
104
  end
@@ -5,12 +5,16 @@ module Jenkins2API
5
5
  module Command
6
6
  # Contains all the commands under +build+ namespace
7
7
  class Build < Jenkins2API::ThorCommand
8
- desc 'slave-name JOB_NAME BUILD_ID', 'Get Node name where a specific build was running'
9
- method_option :ec2id, :default => false, :type => :boolean
8
+ desc 'slave-name JOB_NAME BUILD_ID', 'Get Node name for a specific build'
9
+ method_option :ec2id, default: false, type: :boolean
10
10
  # Displays the name of the slave where the build was executed
11
11
  def slave_name(name, build_id)
12
12
  slave_name = client.build.slave_name(name, build_id)
13
- slave_name = slave_name.match(/(i-[0-9a-zA-Z]+)/).captures.first if options[:ec2id]
13
+ if options[:ec2id]
14
+ slave_name = slave_name.match(/(i-[0-9a-zA-Z]+)/)
15
+ .captures
16
+ .first
17
+ end
14
18
 
15
19
  puts slave_name
16
20
  end
data/lib/commands/job.rb CHANGED
@@ -8,4 +8,3 @@ module Jenkins2API
8
8
  end
9
9
  end
10
10
  end
11
-
data/lib/commands/node.rb CHANGED
@@ -5,29 +5,29 @@ module Jenkins2API
5
5
  module Command
6
6
  # Contains all the commands under +node+ namespace
7
7
  class Node < Jenkins2API::ThorCommand
8
+ # Jenkins java class of the master instance
9
+ MASTER_CLASS = 'hudson.model.Hudson$MasterComputer'.freeze
8
10
 
9
- desc :all, "List all nodes"
11
+ desc :all, 'List all nodes'
10
12
  # List all available nodes
11
13
  def all
12
14
  nodes = client.node.all
13
15
  nodes['computer'].each do |computer|
14
16
  type = 'slave'
15
- type = 'master' if computer['_class'] == 'hudson.model.Hudson$MasterComputer'
16
- puts "[%5s] #{computer['displayName']}" % [type]
17
+ type = 'master' if computer['_class'] == MASTER_CLASS
18
+ printf("[%6s] %s\n", type, computer['displayName'])
17
19
  end
18
20
  end
19
21
 
20
- desc :slaves, "List all slave nodes"
22
+ desc :slaves, 'List all slave nodes'
21
23
  # List all avilable slaves
22
24
  def slaves
23
25
  nodes = client.node.all
24
26
  nodes['computer'].each do |computer|
25
- next if computer['_class'] == 'hudson.model.Hudson$MasterComputer'
27
+ next if computer['_class'] == MASTER_CLASS
26
28
  puts computer['displayName']
27
29
  end
28
30
  end
29
31
  end
30
32
  end
31
33
  end
32
-
33
-
@@ -11,7 +11,7 @@ module Jenkins2API
11
11
  # +name+:: Job name
12
12
  # +build_id+:: ID of the build
13
13
  def all(name, build_id)
14
- @client.build.get(name)['artifacts']
14
+ @client.build.get(name, build_id)['artifacts']
15
15
  end
16
16
 
17
17
  # Download a specific artifact.
@@ -19,11 +19,16 @@ module Jenkins2API
19
19
  # Params:
20
20
  # +name+:: Job name
21
21
  # +build_id+:: ID of the build
22
- # +artifact+:: artifact +Hash+. this function uses only the +relativePath+ property
22
+ # +artifact+:: artifact +Hash+.
23
+ # This function uses only the +relativePath+ property
23
24
  #
24
25
  # Returns with the content of the artifact
25
26
  def get(name, build_id, artifact)
26
- @client.api_request(:get, "/job/#{name}/#{build_id}/artifact/#{artifact['relativePath']}", :raw)
27
+ @client.api_request(
28
+ :get,
29
+ "/job/#{name}/#{build_id}/artifact/#{artifact['relativePath']}",
30
+ :raw
31
+ )
27
32
  end
28
33
  end
29
34
  end
@@ -40,7 +40,11 @@ module Jenkins2API
40
40
  # Return an array of strings.
41
41
  # Each item in that array is a line from the log
42
42
  def logtext_lines(name, build_id)
43
- @client.api_request(:get, "/job/#{name}/#{build_id}/logText/progressiveText", :raw).split("\r\n")
43
+ @client.api_request(
44
+ :get,
45
+ "/job/#{name}/#{build_id}/logText/progressiveText",
46
+ :raw
47
+ ).split("\r\n")
44
48
  end
45
49
 
46
50
  # Get the name of the slave where the build was executed
@@ -50,17 +54,33 @@ module Jenkins2API
50
54
  # +build_id+:: ID of the build
51
55
  def slave_name(name, build_id)
52
56
  log = logtext_lines(name, build_id)
53
- relevant_line = log.select { |line| line.match(/^Running on /) }.first
54
57
 
55
- name = relevant_line.match(/^Running on (.*) in \//).captures.first rescue nil
58
+ line = find_line(log, /^Running on /)
59
+ name = first_match(line, %r{^Running on (.*) in /})
56
60
 
57
61
  if name.nil?
58
- relevant_line = log.select { |line| line.match(/Building remotely on/) }.first
59
- name = relevant_line.match(/Building remotely on (.*) in workspace/).captures.first
62
+ line = find_line(log, /Building remotely on/)
63
+ name = first_match(line, /Building remotely on (.*) in workspace/)
60
64
  end
61
65
 
62
66
  name
63
67
  end
68
+
69
+ private
70
+
71
+ # find the first item in a string array
72
+ # that matches for a given regular expression
73
+ def find_line(lines, expression)
74
+ lines.select { |line| line.match(expression) }.first
75
+ end
76
+
77
+ # return the first capture block from a string
78
+ # that matches for a given regular expression
79
+ def first_match(line, expression)
80
+ line.match(expression).captures.first
81
+ rescue
82
+ nil
83
+ end
64
84
  end
65
85
  end
66
86
  end
data/lib/endpoints/job.rb CHANGED
@@ -7,7 +7,7 @@ module Jenkins2API
7
7
  class Job < BaseEndpoint
8
8
  # Lists all available jobs
9
9
  def list
10
- @client.api_request(:get, "")['jobs']
10
+ @client.api_request(:get, '')['jobs']
11
11
  end
12
12
 
13
13
  # Get all available builds for a specific job
@@ -22,4 +22,3 @@ module Jenkins2API
22
22
  end
23
23
  end
24
24
  end
25
-
@@ -9,9 +9,8 @@ module Jenkins2API
9
9
  #
10
10
  # Returns with slaves and masters also
11
11
  def all
12
- @client.api_request(:get, "/computer")
12
+ @client.api_request(:get, '/computer')
13
13
  end
14
14
  end
15
15
  end
16
16
  end
17
-
data/lib/thor_command.rb CHANGED
@@ -5,11 +5,12 @@ module Jenkins2API
5
5
  # Wrapper class for commands. Checks if credentials are passed or not
6
6
  # and creates a new +Jenkins2API::Client+ instance for commands.
7
7
  class ThorCommand < Thor
8
- class_option :password, :desc => "Password", :aliases => "-p", :required => false
9
- class_option :username, :desc => "Username", :aliases => "-u", :required => false
10
- class_option :server, :desc => "Server path", :aliases => "-s", :required => false
8
+ class_option :password, desc: 'Password', aliases: '-p', required: false
9
+ class_option :username, desc: 'Username', aliases: '-u', required: false
10
+ class_option :server, desc: 'Server path', aliases: '-s', required: false
11
11
 
12
12
  private
13
+
13
14
  # Get or create a new client
14
15
  def client
15
16
  check_option(:server, 'JENKINS_SERVER')
@@ -17,9 +18,9 @@ module Jenkins2API
17
18
  check_option(:password, 'JENKINS_PASSWORD')
18
19
 
19
20
  @client ||= Jenkins2API::Client.new(
20
- :server => options[:server],
21
- :username => options[:username],
22
- :password => options[:password]
21
+ server: options[:server],
22
+ username: options[:username],
23
+ password: options[:password]
23
24
  )
24
25
  end
25
26
 
@@ -27,11 +28,11 @@ module Jenkins2API
27
28
  def check_option(name, env_name)
28
29
  options[name] ||= ENV.fetch(env_name, '')
29
30
 
30
- unless options.has_key?(name.to_s) && options[name] != ''
31
- fail Thor::Error, "#{name} is not defined. " \
32
- "You can specify with --#{name} option " \
33
- "or '#{env_name}' environment variable."
34
- end
31
+ return if options.key?(name.to_s) && options[name] != ''
32
+
33
+ raise Thor::Error, "#{name} is not defined. " \
34
+ "You can specify with --#{name} option " \
35
+ "or '#{env_name}' environment variable."
35
36
  end
36
37
  end
37
38
  end
data/lib/version.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  module Jenkins2API
2
2
  # Current version to publish
3
3
  # Gemspec also uses this constant
4
- VERSION = '1.0.3'
4
+ VERSION = '1.0.4'.freeze
5
5
  end
6
-
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jenkins2-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Balazs Nadasdi
@@ -24,6 +24,90 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0.19'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '12.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '12.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rdoc
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.5'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.5'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sinatra
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.4'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.4'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.48'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.48'
27
111
  description: API client for Jenkins 2 with executable
28
112
  email: balazs.nadasdi@cheppers.com
29
113
  executables: