Commit 8ede8b05 authored by Andy Soiron's avatar Andy Soiron Committed by Markus Koller

Fix subpath in jira issue list feature

The Jira client doesn't automatically add the subpath
like http://jira.example.com/subpath when executing custom
Jira requests.
parent d3796d5d
...@@ -18,14 +18,19 @@ module Jira ...@@ -18,14 +18,19 @@ module Jira
request request
end end
# We have to add the context_path here because the Jira client is not taking it into account
def base_api_url def base_api_url
"/rest/api/#{api_version}" "#{context_path}/rest/api/#{api_version}"
end end
private private
attr_reader :jira_service, :project attr_reader :jira_service, :project
def context_path
client.options[:context_path].to_s
end
# override this method in the specific request class implementation if a differnt API version is required # override this method in the specific request class implementation if a differnt API version is required
def api_version def api_version
JIRA_API_VERSION JIRA_API_VERSION
......
---
title: Fix Jira issue list API calls when Jira server runs relative to a context path
merge_request: 49623
author:
type: fixed
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Jira::Requests::Issues::ListService do RSpec.describe Jira::Requests::Issues::ListService do
include AfterNextHelpers
let(:jira_service) { create(:jira_service) } let(:jira_service) { create(:jira_service) }
let(:params) { {} } let(:params) { {} }
...@@ -33,15 +35,18 @@ RSpec.describe Jira::Requests::Issues::ListService do ...@@ -33,15 +35,18 @@ RSpec.describe Jira::Requests::Issues::ListService do
context 'with jira_service' do context 'with jira_service' do
context 'when validations and params are ok' do context 'when validations and params are ok' do
let(:client) { double(options: { site: 'https://jira.example.com' }) } let(:jira_service) { create(:jira_service, url: 'https://jira.example.com') }
let(:response_body) { '' }
let(:response_headers) { { 'content-type' => 'application/json' } }
let(:expected_url_pattern) { /.*jira.example.com\/rest\/api\/2\/search.*/ }
before do before do
expect(service).to receive(:client).at_least(:once).and_return(client) stub_request(:get, expected_url_pattern).to_return(status: 200, body: response_body, headers: response_headers)
end end
context 'when the request to Jira returns an error' do context 'when the request to Jira returns an error' do
before do before do
expect(client).to receive(:get).and_raise(Timeout::Error) expect_next(JIRA::Client).to receive(:get).and_raise(Timeout::Error)
end end
it 'returns an error response' do it 'returns an error response' do
...@@ -50,10 +55,17 @@ RSpec.describe Jira::Requests::Issues::ListService do ...@@ -50,10 +55,17 @@ RSpec.describe Jira::Requests::Issues::ListService do
end end
end end
context 'when the request does not return any values' do context 'when jira runs on a subpath' do
before do let(:jira_service) { create(:jira_service, url: 'http://jira.example.com/jira') }
expect(client).to receive(:get).and_return([]) let(:expected_url_pattern) { /.*jira.example.com\/jira\/rest\/api\/2\/search.*/ }
it 'takes the subpath into account' do
expect(subject.success?).to be_truthy
end end
end
context 'when the request does not return any values' do
let(:response_body) { [].to_json }
it 'returns a payload with no issues' do it 'returns a payload with no issues' do
payload = subject.payload payload = subject.payload
...@@ -65,14 +77,12 @@ RSpec.describe Jira::Requests::Issues::ListService do ...@@ -65,14 +77,12 @@ RSpec.describe Jira::Requests::Issues::ListService do
end end
context 'when the request returns values' do context 'when the request returns values' do
before do let(:response_body) do
expect(client).to receive(:get).and_return( {
{ total: 375,
"total" => 375, startAt: 0,
"startAt" => 0, issues: [{ key: 'TST-1' }, { key: 'TST-2' }]
"issues" => [{ "key" => 'TST-1' }, { "key" => 'TST-2' }] }.to_json
}
)
end end
it 'returns a payload with jira issues' do it 'returns a payload with jira issues' do
...@@ -88,7 +98,7 @@ RSpec.describe Jira::Requests::Issues::ListService do ...@@ -88,7 +98,7 @@ RSpec.describe Jira::Requests::Issues::ListService do
let(:params) { { page: 3, per_page: 20 } } let(:params) { { page: 3, per_page: 20 } }
it 'honors page and per_page' do it 'honors page and per_page' do
expect(client).to receive(:get).with(include('startAt=40&maxResults=20')).and_return([]) expect_next(JIRA::Client).to receive(:get).with(include('startAt=40&maxResults=20')).and_return([])
subject subject
end end
...@@ -98,14 +108,14 @@ RSpec.describe Jira::Requests::Issues::ListService do ...@@ -98,14 +108,14 @@ RSpec.describe Jira::Requests::Issues::ListService do
let(:params) { {} } let(:params) { {} }
it 'uses the default options' do it 'uses the default options' do
expect(client).to receive(:get).with(include('startAt=0&maxResults=100')) expect_next(JIRA::Client).to receive(:get).with(include('startAt=0&maxResults=100'))
subject subject
end end
end end
it 'requests for default fields' do it 'requests for default fields' do
expect(client).to receive(:get).with(include("fields=#{described_class::DEFAULT_FIELDS}")).and_return([]) expect_next(JIRA::Client).to receive(:get).with(include("fields=#{described_class::DEFAULT_FIELDS}")).and_return([])
subject subject
end end
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Jira::Requests::Projects::ListService do RSpec.describe Jira::Requests::Projects::ListService do
include AfterNextHelpers
let(:jira_service) { create(:jira_service) } let(:jira_service) { create(:jira_service) }
let(:params) { {} } let(:params) { {} }
...@@ -33,15 +35,17 @@ RSpec.describe Jira::Requests::Projects::ListService do ...@@ -33,15 +35,17 @@ RSpec.describe Jira::Requests::Projects::ListService do
context 'with jira_service' do context 'with jira_service' do
context 'when validations and params are ok' do context 'when validations and params are ok' do
let(:client) { double(options: { site: 'https://jira.example.com' }) } let(:response_headers) { { 'content-type' => 'application/json' } }
let(:response_body) { [].to_json }
let(:expected_url_pattern) { /.*jira.example.com\/rest\/api\/2\/project/ }
before do before do
expect(service).to receive(:client).at_least(:once).and_return(client) stub_request(:get, expected_url_pattern).to_return(status: 200, body: response_body, headers: response_headers)
end end
context 'when the request to Jira returns an error' do context 'when the request to Jira returns an error' do
before do before do
expect(client).to receive(:get).and_raise(Timeout::Error) expect_next(JIRA::Client).to receive(:get).and_raise(Timeout::Error)
end end
it 'returns an error response' do it 'returns an error response' do
...@@ -54,10 +58,17 @@ RSpec.describe Jira::Requests::Projects::ListService do ...@@ -54,10 +58,17 @@ RSpec.describe Jira::Requests::Projects::ListService do
end end
end end
context 'when the request does not return any values' do context 'when jira runs on a subpath' do
before do let(:jira_service) { create(:jira_service, url: 'http://jira.example.com/jira') }
expect(client).to receive(:get).and_return([]) let(:expected_url_pattern) { /.*jira.example.com\/jira\/rest\/api\/2\/project/ }
it 'takes the subpath into account' do
expect(subject.success?).to be_truthy
end end
end
context 'when the request does not return any values' do
let(:response_body) { [].to_json }
it 'returns a paylod with no projects returned' do it 'returns a paylod with no projects returned' do
payload = subject.payload payload = subject.payload
...@@ -69,9 +80,7 @@ RSpec.describe Jira::Requests::Projects::ListService do ...@@ -69,9 +80,7 @@ RSpec.describe Jira::Requests::Projects::ListService do
end end
context 'when the request returns values' do context 'when the request returns values' do
before do let(:response_body) { [{ 'key' => 'pr1', 'name' => 'First Project' }, { 'key' => 'pr2', 'name' => 'Second Project' }].to_json }
expect(client).to receive(:get).and_return([{ 'key' => 'pr1', 'name' => 'First Project' }, { 'key' => 'pr2', 'name' => 'Second Project' }])
end
it 'returns a paylod with Jira projects' do it 'returns a paylod with Jira projects' do
payload = subject.payload payload = subject.payload
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment