update_remote_mirror_service_spec.rb 3.4 KB
Newer Older
1 2 3
require 'spec_helper'

describe Projects::UpdateRemoteMirrorService do
4 5
  set(:project) { create(:project, :repository) }
  let(:owner) { project.owner }
6 7 8 9 10 11 12
  let(:remote_project) { create(:forked_project_with_submodules) }
  let(:repository) { project.repository }
  let(:raw_repository) { repository.raw }
  let(:remote_mirror) { project.remote_mirrors.create!(url: remote_project.http_url_to_repo, enabled: true, only_protected_branches: false) }

  subject { described_class.new(project, project.creator) }

13
  describe "#execute" do
14
    before do
15 16 17
      repository.add_branch(owner, 'existing-branch', 'master')

      allow(remote_mirror).to receive(:update_repository).and_return(true)
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
    end

    it "fetches the remote repository" do
      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

      subject.execute(remote_mirror)
    end

    it "succeeds" do
      allow(repository).to receive(:fetch_remote) { sync_remote(repository, remote_mirror.remote_name, local_branch_names) }

      result = subject.execute(remote_mirror)

      expect(result[:status]).to eq(:success)
    end

36
    context 'when syncing all branches' do
37 38 39
      it "push all the branches the first time" do
        allow(repository).to receive(:fetch_remote)

40
        expect(remote_mirror).to receive(:update_repository).with({})
41 42 43 44 45

        subject.execute(remote_mirror)
      end
    end

46 47 48 49
    context 'when only syncing protected branches' do
      let(:unprotected_branch_name) { 'existing-branch' }
      let(:protected_branch_name) do
        project.repository.branch_names.find { |n| n != unprotected_branch_name }
50
      end
51 52
      let!(:protected_branch) do
        create(:protected_branch, project: project, name: protected_branch_name)
53 54
      end

55 56 57
      before do
        project.reload
        remote_mirror.only_protected_branches = true
58 59
      end

60 61 62
      it "sync updated protected branches" do
        allow(repository).to receive(:fetch_remote)
        expect(remote_mirror).to receive(:update_repository).with(only_branches_matching: [protected_branch_name])
63

64
        subject.execute(remote_mirror)
65 66 67 68 69 70
      end
    end
  end

  def sync_remote(repository, remote_name, local_branch_names)
    local_branch_names.each do |branch|
71 72
      commit = repository.commit(branch)
      repository.write_ref("refs/remotes/#{remote_name}/#{branch}", commit.id) if commit
73 74 75 76
    end
  end

  def update_remote_branch(repository, remote_name, branch)
77
    masterrev = repository.commit('master').id
78

79
    repository.write_ref("refs/remotes/#{remote_name}/#{branch}", masterrev, force: true)
80 81 82 83
    repository.expire_branches_cache
  end

  def update_branch(repository, branch)
84
    masterrev = repository.commit('master').id
85

86
    repository.write_ref("refs/heads/#{branch}", masterrev, force: true)
87 88 89 90 91 92 93 94
    repository.expire_branches_cache
  end

  def generate_tags(repository, *tag_names)
    tag_names.each_with_object([]) do |name, tags|
      tag = repository.find_tag(name)
      target = tag.try(:target)
      target_commit = tag.try(:dereferenced_target)
95 96 97 98 99
      tags << Gitlab::Git::Tag.new(repository.raw_repository, {
        name: name,
        target: target,
        target_commit: target_commit
      })
100 101 102 103 104 105 106 107 108
    end
  end

  def local_branch_names
    branch_names = repository.branches.map(&:name)
    # we want the protected branch to be pushed first
    branch_names.unshift(branch_names.delete('master'))
  end
end