debian-mirror-gitlab/lib/gitlab/legacy_github_import/user_formatter.rb

49 lines
1 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
module Gitlab
2018-03-17 18:26:18 +05:30
module LegacyGithubImport
2017-08-17 22:00:37 +05:30
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-07-07 11:18:12 +05:30
return unless email
2017-08-17 22:00:37 +05:30
User.find_by_any_email(email)
.try(:id)
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
def find_by_external_uid
2019-07-07 11:18:12 +05:30
return unless id
2017-08-17 22:00:37 +05:30
identities = ::Identity.arel_table
User.select(:id)
2020-04-22 19:07:51 +05:30
.joins(:identities)
.find_by(identities[:provider].eq(:github).and(identities[:extern_uid].eq(id)))
2017-08-17 22:00:37 +05:30
.try(:id)
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
end
end
end