2018-10-15 14:42:47 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Projects::UpdateRemoteMirrorService do
|
2018-12-05 23:21:45 +05:30
|
|
|
let(:project) { create(:project, :repository) }
|
2018-10-15 14:42:47 +05:30
|
|
|
let(:remote_project) { create(:forked_project_with_submodules) }
|
|
|
|
let(:remote_mirror) { project.remote_mirrors.create!(url: remote_project.http_url_to_repo, enabled: true, only_protected_branches: false) }
|
2018-12-05 23:21:45 +05:30
|
|
|
let(:remote_name) { remote_mirror.remote_name }
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
subject(:service) { described_class.new(project, project.creator) }
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
describe "#execute" do
|
2018-10-15 14:42:47 +05:30
|
|
|
before do
|
2018-12-05 23:21:45 +05:30
|
|
|
project.repository.add_branch(project.owner, 'existing-branch', 'master')
|
2018-11-08 19:23:39 +05:30
|
|
|
|
|
|
|
allow(remote_mirror).to receive(:update_repository).and_return(true)
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
it "ensures the remote exists" do
|
2019-02-15 15:39:39 +05:30
|
|
|
stub_fetch_remote(project, remote_name: remote_name, ssh_auth: remote_mirror)
|
2018-12-05 23:21:45 +05:30
|
|
|
|
|
|
|
expect(remote_mirror).to receive(:ensure_remote!)
|
|
|
|
|
|
|
|
service.execute(remote_mirror)
|
|
|
|
end
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
it "fetches the remote repository" do
|
2018-12-05 23:21:45 +05:30
|
|
|
expect(project.repository)
|
|
|
|
.to receive(:fetch_remote)
|
2019-02-15 15:39:39 +05:30
|
|
|
.with(remote_mirror.remote_name, no_tags: true, ssh_auth: remote_mirror)
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
service.execute(remote_mirror)
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
it "returns success when updated succeeds" do
|
2019-02-15 15:39:39 +05:30
|
|
|
stub_fetch_remote(project, remote_name: remote_name, ssh_auth: remote_mirror)
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
result = service.execute(remote_mirror)
|
2018-10-15 14:42:47 +05:30
|
|
|
|
|
|
|
expect(result[:status]).to eq(:success)
|
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
context 'when syncing all branches' do
|
2018-10-15 14:42:47 +05:30
|
|
|
it "push all the branches the first time" do
|
2019-02-15 15:39:39 +05:30
|
|
|
stub_fetch_remote(project, remote_name: remote_name, ssh_auth: remote_mirror)
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
expect(remote_mirror).to receive(:update_repository).with({})
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
service.execute(remote_mirror)
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
context 'when only syncing protected branches' do
|
2018-12-05 23:21:45 +05:30
|
|
|
it "sync updated protected branches" do
|
2019-02-15 15:39:39 +05:30
|
|
|
stub_fetch_remote(project, remote_name: remote_name, ssh_auth: remote_mirror)
|
2018-12-05 23:21:45 +05:30
|
|
|
protected_branch = create_protected_branch(project)
|
2018-11-08 19:23:39 +05:30
|
|
|
remote_mirror.only_protected_branches = true
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
expect(remote_mirror)
|
|
|
|
.to receive(:update_repository)
|
|
|
|
.with(only_branches_matching: [protected_branch.name])
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
service.execute(remote_mirror)
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
def create_protected_branch(project)
|
|
|
|
branch_name = project.repository.branch_names.find { |n| n != 'existing-branch' }
|
|
|
|
create(:protected_branch, project: project, name: branch_name)
|
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
def stub_fetch_remote(project, remote_name:, ssh_auth:)
|
2018-12-05 23:21:45 +05:30
|
|
|
allow(project.repository)
|
|
|
|
.to receive(:fetch_remote)
|
2019-02-15 15:39:39 +05:30
|
|
|
.with(remote_name, no_tags: true, ssh_auth: ssh_auth) { fetch_remote(project.repository, remote_name) }
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
def fetch_remote(repository, remote_name)
|
|
|
|
local_branch_names(repository).each do |branch|
|
|
|
|
commit = repository.commit(branch)
|
|
|
|
repository.write_ref("refs/remotes/#{remote_name}/#{branch}", commit.id) if commit
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
def local_branch_names(repository)
|
2018-10-15 14:42:47 +05:30
|
|
|
branch_names = repository.branches.map(&:name)
|
|
|
|
# we want the protected branch to be pushed first
|
|
|
|
branch_names.unshift(branch_names.delete('master'))
|
|
|
|
end
|
|
|
|
end
|