Part of https://gitlab.com/gitlab-org/gitlab-ee/issues/9506
CE counterpart: https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/31156