Skip to content

Commit

Permalink
Resolve conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbrodie committed Apr 30, 2024
2 parents e62baba + 3e1deb0 commit 669a600
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 46 deletions.
8 changes: 3 additions & 5 deletions lib/jira/resource/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ def self.meta(client)
# @param [Hash] headers Any additional headers to call Jira.
# @yield |file|
# @yieldparam [IO] file The IO object streaming the download.
def download_file(headers = {}, &)
def download_file(headers = {}, &block)
default_headers = client.options[:default_headers]
URI.open(content, default_headers.merge(headers), &)
URI.parse(content).open(default_headers.merge(headers), &block)
end

# Downloads the file contents as a string object.
Expand All @@ -56,9 +56,7 @@ def download_file(headers = {}, &)
# @param [Hash] headers Any additional headers to call Jira.
# @return [String,NilClass] The file contents.
def download_contents(headers = {})
download_file(headers) do |file|
file.read
end
download_file(headers, &:read)
end

def save!(attrs, path = url)
Expand Down
86 changes: 45 additions & 41 deletions spec/jira/resource/attachment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe JIRA::Resource::Attachment do
subject(:attachment) do
described_class.new(
JIRA::Resource::Attachment.new(

Check failure on line 5 in spec/jira/resource/attachment_spec.rb

View workflow job for this annotation

GitHub Actions / rubocop

RSpec/DescribedClass: Use `described_class` instead of `JIRA::Resource::Attachment`.
client,
issue: JIRA::Resource::Issue.new(client),
attrs: { 'author' => { 'foo' => 'bar' } }
Expand Down Expand Up @@ -35,7 +35,7 @@
end

describe '.meta' do
subject { described_class.meta(client) }
subject { JIRA::Resource::Attachment.meta(client) }

Check failure on line 38 in spec/jira/resource/attachment_spec.rb

View workflow job for this annotation

GitHub Actions / rubocop

RSpec/DescribedClass: Use `described_class` instead of `JIRA::Resource::Attachment`.

let(:response) do
double(
Expand All @@ -60,38 +60,37 @@
end

context 'there is an attachment on an issue' do
let(:client) do
JIRA::Client.new(username: 'username', password: 'password', auth_type: :basic, use_ssl: false)
end
let(:attachment_file_contents) { 'file contents' }
let(:attachment_url) { 'https://localhost:2990/secure/attachment/32323/myfile.txt' }

Check failure on line 67 in spec/jira/resource/attachment_spec.rb

View workflow job for this annotation

GitHub Actions / rubocop

RSpec/EmptyLineAfterFinalLet: Add an empty line after the last `let`.
subject(:attachment) do

Check failure on line 68 in spec/jira/resource/attachment_spec.rb

View workflow job for this annotation

GitHub Actions / rubocop

RSpec/LeadingSubject: Declare `subject` above any other `let` declarations.
described_class.new(
JIRA::Resource::Attachment.new(

Check failure on line 69 in spec/jira/resource/attachment_spec.rb

View workflow job for this annotation

GitHub Actions / rubocop

RSpec/DescribedClass: Use `described_class` instead of `JIRA::Resource::Attachment`.
client,
issue: JIRA::Resource::Issue.new(client),
attrs: { 'author' => { 'foo' => 'bar' }, 'content' => attachment_url }
)
end

let(:client) do
JIRA::Client.new(username: 'username', password: 'password', auth_type: :basic, use_ssl: false)
before(:each) do

Check failure on line 76 in spec/jira/resource/attachment_spec.rb

View workflow job for this annotation

GitHub Actions / rubocop

RSpec/HookArgument: Omit the default `:each` argument for RSpec hooks.
stub_request(:get, attachment_url).to_return(body: attachment_file_contents)
end
let(:attachment_file_contents) { 'file contents' }
let(:file_target) { double(read: :attachment_file_contents) }
let(:attachment_url) { 'https:jirahost/secure/attachment/32323/myfile.txt' }

describe '.download_file' do
it 'passes file object to block' do
expect(URI).to receive(:open).with(attachment_url, anything).and_yield(file_target)
expect(URI).to receive(:parse).with(attachment_url).and_call_original

attachment.download_file do |file|
expect(file).to eq(file_target)
expect(file.read).to eq(attachment_file_contents)
end
end
end

describe '.download_contents' do
it 'downloads the file contents as a string' do
expect(URI).to receive(:open).with(attachment_url, anything).and_return(attachment_file_contents)

result_str = attachment.download_contents

expect(result_str).to eq(attachment_file_contents)
expect(URI).to receive(:parse).with(attachment_url).and_call_original
expect(attachment.download_contents).to eq(attachment_file_contents)
end
end
end
Expand All @@ -105,12 +104,12 @@
double(
body: [
{
id: 10_001,
self: 'http://www.example.com/jira/rest/api/2.0/attachments/10000',
filename: file_name,
created: '2017-07-19T12:23:06.572+0000',
size: file_size,
mimeType: file_mime_type
"id": 10_001,

Check failure on line 107 in spec/jira/resource/attachment_spec.rb

View workflow job for this annotation

GitHub Actions / rubocop

Lint/SymbolConversion: Unnecessary symbol conversion; use `id:` instead.

Check failure on line 107 in spec/jira/resource/attachment_spec.rb

View workflow job for this annotation

GitHub Actions / rubocop

Style/QuotedSymbols: Prefer single-quoted symbols when you don't need string interpolation or special symbols.
"self": 'http://www.example.com/jira/rest/api/2.0/attachments/10000',

Check failure on line 108 in spec/jira/resource/attachment_spec.rb

View workflow job for this annotation

GitHub Actions / rubocop

Lint/SymbolConversion: Unnecessary symbol conversion; use `self:` instead.

Check failure on line 108 in spec/jira/resource/attachment_spec.rb

View workflow job for this annotation

GitHub Actions / rubocop

Style/QuotedSymbols: Prefer single-quoted symbols when you don't need string interpolation or special symbols.
"filename": file_name,
"created": '2017-07-19T12:23:06.572+0000',
"size": file_size,
"mimeType": file_mime_type
}
].to_json
)
Expand All @@ -131,30 +130,34 @@
expect(attachment.mimeType).to eq file_mime_type
expect(attachment.size).to eq file_size
end

context 'when using custom client headers' do
subject(:bearer_attachment) do
described_class.new(
JIRA::Resource::Attachment.new(
bearer_client,
issue: JIRA::Resource::Issue.new(bearer_client),
attrs: { 'author' => { 'foo' => 'bar' } }
)
end

let(:default_headers_given) do
{ 'authorization' => 'Bearer 83CF8B609DE60036A8277BD0E96135751BBC07EB234256D4B65B893360651BF2' }
end
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)
default_headers: default_headers_given )
end
let(:merged_headers) do
{ 'Accept' => 'application/json', 'X-Atlassian-Token' => 'nocheck' }.merge(default_headers_given)
{
'Accept' => 'application/json',
'X-Atlassian-Token' => 'nocheck'
}.merge(default_headers_given)
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)
end
Expand Down Expand Up @@ -190,31 +193,32 @@

context 'when using custom client headers' do
subject(:bearer_attachment) do
described_class.new(
JIRA::Resource::Attachment.new(
bearer_client,
issue: JIRA::Resource::Issue.new(bearer_client),
attrs: { 'author' => { 'foo' => 'bar' } }
)
end

let(:default_headers_given) do
{ 'authorization' => 'Bearer 83CF8B609DE60036A8277BD0E96135751BBC07EB234256D4B65B893360651BF2' }
end
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)
default_headers: default_headers_given )
end
let(:merged_headers) do
{ 'Accept' => 'application/json', 'X-Atlassian-Token' => 'nocheck' }.merge(default_headers_given)
{
'Accept' => 'application/json',
'X-Atlassian-Token' => 'nocheck'
}.merge(default_headers_given)
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)
end
end
end
end
end
end

0 comments on commit 669a600

Please sign in to comment.