Skip to content

Commit

Permalink
Merge pull request #118 from spikegrobstein/feature/read-credentials-…
Browse files Browse the repository at this point in the history
…from-url

read username/password from server_url
  • Loading branch information
arangamani committed Jan 15, 2014
2 parents 5830920 + 7eb2ec0 commit d830e31
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
25 changes: 15 additions & 10 deletions lib/jenkins_api_client/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -105,6 +105,20 @@ 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

# 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

# Username/password are optional as some jenkins servers do not require
# authentication
if @username && !(@password || @password_base64)
Expand All @@ -115,15 +129,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
Expand Down
20 changes: 20 additions & 0 deletions spec/unit_tests/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d830e31

Please sign in to comment.