Preload the tracked context when comparing findings
🤖 AI wrote this code from my instructions.Unreviewed AI output often lands on reviewers and costs them time.
This MR is human verified: I read the full diff and ran the tests myself, because your time is as valuable as mine.
What does this MR do and why?
Vulnerabilities::FindingSignature and Vulnerabilities::Finding each define a scope named eager_load_comparison_entities, and the two have drifted apart:
FindingSignature:includes(finding: [:scanner, :primary_identifier, :project_tracked_context])Finding:includes(:scanner, :primary_identifier)
Security::OverrideUuidsService#override_for runs once per report finding and merges results from both scopes: one lookup by signature, one by location. same_context_finding_from and existing_finding_from_default_branch then read project_tracked_context on every item in that merged list. The signature-matched half comes preloaded. The location-matched half does not, so each distinct location-matched finding with a non-null tracked context triggers an extra security_project_tracked_contexts query.
This MR adds :project_tracked_context to the Finding scope, matching its sibling. The Finding scope has exactly one caller in the codebase, override_uuids_service.rb:155, so no other behaviour changes.
A temporary query probe on override_uuids_service_spec.rb counted queries against security_project_tracked_contexts during execute: 4 before this change, 3 after, in a scenario with one location-only match. That is the one query this change removes. The probe itself is not part of this MR.
The new spec asserts the scope loads all three associations, rather than asserting a query count in the service. The bug is a scope missing an association. A be_loaded check on the scope says that directly and cannot pass for the wrong reason, while a query count in the service spec would be brittle and would not point at the actual defect.
References
None.
How to set up and validate locally
bundle exec rspec ee/spec/models/vulnerabilities/finding_spec.rb
bundle exec rspec ee/spec/services/security/override_uuids_service_spec.rbfinding_spec.rb: 479 examples, 0 failures, 1 pending.
override_uuids_service_spec.rb and override_in_batch_spec.rb together: 19 examples, 0 failures.
Revert the one-line scope change and the new example fails on exactly the new expectation:
Failure/Error: expect(loaded.association(:project_tracked_context)).to be_loaded
expected `...BelongsToAssociation...loaded?` to be truthy, got falseThe scanner and primary_identifier expectations in the same example still pass.
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.