Match items without a status record to their lifecycle default

What does this MR do and why?

Makes one rule hold for work items without a work_item_current_statuses record, for every work item type: they have the current default status of their lifecycle for their state (open, closed, duplicate). Display and the backfill in !259625 (merged) already follow this rule. The Start filter and the status removal check did not, which caused the double counting below.

Bug

On gitlab-org, every epic without a status record displays New (the lifecycle default) but also appears in the Start column of the board. Start was converted from the system-defined To do, and the filter for a converted status also matched record-less items through the system-defined fallback, so these epics matched both New and Start. Enabling status for epics made ~10.8k record-less epics hit this at once. Issues and tasks on gitlab-org had the same problem (#581613 (closed)) until !259625 (merged) gave them status records.

Legacy issue boards are affected too. A board's status lists use StatusFilter, and its Open list excludes the items of every status list through not_in_statuses. Both have to resolve record-less items the same way, or an item appears in two lists.

How each path resolves a record-less open item in gitlab-org

Path Before After
Display (current_status_with_fallback) New, or the mapping target if the default was mapped away New
Filter by New with types (status_roles) Matches Matches
Filter by New with workItemTypeIds or no type argument, as the list and board send No match Matches
Filter by Start, work_item_status_single_column_filter off (with_system_defined_status) Matches No match
Filter by Start, work_item_status_single_column_filter on (without_row_for_pending_types) Matches for types not yet backfilled No match
Filter by a mapping target when the mapped status was a former default (with_status) Matches No match
Legacy board New list (StatusFilter without a type argument) No match Matches
Legacy board Start list Matches No match
Legacy board Open list (not_in_statuses) Excluded only when the board has a Start list Excluded only when the board has a New list
Removal check (SystemDefined::Status#in_use_in_namespace?) Counts as using To do/Start Does not count
Backfill (!259625 (merged)) Saves New Saves New

Fix

  • with_system_defined_status resolves each status-enabled type's lifecycle through WorkItems::Statuses::Lifecycles::Provider. It only includes record-less items of a type when that lifecycle's default for the role is the requested status, or a custom status converted from it. Namespaces on the system-defined lifecycle are unaffected, because the provider returns the system-defined lifecycle for them.
  • without_row_for_pending_types, the fallback used when work_item_status_single_column_filter is on, uses the same check.
  • The removal check uses the same scope. Removing a former default status no longer requires a mapping because of record-less items: they always display the current default, and mappings only apply when a stored record is read, so one never affected them. Removing a status that is still a default still requires a mapping.
  • StatusFilter#requested_work_item_types reads work_item_type_ids as well as issue_types, and defaults to every status-enabled type when neither is given. The work items list and the new board send workItemTypeIds, and the legacy board sends no type at all, so without this record-less items matched no column.
  • HasStatus.default_status_roles_for builds the roles a custom status plays as a lifecycle default, keyed by the id stored in work_item_type_id (persistable_id, so a renamed Issue type still matches). StatusFilter uses it for the requested types, and not_in_statuses uses it for every status-enabled type, so the legacy board Open list excludes the record-less items its New list shows.
  • with_status no longer pulls record-less items under a mapping target because the mapped status used to be a default, and the fallback built by current_status_with_fallback skips mappings. Mappings are date-matched against a stored record's updated_at when the status is read, and a record-less item has no such record.

Changing a lifecycle's default still does not move items that have a status record. The rule only applies to record-less items. Backfills reduce those (#632560 for epics), but new ones appear when status is rolled out for a type or a namespace gains the license, so the rule has to hold for every type.

Database

No migration or schema change. The MR changes which predicates WorkItems::StatusFilter adds to the work items list query for a status filter, so the affected SELECT issues.* statement changes shape in three places, and the legacy board Open list query changes in one. Plans 1 to 4 are from Database Lab (gitlab-production-main, session 76780) and plan 5 from session 78902, all for gitlab-org (namespace_traversal_ids[1] = <root_namespace_id>), which has ~13.3k open record-less epics and is where the bug was reported. The SQL is the statement Rails generates for the GraphQL workItems resolver, captured on GDK and reduced to the predicates that change.

Probe: gitlab-org has no mapping with an old_status_role, so the with_status branch this MR removes never ran there. Plan: https://console.postgres.ai/gitlab/projects/gitlab-production-main/sessions/76780/commands/183949.

Shared statement shape for plans 1 to 4:

SELECT "issues".*
FROM "issues"
LEFT OUTER JOIN "projects" ON "projects"."id" = "issues"."project_id"
LEFT OUTER JOIN "work_item_current_statuses" ON "work_item_current_statuses"."work_item_id" = "issues"."id"
LEFT JOIN project_features ON projects.id = project_features.project_id
WHERE (NOT EXISTS (SELECT 1 FROM "banned_users" WHERE (banned_users.user_id = (issues.author_id + 0))))
  AND (namespace_traversal_ids[1] = <root_namespace_id>)
  AND ("project_features"."issues_access_level" > 0 OR "project_features"."issues_access_level" IS NULL)
  AND "issues"."state_id" = 1
  AND ("issues"."project_id" IS NULL OR "projects"."archived" = FALSE)
  [AND "issues"."work_item_type_id" = <epic_type_id>]
  AND (<status predicate>)
ORDER BY "issues"."created_at" DESC, "issues"."id" DESC
LIMIT 21

1. Filter by New with workItemTypeIds (work items list and the new board), modified

StatusFilter#requested_work_item_types now reads work_item_type_ids, so the record-less branch is added for the requested type.

Before (status predicate):

"work_item_current_statuses"."custom_status_id" IS NOT NULL AND "work_item_current_statuses"."custom_status_id" = <new_status_id>

Plan: https://console.postgres.ai/gitlab/projects/gitlab-production-main/sessions/76780/commands/183950 (BitmapAnd of index_issues_on_work_item_type_id_project_id_created_at_state and idx_issues_root_namespace_created_at, then Index Only Scan on idx_wi_current_statuses_on_wi_id_custom_status_id_unique per epic)

After:

("work_item_current_statuses"."custom_status_id" IS NOT NULL AND "work_item_current_statuses"."custom_status_id" = <new_status_id>
  OR "issues"."work_item_type_id" = <epic_type_id> AND "work_item_current_statuses"."id" IS NULL)

Plan: https://console.postgres.ai/gitlab/projects/gitlab-production-main/sessions/76780/commands/183951 (same BitmapAnd, then Index Scan on index_work_item_current_statuses_on_work_item_id per epic with the OR as a join filter)

2. Filter by New with no type argument, modified

Without a type filter the record-less branch is added once per status-enabled type (issue, task, epic, ticket on gitlab-org).

Before: the same predicate as 1 before, without the work_item_type_id condition.

Plan: https://console.postgres.ai/gitlab/projects/gitlab-production-main/sessions/76780/commands/183952 (Parallel Index Scan on index_work_item_current_statuses_on_custom_status_id over every New record, then issues_pkey lookups filtered by namespace and state)

After:

("work_item_current_statuses"."custom_status_id" IS NOT NULL AND "work_item_current_statuses"."custom_status_id" = <new_status_id>
  OR "issues"."work_item_type_id" = <epic_type_id> AND "work_item_current_statuses"."id" IS NULL
  OR "issues"."work_item_type_id" = <issue_type_id> AND "work_item_current_statuses"."id" IS NULL
  OR "issues"."work_item_type_id" = <task_type_id> AND "work_item_current_statuses"."id" IS NULL
  OR "issues"."work_item_type_id" = <ticket_type_id> AND "work_item_current_statuses"."id" IS NULL)

Plan: https://console.postgres.ai/gitlab/projects/gitlab-production-main/sessions/76780/commands/183953 (Parallel Index Scan Backward on idx_issues_root_namespace_created_at in sort order, status looked up per row on index_work_item_current_statuses_on_work_item_id, stops at the limit)

3. Filter by Start with workItemTypeIds, work_item_status_single_column_filter off, modified

with_system_defined_status no longer adds the record-less branch, because the lifecycle default on gitlab-org is New, not the status converted from To do.

Before:

("work_item_current_statuses"."custom_status_id" IS NOT NULL AND "work_item_current_statuses"."custom_status_id" = <start_status_id>
  OR "work_item_current_statuses"."system_defined_status_id" IS NOT NULL AND "work_item_current_statuses"."system_defined_status_id" = <system_defined_to_do_id>
  OR "work_item_current_statuses"."id" IS NULL AND "issues"."work_item_type_id" IN (<issue_type_id>, <task_type_id>, <epic_type_id>, <ticket_type_id>))

Plan: https://console.postgres.ai/gitlab/projects/gitlab-production-main/sessions/76780/commands/183954 (BitmapAnd on issues as in 1, sorted, then Index Scan on index_work_item_current_statuses_on_work_item_id per epic, stops at the limit because record-less epics match)

After:

("work_item_current_statuses"."custom_status_id" IS NOT NULL AND "work_item_current_statuses"."custom_status_id" = <start_status_id>
  OR "work_item_current_statuses"."system_defined_status_id" IS NOT NULL AND "work_item_current_statuses"."system_defined_status_id" = <system_defined_to_do_id>)

Plan: https://console.postgres.ai/gitlab/projects/gitlab-production-main/sessions/76780/commands/183955 (same BitmapAnd, then Index Scan on index_work_item_current_statuses_on_work_item_id per epic with the predicate as a filter, so every open epic is checked)

4. Filter by Start with workItemTypeIds, work_item_status_single_column_filter on, modified

without_row_for_pending_types uses the same check, so the record-less branch for types not yet backfilled is gone.

Before:

("work_item_current_statuses"."custom_status_id" IS NOT NULL AND "work_item_current_statuses"."custom_status_id" = <start_status_id>
  OR "work_item_current_statuses"."id" IS NULL AND "issues"."work_item_type_id" IN (<epic_type_id>, <ticket_type_id>))

Plan: https://console.postgres.ai/gitlab/projects/gitlab-production-main/sessions/76780/commands/183956 (BitmapAnd on issues as in 1, then Index Scan on index_work_item_current_statuses_on_work_item_id per epic)

After:

"work_item_current_statuses"."custom_status_id" IS NOT NULL AND "work_item_current_statuses"."custom_status_id" = <start_status_id>

Plan: https://console.postgres.ai/gitlab/projects/gitlab-production-main/sessions/76780/commands/183957 (Index Scan on index_work_item_current_statuses_on_custom_status_id for the Start records, then issues lookups filtered by namespace, state and type)

5. Legacy board Open list, modified

The Open list of a legacy board excludes the items of its status lists through not_in_statuses, here for boards with a New and a Start list. On master the Start fallback added one record-less branch for every status-enabled type. This MR drops it (the lifecycle default is New, not Start) and adds the equivalent branch per type through New, so the predicate changes shape but not what it scans. Captured on GDK from Boards::Issues::ListService for a group board. The outer statement is the group board issues query; only the NOT (...) predicate changes.

SELECT "issues".*
FROM "issues"
INNER JOIN "projects" ON "projects"."id" = "issues"."project_id"
LEFT OUTER JOIN "work_item_current_statuses" ON "work_item_current_statuses"."work_item_id" = "issues"."id"
LEFT JOIN project_features ON projects.id = project_features.project_id
WHERE (namespace_traversal_ids[1] = <root_namespace_id>)
  AND ("project_features"."issues_access_level" > 0 OR "project_features"."issues_access_level" IS NULL)
  AND "issues"."state_id" = 1
  AND ("issues"."project_id" IS NULL OR "projects"."archived" = FALSE)
  AND "issues"."work_item_type_id" IN (<incident_type_id>, <issue_type_id>, <ticket_type_id>)
  AND NOT (<the same conditions> AND (<status predicate>))
ORDER BY "issues"."relative_position" ASC NULLS LAST, "issues"."id" ASC
LIMIT 11

Before (status predicate):

"work_item_current_statuses"."custom_status_id" IS NOT NULL AND "work_item_current_statuses"."custom_status_id" = <new_status_id>
  OR "work_item_current_statuses"."custom_status_id" IS NOT NULL AND "work_item_current_statuses"."custom_status_id" = <start_status_id>
  OR "work_item_current_statuses"."system_defined_status_id" IS NOT NULL AND "work_item_current_statuses"."system_defined_status_id" = <system_defined_to_do_id>
  OR "work_item_current_statuses"."id" IS NULL AND "issues"."work_item_type_id" IN (<issue_type_id>, <task_type_id>, <epic_type_id>, <ticket_type_id>)

Plan: https://console.postgres.ai/gitlab/projects/gitlab-production-main/sessions/78902/commands/186960

After:

"work_item_current_statuses"."custom_status_id" IS NOT NULL AND "work_item_current_statuses"."custom_status_id" = <new_status_id>
  OR "issues"."work_item_type_id" = <issue_type_id> AND "work_item_current_statuses"."id" IS NULL
  OR "issues"."work_item_type_id" = <task_type_id> AND "work_item_current_statuses"."id" IS NULL
  OR "issues"."work_item_type_id" = <epic_type_id> AND "work_item_current_statuses"."id" IS NULL
  OR "issues"."work_item_type_id" = <ticket_type_id> AND "work_item_current_statuses"."id" IS NULL
  OR "work_item_current_statuses"."custom_status_id" IS NOT NULL AND "work_item_current_statuses"."custom_status_id" = <start_status_id>
  OR "work_item_current_statuses"."system_defined_status_id" IS NOT NULL AND "work_item_current_statuses"."system_defined_status_id" = <system_defined_to_do_id>

Plan: https://console.postgres.ai/gitlab/projects/gitlab-production-main/sessions/78902/commands/186962

Both plans are the same: BitmapAnd of idx_issues_root_namespace_created_at and idx_open_issues_on_project_and_confidential_and_author_and_id over the group's open issues, a sort on relative_position, then index_work_item_current_statuses_on_work_item_id per issue with the status predicate as a join filter. Buffers are identical (shared hit=509 read=1086156 before, shared hit=510 read=1086157 after). The cost is the legacy board base query over all open issues of the group, not the status predicate; this MR does not change it. For reference, the intermediate state without the New branch (what this MR did before the not_in_statuses change) has the same plan: https://console.postgres.ai/gitlab/projects/gitlab-production-main/sessions/78902/commands/186961.

Filtering by types instead of workItemTypeIds produced the same SQL before and after, so it is not listed separately. The removal check (SystemDefined::Status#in_use_in_namespace?) runs with_system_defined_status as an exists? over the namespace hierarchy, so it loses the same record-less branch as 3. It only runs when a status is removed in the lifecycle editor and is not planned here.

References

Screenshots or screen recordings

No UI changes. The before state is the screenshot on the issue.

Before After
Epics displaying New also listed under Start Start lists only epics with status Start

How to set up and validate locally

Requires an Ultimate license and a top-level group with custom statuses.

  1. In the Rails console, enable the flags for your top-level group:

    group = Group.find_by_full_path('your-top-level-group')
    Feature.enable(:work_item_status_for_epics, group)
    Feature.enable(:planning_view_boards)
  2. In the group, go to Settings > Work items > Statuses and select Edit statuses for the lifecycle used by issues. Rename the converted To do status to Start, add a status named New in the Triage category, and make New the default open status. If the editor does not allow changing the default, do it in the console:

    type = WorkItems::TypesFramework::Provider.new(group).find_by_base_type(:issue)
    lifecycle = WorkItems::Statuses::Lifecycles::Provider.new(group.id).find_by_type(type)
    lifecycle.update!(default_open_status: lifecycle.statuses.find_by!(name: 'New'))
  3. Create two epics in the group, then remove their status records so they fall back to the lifecycle default:

    WorkItem.where(namespace: group).with_issue_type(:epic).find_each { |wi| wi.current_status&.destroy! }
  4. Go to the group's Work items page, switch to the Board view, and group by Status. Both epics show the New status badge.

  5. Before this change, both epics also appear in the Start column. After, they appear only in New.

  6. Optional. Confirm through GraphQL that the Start filter no longer matches them:

    query {
      namespace(fullPath: "your-top-level-group") {
        workItems(status: { name: "Start" }, types: [EPIC], state: opened) { count }
      }
    }
  7. Optional. Repeat steps 4 to 6 with Feature.enable(:work_item_status_single_column_filter, group) to cover the flag-on fallback path.

  8. Legacy board: go to Plan > Issue boards, create a board, and add status lists for New and Start. Create an issue and remove its status record as in step 3. Before this change, the issue appears in Open and Start; after, only in New.

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.

Edited by Matt D'Angelo

Merge request reports

Loading
Loading