Part of https://gitlab.com/gitlab-org/gitlab-ee/issues/6047
CE version of https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/5810