Commit b435b65e authored by Subashis's avatar Subashis

DRY up code

- Move the project blob path for vulnerabiltity to model
- Removed the duplicated specs
parent a380cabb
...@@ -116,12 +116,8 @@ module Types ...@@ -116,12 +116,8 @@ module Types
def location def location
object_location = object.finding&.location object_location = object.finding&.location
if object_location['file'] object_location&.merge!(blob_path: object.blob_path) if object_location['file']
branch = object.finding.pipelines&.last&.sha || project.default_branch object_location&.merge(report_type: object.report_type)
blob_path = ::Gitlab::Routing.url_helpers.project_blob_path(object.project, File.join(branch, object_location['file']))
object_location&.merge!(blob_path: blob_path)
end
object_location&.merge(report_type: object.report_type)
end end
def scanner def scanner
......
...@@ -63,14 +63,7 @@ module VulnerabilitiesHelper ...@@ -63,14 +63,7 @@ module VulnerabilitiesHelper
def vulnerability_finding_data(vulnerability) def vulnerability_finding_data(vulnerability)
data = Vulnerabilities::FindingSerializer.new(current_user: current_user).represent(vulnerability.finding, only: FINDING_FIELDS) data = Vulnerabilities::FindingSerializer.new(current_user: current_user).represent(vulnerability.finding, only: FINDING_FIELDS)
data[:location]['blob_path'] = vulnerability.blob_path if data[:location]['file']
if data[:location]['file']
branch = vulnerability.finding.pipelines&.last&.sha || vulnerability.project.default_branch
path = project_blob_path(vulnerability.project, tree_join(branch, data[:location]['file']))
data[:location]['blob_path'] = path
end
data data
end end
end end
...@@ -161,11 +161,25 @@ module EE ...@@ -161,11 +161,25 @@ module EE
::Vulnerabilities::StatDiff.new(self) ::Vulnerabilities::StatDiff.new(self)
end end
def blob_path
return unless finding_file
::Gitlab::Routing.url_helpers.project_blob_path(project, File.join(pipeline_branch, finding_file))
end
private private
def user_notes_count_service def user_notes_count_service
@user_notes_count_service ||= ::Vulnerabilities::UserNotesCountService.new(self) # rubocop: disable CodeReuse/ServiceClass @user_notes_count_service ||= ::Vulnerabilities::UserNotesCountService.new(self) # rubocop: disable CodeReuse/ServiceClass
end end
def pipeline_branch
finding.pipelines&.last&.sha || project.default_branch
end
def finding_file
finding.file
end
end end
class_methods do class_methods do
......
...@@ -13,4 +13,4 @@ RSpec.describe GitlabSchema.types['VulnerabilityLocationCoverageFuzzing'] do ...@@ -13,4 +13,4 @@ RSpec.describe GitlabSchema.types['VulnerabilityLocationCoverageFuzzing'] do
:blob_path :blob_path
) )
end end
end end
\ No newline at end of file
...@@ -74,70 +74,6 @@ RSpec.describe GitlabSchema.types['Vulnerability'] do ...@@ -74,70 +74,6 @@ RSpec.describe GitlabSchema.types['Vulnerability'] do
end end
end end
describe 'location' do
let_it_be(:pipeline) { create(:ci_pipeline, :success, project: project) }
context 'when there is file' do
let(:query) do
%(
query {
project(fullPath: "#{project.full_path}") {
name
vulnerabilities {
nodes {
location {
... on VulnerabilityLocationSast {
file
blobPath
}
}
}
}
}
}
)
end
let_it_be(:finding) { create(:vulnerabilities_finding, report_type: "sast", pipelines: [pipeline], project: project, vulnerability: vulnerability) }
it 'includes blobPath' do
location = subject.dig('data', 'project', 'vulnerabilities', 'nodes').first['location']
expect(location).to have_key('blobPath')
expect(location['blobPath']).to eq(
::Gitlab::Routing.url_helpers.project_blob_path(project, File.join(finding.pipelines.last.sha, location['file']))
)
end
end
context 'when there is no file' do
let(:query) do
%(
query {
project(fullPath: "#{project.full_path}") {
name
vulnerabilities {
nodes {
location {
... on VulnerabilityLocationDast {
path
}
}
}
}
}
}
)
end
let_it_be(:finding) { create(:vulnerabilities_finding, report_type: "dast", pipelines: [pipeline], project: project, vulnerability: vulnerability) }
it 'does not include blob_path' do
vulnerabilities = subject.dig('data', 'project', 'vulnerabilities', 'nodes')
expect(vulnerabilities.first['location']).not_to have_key('blobPath')
end
end
end
describe 'has_solutions' do describe 'has_solutions' do
let(:query) do let(:query) do
%( %(
......
...@@ -763,4 +763,18 @@ RSpec.describe Vulnerability do ...@@ -763,4 +763,18 @@ RSpec.describe Vulnerability do
it { is_expected.to be_an_instance_of(Vulnerabilities::StatDiff) } it { is_expected.to be_an_instance_of(Vulnerabilities::StatDiff) }
end end
describe '#blob_path' do
let_it_be(:vulnerability) { create(:vulnerability) }
let_it_be(:pipeline) { create(:ci_pipeline) }
let_it_be(:finding) { create(:vulnerabilities_finding, pipelines: [pipeline], vulnerability: vulnerability) }
subject { vulnerability.blob_path }
it 'returns project blob path' do
expect(subject).to eq(
::Gitlab::Routing.url_helpers.project_blob_path(vulnerability.project, File.join(pipeline.sha, vulnerability.finding.file))
)
end
end
end end
...@@ -4,7 +4,8 @@ require 'spec_helper' ...@@ -4,7 +4,8 @@ require 'spec_helper'
RSpec.describe Vulnerabilities::FindingPresenter do RSpec.describe Vulnerabilities::FindingPresenter do
let(:presenter) { described_class.new(occurrence) } let(:presenter) { described_class.new(occurrence) }
let(:occurrence) { build_stubbed(:vulnerabilities_finding) } let(:vulnerability) { create(:vulnerability) }
let(:occurrence) { create(:vulnerabilities_finding, vulnerability: vulnerability) }
describe '#title' do describe '#title' do
subject { presenter.title } subject { presenter.title }
......
...@@ -32,6 +32,7 @@ RSpec.describe 'Query.vulnerabilities.location' do ...@@ -32,6 +32,7 @@ RSpec.describe 'Query.vulnerabilities.location' do
name name
} }
} }
blobPath
} }
... on VulnerabilityLocationDast { ... on VulnerabilityLocationDast {
hostname hostname
...@@ -45,6 +46,7 @@ RSpec.describe 'Query.vulnerabilities.location' do ...@@ -45,6 +46,7 @@ RSpec.describe 'Query.vulnerabilities.location' do
startLine startLine
vulnerableClass vulnerableClass
vulnerableMethod vulnerableMethod
blobPath
} }
... on VulnerabilityLocationSecretDetection { ... on VulnerabilityLocationSecretDetection {
endLine endLine
...@@ -52,6 +54,7 @@ RSpec.describe 'Query.vulnerabilities.location' do ...@@ -52,6 +54,7 @@ RSpec.describe 'Query.vulnerabilities.location' do
startLine startLine
vulnerableClass vulnerableClass
vulnerableMethod vulnerableMethod
blobPath
} }
} }
QUERY QUERY
...@@ -112,6 +115,8 @@ RSpec.describe 'Query.vulnerabilities.location' do ...@@ -112,6 +115,8 @@ RSpec.describe 'Query.vulnerabilities.location' do
create(:vulnerability, project: project, report_type: :dependency_scanning) create(:vulnerability, project: project, report_type: :dependency_scanning)
end end
let_it_be(:pipeline) { create(:ci_pipeline, :success, project: project) }
let_it_be(:metadata) do let_it_be(:metadata) do
{ {
location: { location: {
...@@ -130,7 +135,8 @@ RSpec.describe 'Query.vulnerabilities.location' do ...@@ -130,7 +135,8 @@ RSpec.describe 'Query.vulnerabilities.location' do
create( create(
:vulnerabilities_finding, :vulnerabilities_finding,
vulnerability: vulnerability, vulnerability: vulnerability,
raw_metadata: metadata.to_json raw_metadata: metadata.to_json,
pipelines: [pipeline]
) )
end end
...@@ -149,6 +155,8 @@ RSpec.describe 'Query.vulnerabilities.location' do ...@@ -149,6 +155,8 @@ RSpec.describe 'Query.vulnerabilities.location' do
create(:vulnerability, project: project, report_type: :sast) create(:vulnerability, project: project, report_type: :sast)
end end
let_it_be(:pipeline) { create(:ci_pipeline, :success, project: project) }
let_it_be(:metadata) do let_it_be(:metadata) do
{ {
location: { location: {
...@@ -156,7 +164,8 @@ RSpec.describe 'Query.vulnerabilities.location' do ...@@ -156,7 +164,8 @@ RSpec.describe 'Query.vulnerabilities.location' do
method: 'vulnerable_method', method: 'vulnerable_method',
file: 'vulnerable_file', file: 'vulnerable_file',
start_line: '420', start_line: '420',
end_line: '666' end_line: '666',
blob_path: 'blob/vulnerable_file'
} }
} }
end end
...@@ -165,7 +174,8 @@ RSpec.describe 'Query.vulnerabilities.location' do ...@@ -165,7 +174,8 @@ RSpec.describe 'Query.vulnerabilities.location' do
create( create(
:vulnerabilities_finding, :vulnerabilities_finding,
vulnerability: vulnerability, vulnerability: vulnerability,
raw_metadata: metadata.to_json raw_metadata: metadata.to_json,
pipelines: [pipeline]
) )
end end
...@@ -186,6 +196,8 @@ RSpec.describe 'Query.vulnerabilities.location' do ...@@ -186,6 +196,8 @@ RSpec.describe 'Query.vulnerabilities.location' do
create(:vulnerability, project: project, report_type: :secret_detection) create(:vulnerability, project: project, report_type: :secret_detection)
end end
let_it_be(:pipeline) { create(:ci_pipeline, :success, project: project) }
let_it_be(:metadata) do let_it_be(:metadata) do
{ {
location: { location: {
...@@ -202,7 +214,8 @@ RSpec.describe 'Query.vulnerabilities.location' do ...@@ -202,7 +214,8 @@ RSpec.describe 'Query.vulnerabilities.location' do
create( create(
:vulnerabilities_finding, :vulnerabilities_finding,
vulnerability: vulnerability, vulnerability: vulnerability,
raw_metadata: metadata.to_json raw_metadata: metadata.to_json,
pipelines: [pipeline]
) )
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