gitlab-org--gitlab-foss/lib/gitlab/github_import/user_formatter.rb
Douglas Barbosa Alexandre 2d1e0a0811 GitHub Importer - Find users based on their email address
The returned email by the GitHub API is the user's publicly visible
email address (or null if the user has not specified a public email
address in their profile)
2017-02-20 14:48:12 -03:00

52 lines
1.2 KiB
Ruby

module Gitlab
module GithubImport
class UserFormatter
attr_reader :client, :raw
delegate :id, :login, to: :raw, allow_nil: true
def initialize(client, raw)
@client = client
@raw = raw
end
def gitlab_id
return @gitlab_id if defined?(@gitlab_id)
@gitlab_id = find_by_external_uid || find_by_email
end
private
def email
@email ||= client.user(raw.login).try(:email)
end
def find_by_email
return nil unless email
users = ::User.arel_table
emails = ::Email.arel_table
left_join_emails = users.join(emails, Arel::Nodes::OuterJoin).on(
users[:id].eq(emails[:user_id])
).join_sources
User.select(:id)
.joins(left_join_emails)
.where(users[:email].eq(email).or(emails[:email].eq(email)))
.first.try(:id)
end
def find_by_external_uid
return nil unless id
identities = ::Identity.arel_table
User.select(:id)
.joins(:identities).where(identities[:provider].eq(:github)
.and(identities[:extern_uid].eq(id))).
first.try(:id)
end
end
end
end