Commit 1f632da5 authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch '9424-allow-link-vulnerability-with-issue-from-other-project' into 'master'

Allow to link vulnerability with issues from other projects

See merge request gitlab-org/gitlab!36410
parents 6043f317 b11c3332
...@@ -3,10 +3,5 @@ ...@@ -3,10 +3,5 @@
module Vulnerabilities module Vulnerabilities
class IssueLinkPolicy < BasePolicy class IssueLinkPolicy < BasePolicy
delegate { @subject.vulnerability&.project } delegate { @subject.vulnerability&.project }
with_scope :subject
condition(:cross_project_issue) { @subject.vulnerability&.project != @subject.issue&.project }
rule { cross_project_issue }.prevent :admin_vulnerability_issue_link
end end
end end
---
title: Allow to link vulnerability with issues from other projects
merge_request: 36410
author:
type: changed
...@@ -45,11 +45,12 @@ module API ...@@ -45,11 +45,12 @@ module API
end end
params do params do
requires :target_issue_iid, type: Integer, desc: 'The IID of an issue to relate to' requires :target_issue_iid, type: Integer, desc: 'The IID of an issue to relate to'
optional :target_project_id, type: String, desc: 'The ID of the target project'
optional :link_type, type: String, default: 'related', desc: 'Link type' optional :link_type, type: String, default: 'related', desc: 'Link type'
end end
post ':id/issue_links' do post ':id/issue_links' do
vulnerability = find_and_authorize_vulnerability!(:admin_vulnerability_issue_link) vulnerability = find_and_authorize_vulnerability!(:admin_vulnerability_issue_link)
issue = find_project_issue(params[:target_issue_iid], vulnerability.project_id) issue = find_project_issue(params[:target_issue_iid], params[:target_project_id].presence || vulnerability.project_id)
response = ::VulnerabilityIssueLinks::CreateService.new( response = ::VulnerabilityIssueLinks::CreateService.new(
current_user, vulnerability, issue, link_type: params[:link_type]).execute current_user, vulnerability, issue, link_type: params[:link_type]).execute
......
...@@ -25,12 +25,6 @@ RSpec.describe Vulnerabilities::IssueLinkPolicy do ...@@ -25,12 +25,6 @@ RSpec.describe Vulnerabilities::IssueLinkPolicy do
it { is_expected.to be_disallowed(:admin_vulnerability_issue_link) } it { is_expected.to be_disallowed(:admin_vulnerability_issue_link) }
end end
context 'with missing issue' do
let(:issue) { nil }
it { is_expected.to be_disallowed(:admin_vulnerability_issue_link) }
end
context 'when issue and link belong to the same project' do context 'when issue and link belong to the same project' do
it { is_expected.to be_allowed(:admin_vulnerability_issue_link) } it { is_expected.to be_allowed(:admin_vulnerability_issue_link) }
end end
...@@ -38,7 +32,7 @@ RSpec.describe Vulnerabilities::IssueLinkPolicy do ...@@ -38,7 +32,7 @@ RSpec.describe Vulnerabilities::IssueLinkPolicy do
context "when issue and link don't belong to the same project" do context "when issue and link don't belong to the same project" do
let(:issue) { create(:issue) } let(:issue) { create(:issue) }
it { is_expected.to be_disallowed(:admin_vulnerability_issue_link) } it { is_expected.to be_allowed(:admin_vulnerability_issue_link) }
end end
end end
end end
...@@ -81,6 +81,48 @@ RSpec.describe API::VulnerabilityIssueLinks do ...@@ -81,6 +81,48 @@ RSpec.describe API::VulnerabilityIssueLinks do
end end
end end
context 'with valid target_project_id and target_issue_iid params' do
let_it_be(:other_issue) { create(:issue) }
let(:target_project_id) { other_issue.project_id }
let(:params) { { target_issue_iid: other_issue.iid, target_project_id: target_project_id } }
context 'when target_project_id is invalid' do
let(:target_project_id) { 0 }
it 'responds with "not found" and specific error message' do
create_issue_link
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'when user does not have access to the project' do
it 'responds with "not found" and specific error message' do
create_issue_link
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'when user is authorized with proper permissions to the project' do
before do
other_issue.project.add_developer(user)
end
it 'creates a new vulnerability-issue link' do
create_issue_link
expect(response).to have_gitlab_http_status(:created)
expect(response).to match_response_schema('public_api/v4/vulnerability_issue_link', dir: 'ee')
expect(json_response['id']).to eq Vulnerabilities::IssueLink.last.id
expect(json_response['issue']['id']).to eq other_issue.id
expect(json_response['vulnerability']['id']).to eq vulnerability.id
end
end
end
context 'with unknown issue ID' do context 'with unknown issue ID' do
let(:target_issue_iid) { 0 } let(:target_issue_iid) { 0 }
......
...@@ -46,8 +46,14 @@ RSpec.describe VulnerabilityIssueLinks::CreateService do ...@@ -46,8 +46,14 @@ RSpec.describe VulnerabilityIssueLinks::CreateService do
context 'with missing issue' do context 'with missing issue' do
let(:service) { described_class.new(user, vulnerability, nil) } let(:service) { described_class.new(user, vulnerability, nil) }
it 'responds with an error' do it 'responds with an error', :aggregate_failures do
expect { create_issue_link }.to raise_error(Gitlab::Access::AccessDeniedError) expect { create_issue_link }.not_to change { Vulnerabilities::IssueLink.count }
response = create_issue_link
expect(response).to be_error
expect(response.http_status).to eq 422
expect(response.message).to eq "Issue can't be blank"
end end
end end
...@@ -88,8 +94,8 @@ RSpec.describe VulnerabilityIssueLinks::CreateService do ...@@ -88,8 +94,8 @@ RSpec.describe VulnerabilityIssueLinks::CreateService do
context 'when trying to relate an issue of a different project' do context 'when trying to relate an issue of a different project' do
let(:issue) { create(:issue) } let(:issue) { create(:issue) }
it 'raises an access error' do it 'creates a vulnerability-issue link' do
expect { create_issue_link }.to raise_error(Gitlab::Access::AccessDeniedError) expect { create_issue_link }.to change { Vulnerabilities::IssueLink.count }.by(1)
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