Part of https://gitlab.com/gitlab-org/gitlab-ee/issues/6118
CE version of https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/6046