Extracted from https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/27526 / part of https://gitlab.com/gitlab-org/gitlab-ce/issues/57226.