Reimport with group_by no longer 500s on duplicate same-name finding groups - #16065
Merged
Merged
Conversation
Auto grouping looked up the group with get_or_create keyed on (test, creator, name). A reimport by a different user than the group's creator therefore made a second same-name group in the same test, and once a test held two same-name groups for the importing user (for example after two imports raced), get_or_create raised Finding_Group.MultipleObjectsReturned and reimport-scan returned a 500. The lone-finding path hit the same duplicates through a get() inside a bare except, which swallowed the error and left the finding ungrouped. Look the group up by (test, name) only and reuse the oldest one by id, creating a group (owned by the importing user) only when none exists. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
blakeaowens
approved these changes
Sep 23, 2026
devGregA
approved these changes
Sep 23, 2026
github-merge-queue
Bot
removed this pull request from the merge queue due to failed status checks
Sep 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
[sc-15673]
Description
POST /api/v2/reimport-scan/withgroup_byset could fail with a 500:add_findings_to_auto_groupindojo/finding/helper.pylooked the group up withFinding_Group.objects.get_or_create(test=test, creator=creator, name=name[:255]). Keying on thecreator caused two problems:
created a second group with the same name in the same test and split the findings between them.
same test can leave this, since
get_or_createhas no constraint behind it),get_or_createraised
MultipleObjectsReturnedand the whole reimport returned a 500. This is the path the APItakes by default, because
create_finding_groups_for_all_findingsdefaults to true.The lone-finding branch (
create_finding_groups_for_all_findings=falsewith one finding) had arelated problem. It called
Finding_Group.objects.get(test=test, name=name)inside a bareexcept,so duplicates raised
MultipleObjectsReturned, theexceptswallowed it, and the finding was leftout of any group.
The fix treats an auto group as one per (test, name). A new helper,
get_or_create_auto_finding_group, filters by test and truncated name, orders by id, and reuses theoldest group whoever created it. It only creates a group, owned by the importing user, when none
exists. The lone-finding branch uses the same oldest-group lookup in place of the
get()and bareexcept, and now also truncates the name to 255 characters to match the stored value.This PR adds no uniqueness constraint or migration. Existing databases can already hold duplicate
same-name groups, and a constraint would fail on them. The lookup tolerates those rows and stops
the reimport path from creating new duplicates when a different user reimports. Two imports that
race on the same test can still both insert a group. After this change the next reimport resolves
to the oldest one instead of failing.
Test results
New
unittests/test_reimport_finding_group_duplicates.pyreimports a one-finding Trivy reportthrough the API with
group_by=finding_title,close_old_findings=trueanddo_not_reactivate=true:create_finding_groups_for_all_findingstrue and false: the reimport returns 201 and the finding lands only in the oldest group. Before
the fix, the true case returned 500 with
MultipleObjectsReturnedraised fromadd_findings_to_auto_group, and the false case left the finding ungrouped.group is created. Before the fix, the true case created a second group owned by the importer.
Before the fix, 3 of the 5 tests failed. After the fix, all 5 pass. These suites also pass with the
change:
test_import_reimport,test_reimport_batch_flush,test_reimport_final_drain,test_reimport_prefetch,test_reimporter_persist_seam,test_apiv2_scan_import_options(148tests, 1 skipped), plus
test_importers_importer,test_jira_import_and_pushing_api,test_jira_finding_group_perf,test_finding_group_cleanup,test_reimport_group_jira_sync,test_finding_group_filter_context,test_update_import_historyandtest_importers_performance(155 tests, 7 skipped).
ruff checkwith the pinned version passes on both changed files.After rebasing on the latest
bugfix, the new module plustest_import_reimport,test_finding_group_cleanup,test_reimport_group_jira_syncandtest_apiv2_scan_import_optionsran 146 tests, OK.Documentation
No documentation change. The documented behavior of
group_bydoes not change.Checklist
dev. (Bug fix: rebased on the latestbugfix.)dev.bugfixbranch.🤖 Generated with Claude Code