Commit 48b17dba authored by David Fernandez's avatar David Fernandez

Merge branch 'helm_get_last_chart' into 'master'

Use the last Helm chart when downloading

See merge request gitlab-org/gitlab!68968
parents 854c64da 9dfd15f6
......@@ -6,6 +6,8 @@ module Packages
DEFAULT_PACKAGE_FILES_COUNT = 20
MAX_PACKAGE_FILES_COUNT = 1000
delegate :most_recent!, to: :execute
def initialize(project, channel, params = {})
@project = project
@channel = channel
......
......@@ -77,6 +77,10 @@ class Packages::PackageFile < ApplicationRecord
.where(packages_conan_file_metadata: { conan_package_reference: conan_package_reference })
end
def self.most_recent!
recent.first!
end
mount_file_store_uploader Packages::PackageFileUploader
update_project_statistics project_statistics_name: :packages_size
......
......@@ -66,7 +66,7 @@ module API
get ":channel/charts/:file_name.tgz", requirements: FILE_NAME_REQUIREMENTS do
authorize_read_package!(authorized_user_project)
package_file = Packages::Helm::PackageFilesFinder.new(authorized_user_project, params[:channel], file_name: "#{params[:file_name]}.tgz").execute.last!
package_file = Packages::Helm::PackageFilesFinder.new(authorized_user_project, params[:channel], file_name: "#{params[:file_name]}.tgz").most_recent!
track_package_event('pull_package', :helm, project: authorized_user_project, namespace: authorized_user_project.namespace)
......
......@@ -6,42 +6,51 @@ RSpec.describe ::Packages::Helm::PackageFilesFinder do
let_it_be(:project1) { create(:project) }
let_it_be(:project2) { create(:project) }
let_it_be(:helm_package) { create(:helm_package, project: project1) }
let_it_be(:helm_package_file) { helm_package.package_files.first }
let_it_be(:helm_package_file1) { helm_package.package_files.first }
let_it_be(:helm_package_file2) { create(:helm_package_file, package: helm_package) }
let_it_be(:debian_package) { create(:debian_package, project: project1) }
describe '#execute' do
let(:project) { project1 }
let(:channel) { 'stable' }
let(:params) { {} }
let(:project) { project1 }
let(:channel) { 'stable' }
let(:params) { {} }
let(:service) { described_class.new(project, channel, params) }
subject { described_class.new(project, channel, params).execute }
describe '#execute' do
subject { service.execute }
context 'with empty params' do
it { is_expected.to match_array([helm_package_file]) }
it { is_expected.to eq([helm_package_file2, helm_package_file1]) }
end
context 'with another project' do
let(:project) { project2 }
it { is_expected.to match_array([]) }
it { is_expected.to eq([]) }
end
context 'with another channel' do
let(:channel) { 'staging' }
it { is_expected.to match_array([]) }
it { is_expected.to eq([]) }
end
context 'with file_name' do
let(:params) { { file_name: helm_package_file.file_name } }
context 'with matching file_name' do
let(:params) { { file_name: helm_package_file1.file_name } }
it { is_expected.to match_array([helm_package_file]) }
it { is_expected.to eq([helm_package_file2, helm_package_file1]) }
end
context 'with another file_name' do
let(:params) { { file_name: 'foobar.tgz' } }
it { is_expected.to match_array([]) }
it { is_expected.to eq([]) }
end
end
describe '#most_recent!' do
subject { service.most_recent! }
it { is_expected.to eq(helm_package_file2) }
end
end
......@@ -139,6 +139,10 @@ RSpec.describe Packages::PackageFile, type: :model do
end
end
describe '.most_recent!' do
it { expect(described_class.most_recent!).to eq(debian_package.package_files.last) }
end
describe '#update_file_store callback' do
let_it_be(:package_file) { build(:package_file, :nuget, size: nil) }
......
......@@ -9,16 +9,18 @@ RSpec.describe API::HelmPackages do
let_it_be_with_reload(:project) { create(:project, :public) }
let_it_be(:deploy_token) { create(:deploy_token, read_package_registry: true, write_package_registry: true) }
let_it_be(:project_deploy_token) { create(:project_deploy_token, deploy_token: deploy_token, project: project) }
let_it_be(:package) { create(:helm_package, project: project) }
let_it_be(:package) { create(:helm_package, project: project, without_package_files: true) }
let_it_be(:package_file1) { create(:helm_package_file, package: package) }
let_it_be(:package_file2) { create(:helm_package_file, package: package) }
describe 'GET /api/v4/projects/:id/packages/helm/:channel/index.yaml' do
it_behaves_like 'handling helm chart index requests' do
let(:url) { "/projects/#{project.id}/packages/helm/#{package.package_files.first.helm_channel}/index.yaml" }
let(:url) { "/projects/#{project.id}/packages/helm/stable/index.yaml" }
end
end
describe 'GET /api/v4/projects/:id/packages/helm/:channel/charts/:file_name.tgz' do
let(:url) { "/projects/#{project.id}/packages/helm/#{package.package_files.first.helm_channel}/charts/#{package.name}-#{package.version}.tgz" }
let(:url) { "/projects/#{project.id}/packages/helm/stable/charts/#{package.name}-#{package.version}.tgz" }
subject { get api(url), headers: headers }
......
......@@ -41,7 +41,7 @@ RSpec.shared_examples 'process helm service index request' do |user_type, status
package_entry = yaml_response['entries'][package.name]
expect(package_entry.length).to eq(1)
expect(package_entry.length).to eq(2)
expect(package_entry.first.keys).to contain_exactly('name', 'version', 'apiVersion', 'created', 'digest', 'urls')
expect(package_entry.first['digest']).to eq('fd2b2fa0329e80a2a602c2bb3b40608bcd6ee5cf96cf46fd0d2800a4c129c9db')
expect(package_entry.first['urls']).to eq(["charts/#{package.name}-#{package.version}.tgz"])
......@@ -174,6 +174,13 @@ RSpec.shared_examples 'process helm download content request' do |user_type, sta
context "for user type #{user_type}" do
before do
project.send("add_#{user_type}", user) if user_type != :anonymous && user_type != :not_a_member
expect_next_found_instance_of(::Packages::PackageFile) do |package_file|
expect(package_file).to receive(:file).and_wrap_original do |m, *args|
expect(package_file.id).to eq(package_file2.id)
m.call(*args)
end
end
end
it_behaves_like 'a package tracking event', 'API::HelmPackages', 'pull_package'
......
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