diff options
-rw-r--r-- | lib/gitlab/github_import/pull_request_formatter.rb | 28 | ||||
-rw-r--r-- | spec/lib/gitlab/github_import/pull_request_formatter_spec.rb | 32 |
2 files changed, 50 insertions, 10 deletions
diff --git a/lib/gitlab/github_import/pull_request_formatter.rb b/lib/gitlab/github_import/pull_request_formatter.rb index 3b541a04d5c..9f8182f643e 100644 --- a/lib/gitlab/github_import/pull_request_formatter.rb +++ b/lib/gitlab/github_import/pull_request_formatter.rb @@ -30,11 +30,19 @@ module Gitlab end def source_branch_exists? - source_project.repository.branch_names.include?(source_branch) + source_project.repository.branch_exists?(source_ref) end def source_branch - raw_data.head.ref + @source_branch ||= if source_branch_exists? + source_ref + else + "#{source_ref}-#{short_id(source_sha)}" + end + end + + def short_id(sha, length = 7) + sha.to_s[0..length] end def source_sha @@ -42,11 +50,15 @@ module Gitlab end def target_branch_exists? - target_project.repository.branch_names.include?(target_branch) + target_project.repository.branch_exists?(target_ref) end def target_branch - raw_data.base.ref + @target_branch ||= if target_branch_exists? + target_ref + else + "#{target_ref}-#{short_id(target_sha)}" + end end def target_sha @@ -99,6 +111,10 @@ module Gitlab raw_data.head.repo end + def source_ref + raw_data.head.ref + end + def target_project project end @@ -107,6 +123,10 @@ module Gitlab raw_data.base.repo end + def target_ref + raw_data.base.ref + end + def state @state ||= case true when raw_data.state == 'closed' && raw_data.merged_at.present? diff --git a/spec/lib/gitlab/github_import/pull_request_formatter_spec.rb b/spec/lib/gitlab/github_import/pull_request_formatter_spec.rb index 1d15d3d937e..adedc57719e 100644 --- a/spec/lib/gitlab/github_import/pull_request_formatter_spec.rb +++ b/spec/lib/gitlab/github_import/pull_request_formatter_spec.rb @@ -164,10 +164,20 @@ describe Gitlab::GithubImport::PullRequestFormatter, lib: true do end describe '#source_branch' do - let(:raw_data) { double(base_data) } + context 'when source branch exists' do + let(:raw_data) { double(base_data) } - it 'returns head ref' do - expect(pull_request.source_branch).to eq 'feature' + it 'returns head ref' do + expect(pull_request.source_branch).to eq 'feature' + end + end + + context 'when source branch does not exist' do + let(:raw_data) { double(base_data.merge(head: double(ref: 'removed-branch', sha: '2e5d3239642f9161dcbbc4b70a211a68e5e45e2b'))) } + + it 'returns head ref' do + expect(pull_request.source_branch).to eq 'removed-branch-2e5d3239' + end end end @@ -198,10 +208,20 @@ describe Gitlab::GithubImport::PullRequestFormatter, lib: true do end describe '#target_branch' do - let(:raw_data) { double(base_data) } + context 'when target branch exists' do + let(:raw_data) { double(base_data) } - it 'returns base ref' do - expect(pull_request.target_branch).to eq 'master' + it 'returns base ref' do + expect(pull_request.target_branch).to eq 'master' + end + end + + context 'when target branch does not exist' do + let(:raw_data) { double(base_data.merge(base: double(ref: 'removed-branch', sha: '8ffb3c15a5475e59ae909384297fede4badcb4c7'))) } + + it 'returns head ref' do + expect(pull_request.target_branch).to eq 'removed-branch-8ffb3c15' + end end end |