Skip to content

Commit

Permalink
Fixed Layout/LineLength
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbrodie committed Apr 28, 2024
1 parent ec5fb07 commit 08d48f1
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 45 deletions.
3 changes: 2 additions & 1 deletion lib/jira/base_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ def self.delegate_to_target_class(*method_names)
# The principle purpose of this class is to delegate methods to the corresponding
# non-factory class and automatically prepend the client argument to the argument
# list.
delegate_to_target_class :all, :find, :collection_path, :singular_path, :jql, :get_backlog_issues, :get_board_issues, :get_sprints, :get_sprint_issues, :get_projects, :get_projects_full
delegate_to_target_class :all, :find, :collection_path, :singular_path, :jql, :get_backlog_issues,
:get_board_issues, :get_sprints, :get_sprint_issues, :get_projects, :get_projects_full

# This method needs special handling as it has a default argument value
def build(attrs = {})
Expand Down
15 changes: 10 additions & 5 deletions lib/jira/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ class Client
# The configuration options for this client instance
attr_reader :options

def_delegators :@request_client, :init_access_token, :set_access_token, :set_request_token, :request_token, :access_token, :authenticated?
def_delegators :@request_client, :init_access_token, :set_access_token, :set_request_token, :request_token,
:access_token, :authenticated?

DEFINED_OPTIONS = [
:site,
Expand Down Expand Up @@ -121,11 +122,14 @@ def initialize(options = {})
raise ArgumentError, "Unknown option(s) given: #{unknown_options}" unless unknown_options.empty?

if options[:use_client_cert]
@options[:ssl_client_cert] = OpenSSL::X509::Certificate.new(File.read(@options[:cert_path])) if @options[:cert_path]
@options[:ssl_client_cert] =
OpenSSL::X509::Certificate.new(File.read(@options[:cert_path])) if @options[:cert_path]
@options[:ssl_client_key] = OpenSSL::PKey::RSA.new(File.read(@options[:key_path])) if @options[:key_path]

raise ArgumentError, 'Options: :cert_path or :ssl_client_cert must be set when :use_client_cert is true' unless @options[:ssl_client_cert]
raise ArgumentError, 'Options: :key_path or :ssl_client_key must be set when :use_client_cert is true' unless @options[:ssl_client_key]
raise ArgumentError,
'Options: :cert_path or :ssl_client_cert must be set when :use_client_cert is true' unless @options[:ssl_client_cert]
raise ArgumentError,
'Options: :key_path or :ssl_client_key must be set when :use_client_cert is true' unless @options[:ssl_client_key]
end

case options[:auth_type]
Expand All @@ -137,7 +141,8 @@ def initialize(options = {})
when :basic
@request_client = HttpClient.new(@options)
when :cookie
raise ArgumentError, 'Options: :use_cookies must be true for :cookie authorization type' if @options.key?(:use_cookies) && !@options[:use_cookies]
raise ArgumentError,
'Options: :use_cookies must be true for :cookie authorization type' if @options.key?(:use_cookies) && !@options[:use_cookies]

@options[:use_cookies] = true
@request_client = HttpClient.new(@options)
Expand Down
3 changes: 2 additions & 1 deletion lib/jira/http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ def basic_auth_http_conn
def http_conn(uri)
http_conn =
if @options[:proxy_address]
Net::HTTP.new(uri.host, uri.port, @options[:proxy_address], @options[:proxy_port] || 80, @options[:proxy_username], @options[:proxy_password])
Net::HTTP.new(uri.host, uri.port, @options[:proxy_address], @options[:proxy_port] || 80,
@options[:proxy_username], @options[:proxy_password])
else
Net::HTTP.new(uri.host, uri.port)
end
Expand Down
9 changes: 6 additions & 3 deletions lib/jira/resource/issue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ def self.all(client)
result
end

def self.jql(client, jql, options = { fields: nil, start_at: nil, max_results: nil, expand: nil, validate_query: true })
def self.jql(client, jql, options = { fields: nil, start_at: nil, max_results: nil, expand: nil,
validate_query: true })
url = client.options[:rest_base_path] + "/search?jql=#{CGI.escape(jql)}"

url << "&fields=#{options[:fields].map { |value| CGI.escape(client.Field.name_to_id(value)) }.join(',')}" if options[:fields]
url << "&fields=#{options[:fields].map { |value|
CGI.escape(client.Field.name_to_id(value)) }.join(',')}" if options[:fields]
url << "&startAt=#{CGI.escape(options[:start_at].to_s)}" if options[:start_at]
url << "&maxResults=#{CGI.escape(options[:max_results].to_s)}" if options[:max_results]
url << '&validateQuery=false' if options[:validate_query] === false
Expand Down Expand Up @@ -97,7 +99,8 @@ def editmeta
end

def respond_to?(method_name, _include_all = false)
if attrs.key?('fields') && [method_name.to_s, client.Field.name_to_id(method_name)].any? { |k| attrs['fields'].key?(k) }
if attrs.key?('fields') && [method_name.to_s, client.Field.name_to_id(method_name)].any? { |k|
attrs['fields'].key?(k) }
true
else
super(method_name)
Expand Down
3 changes: 2 additions & 1 deletion lib/jira/resource/issue_picker_suggestions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class IssuePickerSuggestionsFactory < JIRA::BaseFactory # :nodoc:
class IssuePickerSuggestions < JIRA::Base
has_many :sections, class: JIRA::Resource::IssuePickerSuggestionsIssue

def self.all(client, query = '', options = { current_jql: nil, current_issue_key: nil, current_project_id: nil, show_sub_tasks: nil, show_sub_tasks_parent: nil })
def self.all(client, query = '', options = { current_jql: nil, current_issue_key: nil, current_project_id: nil,
show_sub_tasks: nil, show_sub_tasks_parent: nil })
url = client.options[:rest_base_path] + "/issue/picker?query=#{CGI.escape(query)}"

url << "&currentJQL=#{CGI.escape(options[:current_jql])}" if options[:current_jql]
Expand Down
3 changes: 2 additions & 1 deletion spec/integration/webhook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
let(:key) { '2' }

let(:expected_attributes) do
{ 'name' => 'from API', 'url' => 'http://localhost:3000/webhooks/1', 'excludeBody' => false, 'filters' => { 'issue-related-events-section' => '' }, 'events' => [], 'enabled' => true, 'self' => 'http://localhost:2990/jira/rest/webhooks/1.0/webhook/2', 'lastUpdatedUser' => 'admin', 'lastUpdatedDisplayName' => 'admin', 'lastUpdated' => 1_453_306_520_188 }
{ 'name' => 'from API', 'url' => 'http://localhost:3000/webhooks/1', 'excludeBody' => false,
'filters' => { 'issue-related-events-section' => '' }, 'events' => [], 'enabled' => true, 'self' => 'http://localhost:2990/jira/rest/webhooks/1.0/webhook/2', 'lastUpdatedUser' => 'admin', 'lastUpdatedDisplayName' => 'admin', 'lastUpdated' => 1_453_306_520_188 }
end

let(:expected_collection_length) { 1 }
Expand Down
15 changes: 10 additions & 5 deletions spec/jira/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@ class JIRA::Resource::HasManyExample < JIRA::Base # :nodoc:
it 'returns false when an invalid field is set' do # The JIRA REST API apparently ignores fields that you aren't allowed to set manually
response = instance_double('Response', body: '{"errorMessages":["blah"]}', status: 400)
allow(subject).to receive(:new_record?) { false }
expect(client).to receive(:put).with('/foo/bar', '{"invalid_field":"foobar"}').and_raise(JIRA::HTTPError.new(response))
expect(client).to receive(:put).with('/foo/bar',
'{"invalid_field":"foobar"}').and_raise(JIRA::HTTPError.new(response))
expect(subject.save('invalid_field' => 'foobar')).to be_falsey
end

Expand Down Expand Up @@ -300,7 +301,8 @@ class JIRA::Resource::HasManyExample < JIRA::Base # :nodoc:
it 'throws an exception when an invalid field is set' do
response = instance_double('Response', body: '{"errorMessages":["blah"]}', status: 400)
allow(subject).to receive(:new_record?) { false }
expect(client).to receive(:put).with('/foo/bar', '{"invalid_field":"foobar"}').and_raise(JIRA::HTTPError.new(response))
expect(client).to receive(:put).with('/foo/bar',
'{"invalid_field":"foobar"}').and_raise(JIRA::HTTPError.new(response))
expect{ subject.save!('invalid_field' => 'foobar') }.to raise_error(JIRA::HTTPError)
end
end
Expand Down Expand Up @@ -487,7 +489,8 @@ class JIRA::Resource::HasManyExample < JIRA::Base # :nodoc:
end

it 'allows the has_many attributes to be nested inside another attribute' do
subject = JIRA::Resource::HasManyExample.new(client, attrs: { 'nested' => { 'brunchmuffins' => [{ 'id' => '123' }, { 'id' => '456' }] } })
subject = JIRA::Resource::HasManyExample.new(client,
attrs: { 'nested' => { 'brunchmuffins' => [{ 'id' => '123' }, { 'id' => '456' }] } })
expect(subject.brunchmuffins.length).to eq(2)
subject.brunchmuffins.each do |brunchmuffin|
expect(brunchmuffin.class).to eq(JIRA::Resource::Deadbeef)
Expand All @@ -512,7 +515,8 @@ class JIRA::Resource::HasManyExample < JIRA::Base # :nodoc:
end

it 'allows the attribute key to be specified' do
subject = JIRA::Resource::HasManyExample.new(client, attrs: { 'irregularlyNamedThings' => [{ 'id' => '123' }, { 'id' => '456' }] })
subject = JIRA::Resource::HasManyExample.new(client,
attrs: { 'irregularlyNamedThings' => [{ 'id' => '123' }, { 'id' => '456' }] })
expect(subject.irregularly_named_things.length).to eq(2)
subject.irregularly_named_things.each do |thing|
expect(thing.class).to eq(JIRA::Resource::Deadbeef)
Expand Down Expand Up @@ -545,7 +549,8 @@ class JIRA::Resource::HasManyExample < JIRA::Base # :nodoc:
end

it 'allows the has_one attributes to be nested inside another attribute' do
subject = JIRA::Resource::HasOneExample.new(client, attrs: { 'nested' => { 'brunchmuffin' => { 'id' => '123' } } })
subject = JIRA::Resource::HasOneExample.new(client,
attrs: { 'nested' => { 'brunchmuffin' => { 'id' => '123' } } })
expect(subject.brunchmuffin.class).to eq(JIRA::Resource::Deadbeef)
expect(subject.brunchmuffin.id).to eq('123')
end
Expand Down
20 changes: 14 additions & 6 deletions spec/jira/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,13 @@
subject { JIRA::Client.new(options) }

it 'raises an ArgumentError' do
expect { subject }.to raise_exception(ArgumentError, 'Options: :cert_path or :ssl_client_cert must be set when :use_client_cert is true')
expect {
subject }.to raise_exception(ArgumentError,
'Options: :cert_path or :ssl_client_cert must be set when :use_client_cert is true')
options[:ssl_client_cert] = '<cert></cert>'
expect { subject }.to raise_exception(ArgumentError, 'Options: :key_path or :ssl_client_key must be set when :use_client_cert is true')
expect {
subject }.to raise_exception(ArgumentError,
'Options: :key_path or :ssl_client_key must be set when :use_client_cert is true')
end
end
end
Expand All @@ -77,11 +81,13 @@
RSpec.shared_examples 'HttpClient tests' do
it 'makes a valid request' do
%i[delete get head].each do |method|
expect(subject.request_client).to receive(:make_request).with(method, '/path', nil, headers).and_return(successful_response)
expect(subject.request_client).to receive(:make_request).with(method, '/path', nil,
headers).and_return(successful_response)
subject.send(method, '/path', headers)
end
%i[post put].each do |method|
expect(subject.request_client).to receive(:make_request).with(method, '/path', '', merged_headers).and_return(successful_response)
expect(subject.request_client).to receive(:make_request).with(method, '/path', '',
merged_headers).and_return(successful_response)
subject.send(method, '/path', '', headers)
end
end
Expand All @@ -106,11 +112,13 @@
describe 'that call a oauth client' do
specify 'which makes a request' do
%i[delete get head].each do |method|
expect(subject.request_client).to receive(:make_request).with(method, '/path', nil, headers).and_return(successful_response)
expect(subject.request_client).to receive(:make_request).with(method, '/path', nil,
headers).and_return(successful_response)
subject.send(method, '/path', {})
end
%i[post put].each do |method|
expect(subject.request_client).to receive(:make_request).with(method, '/path', '', merged_headers).and_return(successful_response)
expect(subject.request_client).to receive(:make_request).with(method, '/path', '',
merged_headers).and_return(successful_response)
subject.send(method, '/path', '', {})
end
end
Expand Down
18 changes: 12 additions & 6 deletions spec/jira/http_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@
basic_auth_http_conn = double
request = double
allow(basic_client).to receive(:basic_auth_http_conn).and_return(basic_auth_http_conn)
expect(request).to receive(:basic_auth).with(basic_client.options[:username], basic_client.options[:password]).exactly(5).times.and_return(request)
expect(request).to receive(:basic_auth).with(basic_client.options[:username],
basic_client.options[:password]).exactly(5).times.and_return(request)
expect(basic_auth_http_conn).to receive(:request).exactly(5).times.with(request).and_return(response)
%i[delete get head].each do |method|
expect(Net::HTTP.const_get(method.to_s.capitalize)).to receive(:new).with('/path', headers).and_return(request)
Expand All @@ -168,7 +169,8 @@
basic_auth_http_conn = double
request = double
allow(basic_cookie_client).to receive(:basic_auth_http_conn).and_return(basic_auth_http_conn)
expect(request).to receive(:basic_auth).with(basic_cookie_client.options[:username], basic_cookie_client.options[:password]).exactly(5).times.and_return(request)
expect(request).to receive(:basic_auth).with(basic_cookie_client.options[:username],
basic_cookie_client.options[:password]).exactly(5).times.and_return(request)
expect(cookie_response).to receive(:get_fields).with('set-cookie').exactly(5).times
expect(basic_auth_http_conn).to receive(:request).exactly(5).times.with(request).and_return(cookie_response)
%i[delete get head].each do |method|
Expand All @@ -189,7 +191,8 @@
basic_auth_http_conn = double
request = double
allow(client).to receive(:basic_auth_http_conn).and_return(basic_auth_http_conn)
expect(request).to receive(:basic_auth).with(client.options[:username], client.options[:password]).exactly(5).times.and_return(request)
expect(request).to receive(:basic_auth).with(client.options[:username],
client.options[:password]).exactly(5).times.and_return(request)
expect(request).to receive(:add_field).with('Cookie', 'sessionToken=abc123; internal=true').exactly(5).times
expect(cookie_response).to receive(:get_fields).with('set-cookie').exactly(5).times
expect(basic_auth_http_conn).to receive(:request).exactly(5).times.with(request).and_return(cookie_response)
Expand All @@ -212,7 +215,8 @@
expect(Net::HTTP::Get).to receive(:new).with('/foo', headers).and_return(http_request)

expect(basic_auth_http_conn).to receive(:request).with(http_request).and_return(response)
expect(http_request).to receive(:basic_auth).with(basic_client.options[:username], basic_client.options[:password]).and_return(http_request)
expect(http_request).to receive(:basic_auth).with(basic_client.options[:username],
basic_client.options[:password]).and_return(http_request)
allow(basic_client).to receive(:basic_auth_http_conn).and_return(basic_auth_http_conn)
basic_client.make_request(:get, '/foo', body, headers)
end
Expand All @@ -225,7 +229,8 @@
expect(Net::HTTP::Get).to receive(:new).with('/foo', headers).and_return(http_request)

expect(basic_auth_http_conn).to receive(:request).with(http_request).and_return(response)
expect(http_request).to receive(:basic_auth).with(basic_client.options[:username], basic_client.options[:password]).and_return(http_request)
expect(http_request).to receive(:basic_auth).with(basic_client.options[:username],
basic_client.options[:password]).and_return(http_request)
allow(basic_client).to receive(:basic_auth_http_conn).and_return(basic_auth_http_conn)
basic_client.make_request(:get, 'http://mydomain.com/foo', body, headers)
end
Expand Down Expand Up @@ -381,7 +386,8 @@
end

it 'performs a basic http client request' do
expect(request).to receive(:basic_auth).with(basic_client.options[:username], basic_client.options[:password]).and_return(request)
expect(request).to receive(:basic_auth).with(basic_client.options[:username],
basic_client.options[:password]).and_return(request)

subject
end
Expand Down
9 changes: 6 additions & 3 deletions spec/jira/oauth_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@

it 'signs the access_token and performs the request' do
expect(access_token).to receive(:sign!).with(an_instance_of(Net::HTTP::Post::Multipart))
expect(oauth_client.consumer).to receive_message_chain(:http, :request).with(an_instance_of(Net::HTTP::Post::Multipart))
expect(oauth_client.consumer).to receive_message_chain(:http,
:request).with(an_instance_of(Net::HTTP::Post::Multipart))

subject
end
Expand Down Expand Up @@ -169,11 +170,13 @@
mock_access_token = double
allow(oauth__2legged_client).to receive(:access_token).and_return(mock_access_token)
%i[delete get head].each do |method|
expect(mock_access_token).to receive(method).with('/path?any_param=toto&oauth_token=', headers).and_return(response)
expect(mock_access_token).to receive(method).with('/path?any_param=toto&oauth_token=',
headers).and_return(response)
oauth__2legged_client.make_request(method, '/path?any_param=toto', '', headers)
end
%i[post put].each do |method|
expect(mock_access_token).to receive(method).with('/path?any_param=toto&oauth_token=', '', headers).and_return(response)
expect(mock_access_token).to receive(method).with('/path?any_param=toto&oauth_token=', '',
headers).and_return(response)
oauth__2legged_client.make_request(method, '/path?any_param=toto', '', headers)
end
end
Expand Down
12 changes: 8 additions & 4 deletions spec/jira/resource/attachment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@
attrs: { 'author' => { 'foo' => 'bar' } }
)
end
let(:default_headers_given) { { 'authorization' => 'Bearer 83CF8B609DE60036A8277BD0E96135751BBC07EB234256D4B65B893360651BF2' } }
let(:default_headers_given) {
{ 'authorization' => 'Bearer 83CF8B609DE60036A8277BD0E96135751BBC07EB234256D4B65B893360651BF2' } }
let(:bearer_client) do
JIRA::Client.new(username: 'username', password: 'password', auth_type: :basic, use_ssl: false,
default_headers: default_headers_given )
Expand All @@ -149,7 +150,8 @@
end

it 'passes the custom headers' do
expect(bearer_client.request_client).to receive(:request_multipart).with(anything, anything, merged_headers).and_return(response)
expect(bearer_client.request_client).to receive(:request_multipart).with(anything, anything,
merged_headers).and_return(response)

bearer_attachment.save('file' => path_to_file)

Expand Down Expand Up @@ -193,7 +195,8 @@
attrs: { 'author' => { 'foo' => 'bar' } }
)
end
let(:default_headers_given) { { 'authorization' => 'Bearer 83CF8B609DE60036A8277BD0E96135751BBC07EB234256D4B65B893360651BF2' } }
let(:default_headers_given) {
{ 'authorization' => 'Bearer 83CF8B609DE60036A8277BD0E96135751BBC07EB234256D4B65B893360651BF2' } }
let(:bearer_client) do
JIRA::Client.new(username: 'username', password: 'password', auth_type: :basic, use_ssl: false,
default_headers: default_headers_given )
Expand All @@ -203,7 +206,8 @@
end

it 'passes the custom headers' do
expect(bearer_client.request_client).to receive(:request_multipart).with(anything, anything, merged_headers).and_return(response)
expect(bearer_client.request_client).to receive(:request_multipart).with(anything, anything,
merged_headers).and_return(response)

bearer_attachment.save!('file' => path_to_file)

Expand Down
Loading

0 comments on commit 08d48f1

Please sign in to comment.