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
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
#
# @return [Array] the array of objects with JIRA Issuetype ID, Name and Description
def issue_types
return [] if jira_project.blank?
client
.Issuetype
.all
.select { |issue_type| issue_type.id.in?(project_issuetype_ids) }
jira_project
.issuetypes
.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
......
......@@ -111,107 +111,37 @@ RSpec.describe Integrations::Jira do
}
end
let(:classic_issuetypes) 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',
description: 'Jira Bug',
name: 'Bug',
untranslatedName: 'Bug',
subtask: false,
avatarId: 10303
},
{
id: '10003',
description: 'A small piece of work thats part of a larger task.',
name: 'Sub-task',
untranslatedName: 'Sub-task',
subtask: true,
avatarId: 10316
}
]
end
let(:nextgen_issuetypes) do
[
{
id: '2137',
description: 'Very new, yes',
name: 'Next Gen Issue Type 1',
untranslatedName: 'Next Gen Issue Type 1',
subtask: false,
avatarId: 10311
},
{
id: '2138',
description: 'Something',
name: 'Next Gen Issue Type 2',
untranslatedName: 'Next Gen Issue Type 2',
subtask: false,
avatarId: 10303
},
{
id: '2139',
description: 'Subtasks? Meh.',
name: 'Next Gen Issue Type 3',
untranslatedName: 'Next Gen Issue Type 3',
subtask: true,
avatarId: 10316
}
]
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'
]
}
]
}
let(:project_issue_types) do
[
{
id: '10001',
description: 'Jira Bug',
name: 'Bug',
untranslatedName: 'Bug',
subtask: false,
avatarId: 10303
},
{
id: '10003',
description: 'A small piece of work thats part of a larger task.',
name: 'Sub-task',
untranslatedName: 'Sub-task',
subtask: true,
avatarId: 10316
}
]
end
let(:issue_type_mapping_response) do
let(:expected_data) do
{
values: [
{
issueTypeSchemeId: '10126',
issueTypeId: '10003'
},
{
issueTypeSchemeId: '10126',
issueTypeId: '10001'
}
]
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 )
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' }] }) }
......@@ -219,17 +149,43 @@ RSpec.describe Integrations::Jira do
context 'when JIRA project style is next-gen' do
let(:jira_project_style) { 'next-gen' }
let(:project_issue_types) { nextgen_issuetypes }
let(:project_issue_types) do
[
{
id: '2137',
description: 'Very new, yes',
name: 'Next Gen Issue Type 1',
untranslatedName: 'Next Gen Issue Type 1',
subtask: false,
avatarId: 10311
},
{
id: '2138',
description: 'Something',
name: 'Next Gen Issue Type 2',
untranslatedName: 'Next Gen Issue Type 2',
subtask: false,
avatarId: 10303
},
{
id: '2139',
description: 'Subtasks? Meh.',
name: 'Next Gen Issue Type 3',
untranslatedName: 'Next Gen Issue Type 3',
subtask: true,
avatarId: 10316
}
]
end
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
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/issuetype\z}).to_return(body: issue_types_response.to_json, headers: headers)
end
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