Welcome to mirror list, hosted at ThFree Co, Russian Federation.

user_formatter.rb « legacy_github_import « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8fd8354e59cdd53aae575bf6e784e0804fb40b6e (plain)
1
2
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# frozen_string_literal: true

module Gitlab
  module LegacyGithubImport
    class UserFormatter
      attr_reader :client, :raw

      GITEA_GHOST_EMAIL = 'ghost_user@gitea_import_dummy_email.com'

      def initialize(client, raw)
        @client = client
        @raw = raw
      end

      def id
        raw[:id]
      end

      def login
        raw[:login]
      end

      def gitlab_id
        return @gitlab_id if defined?(@gitlab_id)

        @gitlab_id = find_by_external_uid || find_by_email
      end

      private

      def email
        # Gitea marks deleted users as 'Ghost' users and removes them from
        # their system. So for Gitea 'Ghost' users  we need to assign a dummy
        # email address to avoid querying the Gitea api for a non existing user
        if raw[:login] == 'Ghost' && raw[:id] == -1
          @email = GITEA_GHOST_EMAIL
        else
          @email ||= client.user(raw[:login]).to_h[:email]
        end
      end

      def find_by_email
        return unless email

        User.find_by_any_email(email)
            .try(:id)
      end

      # rubocop: disable CodeReuse/ActiveRecord
      def find_by_external_uid
        return unless id

        User.by_provider_and_extern_uid(:github, id).select(:id).first&.id
      end
      # rubocop: enable CodeReuse/ActiveRecord
    end
  end
end