Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read username/password from server_url #118

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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