Import pull requests from GitHub where the target branch was removed

parent 8532dc0d
...@@ -75,10 +75,11 @@ module Gitlab ...@@ -75,10 +75,11 @@ module Gitlab
.map { |raw| PullRequestFormatter.new(project, raw) } .map { |raw| PullRequestFormatter.new(project, raw) }
.reject(&:cross_project?) .reject(&:cross_project?)
source_branches_removed = pull_requests.reject(&:source_branch_exists?) source_branches_removed = pull_requests.reject(&:source_branch_exists?).map { |pr| [pr.source_branch, pr.source_sha] }
source_branches_removed.each do |pull_request| target_branches_removed = pull_requests.reject(&:target_branch_exists?).map { |pr| [pr.target_branch, pr.target_sha] }
client.create_ref(repo, "refs/heads/#{pull_request.source_branch}", pull_request.source_sha) branches_removed = source_branches_removed | target_branches_removed
end
create_refs(branches_removed)
project.repository.fetch_ref(repo_url, '+refs/heads/*', 'refs/heads/*') project.repository.fetch_ref(repo_url, '+refs/heads/*', 'refs/heads/*')
...@@ -92,15 +93,25 @@ module Gitlab ...@@ -92,15 +93,25 @@ module Gitlab
end end
end end
source_branches_removed.each do |pull_request| delete_refs(branches_removed)
client.delete_ref(repo, "heads/#{pull_request.source_branch}")
end
true true
rescue ActiveRecord::RecordInvalid => e rescue ActiveRecord::RecordInvalid => e
raise Projects::ImportService::Error, e.message raise Projects::ImportService::Error, e.message
end end
def create_refs(branches)
branches.each do |branch|
client.create_ref(repo, "refs/heads/#{branch.first}", branch.last)
end
end
def delete_refs(branches)
branches.each do |branch|
client.delete_ref(repo, "heads/#{branch.first}")
end
end
def apply_labels(number, issuable) def apply_labels(number, issuable)
issue = client.issue(project.import_source, number) issue = client.issue(project.import_source, number)
......
...@@ -9,7 +9,7 @@ module Gitlab ...@@ -9,7 +9,7 @@ module Gitlab
source_project: source_project, source_project: source_project,
source_branch: source_branch, source_branch: source_branch,
target_project: target_project, target_project: target_project,
target_branch: target_branch.name, target_branch: target_branch,
state: state, state: state,
milestone: milestone, milestone: milestone,
author_id: author_id, author_id: author_id,
...@@ -43,6 +43,18 @@ module Gitlab ...@@ -43,6 +43,18 @@ module Gitlab
raw_data.head.sha raw_data.head.sha
end end
def target_branch_exists?
target_project.repository.branch_names.include?(target_branch)
end
def target_branch
raw_data.base.ref
end
def target_sha
raw_data.base.sha
end
private private
def assigned? def assigned?
...@@ -93,10 +105,6 @@ module Gitlab ...@@ -93,10 +105,6 @@ module Gitlab
raw_data.base.repo raw_data.base.repo
end end
def target_branch
target_project.repository.find_branch(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?
......
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