Set tracked context on trigger-created vulnerability reads
What does this MR do and why?
Fixes a regression from commit d9418042 ("Add finding to upsert service & callers"). It only happens when the turn_off_vulnerability_read_create_db_trigger_function flag is off (the default while it rolls out).
The problem
CreateServiceBasesets the default-branch tracked context on the finding.- In the transaction,
finding.update!(vulnerability_id: ...)fires theinsert_or_update_vulnerability_readsDB trigger. The trigger doesn't know about the tracked context column, so the newvulnerability_readsrow has a NULL context. UpsertServicesees the read already exists and runsupdate_all(security_project_tracked_context_id: X).- That update is now scoped with
by_tracked_context(X), which doesn't match the NULL row. The context is never set.
The existing spec passes only because specs enable all feature flags, which skips the trigger path. StarboardVulnerabilityCreateService follows the same pattern and is affected the same way.
The fix
In ee/app/services/vulnerabilities/reads/upsert_service.rb, the update now also matches reads with no context: reads.by_tracked_context([context_id, nil]). Reads assigned to a different context are still left alone.
References
- #608065 (closed) (issue where this was found)
- #553939 (trigger flag rollout)
- d9418042 (regression commit)
How to set up and validate locally
- In a Rails console, run
Feature.disable(:turn_off_vulnerability_read_create_db_trigger_function). - Create a vulnerability manually, either from Secure > Vulnerability report > Submit vulnerability, or with the
vulnerabilityCreateGraphQL mutation. - In the console, check that
Vulnerability.last.vulnerability_read.security_project_tracked_context_idis present and equalsVulnerability.last.finding.security_project_tracked_context_id. Before the fix it was nil.
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.