Class: Gitlab::BitbucketServerImport::UserFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/bitbucket_server_import/user_finder.rb

Overview

Class that can be used for finding a GitLab user ID based on a BitBucket user

Constant Summary collapse

CACHE_KEY =
'bitbucket_server-importer/user-finder/%{project_id}/%{by}/%{value}'
CACHE_USER_ID_NOT_FOUND =
-1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project) ⇒ UserFinder

project - An instance of ‘Project`



14
15
16
# File 'lib/gitlab/bitbucket_server_import/user_finder.rb', line 14

def initialize(project)
  @project = project
end

Instance Attribute Details

#projectObject (readonly)

Returns the value of attribute project.



8
9
10
# File 'lib/gitlab/bitbucket_server_import/user_finder.rb', line 8

def project
  @project
end

Instance Method Details

#author_id(object) ⇒ Object



18
19
20
# File 'lib/gitlab/bitbucket_server_import/user_finder.rb', line 18

def author_id(object)
  uid(object) || project.creator_id
end

#find_user_id(by:, value:) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/gitlab/bitbucket_server_import/user_finder.rb', line 34

def find_user_id(by:, value:)
  return unless value

  cache_key = build_cache_key(by, value)
  cached_id = cache.read_integer(cache_key)

  return if cached_id == CACHE_USER_ID_NOT_FOUND
  return cached_id if cached_id

  user = if by == :email
           User.find_by_any_email(value, confirmed: true)
         else
           User.find_by_username(value)
         end

  user&.id.tap do |id|
    cache.write(cache_key, id || CACHE_USER_ID_NOT_FOUND)
  end
end

#uid(object) ⇒ Object

Object should behave as a object so we can remove object.is_a?(Hash) check This will be fixed in gitlab.com/gitlab-org/gitlab/-/issues/412328



24
25
26
27
28
29
30
31
32
# File 'lib/gitlab/bitbucket_server_import/user_finder.rb', line 24

def uid(object)
  # We want this to only match either username or email depending on the flag state.
  # There should be no fall-through.
  if Feature.enabled?(:bitbucket_server_user_mapping_by_username, type: :ops)
    find_user_id(by: :username, value: object.is_a?(Hash) ? object[:author_username] : object.author_username)
  else
    find_user_id(by: :email, value: object.is_a?(Hash) ? object[:author_email] : object.author_email)
  end
end