Commit 8e4e0631 authored by Alessio Caiazza's avatar Alessio Caiazza

Fix diverged from upstream detection

In case pull_mirror_branch_prefix is set, upstream branch names do not
have the prefix
parent b9ec197c
......@@ -15,6 +15,7 @@ module EE
include Elastic::RepositoriesSearch
delegate :checksum, :find_remote_root_ref, to: :raw_repository
delegate :pull_mirror_branch_prefix, to: :project
end
# Transiently sets a configuration variable
......@@ -33,6 +34,19 @@ module EE
expire_content_cache
end
def upstream_branch_name(branch_name)
return branch_name unless ::Feature.enabled?(:pull_mirror_branch_prefix, project)
return branch_name unless pull_mirror_branch_prefix
# when pull_mirror_branch_prefix is set, a branch not starting with it
# is a local branch that doesn't tracking upstream
if branch_name.start_with?(pull_mirror_branch_prefix)
branch_name.delete_prefix(pull_mirror_branch_prefix)
else
nil
end
end
def fetch_upstream(url, forced: false)
add_remote(MIRROR_REMOTE, url)
fetch_remote(MIRROR_REMOTE, ssh_auth: project&.import_data, forced: forced)
......@@ -43,8 +57,11 @@ module EE
end
def diverged_from_upstream?(branch_name)
upstream_branch = upstream_branch_name(branch_name)
return false unless upstream_branch
branch_commit = commit("refs/heads/#{branch_name}")
upstream_commit = commit("refs/remotes/#{MIRROR_REMOTE}/#{branch_name}")
upstream_commit = commit("refs/remotes/#{MIRROR_REMOTE}/#{upstream_branch}")
if branch_commit && upstream_commit
!raw_repository.ancestor?(branch_commit.id, upstream_commit.id)
......@@ -65,8 +82,11 @@ module EE
end
def up_to_date_with_upstream?(branch_name)
upstream_branch = upstream_branch_name(branch_name)
return false unless upstream_branch
branch_commit = commit("refs/heads/#{branch_name}")
upstream_commit = commit("refs/remotes/#{MIRROR_REMOTE}/#{branch_name}")
upstream_commit = commit("refs/remotes/#{MIRROR_REMOTE}/#{upstream_branch}")
if branch_commit && upstream_commit
ancestor?(branch_commit.id, upstream_commit.id)
......
......@@ -20,6 +20,14 @@ describe Repository do
rugged.references.create("refs/remotes/#{remote_name}/#{branch_name}", target.id)
end
describe 'delegated methods' do
subject { repository }
it { is_expected.to delegate_method(:checksum).to(:raw_repository) }
it { is_expected.to delegate_method(:find_remote_root_ref).to(:raw_repository) }
it { is_expected.to delegate_method(:pull_mirror_branch_prefix).to(:project) }
end
describe '#after_sync' do
it 'expires repository cache' do
expect(repository).to receive(:expire_all_method_caches)
......@@ -208,4 +216,61 @@ describe Repository do
expect(project.repository.insights_config_for(project.repository.root_ref)).to eq("monthlyBugsCreated:\n title: My chart")
end
end
describe '#upstream_branch_name' do
let(:pull_mirror_branch_prefix) { 'upstream/' }
let(:branch_name) { 'upstream/master' }
subject { repository.upstream_branch_name(branch_name) }
before do
project.update(pull_mirror_branch_prefix: pull_mirror_branch_prefix)
end
it { is_expected.to eq('master') }
context 'when the branch is local (not mirrored)' do
let(:branch_name) { 'a-local-branch' }
it { is_expected.to be_nil }
end
context 'when pull_mirror_branch_prefix is nil' do
let(:pull_mirror_branch_prefix) { nil }
it { is_expected.to eq(branch_name) }
end
context 'when pull_mirror_branch_prefix is empty' do
let(:pull_mirror_branch_prefix) { '' }
it { is_expected.to eq(branch_name) }
end
context 'when pull_mirror_branch_prefix feature flag is disabled' do
before do
stub_feature_flags(pull_mirror_branch_prefix: false)
end
it { is_expected.to eq(branch_name) }
context 'when the branch is local (not mirrored)' do
let(:branch_name) { 'a-local-branch' }
it { is_expected.to eq(branch_name) }
end
context 'when pull_mirror_branch_prefix is nil' do
let(:pull_mirror_branch_prefix) { nil }
it { is_expected.to eq(branch_name) }
end
context 'when pull_mirror_branch_prefix is empty' do
let(:pull_mirror_branch_prefix) { '' }
it { is_expected.to eq(branch_name) }
end
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