2018-11-17 00:37:17 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-02-03 15:07:22 +00:00
|
|
|
module Gitlab
|
2017-10-13 16:50:36 +00:00
|
|
|
module LegacyGithubImport
|
2017-02-03 15:07:22 +00:00
|
|
|
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
|
2019-02-08 12:19:53 +00:00
|
|
|
return unless email
|
2017-02-03 15:07:22 +00:00
|
|
|
|
2017-02-20 17:45:40 +00:00
|
|
|
User.find_by_any_email(email)
|
|
|
|
.try(:id)
|
2017-02-03 15:07:22 +00:00
|
|
|
end
|
|
|
|
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-02-03 15:07:22 +00:00
|
|
|
def find_by_external_uid
|
2019-02-08 12:19:53 +00:00
|
|
|
return unless id
|
2017-02-03 15:07:22 +00:00
|
|
|
|
|
|
|
identities = ::Identity.arel_table
|
|
|
|
|
|
|
|
User.select(:id)
|
2020-03-31 12:08:09 +00:00
|
|
|
.joins(:identities)
|
|
|
|
.find_by(identities[:provider].eq(:github).and(identities[:extern_uid].eq(id)))
|
2017-02-20 17:42:52 +00:00
|
|
|
.try(:id)
|
2017-02-03 15:07:22 +00:00
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-02-03 15:07:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|