Commit 0dcfff08 authored by Rubén Dávila's avatar Rubén Dávila

Fix broken specs and add more specs to UpdateRemoteMirrorService.

* Also addressed feedback from https://gitlab.com/gitlab-org/gitlab-ee/commit/fcbfe56207f64da07deb7f3a8cbdcd505069edbd
parent bf30f919
......@@ -115,11 +115,6 @@ class RemoteMirror < ActiveRecord::Base
result.to_s
end
def full_url
mirror_url = Gitlab::ImportUrl.new(url, credentials: credentials)
mirror_url.full_url
end
private
def url_availability
......
......@@ -77,7 +77,7 @@ class Repository
return @has_visible_content unless @has_visible_content.nil?
@has_visible_content = cache.fetch(:has_visible_content?) do
local_branches.size > 0
branch_count > 0
end
end
......@@ -133,7 +133,7 @@ class Repository
end
def find_branch(name)
branches.find { |branch| branch.name == name }
raw_repository.branches.find { |branch| branch.name == name }
end
def find_tag(name)
......@@ -248,7 +248,7 @@ class Repository
end
def branch_count
@branch_count ||= cache.fetch(:branch_count) { local_branches.size }
@branch_count ||= cache.fetch(:branch_count) { branches.size }
end
def tag_count
......
......@@ -939,6 +939,41 @@ describe Repository, models: true do
end
end
describe '#push_branches' do
it 'push branches to the remote repo' do
expect_any_instance_of(Gitlab::Shell).to receive(:push_branches).
with('project_name', 'remote_name', ['branch'])
repository.push_branches('project_name', 'remote_name', ['branch'])
end
end
describe '#delete_remote_branches' do
it 'delete branches to the remote repo' do
expect_any_instance_of(Gitlab::Shell).to receive(:delete_remote_branches).
with('project_name', 'remote_name', ['branch'])
repository.delete_remote_branches('project_name', 'remote_name', ['branch'])
end
end
describe '#remove_remote' do
it 'remove a remote reference' do
repository.add_remote('upstream', 'http://repo.test')
expect(repository.remove_remote('upstream')).to eq(true)
end
end
describe '#remote_tags' do
it 'gets the remote tags' do
expect_any_instance_of(Gitlab::Shell).to receive(:list_remote_tags).
with(repository.path_with_namespace, 'upstream')
repository.remote_tags('upstream')
end
end
describe '#local_branches' do
it 'returns the local branches' do
masterrev = repository.find_branch('master').target
......@@ -950,9 +985,31 @@ describe Repository, models: true do
end
end
describe '#remote_branches' do
it 'returns the remote branches' do
masterrev = repository.find_branch('master').target
create_remote_branch('joe', 'remote_branch', masterrev)
repository.add_branch(user, 'local_branch', masterrev)
expect(repository.remote_branches('joe').any? { |branch| branch.name == 'local_branch' }).to eq(false)
expect(repository.remote_branches('joe').any? { |branch| branch.name == 'remote_branch' }).to eq(true)
end
end
describe '#upstream_branches' do
it 'returns branches from the upstream remote' do
masterrev = repository.find_branch('master').target
create_remote_branch('upstream', 'upstream_branch', masterrev)
expect(repository.upstream_branches.size).to eq(1)
expect(repository.upstream_branches.first).to be_an_instance_of(Gitlab::Git::Branch)
expect(repository.upstream_branches.first.name).to eq('upstream_branch')
end
end
def create_remote_branch(remote_name, branch_name, target)
rugged = repository.rugged
rugged.references.create("refs/remotes/#{remote_name}/#{branch_name}", target)
ref = rugged.references.create("refs/remotes/#{remote_name}/#{branch_name}", target)
end
end
......@@ -13,10 +13,13 @@ describe Projects::UpdateRemoteMirrorService do
describe "#execute" do
before do
create_branch(repository, 'existing-branch')
allow(repository).to receive(:remote_tags) { generate_tags(repository, 'v1.0.0', 'v1.1.0') }
end
it "fetches the remote repository" do
expect(repository).to receive(:fetch_remote).with(remote_mirror.ref_name)
expect(repository).to receive(:fetch_remote).with(remote_mirror.ref_name, no_tags: true) do
sync_remote(repository, remote_mirror.ref_name, all_branches)
end
subject.execute(remote_mirror)
end
......@@ -29,7 +32,7 @@ describe Projects::UpdateRemoteMirrorService do
expect(result[:status]).to eq(:success)
end
describe 'Updating branches' do
describe 'Syncing branches' do
it "push all the branches the first time" do
allow(repository).to receive(:fetch_remote)
......@@ -78,6 +81,48 @@ describe Projects::UpdateRemoteMirrorService do
end
end
describe 'Syncing tags' do
before do
allow(repository).to receive(:fetch_remote) { sync_remote(repository, remote_mirror.ref_name, all_branches) }
end
context 'when there are not tags to push' do
it 'should not try to push tags' do
allow(repository).to receive(:remote_tags) { {} }
allow(repository).to receive(:tags) { [] }
expect(repository).not_to receive(:push_tags)
subject.execute(remote_mirror)
end
end
context 'when there are some tags to push' do
it 'should push tags to remote' do
allow(repository).to receive(:remote_tags) { {} }
expect(repository).to receive(:push_branches).with(
project.path_with_namespace, remote_mirror.ref_name, ['v1.0.0', 'v1.1.0']
)
subject.execute(remote_mirror)
end
end
context 'when there are some tags to delete' do
it 'should delete tags from remote' do
allow(repository).to receive(:remote_tags) { generate_tags(repository, 'v1.0.0', 'v1.1.0') }
repository.rm_tag('v1.0.0')
expect(repository).to receive(:delete_remote_branches).with(
project.path_with_namespace, remote_mirror.ref_name,['v1.0.0']
)
subject.execute(remote_mirror)
end
end
end
end
def create_branch(repository, branch_name)
......@@ -114,4 +159,10 @@ describe Projects::UpdateRemoteMirrorService do
rugged.references.delete("refs/heads/#{branch}")
repository.expire_branches_cache
end
def generate_tags(repository, *tag_names)
tag_names.each_with_object({}) do |name, tags|
tags[name] = repository.find_tag(name).try(:target)
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment