Refactor the logic of updating head pipelines for merge requests See merge request gitlab-org/gitlab-ce!23502