Commit c69f02df authored by Michał Zając's avatar Michał Zając

Handle team-managed (next-gen) Jira projects

Creating Jira issues from Vulnerabilities will fail if you select a
team-manged Jira project (so called next-gen project). This is because
next-gen projects do not have issue type schemes and trying to fetch
issue types that way will yield issue type IDs that are invalid for a
next-gen project.

This change fetches issue types from the project details endpoint

Changelog: changed
EE: true
parent 5bb160d3
...@@ -108,27 +108,39 @@ module EE ...@@ -108,27 +108,39 @@ module EE
def project_issuetype_ids def project_issuetype_ids
strong_memoize(:project_issuetype_ids) do strong_memoize(:project_issuetype_ids) do
if data_fields.deployment_server? if data_fields.deployment_server?
query_url = Addressable::URI.join("#{client.options[:rest_base_path]}/", 'project/', project_key) project_issuetype_ids_from_project_details
client
.get(query_url.to_s)
.fetch('issueTypes', [])
.map { |issue_type| issue_type['id'] }
elsif data_fields.deployment_cloud? elsif data_fields.deployment_cloud?
query_url = Addressable::URI.join("#{client.options[:rest_base_path]}/", 'issuetypescheme/', 'mapping') # According to https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issue-type-schemes/#api-rest-api-3-issuetypescheme-project-get
query_url.query_values = { 'issueTypeSchemeId' => project_issuetype_scheme_ids } # only classic projects has issue type schemes
next project_issuetype_ids_from_project_details if jira_project.style == "next-gen"
client project_issuetype_ids_from_issuetypescheme_mappings
.get(query_url.to_s)
.fetch('values', [])
.map { |schemes| schemes['issueTypeId'] }
else else
raise NotImplementedError raise NotImplementedError
end end
end end
end end
# Returns list of available Issue tTpes in selected JIRA Project 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 # @return [Array] the array of objects with JIRA Issuetype ID, Name and Description
def issue_types def issue_types
......
...@@ -103,41 +103,15 @@ RSpec.describe Integrations::Jira do ...@@ -103,41 +103,15 @@ RSpec.describe Integrations::Jira do
end end
context 'when deployment type is cloud' do context 'when deployment type is cloud' do
let(:project_info_result) { { 'id' => '10000' } } let(:project_info_result) do
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: [ 'id' => '10000',
{ 'style' => jira_project_style,
issueTypeSchemeId: '10126', 'issueTypes' => project_issue_types
issueTypeId: '10003'
},
{
issueTypeSchemeId: '10126',
issueTypeId: '10001'
}
]
} }
end end
let(:issue_types_response) do let(:classic_issuetypes) do
[ [
{ {
id: '10004', id: '10004',
...@@ -166,15 +140,100 @@ RSpec.describe Integrations::Jira do ...@@ -166,15 +140,100 @@ RSpec.describe Integrations::Jira do
] ]
end end
before do let(:nextgen_issuetypes) 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/project/GL\z}).with(basic_auth: %w(gitlab_jira_username gitlab_jira_password)).to_return(body: { 'id' => '10000' }.to_json, headers: headers) {
WebMock.stub_request(:get, %r{api/2/issuetype\z}).to_return(body: issue_types_response.to_json, headers: headers) id: '2137',
WebMock.stub_request(:get, %r{api/2/issuetypescheme/project\?projectId\=10000\z}).to_return(body: issue_type_scheme_response.to_json, headers: headers) description: 'Very new, yes',
WebMock.stub_request(:get, %r{api/2/issuetypescheme/mapping\?issueTypeSchemeId\=10126\z}).to_return(body: issue_type_mapping_response.to_json, headers: headers) 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'
]
}
]
}
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 end
it { is_expected.to eq(success: true, result: { jira: true }, data: { issuetypes: [{ id: '10001', name: 'Bug', description: 'Jira Bug' }] }) } 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
{
issuetypes: nextgen_issuetypes.select { |issue_type| !issue_type[:subtask] }.map { |issue_type| issue_type.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) }
end
end end
context 'when deployment type is server' do context 'when deployment type is server' do
......
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