Commit 1bcfe75e authored by Dmytro Zaporozhets's avatar Dmytro Zaporozhets

Merge branch '208830-conan-package-reference-fix' into 'master'

Add conan package reference filter to package file finder

See merge request gitlab-org/gitlab!26420
parents e7935f19 ab456a89
---
title: Fix package file finder for conan packages with a conan_package_reference filter
merge_request: 26240
author:
type: fixed
# frozen_string_literal: true
module Packages
module Conan
class PackageFileFinder < ::Packages::PackageFileFinder
private
def package_files
files = super
files = by_conan_file_type(files)
files = by_conan_package_reference(files)
files
end
def by_conan_file_type(files)
return files unless params[:conan_file_type]
files.with_conan_file_type(params[:conan_file_type])
end
def by_conan_package_reference(files)
return files unless params[:conan_package_reference]
files.with_conan_package_reference(params[:conan_package_reference])
end
end
end
end
......@@ -22,7 +22,6 @@ class Packages::PackageFileFinder
files = package.package_files
files = by_file_name(files)
files = by_conan_file_type(files)
files
end
......@@ -34,10 +33,4 @@ class Packages::PackageFileFinder
files.with_file_name(file_name)
end
end
def by_conan_file_type(files)
return files unless params[:conan_file_type]
files.with_conan_file_type(params[:conan_file_type])
end
end
......@@ -30,6 +30,11 @@ class Packages::PackageFile < ApplicationRecord
.where(packages_conan_file_metadata: { conan_file_type: ::Packages::ConanFileMetadatum.conan_file_types[file_type] })
end
scope :with_conan_package_reference, ->(conan_package_reference) do
joins(:conan_file_metadatum)
.where(packages_conan_file_metadata: { conan_package_reference: conan_package_reference })
end
mount_uploader :file, Packages::PackageFileUploader
with_replicator Geo::PackageFileReplicator
......
......@@ -92,8 +92,13 @@ module API
def download_package_file(file_type)
authorize!(:read_package, project)
package_file = ::Packages::PackageFileFinder
.new(package, "#{params[:file_name]}", conan_file_type: file_type).execute!
package_file = ::Packages::Conan::PackageFileFinder
.new(
package,
params[:file_name].to_s,
conan_file_type: file_type,
conan_package_reference: params[:conan_package_reference]
).execute!
track_event('pull_package') if params[:file_name] == ::Packages::ConanFileMetadatum::PACKAGE_BINARY
......
# frozen_string_literal: true
require 'spec_helper'
describe ::Packages::Conan::PackageFileFinder do
let_it_be(:package) { create(:conan_package) }
let_it_be(:package_file) { package.package_files.first }
let(:package_file_name) { package_file.file_name }
let(:params) { {} }
RSpec.shared_examples 'package file finder examples' do
it { is_expected.to eq(package_file) }
context 'with conan_file_type' do
# conan packages contain a conanmanifest.txt file for both conan_file_types
let(:package_file_name) { 'conanmanifest.txt' }
let(:params) { { conan_file_type: :recipe_file } }
it { expect(subject.conan_file_type).to eq('recipe_file') }
end
context 'with conan_package_reference' do
let_it_be(:other_package) { create(:conan_package) }
let_it_be(:package_file_name) { 'conan_package.tgz' }
let_it_be(:package_file) { package.package_files.find_by(file_name: package_file_name) }
let(:params) do
{ conan_package_reference: package_file.conan_file_metadatum.conan_package_reference }
end
it { expect(subject).to eq(package_file) }
end
context 'with file_name_like' do
let(:package_file_name) { package_file.file_name.upcase }
let(:params) { { with_file_name_like: true } }
it { is_expected.to eq(package_file) }
end
end
describe '#execute' do
subject { described_class.new(package, package_file_name, params).execute }
it_behaves_like 'package file finder examples'
context 'with unknown file_name' do
let(:package_file_name) { 'unknown.jpg' }
it { expect(subject).to be_nil }
end
end
describe '#execute!' do
subject { described_class.new(package, package_file_name, params).execute! }
it_behaves_like 'package file finder examples'
context 'with unknown file_name' do
let(:package_file_name) { 'unknown.jpg' }
it { expect { subject }.to raise_error(ActiveRecord::RecordNotFound) }
end
end
end
......@@ -10,15 +10,6 @@ describe Packages::PackageFileFinder do
RSpec.shared_examples 'package file finder examples' do
it { is_expected.to eq(package_file) }
context 'with conan_file_type' do
let_it_be(:package) { create(:conan_package) }
# conan packages contain a conanmanifest.txt file for both conan_file_types
let(:package_file_name) { 'conanmanifest.txt' }
let(:params) { { conan_file_type: :recipe_file } }
it { expect(subject.conan_file_type).to eq('recipe_file') }
end
context 'with file_name_like' do
let(:package_file_name) { package_file.file_name.upcase }
let(:params) { { with_file_name_like: true } }
......
......@@ -40,6 +40,17 @@ RSpec.describe Packages::PackageFile, type: :model do
end
end
describe '.with_conan_package_reference' do
let_it_be(:non_matching_package_file) { create(:package_file, :nuget) }
let_it_be(:package_file) { create(:conan_package_file, :conan_package) }
let_it_be(:reference) { package_file.conan_file_metadatum.conan_package_reference}
it 'returns matching packages' do
expect(described_class.with_conan_package_reference(reference))
.to eq([package_file])
end
end
describe '#update_file_metadata callback' do
let(:package_file) { build(:package_file, :nuget, file_store: 0, size: nil) }
......
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