From 8b2a13a107913692d3e172599161264eebac96fd Mon Sep 17 00:00:00 2001 From: Ahmad Sherif Date: Fri, 3 Mar 2017 14:41:46 +0200 Subject: [PATCH] Fetch GH PR source refs in bulk --- app/models/repository.rb | 5 +++++ lib/gitlab/github_import/importer.rb | 12 +++++++++++- lib/gitlab/github_import/pull_request_formatter.rb | 4 ++-- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/app/models/repository.rb b/app/models/repository.rb index 6ab04440ca87..840ad3868756 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -1032,6 +1032,11 @@ def fetch_ref(source_path, source_ref, target_ref) Gitlab::Popen.popen(args, path_to_repo) end + def fetch_refs(source_path, refspecs) + args = %W(#{Gitlab.config.git.bin_path} fetch --no-tags -f #{source_path}) + refspecs + Gitlab::Popen.popen(args, path_to_repo) + end + def create_ref(ref, ref_path) fetch_ref(path_to_repo, ref, ref_path) end diff --git a/lib/gitlab/github_import/importer.rb b/lib/gitlab/github_import/importer.rb index eea4a91f17d4..0c7c9327c508 100644 --- a/lib/gitlab/github_import/importer.rb +++ b/lib/gitlab/github_import/importer.rb @@ -130,13 +130,23 @@ def import_issues def import_pull_requests fetch_resources(:pull_requests, repo, state: :all, sort: :created, direction: :asc, per_page: 100) do |pull_requests| + project.repository.expire_branches_cache + + refspecs = pull_requests.map do |raw| + gh_pull_request = PullRequestFormatter.new(project, raw, client) + next if gh_pull_request.source_branch_exists? + + "pull/#{gh_pull_request.number}/head:#{gh_pull_request.source_branch_name}" + end + + project.repository.fetch_refs(repo_url, refspecs.compact) + pull_requests.each do |raw| gh_pull_request = PullRequestFormatter.new(project, raw, client) next unless gh_pull_request.valid? begin - restore_source_branch(gh_pull_request) unless gh_pull_request.source_branch_exists? restore_target_branch(gh_pull_request) unless gh_pull_request.target_branch_exists? merge_request = gh_pull_request.create! diff --git a/lib/gitlab/github_import/pull_request_formatter.rb b/lib/gitlab/github_import/pull_request_formatter.rb index add7236e3398..ed90c3617fdf 100644 --- a/lib/gitlab/github_import/pull_request_formatter.rb +++ b/lib/gitlab/github_import/pull_request_formatter.rb @@ -39,7 +39,7 @@ def source_branch def source_branch_name @source_branch_name ||= begin if cross_project? - "pull/#{number}/#{source_branch_repo.full_name}/#{source_branch_ref}" + "pull-source/#{number}/#{source_branch_repo.full_name}/#{source_branch_ref}" else source_branch_exists? ? source_branch_ref : "pull/#{number}/#{source_branch_ref}" end @@ -52,7 +52,7 @@ def target_branch def target_branch_name @target_branch_name ||= begin - target_branch_exists? ? target_branch_ref : "pull/#{number}/#{target_branch_ref}" + target_branch_exists? ? target_branch_ref : "pull-target/#{number}/#{target_branch_ref}" end end -- GitLab