This is a rework of !1263 (merged). See the discussion in gitlab-org/gitlab!140898 (comment 1730081887).