Commit 514e4dab authored by Michał Zając's avatar Michał Zając

Simplify issue types fetching logic

Jira API exposes `issueTypes` on Project details API which means we can
simplify the logic used to fetch issue types available for any given
project.
parent c69f02df
...@@ -87,71 +87,22 @@ module EE ...@@ -87,71 +87,22 @@ module EE
end end
end end
# Returns list of Issue Type Scheme IDs in selected JIRA Project
#
# @return [Array] the array of IDs
def project_issuetype_scheme_ids
raise NotImplementedError unless data_fields.deployment_cloud?
query_url = Addressable::URI.join("#{client.options[:rest_base_path]}/", 'issuetypescheme/', 'project')
query_url.query_values = { 'projectId' => jira_project_id }
client
.get(query_url.to_s)
.fetch('values', [])
.map { |schemes| schemes.dig('issueTypeScheme', 'id') }
end
# Returns list of Issue Type IDs available in active Issue Type Scheme in selected JIRA Project
#
# @return [Array] the array of IDs
def project_issuetype_ids
strong_memoize(:project_issuetype_ids) do
if data_fields.deployment_server?
project_issuetype_ids_from_project_details
elsif data_fields.deployment_cloud?
# According to https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issue-type-schemes/#api-rest-api-3-issuetypescheme-project-get
# only classic projects has issue type schemes
next project_issuetype_ids_from_project_details if jira_project.style == "next-gen"
project_issuetype_ids_from_issuetypescheme_mappings
else
raise NotImplementedError
end
end
end
def project_issuetype_ids_from_issuetypescheme_mappings
query_url = Addressable::URI.join("#{client.options[:rest_base_path]}/", 'issuetypescheme/', 'mapping')
query_url.query_values = { 'issueTypeSchemeId' => project_issuetype_scheme_ids }
client
.get(query_url.to_s)
.fetch('values', [])
.map { |schemes| schemes['issueTypeId'] }
end
def project_issuetype_ids_from_project_details
query_url = Addressable::URI.join("#{client.options[:rest_base_path]}/", 'project/', project_key)
client
.get(query_url.to_s)
.fetch('issueTypes', [])
.map { |issue_type| issue_type['id'] }
end
# Returns list of available Issue types in selected JIRA Project # Returns list of available Issue types in selected JIRA Project
# #
# @return [Array] the array of objects with JIRA Issuetype ID, Name and Description # @return [Array] the array of objects with JIRA Issuetype ID, Name and Description
def issue_types def issue_types
return [] if jira_project.blank? return [] if jira_project.blank?
client jira_project
.Issuetype .issuetypes
.all
.select { |issue_type| issue_type.id.in?(project_issuetype_ids) }
.reject { |issue_type| issue_type.subtask } .reject { |issue_type| issue_type.subtask }
.map { |issue_type| { id: issue_type.id, name: issue_type.name, description: issue_type.description } } .map do |issue_type|
{
id: issue_type.id,
name: issue_type.name,
description: issue_type.description
}
end
end end
end end
end end
......
...@@ -111,16 +111,10 @@ RSpec.describe Integrations::Jira do ...@@ -111,16 +111,10 @@ RSpec.describe Integrations::Jira do
} }
end end
let(:classic_issuetypes) do context 'when JIRA project style is classic' do
let(:jira_project_style) { 'classic' }
let(:project_issue_types) do
[ [
{
id: '10004',
description: 'A new feature of the product, which has yet to be developed.',
name: 'New Feature',
untranslatedName: 'New Feature',
subtask: false,
avatarId: 10311
},
{ {
id: '10001', id: '10001',
description: 'Jira Bug', description: 'Jira Bug',
...@@ -140,7 +134,22 @@ RSpec.describe Integrations::Jira do ...@@ -140,7 +134,22 @@ RSpec.describe Integrations::Jira do
] ]
end end
let(:nextgen_issuetypes) do let(:expected_data) do
{
issuetypes: project_issue_types.select { |it| !it[:subtask] }.map { |it| it.slice(*%i[id name description]) }
}
end
before do
WebMock.stub_request(:get, %r{api/2/project/GL}).with(basic_auth: %w(gitlab_jira_username gitlab_jira_password)).to_return(body: project_info_result.to_json )
end
it { is_expected.to eq(success: true, result: { jira: true }, data: { issuetypes: [{ id: '10001', name: 'Bug', description: 'Jira Bug' }] }) }
end
context 'when JIRA project style is next-gen' do
let(:jira_project_style) { 'next-gen' }
let(:project_issue_types) do
[ [
{ {
id: '2137', id: '2137',
...@@ -169,67 +178,14 @@ RSpec.describe Integrations::Jira do ...@@ -169,67 +178,14 @@ RSpec.describe Integrations::Jira do
] ]
end end
let(:issue_types_response) { classic_issuetypes + nextgen_issuetypes }
context 'when JIRA project style is classic' do
let(:jira_project_style) { 'classic' }
let(:project_issue_types) { classic_issuetypes }
let(:issue_type_scheme_response) do
{
values: [
{
issueTypeScheme: {
id: '10126',
name: 'GV: Software Development Issue Type Scheme',
defaultIssueTypeId: '10001'
},
projectIds: [
'10000'
]
}
]
}
end
let(:issue_type_mapping_response) do
{
values: [
{
issueTypeSchemeId: '10126',
issueTypeId: '10003'
},
{
issueTypeSchemeId: '10126',
issueTypeId: '10001'
}
]
}
end
before do
WebMock.stub_request(:get, %r{api/2/project/GL}).with(basic_auth: %w(gitlab_jira_username gitlab_jira_password)).to_return(body: project_info_result.to_json )
WebMock.stub_request(:get, %r{api/2/issuetype\z}).to_return(body: issue_types_response.to_json, headers: headers)
WebMock.stub_request(:get, %r{api/2/issuetypescheme/project\?projectId\=10000\z}).to_return(body: issue_type_scheme_response.to_json, headers: headers)
WebMock.stub_request(:get, %r{api/2/issuetypescheme/mapping\?issueTypeSchemeId\=10126\z}).to_return(body: issue_type_mapping_response.to_json, headers: headers)
end
it { is_expected.to eq(success: true, result: { jira: true }, data: { issuetypes: [{ id: '10001', name: 'Bug', description: 'Jira Bug' }] }) }
end
context 'when JIRA project style is next-gen' do
let(:jira_project_style) { 'next-gen' }
let(:project_issue_types) { nextgen_issuetypes }
let(:expected_data) do let(:expected_data) do
{ {
issuetypes: nextgen_issuetypes.select { |issue_type| !issue_type[:subtask] }.map { |issue_type| issue_type.slice(*%i[id name description]) } issuetypes: project_issue_types.select { |it| !it[:subtask] }.map { |it| it.slice(*%i[id name description]) }
} }
end end
before do before do
WebMock.stub_request(:get, %r{api/2/project/GL}).with(basic_auth: %w(gitlab_jira_username gitlab_jira_password)).to_return(body: project_info_result.to_json, headers: headers) WebMock.stub_request(:get, %r{api/2/project/GL}).with(basic_auth: %w(gitlab_jira_username gitlab_jira_password)).to_return(body: project_info_result.to_json, headers: headers)
WebMock.stub_request(:get, %r{api/2/issuetype\z}).to_return(body: issue_types_response.to_json, headers: headers)
end end
it { is_expected.to eq(success: true, result: { jira: true }, data: expected_data) } it { is_expected.to eq(success: true, result: { jira: true }, data: expected_data) }
......
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