Commit e98eae64 authored by Michał Zając's avatar Michał Zając Committed by Markus Koller

Match issue types schema using project key on JIRA Server

parent 66ce587c
......@@ -89,6 +89,8 @@ module EE
#
# @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 }
......@@ -103,13 +105,24 @@ module EE
# @return [Array] the array of IDs
def project_issuetype_ids
strong_memoize(:project_issuetype_ids) do
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'] }
if data_fields.deployment_server?
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'] }
elsif data_fields.deployment_cloud?
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'] }
else
raise NotImplementedError
end
end
end
......
......@@ -15,6 +15,11 @@ RSpec.describe JiraService do
}
end
before do
allow(jira_service.data_fields).to receive(:deployment_cloud?).and_return(true)
allow(jira_service.data_fields).to receive(:deployment_server?).and_return(false)
end
describe 'validations' do
it 'validates presence of project_key if issues_enabled' do
jira_service.project_key = ''
......@@ -111,80 +116,150 @@ RSpec.describe JiraService do
end
context 'when vulnerabilities integration is enabled' do
let(:project_info_result) { { 'id' => '10000' } }
before do
allow(jira_service.project).to receive(:jira_vulnerabilities_integration_enabled?).and_return(true)
end
let(:issue_type_scheme_response) do
{
values: [
{
issueTypeScheme: {
id: '10126',
name: 'GV: Software Development Issue Type Scheme',
defaultIssueTypeId: '10001'
context 'when deployment type is cloud' do
let(:project_info_result) { { 'id' => '10000' } }
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'
},
projectIds: [
'10000'
]
}
]
}
end
{
issueTypeSchemeId: '10126',
issueTypeId: '10001'
}
]
}
end
let(:issue_type_mapping_response) do
{
values: [
let(:issue_types_response) 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
},
{
issueTypeSchemeId: '10126',
issueTypeId: '10003'
id: '10001',
description: 'Jira Bug',
name: 'Bug',
untranslatedName: 'Bug',
subtask: false,
avatarId: 10303
},
{
issueTypeSchemeId: '10126',
issueTypeId: '10001'
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
before do
WebMock.stub_request(:get, /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, /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, /api\/2\/issuetype\z/).to_return(body: issue_types_response.to_json, headers: headers)
WebMock.stub_request(:get, /api\/2\/issuetypescheme\/project\?projectId\=10000\z/).to_return(body: issue_type_scheme_response.to_json, headers: headers)
WebMock.stub_request(:get, /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
let(:issue_types_response) do
[
context 'when deployment type is server' do
let(:project_info_result) 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
"id": "10000",
"issueTypes": issue_types_response
}
]
end
end
before do
allow(jira_service.project).to receive(:jira_vulnerabilities_integration_enabled?).and_return(true)
let(:issue_types_response) do
[
{
"avatarId": 10318,
"description": "A task that needs to be done.",
"iconUrl": "http://jira.reali.sh:8080/secure/viewavatar?size=xsmall&avatarId=10318&avatarType=issuetype",
"id": "10003",
"name": "Task",
"self": "http://jira.reali.sh:8080/rest/api/2/issuetype/10003",
"subtask": false
},
{
"description": "The sub-task of the issue",
"iconUrl": "http://jira.reali.sh:8080/images/icons/issuetypes/subtask_alternate.png",
"id": "10000",
"name": "Sub-task",
"self": "http://jira.reali.sh:8080/rest/api/2/issuetype/10000",
"subtask": true
},
{
"description": "Created by Jira Software - do not edit or delete. Issue type for a user story.",
"iconUrl": "http://jira.reali.sh:8080/images/icons/issuetypes/story.svg",
"id": "10002",
"name": "Story",
"self": "http://jira.reali.sh:8080/rest/api/2/issuetype/10002",
"subtask": false
},
{
"avatarId": 10303,
"description": "A problem which impairs or prevents the functions of the product.",
"iconUrl": "http://jira.reali.sh:8080/secure/viewavatar?size=xsmall&avatarId=10303&avatarType=issuetype",
"id": "10004",
"name": "Bug",
"self": "http://jira.reali.sh:8080/rest/api/2/issuetype/10004",
"subtask": false
},
{
"description": "Created by Jira Software - do not edit or delete. Issue type for a big user story that needs to be broken down.",
"iconUrl": "http://jira.reali.sh:8080/images/icons/issuetypes/epic.svg",
"id": "10001",
"name": "Epic",
"self": "http://jira.reali.sh:8080/rest/api/2/issuetype/10001",
"subtask": false
}
]
end
WebMock.stub_request(:get, /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, /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, /api\/2\/issuetype\z/).to_return(body: issue_types_response.to_json, headers: headers)
WebMock.stub_request(:get, /api\/2\/issuetypescheme\/project\?projectId\=10000\z/).to_return(body: issue_type_scheme_response.to_json, headers: headers)
WebMock.stub_request(:get, /api\/2\/issuetypescheme\/mapping\?issueTypeSchemeId\=10126\z/).to_return(body: issue_type_mapping_response.to_json, headers: headers)
end
before do
allow(jira_service.data_fields).to receive(:deployment_cloud?).and_return(false)
allow(jira_service.data_fields).to receive(:deployment_server?).and_return(true)
it { is_expected.to eq(success: true, result: { jira: true }, data: { issuetypes: [{ id: '10001', name: 'Bug', description: 'Jira Bug' }] }) }
WebMock.stub_request(:get, /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, /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: { issuetypes: [{ description: "A task that needs to be done.", id: "10003", name: "Task" }, { description: "Created by Jira Software - do not edit or delete. Issue type for a user story.", id: "10002", name: "Story" }, { description: "A problem which impairs or prevents the functions of the product.", id: "10004", name: "Bug" }, { description: "Created by Jira Software - do not edit or delete. Issue type for a big user story that needs to be broken down.", id: "10001", name: "Epic" }] }) }
end
end
end
end
......
......@@ -62,6 +62,7 @@ FactoryBot.define do
project_key { nil }
vulnerabilities_enabled { false }
vulnerabilities_issuetype { nil }
deployment_type { 'cloud' }
end
before(:create) do |service, evaluator|
......@@ -72,7 +73,7 @@ FactoryBot.define do
jira_issue_transition_id: evaluator.jira_issue_transition_id,
username: evaluator.username, password: evaluator.password, issues_enabled: evaluator.issues_enabled,
project_key: evaluator.project_key, vulnerabilities_enabled: evaluator.vulnerabilities_enabled,
vulnerabilities_issuetype: evaluator.vulnerabilities_issuetype
vulnerabilities_issuetype: evaluator.vulnerabilities_issuetype, deployment_type: evaluator.deployment_type
)
end
end
......
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