gitlab-org--gitlab-foss/lib/gitlab/phabricator_import/user_finder.rb
Bob Van Landuyt 32184839c3 Fetch users from Phabricator to link to issues
We fetch the users from Phabricator based on their Phabricator ID. If
a user with the same username exists and is a member of the project,
we set them as assignee or author.

When a user is applicable, we also cache it in Redis so we don't have
to perform the request again for the same phid.
2019-07-10 17:15:43 +02:00

52 lines
1.2 KiB
Ruby

# frozen_string_literal: true
module Gitlab
module PhabricatorImport
class UserFinder
def initialize(project, phids)
@project, @phids = project, phids
@loaded_phids = Set.new
end
def find(phid)
found_user = object_map.get_gitlab_model(phid) do
find_user_for_phid(phid)
end
loaded_phids << phid
found_user
end
private
attr_reader :project, :phids, :loaded_phids
def object_map
@object_map ||= Gitlab::PhabricatorImport::Cache::Map.new(project)
end
def find_user_for_phid(phid)
phabricator_user = phabricator_users.find { |u| u.phabricator_id == phid }
return unless phabricator_user
project.authorized_users.find_by_username(phabricator_user.username)
end
def phabricator_users
@user_responses ||= client.users(users_to_request).flat_map(&:users)
end
def users_to_request
phids - loaded_phids.to_a
end
def client
@client ||=
Gitlab::PhabricatorImport::Conduit::User
.new(phabricator_url: project.import_data.data['phabricator_url'],
api_token: project.import_data.credentials[:api_token])
end
end
end
end