Commit e4be7cb6 authored by David Fernandez's avatar David Fernandez

Merge branch 'debian_process_changes_service' into 'master'

Add Packages::Debian::ProcessChangesService

See merge request gitlab-org/gitlab!57413
parents 5067d2b5 9612794d
......@@ -58,21 +58,22 @@ module Packages
file_type == :dsc || file_type == :buildinfo || file_type == :changes
end
def extracted_fields
if file_type_debian?
package_file.file.use_file do |file_path|
::Packages::Debian::ExtractDebMetadataService.new(file_path).execute
end
elsif file_type_meta?
package_file.file.use_file do |file_path|
::Packages::Debian::ParseDebian822Service.new(File.read(file_path)).execute.each_value.first
def fields
strong_memoize(:fields) do
if file_type_debian?
package_file.file.use_file do |file_path|
::Packages::Debian::ExtractDebMetadataService.new(file_path).execute
end
elsif file_type_meta?
package_file.file.use_file do |file_path|
::Packages::Debian::ParseDebian822Service.new(File.read(file_path)).execute.each_value.first
end
end
end
end
def extract_metadata
fields = extracted_fields
architecture = fields.delete('Architecture') if file_type_debian?
architecture = fields['Architecture'] if file_type_debian?
{
file_type: file_type,
......
# frozen_string_literal: true
module Packages
module Debian
class ProcessChangesService
include ExclusiveLeaseGuard
include Gitlab::Utils::StrongMemoize
# used by ExclusiveLeaseGuard
DEFAULT_LEASE_TIMEOUT = 1.hour.to_i.freeze
def initialize(package_file, creator)
@package_file, @creator = package_file, creator
end
def execute
try_obtain_lease do
# return if changes file has already been processed
break if package_file.debian_file_metadatum&.changes?
validate!
package_file.transaction do
update_files_metadata
update_changes_metadata
end
end
end
private
attr_reader :package_file, :creator
def validate!
raise ArgumentError, 'invalid package file' unless package_file.debian_file_metadatum
raise ArgumentError, 'invalid package file' unless package_file.debian_file_metadatum.unknown?
raise ArgumentError, 'invalid package file' unless metadata[:file_type] == :changes
end
def update_files_metadata
files.each do |filename, entry|
entry.package_file.package = package
file_metadata = ::Packages::Debian::ExtractMetadataService.new(entry.package_file).execute
entry.package_file.debian_file_metadatum.update!(
file_type: file_metadata[:file_type],
component: files[filename].component,
architecture: file_metadata[:architecture],
fields: file_metadata[:fields]
)
entry.package_file.save!
end
end
def update_changes_metadata
package_file.update!(package: package)
package_file.debian_file_metadatum.update!(
file_type: metadata[:file_type],
fields: metadata[:fields]
)
end
def metadata
strong_memoize(:metadata) do
::Packages::Debian::ExtractChangesMetadataService.new(package_file).execute
end
end
def files
metadata[:files]
end
def project
package_file.package.project
end
def package
strong_memoize(:package) do
params = {
'name': metadata[:fields]['Source'],
'version': metadata[:fields]['Version'],
'distribution_name': metadata[:fields]['Distribution']
}
response = Packages::Debian::FindOrCreatePackageService.new(project, creator, params).execute
response.payload[:package]
end
end
# used by ExclusiveLeaseGuard
def lease_key
"packages:debian:process_changes_service:package_file:#{package_file.id}"
end
# used by ExclusiveLeaseGuard
def lease_timeout
DEFAULT_LEASE_TIMEOUT
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Packages::Debian::ProcessChangesService do
describe '#execute' do
let_it_be(:user) { create(:user) }
let_it_be_with_reload(:distribution) { create(:debian_project_distribution, :with_file, codename: 'unstable') }
let_it_be(:incoming) { create(:debian_incoming, project: distribution.project) }
let(:package_file) { incoming.package_files.last }
subject { described_class.new(package_file, user) }
context 'with valid package file' do
it 'updates package and package file', :aggregate_failures do
expect { subject.execute }
.to change { Packages::Package.count }.from(1).to(2)
.and not_change { Packages::PackageFile.count }
.and change { incoming.package_files.count }.from(7).to(0)
created_package = Packages::Package.last
expect(created_package.name).to eq 'sample'
expect(created_package.version).to eq '1.2.3~alpha2'
expect(created_package.creator).to eq user
end
end
context 'with invalid package file' do
let(:package_file) { incoming.package_files.first }
it 'raise ExtractionError', :aggregate_failures do
expect { subject.execute }
.to not_change { Packages::Package.count }
.and not_change { Packages::PackageFile.count }
.and not_change { incoming.package_files.count }
.and not_change { distribution.reload.needs_update? }
.and raise_error(Packages::Debian::ExtractChangesMetadataService::ExtractionError, 'is not a changes file')
end
end
context 'when creating package fails' do
before do
allow_next_instance_of(::Packages::Debian::FindOrCreatePackageService) do |find_or_create_package_service|
expect(find_or_create_package_service).to receive(:execute).and_raise(ActiveRecord::ConnectionTimeoutError, 'connect timeout')
end
end
it 'remove the package file', :aggregate_failures do
expect { subject.execute }
.to not_change { Packages::Package.count }
.and not_change { Packages::PackageFile.count }
.and not_change { incoming.package_files.count }
.and not_change { distribution.reload.needs_update? }
.and raise_error(ActiveRecord::ConnectionTimeoutError, 'connect timeout')
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