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

Introduce set_cookie for handling jenkins session id #292

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ rvm:
- 2.5.8
- 2.6.6
- 2.7.1
- 3.0.0

script:
- bundle exec rake unit_tests
Expand Down
4 changes: 2 additions & 2 deletions jenkins_api_client.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ Gem::Specification.new do |s|
s.files = `git ls-files -z`.split("\x0").select { |f| f.match(%r{lib/|bin/|java_deps/|gemspec}) }
s.require_paths = ['lib']
s.homepage = 'https://github.com/arangamani/jenkins_api_client'
s.required_ruby_version = ::Gem::Requirement.new('~> 2.1')
s.rubygems_version = "2.4.5.1"
s.required_ruby_version = ::Gem::Requirement.new('>= 2.0.0')
s.rubygems_version = "3.2.15"
s.summary = "Jenkins JSON API Client"
s.licenses = ["MIT"]

Expand Down
11 changes: 11 additions & 0 deletions lib/jenkins_api_client/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ def make_http_request(request, follow_redirect = @follow_redirects)
# Pick out some useful header info before we return
@jenkins_version = response['X-Jenkins']
@hudson_version = response['X-Hudson']
set_cookie(response)

return response
end
Expand Down Expand Up @@ -694,6 +695,16 @@ def exec_cli(command, args = [])

private

# Handles Set-Cookie header for jenkins session
#
def set_cookie(response)
cookie = response['Set-Cookie']
return unless cookie

@logger.debug "Setting cookie: @cookies: #{@cookies}, cookie: #{cookie}"
@cookies = @cookies ? "#{@cookies};#{cookie}" : cookie
end

# Obtains the crumb from Jenkins' crumb issuer
#
# @return [Hash<String, String>] the crumb response from Jenkins' crumb
Expand Down
2 changes: 1 addition & 1 deletion lib/jenkins_api_client/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1687,7 +1687,7 @@ def artifact_exists?(job_name, build_number = 0)
def find_artifacts(job_name, build_number = nil)
response_json = get_build_details(job_name, build_number)
artifact_path(build_details: response_json).map do |p|
URI.escape("#{response_json['url']}artifact/#{p['relativePath']}")
CGI.escape("#{response_json['url']}artifact/#{p['relativePath']}")
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/unit_tests/job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -710,13 +710,13 @@

describe "#find_artifact" do
it "accepts job name and build number and return artifact path" do
expected_path = URI.escape("https://example.com/DEFAULT-VIEW/view/VIEW-NAME/job/test_job/2/artifact/somepath/output.json")
expected_path = CGI.escape("https://example.com/DEFAULT-VIEW/view/VIEW-NAME/job/test_job/2/artifact/somepath/output.json")
@client.should_receive(:api_get_request).and_return(@sample_json_build_response)
expect(@job.find_artifact('test_job', 1)).to eql(expected_path)
end

it "accepts job name and uses latest build number if build number not provided and return artifact path" do
expected_path = URI.escape("https://example.com/DEFAULT-VIEW/view/VIEW-NAME/job/test_job/2/artifact/somepath/output.json")
expected_path = CGI.escape("https://example.com/DEFAULT-VIEW/view/VIEW-NAME/job/test_job/2/artifact/somepath/output.json")
@client.should_receive(:api_get_request).and_return(@sample_json_job_response, @sample_json_build_response)
expect(@job.find_artifact('test_job')).to eql(expected_path)
end
Expand Down