Commit 5482501f authored by Subashis's avatar Subashis

Address suggestions about improvments

- Fix specs
- Address comments to make code cleaner
- Use blob_path method in vulnerability_presenter
parent b435b65e
......@@ -116,8 +116,8 @@ module Types
def location
object_location = object.finding&.location
object_location&.merge!(blob_path: object.blob_path) if object_location['file']
object_location&.merge(report_type: object.report_type)
object_location.merge!(blob_path: object.blob_path).compact!
object_location.merge(report_type: object.report_type)
end
def scanner
......
......@@ -63,7 +63,7 @@ module VulnerabilitiesHelper
def vulnerability_finding_data(vulnerability)
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']
data[:location].merge!('blob_path' => vulnerability.blob_path).compact!
data
end
end
......@@ -127,6 +127,8 @@ module EE
:cve_value, :cwe_value, :other_identifier_values,
to: :finding, allow_nil: true
delegate :file, to: :finding, prefix: true, private: true
def to_reference(from = nil, full: false)
project
.to_reference_base(from, full: full)
......@@ -164,7 +166,7 @@ module EE
def blob_path
return unless finding_file
::Gitlab::Routing.url_helpers.project_blob_path(project, File.join(pipeline_branch, finding_file))
::Gitlab::Routing.url_helpers.project_blob_path(project, File.join(finding.pipeline_branch, finding_file))
end
private
......@@ -172,14 +174,6 @@ module EE
def user_notes_count_service
@user_notes_count_service ||= ::Vulnerabilities::UserNotesCountService.new(self) # rubocop: disable CodeReuse/ServiceClass
end
def pipeline_branch
finding.pipelines&.last&.sha || project.default_branch
end
def finding_file
finding.file
end
end
class_methods do
......
......@@ -372,6 +372,10 @@ module Vulnerabilities
Gitlab::UUID.v5?(uuid) ? uuid : Gitlab::UUID.v5(uuid_v5_name)
end
def pipeline_branch
pipelines&.last&.sha || project.default_branch
end
protected
def first_fingerprint
......
......@@ -34,7 +34,7 @@ class VulnerabilityPresenter < Gitlab::View::Presenter::Delegated
def blob_path
return unless file
path_with_line_number(project_blob_path(vulnerability.project, File.join(pipeline_branch, file)))
path_with_line_number(vulnerability.blob_path)
end
def scanner
......
......@@ -42,7 +42,7 @@ RSpec.describe Projects::Security::VulnerabilitiesController do
end
context "when there's no attached pipeline" do
let_it_be(:finding) { create(:vulnerabilities_finding, vulnerability: vulnerability) }
let_it_be(:finding) { create(:vulnerabilities_finding, vulnerability: vulnerability, project: vulnerability.project ) }
it 'renders the vulnerability page' do
show_vulnerability
......
......@@ -773,7 +773,7 @@ RSpec.describe Vulnerability do
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))
"/#{vulnerability.project.namespace.path}/#{vulnerability.project.name}/-/blob/#{pipeline.sha}/#{vulnerability.finding.file}"
)
end
end
......
......@@ -4,8 +4,7 @@ require 'spec_helper'
RSpec.describe Vulnerabilities::FindingPresenter do
let(:presenter) { described_class.new(occurrence) }
let(:vulnerability) { create(:vulnerability) }
let(:occurrence) { create(:vulnerabilities_finding, vulnerability: vulnerability) }
let(:occurrence) { build_stubbed(:vulnerabilities_finding) }
describe '#title' do
subject { presenter.title }
......
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