Class: Packages::Package

Inherits:
ApplicationRecord show all
Includes:
EachBatch, Gitlab::SQL::Pattern, Gitlab::Utils::StrongMemoize, Installable, Sortable, UsageStatistics
Defined in:
app/models/packages/package.rb

Constant Summary collapse

DISPLAYABLE_STATUSES =
[:default, :error].freeze
INSTALLABLE_STATUSES =
[:default, :hidden].freeze

Constants included from Gitlab::SQL::Pattern

Gitlab::SQL::Pattern::MIN_CHARS_FOR_PARTIAL_MATCHING, Gitlab::SQL::Pattern::REGEX_QUOTED_WORD

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Methods inherited from ApplicationRecord

cached_column_list, #create_or_load_association, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, pluck_primary_key, 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 SensitiveSerializableHash

#serializable_hash

Class Method Details

.by_file_name_and_sha256(file_name, sha256) ⇒ Object


193
194
195
196
# File 'app/models/packages/package.rb', line 193

def self.by_file_name_and_sha256(file_name, sha256)
  joins(:package_files)
    .where(packages_package_files: { file_name: file_name, file_sha256: sha256 }).last!
end

.by_name_and_file_name(name, file_name) ⇒ Object


187
188
189
190
191
# File 'app/models/packages/package.rb', line 187

def self.by_name_and_file_name(name, file_name)
  with_name(name)
    .joins(:package_files)
    .where(packages_package_files: { file_name: file_name }).last!
end

.by_name_and_version!(name, version) ⇒ Object


198
199
200
# File 'app/models/packages/package.rb', line 198

def self.by_name_and_version!(name, version)
  find_by!(name: name, version: version)
end

.keyset_pagination_order(join_class:, column_name:, direction: :asc) ⇒ Object


229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'app/models/packages/package.rb', line 229

def self.keyset_pagination_order(join_class:, column_name:, direction: :asc)
  join_table = join_class.table_name
  asc_order_expression = join_class.arel_table[column_name].asc.nulls_last
  desc_order_expression = join_class.arel_table[column_name].desc.nulls_first
  order_direction = direction == :asc ? asc_order_expression : desc_order_expression
  reverse_order_direction = direction == :asc ? desc_order_expression : asc_order_expression
  arel_order_classes = ::Gitlab::Pagination::Keyset::ColumnOrderDefinition::AREL_ORDER_CLASSES.invert

  ::Gitlab::Pagination::Keyset::Order.build([
    ::Gitlab::Pagination::Keyset::ColumnOrderDefinition.new(
      attribute_name: "#{join_table}_#{column_name}",
      column_expression: join_class.arel_table[column_name],
      order_expression: order_direction,
      reversed_order_expression: reverse_order_direction,
      order_direction: direction,
      distinct: false,
      add_to_projections: true
    ),
    ::Gitlab::Pagination::Keyset::ColumnOrderDefinition.new(
      attribute_name: 'id',
      order_expression: arel_order_classes[direction].new(Packages::Package.arel_table[:id]),
      add_to_projections: true
    )
  ])
end

.only_maven_packages_with_path(path, use_cte: false) ⇒ Object


172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'app/models/packages/package.rb', line 172

def self.only_maven_packages_with_path(path, use_cte: false)
  if use_cte
    # This is an optimization fence which assumes that looking up the Metadatum record by path (globally)
    # and then filter down the packages (by project or by group and subgroups) will be cheaper than
    # looking up all packages within a project or group and filter them by path.

    inner_query = Packages::Maven::Metadatum.where(path: path).select(:id, :package_id)
    cte = Gitlab::SQL::CTE.new(:maven_metadata_by_path, inner_query)
    with(cte.to_arel)
      .joins('INNER JOIN maven_metadata_by_path ON maven_metadata_by_path.package_id=packages_packages.id')
  else
    joins(:maven_metadatum).where(packages_maven_metadata: { path: path })
  end
end

.pluck_namesObject


202
203
204
# File 'app/models/packages/package.rb', line 202

def self.pluck_names
  pluck(:name)
end

.pluck_versionsObject


206
207
208
# File 'app/models/packages/package.rb', line 206

def self.pluck_versions
  pluck(:version)
end

.sort_by_attribute(method) ⇒ Object


210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'app/models/packages/package.rb', line 210

def self.sort_by_attribute(method)
  case method.to_s
  when 'created_asc' then order_created
  when 'created_at_asc' then order_created
  when 'name_asc' then order_name
  when 'name_desc' then order_name_desc
  when 'version_asc' then order_version
  when 'version_desc' then order_version_desc
  when 'type_asc' then order_type
  when 'type_desc' then order_type_desc
  when 'project_name_asc' then order_project_name
  when 'project_name_desc' then order_project_name_desc
  when 'project_path_asc' then order_project_path
  when 'project_path_desc' then order_project_path_desc
  else
    order_created_desc
  end
end

Instance Method Details

#create_build_infos!(build) ⇒ Object


305
306
307
308
309
310
# File 'app/models/packages/package.rb', line 305

def create_build_infos!(build)
  return unless build&.pipeline

  # TODO: use an upsert call when https://gitlab.com/gitlab-org/gitlab/-/issues/339093 is implemented
  build_infos.find_or_create_by!(pipeline: build.pipeline)
end

#debian_incoming?Boolean

Returns:

  • (Boolean)

285
286
287
# File 'app/models/packages/package.rb', line 285

def debian_incoming?
  debian? && version.nil?
end

#debian_package?Boolean

Returns:

  • (Boolean)

289
290
291
# File 'app/models/packages/package.rb', line 289

def debian_package?
  debian? && !version.nil?
end

#infrastructure_package?Boolean

Returns:

  • (Boolean)

281
282
283
# File 'app/models/packages/package.rb', line 281

def infrastructure_package?
  terraform_module?
end

#mark_package_files_for_destructionObject


312
313
314
315
316
# File 'app/models/packages/package.rb', line 312

def mark_package_files_for_destruction
  return unless pending_destruction?

  ::Packages::MarkPackageFilesForDestructionWorker.perform_async(id)
end

#original_build_infoObject

Technical debt: to be removed in gitlab.com/gitlab-org/gitlab/-/issues/281937


266
267
268
269
270
# File 'app/models/packages/package.rb', line 266

def original_build_info
  strong_memoize(:original_build_info) do
    build_infos.first
  end
end

#package_settingsObject


293
294
295
296
297
# File 'app/models/packages/package.rb', line 293

def package_settings
  strong_memoize(:package_settings) do
    project.namespace.package_settings
  end
end

#pipelineObject

Technical debt: to be removed in gitlab.com/gitlab-org/gitlab/-/issues/281937


273
274
275
# File 'app/models/packages/package.rb', line 273

def pipeline
  original_build_info&.pipeline
end

#sync_maven_metadata(user) ⇒ Object


299
300
301
302
303
# File 'app/models/packages/package.rb', line 299

def (user)
  return unless maven? && version? && user

  ::Packages::Maven::Metadata::SyncWorker.perform_async(user.id, project.id, name)
end

#tag_namesObject


277
278
279
# File 'app/models/packages/package.rb', line 277

def tag_names
  tags.pluck(:name)
end

#versionsObject


255
256
257
258
259
260
261
262
263
# File 'app/models/packages/package.rb', line 255

def versions
  project.packages
         .preload_pipelines
         .including_tags
         .with_name(name)
         .where.not(version: version)
         .with_package_type(package_type)
         .order(:version)
end