Fix nil current_status fallback leaking items into wrong filter
What does this MR do and why?
Fixes a bug where work items with no current_status record (pre-feature items)
incorrectly appeared in board columns and issue list filters for a custom status
that was not their lifecycle's default open status.
Solves https://gitlab.com/gitlab-com/request-for-help/-/work_items/4279
Root cause
with_system_defined_status includes a nil-current_status fallback: when an
item has no current_status record, it falls back to the item's open/closed state
to determine which status it logically belongs to. This fallback is correct for
namespaces using the system-defined lifecycle, where "To do" (id: 1) is always the
default open status.
The problem occurs when a namespace has a custom lifecycle where the
default_open_status is a different status — for example, "Open" (category:
triage) — and separately has a "Backlog" status (category: to_do) that was
converted from the system-defined "To do". Because with_system_defined_status
only ever checked the system-defined lifecycle's hardcoded default_open_status_id
(always 1), filtering for "Backlog" would unconditionally include all open items
with no current_status record, regardless of what the custom lifecycle designated
as its default open status.
These items then displayed with the "Open" badge in the Backlog column because the
instance-level status_with_fallback correctly uses the custom lifecycle to resolve
the display status — but the filter scope and the display resolution disagreed.
Fix
Pass the effective custom lifecycle into with_system_defined_status so the nil
fallback compares against the custom lifecycle's default_open_status (and its
converted_from_system_defined_status_identifier) rather than the hardcoded
system-defined default_open_status_id. StatusFilter derives the lifecycle from
the requested work item types and threads it through to the scope.
When no lifecycle is passed (e.g. direct scope calls without filter context), the behaviour is unchanged.
How to set up and validate locally
Setup:
- Create a group and project
- Create a status called "Open" (category:
triage, noconverted_from_system_defined_status_identifier) - Rename "To do" to "Backlog" status (category:
to_do,converted_from_system_defined_status_identifier: 1) - Set "Open" as default open status
- Create one issue with
current_statuspointing to "Backlog" - Create a second issue and destroy its
current_statusrecord to simulate a pre-feature item
Before fix: filtering by "Backlog" returns both issues. The second issue displays with the "Open" badge.
After fix: filtering by "Backlog" returns only the first issue.
Database query
The change to with_system_defined_status conditionally adjusts which
system-defined status ID is used to guard the nil-current_status fallback. It
does not add new JOINs or change the structure of the generated SQL — the query
shape is identical to the existing scope. No new queries are introduced.
Related to #535304 (closed)