BigW Consortium Gitlab

user_formatter.rb 929 Bytes
Newer Older
1
module Gitlab
2
  module LegacyGithubImport
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
    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

28 29
        User.find_by_any_email(email)
            .try(:id)
30 31 32 33 34 35 36 37 38
      end

      def find_by_external_uid
        return nil unless id

        identities = ::Identity.arel_table

        User.select(:id)
            .joins(:identities).where(identities[:provider].eq(:github)
39 40 41
            .and(identities[:extern_uid].eq(id)))
            .first
            .try(:id)
42 43 44 45
      end
    end
  end
end