This is continuation of https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/3653
cc @DouweM @grzesiek
Fixes https://gitlab.com/gitlab-org/gitlab-ce/issues/17551 Fixes https://gitlab.com/gitlab-org/gitlab-ce/issues/15625