Commit 12be4424 authored by Sean McGivern's avatar Sean McGivern

Merge branch 'fix-vulnerability-feeback-n-plus-1' into 'master'

Fix N+1 queries when loading vulnerability feedback

See merge request gitlab-org/gitlab-ee!14638
parents fe4c998d e1dad9f6
---
title: Fix N+1 queries in vulnerabilities API
merge_request: 14638
author:
type: performance
......@@ -50,11 +50,15 @@ module API
get ':id/vulnerabilities' do
authorize! :read_project_security_dashboard, user_project
vulnerability_occurrences = Kaminari.paginate_array(
vulnerability_occurrences_by(declared_params.merge(project: user_project))
vulnerability_occurrences = paginate(
Kaminari.paginate_array(
vulnerability_occurrences_by(declared_params.merge(project: user_project))
)
)
present paginate(vulnerability_occurrences),
Gitlab::Vulnerabilities::OccurrencesPreloader.preload_feedback!(vulnerability_occurrences)
present vulnerability_occurrences,
with: ::Vulnerabilities::OccurrenceEntity,
request: GrapeRequestProxy.new(request, current_user)
end
......
......@@ -9,8 +9,15 @@ module Gitlab
class OccurrencesPreloader
def self.preload!(occurrences)
occurrences.all_preloaded.tap do |occurrences|
occurrences.each(&:issue_feedback)
occurrences.each(&:dismissal_feedback)
preload_feedback!(occurrences)
end
end
def self.preload_feedback!(occurrences)
occurrences.each do |occurrence|
occurrence.dismissal_feedback
occurrence.issue_feedback
occurrence.merge_request_feedback
end
end
end
......
......@@ -52,6 +52,14 @@ describe API::Vulnerabilities do
expect(json_response.map { |v| v['report_type'] }.uniq).to match_array %w[dependency_scanning sast]
end
it 'does not have N+1 queries' do
control_count = ActiveRecord::QueryRecorder.new do
get api("/projects/#{project.id}/vulnerabilities", user), params: { report_type: 'dependency_scanning' }
end.count
expect { get api("/projects/#{project.id}/vulnerabilities", user) }.not_to exceed_query_limit(control_count)
end
describe 'filtering' do
it 'returns vulnerabilities with sast report_type' do
occurrence_count = (sast_report.occurrences.count - 1).to_s
......
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