Commit 92b2eb67 authored by Kerri Miller's avatar Kerri Miller

Merge branch 'debian_get_or_create_package_service' into 'master'

Add Packages::Debian::FindOrCreatePackageService [RUN ALL RSPEC]

See merge request gitlab-org/gitlab!51891
parents a8d09910 54c57605
......@@ -91,6 +91,12 @@ class Packages::Package < ApplicationRecord
joins(:conan_metadatum).where(packages_conan_metadata: { package_username: package_username })
end
scope :with_debian_codename, -> (codename) do
debian
.joins(:debian_distribution)
.where(Packages::Debian::ProjectDistribution.table_name => { codename: codename })
end
scope :preload_debian_file_metadata, -> { preload(package_files: :debian_file_metadatum) }
scope :with_composer_target, -> (target) do
includes(:composer_metadatum)
.joins(:composer_metadatum)
......
......@@ -2,7 +2,7 @@
module Packages
module Debian
class GetOrCreateIncomingService < ::Packages::CreatePackageService
class FindOrCreateIncomingService < ::Packages::CreatePackageService
def execute
find_or_create_package!(:debian, name: 'incoming', version: nil)
end
......
# frozen_string_literal: true
module Packages
module Debian
class FindOrCreatePackageService < ::Packages::CreatePackageService
include Gitlab::Utils::StrongMemoize
def execute
package = project.packages
.debian
.with_name(params[:name])
.with_version(params[:version])
.with_debian_codename(params[:distribution_name])
.first
package ||= create_package!(
:debian,
debian_publication_attributes: { distribution_id: distribution.id }
)
ServiceResponse.success(payload: { package: package })
end
private
def distribution
strong_memoize(:distribution) do
Packages::Debian::DistributionsFinder.new(project, codename: params[:distribution_name]).execute.last!
end
end
end
end
end
......@@ -22,6 +22,14 @@ RSpec.describe Packages::Package, type: :model do
it { is_expected.to have_one(:rubygems_metadatum).inverse_of(:package) }
end
describe '.with_debian_codename' do
let_it_be(:publication) { create(:debian_publication) }
subject { described_class.with_debian_codename(publication.distribution.codename).to_a }
it { is_expected.to contain_exactly(publication.package) }
end
describe '.with_composer_target' do
let!(:package1) { create(:composer_package, :with_metadatum, sha: '123') }
let!(:package2) { create(:composer_package, :with_metadatum, sha: '123') }
......
......@@ -2,7 +2,7 @@
require 'spec_helper'
RSpec.describe Packages::Debian::GetOrCreateIncomingService do
RSpec.describe Packages::Debian::FindOrCreateIncomingService do
let_it_be(:project) { create(:project) }
let_it_be(:user) { create(:user) }
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Packages::Debian::FindOrCreatePackageService do
let_it_be(:distribution) { create(:debian_project_distribution) }
let_it_be(:project) { distribution.project }
let_it_be(:user) { create(:user) }
let(:params) { { name: 'foo', version: '1.0+debian', distribution_name: distribution.codename } }
subject(:service) { described_class.new(project, user, params) }
describe '#execute' do
subject { service.execute }
let(:package) { subject.payload[:package] }
context 'run once' do
it 'creates a new package', :aggregate_failures do
expect { subject }.to change { ::Packages::Package.count }.by(1)
expect(subject).to be_success
expect(package).to be_valid
expect(package.project_id).to eq(project.id)
expect(package.creator_id).to eq(user.id)
expect(package.name).to eq('foo')
expect(package.version).to eq('1.0+debian')
expect(package).to be_debian
expect(package.debian_publication.distribution).to eq(distribution)
end
end
context 'run twice' do
let(:subject2) { service.execute }
let(:package2) { service.execute.payload[:package] }
it 'returns the same object' do
expect { subject }.to change { ::Packages::Package.count }.by(1)
expect { package2 }.not_to change { ::Packages::Package.count }
expect(package2.id).to eq(package.id)
end
end
context 'with non-existing distribution' do
let(:params) { { name: 'foo', version: '1.0+debian', distribution_name: 'not-existing' } }
it 'raises ActiveRecord::RecordNotFound' do
expect { package }.to raise_error(ActiveRecord::RecordNotFound)
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