Commit ad9bec95 authored by Dylan Griffith's avatar Dylan Griffith

Merge branch '199673-package-metadataum-associations' into 'master'

Validate package_type for metadatum records

Closes #199673

See merge request gitlab-org/gitlab!24530
parents c812b319 fc7613e1
# frozen_string_literal: true
class Packages::ConanMetadatum < ApplicationRecord
belongs_to :package, inverse_of: :conan_metadatum
belongs_to :package, -> { where(package_type: :conan) }, inverse_of: :conan_metadatum
validates :package, presence: true
......@@ -13,6 +13,8 @@ class Packages::ConanMetadatum < ApplicationRecord
presence: true,
format: { with: Gitlab::Regex.conan_recipe_component_regex }
validate :conan_package_type
def recipe
"#{package.name}/#{package.version}@#{package_username}/#{package_channel}"
end
......@@ -28,4 +30,12 @@ class Packages::ConanMetadatum < ApplicationRecord
def self.full_path_from(package_username:)
package_username.tr('+', '/')
end
private
def conan_package_type
unless package && package.conan?
errors.add(:base, 'Package type must be Conan')
end
end
end
# frozen_string_literal: true
class Packages::MavenMetadatum < ApplicationRecord
belongs_to :package
belongs_to :package, -> { where(package_type: :maven) }
validates :package, presence: true
......@@ -15,4 +15,14 @@ class Packages::MavenMetadatum < ApplicationRecord
validates :app_name,
presence: true,
format: { with: Gitlab::Regex.maven_app_name_regex }
validate :maven_package_type
private
def maven_package_type
unless package && package.maven?
errors.add(:base, 'Package type must be Maven')
end
end
end
......@@ -4,7 +4,7 @@ FactoryBot.define do
project
name { 'my/company/app/my-app' }
sequence(:version) { |n| "1.#{n}-SNAPSHOT" }
package_type { 'maven' }
package_type { :maven }
factory :maven_package do
maven_metadatum
......@@ -23,7 +23,7 @@ FactoryBot.define do
factory :npm_package do
sequence(:name) { |n| "@#{project.root_namespace.path}/package-#{n}"}
version { '1.0.0' }
package_type { 'npm' }
package_type { :npm }
after :create do |package|
create :package_file, :npm, package: package
......@@ -57,7 +57,7 @@ FactoryBot.define do
sequence(:name) { |n| "package-#{n}" }
version { '1.0.0' }
package_type { 'conan' }
package_type { :conan }
after :create do |package|
create :conan_package_file, :conan_recipe_file, package: package
......@@ -180,7 +180,7 @@ FactoryBot.define do
end
factory :maven_metadatum, class: 'Packages::MavenMetadatum' do
package
association :package, package_type: :maven
path { 'my/company/app/my-app/1.0-SNAPSHOT' }
app_group { 'my.company.app' }
app_name { 'my-app' }
......@@ -188,7 +188,7 @@ FactoryBot.define do
end
factory :conan_metadatum, class: 'Packages::ConanMetadatum' do
package
association :package, package_type: :conan
package_username { 'username' }
package_channel { 'stable' }
end
......
......@@ -26,6 +26,16 @@ RSpec.describe Packages::ConanMetadatum, type: :model do
it { is_expected.not_to allow_value("my(channel)").for(:package_channel) }
it { is_expected.not_to allow_value("my@channel").for(:package_channel) }
end
describe '#conan_package_type' do
it "will not allow a package with a different package_type" do
package = build('package')
conan_metadatum = build('conan_metadatum', package: package)
expect(conan_metadatum).not_to be_valid
expect(conan_metadatum.errors.to_a).to include("Package type must be Conan")
end
end
end
describe '#recipe' do
......
......@@ -26,5 +26,15 @@ RSpec.describe Packages::MavenMetadatum, type: :model do
it { is_expected.to allow_value("my/domain/com/my-app/1.0-SNAPSHOT").for(:path) }
it { is_expected.not_to allow_value("my(domain)com.my-app").for(:path) }
end
describe '#maven_package_type' do
it "will not allow a package with a different package_type" do
package = build('conan_package')
maven_metadatum = build('maven_metadatum', package: package)
expect(maven_metadatum).not_to be_valid
expect(maven_metadatum.errors.to_a).to include("Package type must be Maven")
end
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