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