Import pull requests from GitHub where the target branch was removed

This commit is contained in:
Douglas Barbosa Alexandre 2016-04-22 15:25:04 -03:00
parent 8532dc0d77
commit 9b4dc552cb
2 changed files with 31 additions and 12 deletions

View File

@ -75,10 +75,11 @@ module Gitlab
.map { |raw| PullRequestFormatter.new(project, raw) }
.reject(&:cross_project?)
source_branches_removed = pull_requests.reject(&:source_branch_exists?)
source_branches_removed.each do |pull_request|
client.create_ref(repo, "refs/heads/#{pull_request.source_branch}", pull_request.source_sha)
end
source_branches_removed = pull_requests.reject(&:source_branch_exists?).map { |pr| [pr.source_branch, pr.source_sha] }
target_branches_removed = pull_requests.reject(&:target_branch_exists?).map { |pr| [pr.target_branch, pr.target_sha] }
branches_removed = source_branches_removed | target_branches_removed
create_refs(branches_removed)
project.repository.fetch_ref(repo_url, '+refs/heads/*', 'refs/heads/*')
@ -92,15 +93,25 @@ module Gitlab
end
end
source_branches_removed.each do |pull_request|
client.delete_ref(repo, "heads/#{pull_request.source_branch}")
end
delete_refs(branches_removed)
true
rescue ActiveRecord::RecordInvalid => e
raise Projects::ImportService::Error, e.message
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)
issue = client.issue(project.import_source, number)

View File

@ -9,7 +9,7 @@ module Gitlab
source_project: source_project,
source_branch: source_branch,
target_project: target_project,
target_branch: target_branch.name,
target_branch: target_branch,
state: state,
milestone: milestone,
author_id: author_id,
@ -43,6 +43,18 @@ module Gitlab
raw_data.head.sha
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
def assigned?
@ -93,10 +105,6 @@ module Gitlab
raw_data.base.repo
end
def target_branch
target_project.repository.find_branch(raw_data.base.ref)
end
def state
@state ||= case true
when raw_data.state == 'closed' && raw_data.merged_at.present?