Commit 4e2a38e8 authored by Dylan Griffith's avatar Dylan Griffith

Merge branch '213929-move-package-model-to-core' into 'master'

Move package model to core [RUN AS-IF-FOSS]

See merge request gitlab-org/gitlab!33696
parents ad33a4c5 9752dac7
......@@ -859,7 +859,7 @@ Rails/SaveBang:
- 'ee/spec/models/operations/feature_flag_spec.rb'
- 'ee/spec/models/operations/feature_flags/strategy_spec.rb'
- 'ee/spec/models/operations/feature_flags/user_list_spec.rb'
- 'ee/spec/models/packages/package_spec.rb'
- 'spec/models/packages/package_spec.rb'
- 'ee/spec/models/project_ci_cd_setting_spec.rb'
- 'ee/spec/models/project_services/github_service_spec.rb'
- 'ee/spec/models/project_services/jenkins_service_spec.rb'
......
# frozen_string_literal: true
class Packages::PackageFile < ApplicationRecord
include UpdateProjectStatistics
include ::Gitlab::Geo::ReplicableModel
delegate :project, :project_id, to: :package
delegate :conan_file_type, to: :conan_file_metadatum
......@@ -34,31 +33,10 @@ class Packages::PackageFile < ApplicationRecord
mount_uploader :file, Packages::PackageFileUploader
with_replicator Geo::PackageFileReplicator
after_save :update_file_metadata, if: :saved_change_to_file?
update_project_statistics project_statistics_name: :packages_size
def self.replicables_for_geo_node
return self.all unless Gitlab::Geo.current_node.selective_sync?
query = ::Packages::Package.where(project_id: Gitlab::Geo.current_node.projects).select(:id)
cte = Gitlab::SQL::CTE.new(:restricted_packages, query)
replicable_table = self.arel_table
inner_join_restricted_packages =
cte.table
.join(replicable_table, Arel::Nodes::InnerJoin)
.on(cte.table[:id].eq(replicable_table[:package_id]))
.join_sources
self
.with(cte.to_arel)
.from(cte.table)
.joins(inner_join_restricted_packages)
end
def update_file_metadata
# The file.object_store is set during `uploader.store!`
# which happens after object is inserted/updated
......@@ -66,11 +44,6 @@ class Packages::PackageFile < ApplicationRecord
self.update_column(:size, file.size) unless file.size == self.size
end
def log_geo_deleted_event
# Keep empty for now. Should be addressed in future
# by https://gitlab.com/gitlab-org/gitlab/issues/7891
end
def download_path
Gitlab::Routing.url_helpers.download_project_package_file_path(project, self)
end
......@@ -79,3 +52,5 @@ class Packages::PackageFile < ApplicationRecord
file_store == ::Packages::PackageFileUploader::Store::LOCAL
end
end
Packages::PackageFile.prepend_if_ee('EE::Packages::PackageFileGeo')
......@@ -190,6 +190,10 @@ class Project < ApplicationRecord
has_many :forks, through: :forked_to_members, source: :project, inverse_of: :forked_from_project
has_many :fork_network_projects, through: :fork_network, source: :projects
# Packages
has_many :packages, class_name: 'Packages::Package'
has_many :package_files, through: :packages, class_name: 'Packages::PackageFile'
has_one :import_state, autosave: true, class_name: 'ProjectImportState', inverse_of: :project
has_one :import_export_upload, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
has_many :export_jobs, class_name: 'ProjectExportJob'
......@@ -2421,6 +2425,22 @@ class Project < ApplicationRecord
end
alias_method :service_desk_enabled?, :service_desk_enabled
def root_namespace
if namespace.has_parent?
namespace.root_ancestor
else
namespace
end
end
def package_already_taken?(package_name)
namespace.root_ancestor.all_projects
.joins(:packages)
.where.not(id: id)
.merge(Packages::Package.with_name(package_name))
.exists?
end
private
def find_service(services, name)
......
......@@ -347,12 +347,10 @@ Settings.uploads['object_store']['remote_directory'] ||= 'uploads'
#
# Packages
#
Gitlab.ee do
Settings['packages'] ||= Settingslogic.new({})
Settings.packages['enabled'] = true if Settings.packages['enabled'].nil?
Settings.packages['storage_path'] = Settings.absolute(Settings.packages['storage_path'] || File.join(Settings.shared['path'], "packages"))
Settings.packages['object_store'] = ObjectStoreSettings.legacy_parse(Settings.packages['object_store'])
end
Settings['packages'] ||= Settingslogic.new({})
Settings.packages['enabled'] = true if Settings.packages['enabled'].nil?
Settings.packages['storage_path'] = Settings.absolute(Settings.packages['storage_path'] || File.join(Settings.shared['path'], "packages"))
Settings.packages['object_store'] = ObjectStoreSettings.legacy_parse(Settings.packages['object_store'])
#
# Dependency Proxy
......
# frozen_string_literal: true
module EE
module Packages
module PackageFileGeo
extend ActiveSupport::Concern
prepended do
include ::Gitlab::Geo::ReplicableModel
with_replicator Geo::PackageFileReplicator
end
class_methods do
def replicables_for_geo_node
return self.all unless ::Gitlab::Geo.current_node.selective_sync?
query = ::Packages::Package.where(project_id: ::Gitlab::Geo.current_node.projects).select(:id)
cte = ::Gitlab::SQL::CTE.new(:restricted_packages, query)
replicable_table = self.arel_table
inner_join_restricted_packages =
cte.table
.join(replicable_table, Arel::Nodes::InnerJoin)
.on(cte.table[:id].eq(replicable_table[:package_id]))
.join_sources
self
.with(cte.to_arel)
.from(cte.table)
.joins(inner_join_restricted_packages)
end
end
def log_geo_deleted_event
# Keep empty for now. Should be addressed in future
# by https://gitlab.com/gitlab-org/gitlab/issues/7891
end
end
end
end
......@@ -75,8 +75,6 @@ module EE
has_many :software_license_policies, inverse_of: :project, class_name: 'SoftwareLicensePolicy'
has_many :software_licenses, through: :software_license_policies
accepts_nested_attributes_for :software_license_policies, allow_destroy: true
has_many :packages, class_name: 'Packages::Package'
has_many :package_files, through: :packages, class_name: 'Packages::PackageFile'
has_many :merge_trains, foreign_key: 'target_project_id', inverse_of: :target_project
has_many :operations_feature_flags, class_name: 'Operations::FeatureFlag'
......@@ -630,14 +628,6 @@ module EE
instance.token
end
def root_namespace
if namespace.has_parent?
namespace.root_ancestor
else
namespace
end
end
override :lfs_http_url_to_repo
def lfs_http_url_to_repo(operation)
return super unless ::Gitlab::Geo.secondary_with_primary?
......@@ -650,14 +640,6 @@ module EE
super.presence || build_feature_usage
end
def package_already_taken?(package_name)
namespace.root_ancestor.all_projects
.joins(:packages)
.where.not(id: id)
.merge(Packages::Package.with_name(package_name))
.exists?
end
def adjourned_deletion?
feature_available?(:adjourned_deletion_for_projects_and_groups) &&
::Gitlab::CurrentSettings.deletion_adjourned_period > 0
......
......@@ -24,7 +24,7 @@ module EE
super
return unless group.packages_feature_available?
npm_packages = Packages::GroupPackagesFinder.new(current_user, group, package_type: :npm).execute
npm_packages = ::Packages::GroupPackagesFinder.new(current_user, group, package_type: :npm).execute
if different_root_ancestor? && npm_packages.exists?
raise_ee_transfer_error(:group_contains_npm_packages)
end
......
......@@ -118,7 +118,7 @@ module EE
return true if params[:path].blank?
return true if !group.has_parent? && group.path == params[:path]
npm_packages = Packages::GroupPackagesFinder.new(current_user, group, package_type: :npm).execute
npm_packages = ::Packages::GroupPackagesFinder.new(current_user, group, package_type: :npm).execute
if npm_packages.exists?
group.errors.add(:path, s_('GroupSettings|cannot change when group contains projects with NPM packages'))
return
......
......@@ -2,71 +2,6 @@
require 'spec_helper'
RSpec.describe Packages::PackageFile, type: :model do
describe 'relationships' do
it { is_expected.to belong_to(:package) }
it { is_expected.to have_one(:conan_file_metadatum) }
end
describe 'validations' do
it { is_expected.to validate_presence_of(:package) }
end
context 'with package filenames' do
let_it_be(:package_file1) { create(:package_file, :xml, file_name: 'FooBar') }
let_it_be(:package_file2) { create(:package_file, :xml, file_name: 'ThisIsATest') }
describe '.with_file_name' do
let(:filename) { 'FooBar' }
subject { described_class.with_file_name(filename) }
it { is_expected.to match_array([package_file1]) }
end
describe '.with_file_name_like' do
let(:filename) { 'foobar' }
subject { described_class.with_file_name_like(filename) }
it { is_expected.to match_array([package_file1]) }
end
end
it_behaves_like 'UpdateProjectStatistics' do
subject { build(:package_file, :jar, size: 42) }
before do
allow_any_instance_of(Packages::PackageFileUploader).to receive(:size).and_return(42)
end
end
describe '.with_conan_package_reference' do
let_it_be(:non_matching_package_file) { create(:package_file, :nuget) }
let_it_be(:metadatum) { create(:conan_file_metadatum, :package_file) }
let_it_be(:reference) { metadatum.conan_package_reference}
it 'returns matching packages' do
expect(described_class.with_conan_package_reference(reference))
.to eq([metadatum.package_file])
end
end
describe '#update_file_metadata callback' do
let_it_be(:package_file) { build(:package_file, :nuget, file_store: nil, size: nil) }
subject { package_file.save! }
it 'updates metadata columns' do
expect(package_file)
.to receive(:update_file_metadata)
.and_call_original
expect { subject }
.to change { package_file.file_store }.from(nil).to(::Packages::PackageFileUploader::Store::LOCAL)
.and change { package_file.size }.from(nil).to(3513)
end
end
describe '#calculate_checksum!' do
let(:package_file) { create(:conan_package_file, :conan_recipe_file) }
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Packages::PackageFile, type: :model do
describe 'relationships' do
it { is_expected.to belong_to(:package) }
it { is_expected.to have_one(:conan_file_metadatum) }
end
describe 'validations' do
it { is_expected.to validate_presence_of(:package) }
end
context 'with package filenames' do
let_it_be(:package_file1) { create(:package_file, :xml, file_name: 'FooBar') }
let_it_be(:package_file2) { create(:package_file, :xml, file_name: 'ThisIsATest') }
describe '.with_file_name' do
let(:filename) { 'FooBar' }
subject { described_class.with_file_name(filename) }
it { is_expected.to match_array([package_file1]) }
end
describe '.with_file_name_like' do
let(:filename) { 'foobar' }
subject { described_class.with_file_name_like(filename) }
it { is_expected.to match_array([package_file1]) }
end
end
it_behaves_like 'UpdateProjectStatistics' do
subject { build(:package_file, :jar, size: 42) }
before do
allow_any_instance_of(Packages::PackageFileUploader).to receive(:size).and_return(42)
end
end
describe '.with_conan_package_reference' do
let_it_be(:non_matching_package_file) { create(:package_file, :nuget) }
let_it_be(:metadatum) { create(:conan_file_metadatum, :package_file) }
let_it_be(:reference) { metadatum.conan_package_reference}
it 'returns matching packages' do
expect(described_class.with_conan_package_reference(reference))
.to eq([metadatum.package_file])
end
end
describe '#update_file_metadata callback' do
let_it_be(:package_file) { build(:package_file, :nuget, file_store: nil, size: nil) }
subject { package_file.save! }
it 'updates metadata columns' do
expect(package_file)
.to receive(:update_file_metadata)
.and_call_original
expect { subject }
.to change { package_file.file_store }.from(nil).to(::Packages::PackageFileUploader::Store::LOCAL)
.and change { package_file.size }.from(nil).to(3513)
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment