Commit 8d4a4a85 authored by Steve Abrams's avatar Steve Abrams

Merge branch '331733-githubimporter-optimize-pull-request-mergedby-importer-2' into 'master'

GithubImporter: Optimize Pull Request MergedBy Importer

See merge request gitlab-org/gitlab!69478
parents 10686c27 4ac0e671
...@@ -31,9 +31,7 @@ module Gitlab ...@@ -31,9 +31,7 @@ module Gitlab
end end
def each_object_to_import def each_object_to_import
project.merge_requests.with_state(:merged).find_each do |merge_request| merge_requests_to_import.find_each do |merge_request|
next if already_imported?(merge_request)
Gitlab::GithubImport::ObjectCounter.increment(project, object_type, :fetched) Gitlab::GithubImport::ObjectCounter.increment(project, object_type, :fetched)
pull_request = client.pull_request(project.import_source, merge_request.iid) pull_request = client.pull_request(project.import_source, merge_request.iid)
...@@ -42,6 +40,17 @@ module Gitlab ...@@ -42,6 +40,17 @@ module Gitlab
mark_as_imported(merge_request) mark_as_imported(merge_request)
end end
end end
private
# Returns only the merge requests that still have merged_by to be imported.
def merge_requests_to_import
project.merge_requests.id_not_in(already_imported_objects).with_state(:merged)
end
def already_imported_objects
Gitlab::Cache::Import::Caching.values_from_set(already_imported_cache_key)
end
end end
end end
end end
......
...@@ -86,7 +86,7 @@ module Gitlab ...@@ -86,7 +86,7 @@ module Gitlab
# Returns only the merge requests that still have reviews to be imported. # Returns only the merge requests that still have reviews to be imported.
def merge_requests_to_import def merge_requests_to_import
project.merge_requests.where.not(id: already_imported_merge_requests) # rubocop: disable CodeReuse/ActiveRecord project.merge_requests.id_not_in(already_imported_merge_requests)
end end
def already_imported_merge_requests def already_imported_merge_requests
......
...@@ -4,7 +4,8 @@ require 'spec_helper' ...@@ -4,7 +4,8 @@ require 'spec_helper'
RSpec.describe Gitlab::GithubImport::Importer::PullRequestsMergedByImporter do RSpec.describe Gitlab::GithubImport::Importer::PullRequestsMergedByImporter do
let(:client) { double } let(:client) { double }
let(:project) { create(:project, import_source: 'http://somegithub.com') }
let_it_be(:project) { create(:project, import_source: 'http://somegithub.com') }
subject { described_class.new(project, client) } subject { described_class.new(project, client) }
...@@ -27,14 +28,11 @@ RSpec.describe Gitlab::GithubImport::Importer::PullRequestsMergedByImporter do ...@@ -27,14 +28,11 @@ RSpec.describe Gitlab::GithubImport::Importer::PullRequestsMergedByImporter do
end end
describe '#each_object_to_import', :clean_gitlab_redis_cache do describe '#each_object_to_import', :clean_gitlab_redis_cache do
it 'fetchs the merged pull requests data' do let!(:merge_request) do
create( create(:merged_merge_request, iid: 999, source_project: project, target_project: project)
:merged_merge_request, end
iid: 999,
source_project: project,
target_project: project
)
it 'fetches the merged pull requests data' do
pull_request = double pull_request = double
allow(client) allow(client)
...@@ -48,5 +46,16 @@ RSpec.describe Gitlab::GithubImport::Importer::PullRequestsMergedByImporter do ...@@ -48,5 +46,16 @@ RSpec.describe Gitlab::GithubImport::Importer::PullRequestsMergedByImporter do
subject.each_object_to_import {} subject.each_object_to_import {}
end end
it 'skips cached merge requests' do
Gitlab::Cache::Import::Caching.set_add(
"github-importer/already-imported/#{project.id}/pull_requests_merged_by",
merge_request.id
)
expect(client).not_to receive(:pull_request)
subject.each_object_to_import {}
end
end end
end end
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