Commit 7cf74b8f authored by Bob Van Landuyt's avatar Bob Van Landuyt

Merge branch...

Merge branch '329235-error-creating-new-jira-issue-from-vulnerability-after-previous-jira-issue-is-deleted' into 'master'

Check for deleted JIRA issue before creating one from Vulnerability

See merge request gitlab-org/gitlab!78323
parents 04e6c0b9 cb2ee27d
...@@ -18,5 +18,7 @@ module Vulnerabilities ...@@ -18,5 +18,7 @@ module Vulnerabilities
message: N_('already has a "created" issue link') message: N_('already has a "created" issue link')
}, },
if: :created? if: :created?
scope :created_for_vulnerability, -> (vulnerability) { where(vulnerability: vulnerability, link_type: 'created') }
end end
end end
...@@ -13,6 +13,9 @@ module VulnerabilityExternalIssueLinks ...@@ -13,6 +13,9 @@ module VulnerabilityExternalIssueLinks
def execute def execute
return error(['External provider service is not configured to create issues.']) unless external_provider_service&.configured_to_create_issues_from_vulnerabilities? return error(['External provider service is not configured to create issues.']) unless external_provider_service&.configured_to_create_issues_from_vulnerabilities?
remove_links_for_nonexistent_external_issues
return error(external_issue_link.errors.full_messages) unless external_issue_link.valid? return error(external_issue_link.errors.full_messages) unless external_issue_link.valid?
external_issue = create_external_issue external_issue = create_external_issue
...@@ -57,6 +60,14 @@ module VulnerabilityExternalIssueLinks ...@@ -57,6 +60,14 @@ module VulnerabilityExternalIssueLinks
) )
end end
def remove_links_for_nonexistent_external_issues
Vulnerabilities::ExternalIssueLink.created_for_vulnerability(vulnerability).each do |link|
unless external_provider_service.find_issue(link.external_issue_key)
link.destroy
end
end
end
def success def success
ServiceResponse.success(payload: { record: external_issue_link }) ServiceResponse.success(payload: { record: external_issue_link })
end end
......
...@@ -45,4 +45,12 @@ RSpec.describe Vulnerabilities::ExternalIssueLink do ...@@ -45,4 +45,12 @@ RSpec.describe Vulnerabilities::ExternalIssueLink do
end end
end end
end end
describe 'created_for_vulnerability' do
let_it_be(:external_links) { create_list(:vulnerabilities_external_issue_link, 2, :created) }
it 'gets external issue links for the specified vulnerability' do
expect(described_class.created_for_vulnerability(external_links.first.vulnerability).take).to eq(external_links.first)
end
end
end end
...@@ -38,10 +38,35 @@ RSpec.describe VulnerabilityExternalIssueLinks::CreateService do ...@@ -38,10 +38,35 @@ RSpec.describe VulnerabilityExternalIssueLinks::CreateService do
create(:vulnerabilities_external_issue_link, vulnerability: vulnerability) create(:vulnerabilities_external_issue_link, vulnerability: vulnerability)
end end
it { is_expected.not_to be_success } context 'and the external issue exists' do
let!(:jira_issue) { ExternalIssue.new('JIRA-123', project) }
it 'returns response with error messages' do before do
expect(subject.message).to eq(['Vulnerability already has a "created" issue link']) allow(jira_integration).to receive(:find_issue).and_return(jira_issue)
end
it { is_expected.not_to be_success }
it 'returns response with error messages' do
expect(subject.message).to match_array(['Vulnerability already has a "created" issue link'])
end
end
context 'and the external issue does not exist' do
let(:jira_issue_id) { nil }
let(:errors) { {} }
let(:jira_issue) { double(has_errors?: errors.present?, id: jira_issue_id, errors: errors) }
before do
allow(jira_integration).to receive(:create_issue).and_return(jira_issue)
allow(jira_integration).to receive(:find_issue).and_return(nil)
end
it 'creates issue using jira service' do
expect(jira_integration).to receive(:create_issue).with("Investigate vulnerability: #{vulnerability.title}", kind_of(String), user)
subject
end
end end
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