Part of https://gitlab.com/gitlab-org/gitlab-ee/issues/9506
EE counterpart: https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/14845