Commit 070b79a3 authored by Mehmet Emin INAC's avatar Mehmet Emin INAC

Delegate `project_id` to `pipeline`

Instead of reading the attribute directly from pipeline, we should
delegate the `project_id` call to `pipeline`.
parent 88faaeef
......@@ -111,7 +111,7 @@ module Security
def all_security_findings
pipeline.security_findings
.with_build_and_artifacts
.with_pipeline_entities
.with_scan
.with_scanner
.deduplicated
......
......@@ -38,7 +38,7 @@ module Security
.where('vulnerability_feedback.project_fingerprint = security_findings.project_fingerprint'))
end
scope :ordered, -> { order(severity: :desc, confidence: :desc, id: :asc) }
scope :with_build_and_artifacts, -> { includes(build: [:job_artifacts, :pipeline]) }
scope :with_pipeline_entities, -> { includes(build: [:job_artifacts, pipeline: :project]) }
scope :with_scan, -> { includes(:scan) }
scope :with_scanner, -> { includes(:scanner) }
scope :deduplicated, -> { where(deduplicated: true) }
......
......@@ -8,6 +8,8 @@ module Gitlab
attr_reader :created_at, :type, :pipeline, :findings, :scanners, :identifiers
attr_accessor :scan, :scanned_resources, :error
delegate :project_id, to: :pipeline
def initialize(type, pipeline, created_at)
@type = type
@pipeline = pipeline
......@@ -55,15 +57,6 @@ module Gitlab
def primary_scanner
scanners.first&.second
end
# It's important to read the `project_id` attribute instead of calling
# `project_id` on pipeline. Because the `Ci::Pipeline` delegates the `project_id`
# call to the `project` relation even though it already has the attribute called
# `project_id`. By reading attribute directly from the entity, we are preventing
# an extra database query to load the project.
def project_id
pipeline&.read_attribute(:project_id)
end
end
end
end
......
......@@ -91,7 +91,7 @@ RSpec.describe Security::FindingsFinder do
end
it 'does not cause N+1 queries' do
expect { finder_result }.not_to exceed_query_limit(8)
expect { finder_result }.not_to exceed_query_limit(9)
end
describe '#current_page' do
......
......@@ -6,9 +6,11 @@ RSpec.describe Gitlab::Ci::Reports::Security::Report do
let_it_be(:pipeline) { create(:ci_pipeline) }
let(:created_at) { 2.weeks.ago }
let(:report) { described_class.new('sast', pipeline, created_at) }
subject(:report) { described_class.new('sast', pipeline, created_at) }
it { expect(report.type).to eq('sast') }
it { is_expected.to delegate_method(:project_id).to(:pipeline) }
describe '#add_scanner' do
let(:scanner) { create(:ci_reports_security_scanner, external_id: 'find_sec_bugs') }
......@@ -137,10 +139,4 @@ RSpec.describe Gitlab::Ci::Reports::Security::Report do
it { is_expected.to eq(scanner_1) }
end
describe '#project_id' do
subject { report.project_id }
it { is_expected.to eq(pipeline.project_id) }
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