Commit c08f828f authored by Igor Drozdov's avatar Igor Drozdov

Merge branch '290288-composer-cache-prep' into 'master'

Adds version cache field to composer metadata

See merge request gitlab-org/gitlab!50906
parents 928e2216 6828a063
...@@ -20,33 +20,17 @@ module Packages ...@@ -20,33 +20,17 @@ module Packages
end end
def package_versions(packages = @packages) def package_versions(packages = @packages)
{ 'packages' => { packages.first.name => package_versions_map(packages) } } package_versions_index(packages).as_json
end end
private private
def package_versions_map(packages) def package_versions_sha(packages = @packages)
packages.each_with_object({}) do |package, map| package_versions_index(packages).sha
map[package.version] = package_metadata(package)
end
end end
def package_metadata(package) def package_versions_index(packages)
json = package.composer_metadatum.composer_json ::Gitlab::Composer::VersionIndex.new(packages)
json.merge('dist' => package_dist(package), 'uid' => package.id, 'version' => package.version)
end
def package_dist(package)
sha = package.composer_metadatum.target_sha
archive_api_path = api_v4_projects_packages_composer_archives_package_name_path({ id: package.project_id, package_name: package.name, format: '.zip' }, true)
{
'type' => 'zip',
'url' => expose_url(archive_api_path) + "?sha=#{sha}",
'reference' => sha,
'shasum' => ''
}
end end
def providers_map def providers_map
...@@ -59,10 +43,6 @@ module Packages ...@@ -59,10 +43,6 @@ module Packages
map map
end end
def package_versions_sha(packages)
Digest::SHA256.hexdigest(package_versions(packages).to_json)
end
def provider_sha def provider_sha
Digest::SHA256.hexdigest(provider.to_json) Digest::SHA256.hexdigest(provider.to_json)
end end
......
---
title: Add version cache field to composer metadata
merge_request: 50906
author:
type: other
# frozen_string_literal: true
class AddVersionShaCacheToComposerMetadata < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
def up
with_lock_retries do
add_column :packages_composer_metadata, :version_cache_sha, :binary, null: true
end
end
def down
with_lock_retries do
remove_column :packages_composer_metadata, :version_cache_sha, :binary
end
end
end
2278b1e4e19b5306e4b616eb87b622427ef2dcf73dae761739cb3106d5e64718
\ No newline at end of file
...@@ -14712,7 +14712,8 @@ ALTER SEQUENCE packages_build_infos_id_seq OWNED BY packages_build_infos.id; ...@@ -14712,7 +14712,8 @@ ALTER SEQUENCE packages_build_infos_id_seq OWNED BY packages_build_infos.id;
CREATE TABLE packages_composer_metadata ( CREATE TABLE packages_composer_metadata (
package_id bigint NOT NULL, package_id bigint NOT NULL,
target_sha bytea NOT NULL, target_sha bytea NOT NULL,
composer_json jsonb DEFAULT '{}'::jsonb NOT NULL composer_json jsonb DEFAULT '{}'::jsonb NOT NULL,
version_cache_sha bytea
); );
CREATE TABLE packages_conan_file_metadata ( CREATE TABLE packages_conan_file_metadata (
......
# frozen_string_literal: true
module Gitlab
module Composer
class VersionIndex
include API::Helpers::RelatedResourcesHelpers
def initialize(packages)
@packages = packages
end
def as_json(_options = nil)
{ 'packages' => { @packages.first.name => package_versions_map } }
end
def sha
Digest::SHA256.hexdigest(to_json)
end
private
def package_versions_map
@packages.each_with_object({}) do |package, map|
map[package.version] = package_metadata(package)
end
end
def package_metadata(package)
json = package.composer_metadatum.composer_json
json.merge('dist' => package_dist(package), 'uid' => package.id, 'version' => package.version)
end
def package_dist(package)
sha = package.composer_metadatum.target_sha
archive_api_path = api_v4_projects_packages_composer_archives_package_name_path({ id: package.project_id, package_name: package.name, format: '.zip' }, true)
{
'type' => 'zip',
'url' => expose_url(archive_api_path) + "?sha=#{sha}",
'reference' => sha,
'shasum' => ''
}
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Composer::VersionIndex do
let_it_be(:package_name) { 'sample-project' }
let_it_be(:json) { { 'name' => package_name } }
let_it_be(:group) { create(:group) }
let_it_be(:project) { create(:project, :custom_repo, files: { 'composer.json' => json.to_json }, group: group) }
let_it_be(:package1) { create(:composer_package, :with_metadatum, project: project, name: package_name, version: '1.0.0', json: json) }
let_it_be(:package2) { create(:composer_package, :with_metadatum, project: project, name: package_name, version: '2.0.0', json: json) }
let(:branch) { project.repository.find_branch('master') }
let(:packages) { [package1, package2] }
describe '#as_json' do
subject(:index) { described_class.new(packages).as_json }
def expected_json(package)
{
'dist' => {
'reference' => branch.target,
'shasum' => '',
'type' => 'zip',
'url' => "http://localhost/api/v4/projects/#{project.id}/packages/composer/archives/#{package.name}.zip?sha=#{branch.target}"
},
'name' => package.name,
'uid' => package.id,
'version' => package.version
}
end
it 'returns the packages json' do
packages = index['packages'][package_name]
expect(packages['1.0.0']).to eq(expected_json(package1))
expect(packages['2.0.0']).to eq(expected_json(package2))
end
end
describe '#sha' do
subject(:sha) { described_class.new(packages).sha }
it 'returns the json SHA' do
expect(sha).to match /^[A-Fa-f0-9]{64}$/
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