Commit cb2ee27d authored by Jonathan Schafer's avatar Jonathan Schafer Committed by Bob Van Landuyt

Check for removed external issues before creation

Changelog: changed
EE:true
parent fc586beb
......@@ -18,5 +18,7 @@ module Vulnerabilities
message: N_('already has a "created" issue link')
},
if: :created?
scope :created_for_vulnerability, -> (vulnerability) { where(vulnerability: vulnerability, link_type: 'created') }
end
end
......@@ -13,6 +13,9 @@ module VulnerabilityExternalIssueLinks
def execute
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?
external_issue = create_external_issue
......@@ -57,6 +60,14 @@ module VulnerabilityExternalIssueLinks
)
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
ServiceResponse.success(payload: { record: external_issue_link })
end
......
......@@ -45,4 +45,12 @@ RSpec.describe Vulnerabilities::ExternalIssueLink do
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
......@@ -38,10 +38,35 @@ RSpec.describe VulnerabilityExternalIssueLinks::CreateService do
create(:vulnerabilities_external_issue_link, vulnerability: vulnerability)
end
context 'and the external issue exists' do
let!(:jira_issue) { ExternalIssue.new('JIRA-123', project) }
before do
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 eq(['Vulnerability already has a "created" issue link'])
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
......
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