From 397ef0264b7b308014943f1ea0c88ba1a6499443 Mon Sep 17 00:00:00 2001 From: Spike Grobstein Date: Wed, 15 Jan 2014 11:26:38 -0500 Subject: [PATCH 1/2] support username/password in URL given that the URL is structured like: http://username:password@hostname.example.com use that username/password combination rather than requiring explicit :username and :password options in the JenkinsApi::Client initializer. --- lib/jenkins_api_client/client.rb | 20 +++++++++++--------- spec/unit_tests/client_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/lib/jenkins_api_client/client.rb b/lib/jenkins_api_client/client.rb index 22c0d475..88b2c0a0 100644 --- a/lib/jenkins_api_client/client.rb +++ b/lib/jenkins_api_client/client.rb @@ -105,6 +105,17 @@ def initialize(args) " to Jenkins" end + # Get info from the server_url, if we got one + if @server_url + server_uri = URI.parse(@server_url) + @server_ip = server_uri.host + @server_port = server_uri.port + @ssl = server_uri.scheme == "https" + @jenkins_path = server_uri.path + @username ||= server_uri.user + @password ||= server_uri.password + end + # Username/password are optional as some jenkins servers do not require # authentication if @username && !(@password || @password_base64) @@ -115,15 +126,6 @@ def initialize(args) " both left nil" end - # Get info from the server_url, if we got one - if @server_url - server_uri = URI.parse(@server_url) - @server_ip = server_uri.host - @server_port = server_uri.port - @ssl = server_uri.scheme == "https" - @jenkins_path = server_uri.path - end - @jenkins_path ||= "" @jenkins_path.gsub!(/\/$/,"") # remove trailing slash if there is one @server_port = DEFAULT_SERVER_PORT unless @server_port diff --git a/spec/unit_tests/client_spec.rb b/spec/unit_tests/client_spec.rb index 829e4dd7..19485c5d 100644 --- a/spec/unit_tests/client_spec.rb +++ b/spec/unit_tests/client_spec.rb @@ -49,6 +49,26 @@ ).not_to raise_error end + it "initializes the username and password from server_url" do + client = JenkinsApi::Client.new( + :server_url => 'http://someuser:asdf@localhost' + ) + + client.instance_variable_get('@username').should == 'someuser' + client.instance_variable_get('@password').should == 'asdf' + end + + it "uses explicit username, password over one in the server_url" do + client = JenkinsApi::Client.new( + :server_url => 'http://someuser:asdf@localhost', + :username => 'otheruser', + :password => '1234' + ) + + client.instance_variable_get('@username').should == 'otheruser' + client.instance_variable_get('@password').should == '1234' + end + it "initializes with proxy args without exception" do expect( lambda do From 7eb2ec0cbe360e0720f1fea159759dfded9a78c2 Mon Sep 17 00:00:00 2001 From: Spike Grobstein Date: Wed, 15 Jan 2014 12:26:59 -0500 Subject: [PATCH 2/2] added some comments to document --- lib/jenkins_api_client/client.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/jenkins_api_client/client.rb b/lib/jenkins_api_client/client.rb index 88b2c0a0..ea6267d8 100644 --- a/lib/jenkins_api_client/client.rb +++ b/lib/jenkins_api_client/client.rb @@ -69,7 +69,7 @@ class Client # # @option args [String] :server_ip the IP address of the Jenkins CI server # @option args [String] :server_port the port on which the Jenkins listens - # @option args [String] :server_url the full URL address of the Jenkins CI server (http/https) + # @option args [String] :server_url the full URL address of the Jenkins CI server (http/https). This can include username/password. :username/:password options will override any user/pass value in the URL # @option args [String] :username the username used for connecting to the server (optional) # @option args [String] :password the password or API Key for connecting to the CI server (optional) # @option args [String] :password_base64 the password with base64 encoded format for connecting to the CI @@ -112,6 +112,9 @@ def initialize(args) @server_port = server_uri.port @ssl = server_uri.scheme == "https" @jenkins_path = server_uri.path + + # read username and password from the URL + # only set if @username and @password are not already set via explicit options @username ||= server_uri.user @password ||= server_uri.password end