Import PRs where branch names were reused across PRs

parent 7e1f14e2
...@@ -30,11 +30,19 @@ module Gitlab ...@@ -30,11 +30,19 @@ module Gitlab
end end
def source_branch_exists? def source_branch_exists?
source_project.repository.branch_names.include?(source_branch) source_project.repository.branch_exists?(source_ref)
end end
def source_branch 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 end
def source_sha def source_sha
...@@ -42,11 +50,15 @@ module Gitlab ...@@ -42,11 +50,15 @@ module Gitlab
end end
def target_branch_exists? def target_branch_exists?
target_project.repository.branch_names.include?(target_branch) target_project.repository.branch_exists?(target_ref)
end end
def target_branch def target_branch
raw_data.base.ref @target_branch ||= if target_branch_exists?
target_ref
else
"#{target_ref}-#{short_id(target_sha)}"
end
end end
def target_sha def target_sha
...@@ -99,6 +111,10 @@ module Gitlab ...@@ -99,6 +111,10 @@ module Gitlab
raw_data.head.repo raw_data.head.repo
end end
def source_ref
raw_data.head.ref
end
def target_project def target_project
project project
end end
...@@ -107,6 +123,10 @@ module Gitlab ...@@ -107,6 +123,10 @@ module Gitlab
raw_data.base.repo raw_data.base.repo
end end
def target_ref
raw_data.base.ref
end
def state def state
@state ||= case true @state ||= case true
when raw_data.state == 'closed' && raw_data.merged_at.present? when raw_data.state == 'closed' && raw_data.merged_at.present?
......
...@@ -164,6 +164,7 @@ describe Gitlab::GithubImport::PullRequestFormatter, lib: true do ...@@ -164,6 +164,7 @@ describe Gitlab::GithubImport::PullRequestFormatter, lib: true do
end end
describe '#source_branch' do describe '#source_branch' do
context 'when source branch exists' do
let(:raw_data) { double(base_data) } let(:raw_data) { double(base_data) }
it 'returns head ref' do it 'returns head ref' do
...@@ -171,6 +172,15 @@ describe Gitlab::GithubImport::PullRequestFormatter, lib: true do ...@@ -171,6 +172,15 @@ describe Gitlab::GithubImport::PullRequestFormatter, lib: true do
end end
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
describe '#source_sha' do describe '#source_sha' do
let(:raw_data) { double(base_data) } let(:raw_data) { double(base_data) }
...@@ -198,6 +208,7 @@ describe Gitlab::GithubImport::PullRequestFormatter, lib: true do ...@@ -198,6 +208,7 @@ describe Gitlab::GithubImport::PullRequestFormatter, lib: true do
end end
describe '#target_branch' do describe '#target_branch' do
context 'when target branch exists' do
let(:raw_data) { double(base_data) } let(:raw_data) { double(base_data) }
it 'returns base ref' do it 'returns base ref' do
...@@ -205,6 +216,15 @@ describe Gitlab::GithubImport::PullRequestFormatter, lib: true do ...@@ -205,6 +216,15 @@ describe Gitlab::GithubImport::PullRequestFormatter, lib: true do
end end
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
describe '#target_sha' do describe '#target_sha' do
let(:raw_data) { double(base_data) } let(:raw_data) { double(base_data) }
......
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