Class: Import::SourceUser

Inherits:
ApplicationRecord show all
Includes:
EachBatch, Gitlab::SQL::Pattern
Defined in:
app/models/import/source_user.rb

Constant Summary collapse

SORT_ORDERS =
{
  source_name_asc: { order_by: 'source_name', sort: 'asc' },
  source_name_desc: { order_by: 'source_name', sort: 'desc' },
  status_asc: { order_by: 'status', sort: 'asc' },
  status_desc: { order_by: 'status', sort: 'desc' },
  id_asc: { order_by: 'id', sort: 'asc' },
  id_desc: { order_by: 'id', sort: 'desc' }
}.freeze
STATUSES =
{
  pending_reassignment: 0,
  awaiting_approval: 1,
  reassignment_in_progress: 2,
  rejected: 3,
  failed: 4,
  completed: 5,
  keep_as_placeholder: 6
}.freeze
ACCEPTED_STATUSES =
%i[reassignment_in_progress completed failed].freeze
REASSIGNABLE_STATUSES =
%i[pending_reassignment rejected].freeze
CANCELABLE_STATUSES =
%i[awaiting_approval rejected].freeze

Constants included from Gitlab::SQL::Pattern

Gitlab::SQL::Pattern::MIN_CHARS_FOR_PARTIAL_MATCHING, Gitlab::SQL::Pattern::REGEX_QUOTED_TERM

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Constants included from HasCheckConstraints

HasCheckConstraints::NOT_NULL_CHECK_PATTERN

Constants included from ResetOnColumnErrors

ResetOnColumnErrors::MAX_RESET_PERIOD

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::SQL::Pattern

split_query_to_search_terms

Methods inherited from ApplicationRecord

===, cached_column_list, #create_or_load_association, current_transaction, declarative_enum, default_select_columns, delete_all_returning, #deleted_from_database?, id_in, id_not_in, iid_in, nullable_column?, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, #to_ability_name, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order

Methods included from Organizations::Sharding

#sharding_organization

Methods included from ResetOnColumnErrors

#reset_on_union_error, #reset_on_unknown_attribute_error

Methods included from Gitlab::SensitiveSerializableHash

#serializable_hash

Class Method Details

.find_by_namespace_and_token(namespace_id:, reassignment_token:) ⇒ Object



131
132
133
134
135
136
# File 'app/models/import/source_user.rb', line 131

def find_by_namespace_and_token(namespace_id:, reassignment_token:)
  find_by(
    namespace_id: namespace_id,
    reassignment_token: reassignment_token
  )
end

.find_source_user(source_user_identifier:, namespace:, source_hostname:, import_type:) ⇒ Object



120
121
122
123
124
125
126
127
128
129
# File 'app/models/import/source_user.rb', line 120

def find_source_user(source_user_identifier:, namespace:, source_hostname:, import_type:)
  return unless namespace

  find_by(
    source_user_identifier: source_user_identifier,
    namespace_id: namespace.id,
    source_hostname: source_hostname,
    import_type: import_type
  )
end

.namespace_placeholder_user_count(namespace, limit:) ⇒ Object



150
151
152
153
# File 'app/models/import/source_user.rb', line 150

def namespace_placeholder_user_count(namespace, limit:)
  for_namespace(namespace).distinct.limit(limit).count(:placeholder_user_id) -
    (namespace.namespace_import_user.present? ? 1 : 0)
end

.search(query) ⇒ Object



138
139
140
141
142
# File 'app/models/import/source_user.rb', line 138

def search(query)
  return none unless query.is_a?(String)

  fuzzy_search(query, [:source_name, :source_username])
end

.sort_by_attribute(method) ⇒ Object



144
145
146
147
148
# File 'app/models/import/source_user.rb', line 144

def sort_by_attribute(method)
  sort_order = SORT_ORDERS[method&.to_sym] || SORT_ORDERS[:source_name_asc]

  reorder(sort_order[:order_by] => sort_order[:sort])
end

.source_users_with_missing_information(namespace:, source_hostname:, import_type:) ⇒ Object



155
156
157
158
159
160
161
162
# File 'app/models/import/source_user.rb', line 155

def source_users_with_missing_information(namespace:, source_hostname:, import_type:)
  for_namespace(namespace)
    .by_source_hostname(source_hostname)
    .by_import_type(import_type)
    .and(
      where(source_name: nil).or(where(source_username: nil))
    )
end

Instance Method Details

#accepted_status?Boolean



173
174
175
# File 'app/models/import/source_user.rb', line 173

def accepted_status?
  STATUSES.slice(*ACCEPTED_STATUSES).value?(status)
end

#cancelable_status?Boolean



181
182
183
# File 'app/models/import/source_user.rb', line 181

def cancelable_status?
  STATUSES.slice(*CANCELABLE_STATUSES).value?(status)
end

#mapped_userObject



165
166
167
# File 'app/models/import/source_user.rb', line 165

def mapped_user
  accepted_status? ? reassign_to_user : placeholder_user
end

#mapped_user_idObject



169
170
171
# File 'app/models/import/source_user.rb', line 169

def mapped_user_id
  accepted_status? ? reassign_to_user_id : placeholder_user_id
end

#reassignable_status?Boolean



177
178
179
# File 'app/models/import/source_user.rb', line 177

def reassignable_status?
  STATUSES.slice(*REASSIGNABLE_STATUSES).value?(status)
end

#validate_source_hostnameObject



185
186
187
188
189
190
191
192
193
# File 'app/models/import/source_user.rb', line 185

def validate_source_hostname
  return unless source_hostname

  uri = Gitlab::Utils.parse_url(source_hostname)

  return if uri && uri.scheme && uri.host && uri.path.blank?

  errors.add(:source_hostname, :invalid, message: 'must contain scheme and host, and not path')
end