From 9b4dc552cb51faee38baffa6f29954d795282658 Mon Sep 17 00:00:00 2001 From: Douglas Barbosa Alexandre <dbalexandre@gmail.com> Date: Fri, 22 Apr 2016 15:25:04 -0300 Subject: [PATCH] Import pull requests from GitHub where the target branch was removed --- lib/gitlab/github_import/importer.rb | 25 +++++++++++++------ .../github_import/pull_request_formatter.rb | 18 +++++++++---- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/lib/gitlab/github_import/importer.rb b/lib/gitlab/github_import/importer.rb index a3f27891784..bc25fc8c0b4 100644 --- a/lib/gitlab/github_import/importer.rb +++ b/lib/gitlab/github_import/importer.rb @@ -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) diff --git a/lib/gitlab/github_import/pull_request_formatter.rb b/lib/gitlab/github_import/pull_request_formatter.rb index be970f724c6..ef32a23b045 100644 --- a/lib/gitlab/github_import/pull_request_formatter.rb +++ b/lib/gitlab/github_import/pull_request_formatter.rb @@ -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? -- GitLab