Class: Jenkins2::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/jenkins2/connection.rb

Overview

Creates a “connection” to Jenkins and makes all the HTTP requests.

Instance Method Summary collapse

Constructor Details

#initialize(server) ⇒ Connection

Parameters:

server

Jenkins Server URL.



12
13
14
15
# File 'lib/jenkins2/connection.rb', line 12

def initialize(server)
	@server = server
	@crumb = nil
end

Instance Method Details

#api_request(method, uri, body = nil, headers = nil) {|req| ... } ⇒ Object

Makes request, using method provided. Yields request, if block given. Retries request with updated crumbs, if “No valid crumbs” error received.

Parameters:

method

Net:HTTP class to instantiate (e.g. Net::HTTP::Post, Net::HTTP::Get)

url

Absolute URI to make request to.

body

Request body if applicable.

headers

Request headers.

Returns:

Net::HTTP::Response

Yields:

  • (req)


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/jenkins2/connection.rb', line 94

def api_request(method, uri, body=nil, headers=nil)
	req = method.new(URI(uri), headers)
	req.basic_auth @user, @key if @user and @key
	req.body = body
	yield req if block_given?
	Net::HTTP.start(req.uri.hostname, req.uri.port,
		use_ssl: req.uri.scheme == 'https', verify_mode: OpenSSL::SSL::VERIFY_NONE) do |http|
		begin
			req[@crumb['crumbRequestField']] = @crumb['crumb'] if @crumb
			Log.debug{ "Request uri: #{req.uri}" }
			Log.debug{ "Request content_type: #{req.content_type}, body: #{req.body}" }
			response = http.request req
			handle_response response
		rescue Jenkins2::NoValidCrumbMatcher
			update_crumbs
			retry
		end
	end
end

#basic_auth(user, key) ⇒ Object

Add basic auth to existing connection. Returns self.

Parameters:

user

Jenkins API user.

key

Jenkins API key.



21
22
23
24
# File 'lib/jenkins2/connection.rb', line 21

def basic_auth(user, key)
	@user, @key = user, key
	self
end

#build_uri(relative_or_absolute, params = {}) ⇒ Object

Builds URI. Appends parameters.

Parameters:

relative_or_absolute

Absolute URI or URI path. If path, Jenkins server will be prepended.

params

Parameter hash. It will be converted to URL parameters.

Returns:

Absolute URI with parameters



78
79
80
81
82
83
# File 'lib/jenkins2/connection.rb', line 78

def build_uri(relative_or_absolute, params={})
	result = ::URI.parse relative_or_absolute
	result = ::URI.parse ::File.join(@server.to_s, relative_or_absolute) unless result.absolute?
	result.query = ::URI.encode_www_form params
	result
end

#get(path, params = {}, &block) ⇒ Object

Makes GET request. Yields request, if block given.

Parameters:

path

Path to make request to.

params

Parameter hash. It will be converted to URL parameters.

&block

Yields Net::HTTP::Get request.

Returns:

Net::HTTP::Response



44
45
46
# File 'lib/jenkins2/connection.rb', line 44

def get(path, params={}, &block)
	api_request(Net::HTTP::Get, build_uri(path, params), &block)
end

#get_json(path, params = {}, &block) ⇒ Object

Appends api/json to passed path and makes GET request. Yields request, if block given.

Parameters:

path

Path to make request to. “api/json” will be appened to the end.

params

Parameter hash. It will be converted to URL parameters.

&block

Yields Net::HTTP::Get request.

Returns:

Net::HTTP::Response



33
34
35
# File 'lib/jenkins2/connection.rb', line 33

def get_json(path, params={}, &block)
	get(::File.join(path, 'api/json'), params, &block)
end

#handle_response(response) ⇒ Object

Handles Jenkins response. Tries parsing error messages in some cases, then reraises exception from Jenkins2 namespace.

Returns:

HTTPResponse

Raises:

Different Net::HTTP and Jenkins2 errors.



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/jenkins2/connection.rb', line 127

def handle_response(response)
	Log.debug{ "Response: #{response.code}, #{response.body}" }
	case response
	when Net::HTTPNotFound
		raise Jenkins2::NotFoundError, response
	when Net::HTTPBadRequest
		raise Jenkins2::BadRequestError, response
	when Net::HTTPServiceUnavailable
		raise Jenkins2::ServiceUnavailableError, response
	when Net::HTTPInternalServerError
		raise Jenkins2::InternalServerError, response
	when Net::HTTPClientError, Net::HTTPServerError # 4XX, 5XX
		response.value
	else
		response
	end
end

#head(path, params = {}, &block) ⇒ Object

Makes HEAD request. Yields request, if block given.

Parameters:

path

Path to make request to.

params

Parameter hash. It will be converted to URL parameters.

&block

Yields Net::HTTP::Head request.

Returns:

Net::HTTP::Response



55
56
57
# File 'lib/jenkins2/connection.rb', line 55

def head(path, params={}, &block)
	api_request(Net::HTTP::Head, build_uri(path, params), &block)
end

#post(path, body = nil, params = {}, &block) ⇒ Object

Makes POST request. Yields request, if block given.

Parameters:

path

Path to make request to.

body

Post request body.

params

Parameter hash. It will be converted to URL parameters.

&block

Yields Net::HTTP::Post request.

Returns:

Net::HTTP::Response



67
68
69
70
# File 'lib/jenkins2/connection.rb', line 67

def post(path, body=nil, params={}, &block)
	headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }
	api_request(Net::HTTP::Post, build_uri(path, params), body, headers, &block)
end

#update_crumbsObject

Updates crumbs for current connection. These crumbs will be submitted with all requests.

Returns:

Crumbs as hash



117
118
119
# File 'lib/jenkins2/connection.rb', line 117

def update_crumbs
	@crumb = JSON.parse(get_json('/crumbIssuer').body)
end