From 578137f6e4817591e98019bf3e88065fae0a2879 Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Wed, 22 Aug 2018 10:43:15 -0700 Subject: Fix remote mirrors failing if Git remotes have not been added Remote mirrors only get created when the URL changes, However, during the GCP migration, the remote mirror did not get created automatically. Plus, there's no guarantee someone restoring a repository from backup would have this remote. We now add the remote each time we attempt to fetch from the repository. This works because Gitaly doesn't throw up an exception or error if the remote already exists: https://gitlab.com/gitlab-org/gitaly/issues/1317 In the future, we should attempt to add if the remote doesn't exist: https://gitlab.com/gitlab-org/gitaly/issues/1316 Closes #50562 --- app/models/remote_mirror.rb | 9 +++++++++ app/services/projects/update_remote_mirror_service.rb | 1 + changelogs/unreleased/sh-fix-issue-50562.yml | 5 +++++ spec/models/remote_mirror_spec.rb | 12 ++++++++++++ spec/services/projects/update_remote_mirror_service_spec.rb | 1 + 5 files changed, 28 insertions(+) create mode 100644 changelogs/unreleased/sh-fix-issue-50562.yml diff --git a/app/models/remote_mirror.rb b/app/models/remote_mirror.rb index 833faf3bc82..c1f53b5da4f 100644 --- a/app/models/remote_mirror.rb +++ b/app/models/remote_mirror.rb @@ -150,6 +150,15 @@ class RemoteMirror < ActiveRecord::Base result.to_s end + def ensure_remote! + return unless project + return unless remote_name && url + + # If this fails or the remote already exists, we won't know due to + # https://gitlab.com/gitlab-org/gitaly/issues/1317 + project.repository.add_remote(remote_name, url) + end + private def raw diff --git a/app/services/projects/update_remote_mirror_service.rb b/app/services/projects/update_remote_mirror_service.rb index 4651f7c4f8f..591b38b8151 100644 --- a/app/services/projects/update_remote_mirror_service.rb +++ b/app/services/projects/update_remote_mirror_service.rb @@ -10,6 +10,7 @@ module Projects return success unless remote_mirror.enabled? begin + remote_mirror.ensure_remote! repository.fetch_remote(remote_mirror.remote_name, no_tags: true) opts = {} diff --git a/changelogs/unreleased/sh-fix-issue-50562.yml b/changelogs/unreleased/sh-fix-issue-50562.yml new file mode 100644 index 00000000000..a207dd28622 --- /dev/null +++ b/changelogs/unreleased/sh-fix-issue-50562.yml @@ -0,0 +1,5 @@ +--- +title: Fix remote mirrors failing if Git remotes have not been added +merge_request: 21351 +author: +type: fixed diff --git a/spec/models/remote_mirror_spec.rb b/spec/models/remote_mirror_spec.rb index c2ef0435c8e..269d5deca20 100644 --- a/spec/models/remote_mirror_spec.rb +++ b/spec/models/remote_mirror_spec.rb @@ -220,6 +220,18 @@ describe RemoteMirror do end end + context '#ensure_remote!' do + let(:remote_mirror) { create(:project, :repository, :remote_mirror).remote_mirrors.first } + + it 'adds a remote multiple times with no errors' do + expect(remote_mirror.project.repository).to receive(:add_remote).with(remote_mirror.remote_name, remote_mirror.url).twice.and_call_original + + 2.times do + remote_mirror.ensure_remote! + end + end + end + context '#updated_since?' do let(:remote_mirror) { create(:project, :repository, :remote_mirror).remote_mirrors.first } let(:timestamp) { Time.now - 5.minutes } diff --git a/spec/services/projects/update_remote_mirror_service_spec.rb b/spec/services/projects/update_remote_mirror_service_spec.rb index 5c2e79ff9af..96e8a80b334 100644 --- a/spec/services/projects/update_remote_mirror_service_spec.rb +++ b/spec/services/projects/update_remote_mirror_service_spec.rb @@ -18,6 +18,7 @@ describe Projects::UpdateRemoteMirrorService do end it "fetches the remote repository" do + expect(remote_mirror).to receive(:ensure_remote!).and_call_original expect(repository).to receive(:fetch_remote).with(remote_mirror.remote_name, no_tags: true) do sync_remote(repository, remote_mirror.remote_name, local_branch_names) end -- cgit v1.2.1