From 2475461aa28c110fbfd24aed73354865ffdb6a48 Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Thu, 7 Jan 2021 06:10:18 +0000 Subject: [PATCH] Add latest changes from gitlab-org/gitlab@master --- app/assets/javascripts/flash.js | 4 +- .../debian/extract_metadata_service.rb | 85 +++++++++++++++++++ qa/qa/runtime/feature.rb | 2 +- .../debian/extract_metadata_service_spec.rb | 59 +++++++++++++ 4 files changed, 147 insertions(+), 3 deletions(-) create mode 100644 app/services/packages/debian/extract_metadata_service.rb create mode 100644 spec/services/packages/debian/extract_metadata_service_spec.rb diff --git a/app/assets/javascripts/flash.js b/app/assets/javascripts/flash.js index 0f7b631e673..d14af53746e 100644 --- a/app/assets/javascripts/flash.js +++ b/app/assets/javascripts/flash.js @@ -69,7 +69,7 @@ const removeFlashClickListener = (flashEl, fadeTransition) => { * @param {String} message Flash message text * @param {String} type Type of Flash, it can be `notice`, `success`, `warning` or `alert` (default) * @param {Object} parent Reference to parent element under which Flash needs to appear - * @param {Object} actonConfig Map of config to show action on banner + * @param {Object} actionConfig Map of config to show action on banner * @param {String} href URL to which action config should point to (default: '#') * @param {String} title Title of action * @param {Function} clickHandler Method to call when action is clicked on @@ -119,7 +119,7 @@ const deprecatedCreateFlash = function deprecatedCreateFlash( * @param {String} options.message Flash message text * @param {String} options.type Type of Flash, it can be `notice`, `success`, `warning` or `alert` (default) * @param {Object} options.parent Reference to parent element under which Flash needs to appear - * @param {Object} options.actonConfig Map of config to show action on banner + * @param {Object} options.actionConfig Map of config to show action on banner * @param {String} href URL to which action config should point to (default: '#') * @param {String} title Title of action * @param {Function} clickHandler Method to call when action is clicked on diff --git a/app/services/packages/debian/extract_metadata_service.rb b/app/services/packages/debian/extract_metadata_service.rb new file mode 100644 index 00000000000..fd5832bc0ba --- /dev/null +++ b/app/services/packages/debian/extract_metadata_service.rb @@ -0,0 +1,85 @@ +# frozen_string_literal: true + +module Packages + module Debian + class ExtractMetadataService + include Gitlab::Utils::StrongMemoize + + ExtractionError = Class.new(StandardError) + + def initialize(package_file) + @package_file = package_file + end + + def execute + raise ExtractionError.new('invalid package file') unless valid_package_file? + + extract_metadata + end + + private + + attr_reader :package_file + + def valid_package_file? + package_file && + package_file.package&.debian? && + package_file.file.size > 0 # rubocop:disable Style/ZeroLengthPredicate + end + + def file_type_basic + %i[dsc deb udeb buildinfo changes].each do |format| + return format if package_file.file_name.end_with?(".#{format}") + end + + nil + end + + def file_type_source + # https://manpages.debian.org/buster/dpkg-dev/dpkg-source.1.en.html + %i[gzip bzip2 lzma xz].each do |format| + return :source if package_file.file_name.end_with?(".tar.#{format}") + end + + nil + end + + def file_type + strong_memoize(:file_type) do + file_type_basic || file_type_source || :unknown + end + end + + def file_type_debian? + file_type == :deb || file_type == :udeb + end + + def file_type_meta? + 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 + end + end + end + + def extract_metadata + fields = extracted_fields + architecture = fields.delete(:Architecture) if file_type_debian? + + { + file_type: file_type, + architecture: architecture, + fields: fields + } + end + end + end +end diff --git a/qa/qa/runtime/feature.rb b/qa/qa/runtime/feature.rb index a48bc216ac2..dd7f9cf898c 100644 --- a/qa/qa/runtime/feature.rb +++ b/qa/qa/runtime/feature.rb @@ -32,7 +32,7 @@ module QA def enabled?(key, **scopes) feature = JSON.parse(get_features).find { |flag| flag['name'] == key.to_s } - feature && feature['state'] == 'on' || feature['state'] == 'conditional' && scopes.present? && enabled_scope?(feature['gates'], scopes) + feature && (feature['state'] == 'on' || feature['state'] == 'conditional' && scopes.present? && enabled_scope?(feature['gates'], scopes)) end private diff --git a/spec/services/packages/debian/extract_metadata_service_spec.rb b/spec/services/packages/debian/extract_metadata_service_spec.rb new file mode 100644 index 00000000000..0aa9a67b263 --- /dev/null +++ b/spec/services/packages/debian/extract_metadata_service_spec.rb @@ -0,0 +1,59 @@ +# frozen_string_literal: true +require 'spec_helper' + +RSpec.describe Packages::Debian::ExtractMetadataService do + let(:service) { described_class.new(package_file) } + + subject { service.execute } + + RSpec.shared_context 'Debian ExtractMetadata Service' do |trait| + let(:package_file) { create(:debian_package_file, trait) } + end + + RSpec.shared_examples 'Test Debian ExtractMetadata Service' do |expected_file_type, expected_architecture, expected_fields| + it "returns file_type #{expected_file_type.inspect}" do + expect(subject[:file_type]).to eq(expected_file_type) + end + + it "returns architecture #{expected_architecture.inspect}" do + expect(subject[:architecture]).to eq(expected_architecture) + end + + it "returns fields #{expected_fields.nil? ? '' : 'including '}#{expected_fields.inspect}" do + if expected_fields.nil? + expect(subject[:fields]).to be_nil + else + expect(subject[:fields]).to include(**expected_fields) + end + end + end + + using RSpec::Parameterized::TableSyntax + + where(:case_name, :trait, :expected_file_type, :expected_architecture, :expected_fields) do + 'with invalid' | :invalid | :unknown | nil | nil + 'with source' | :source | :source | nil | nil + 'with dsc' | :dsc | :dsc | nil | { 'Binary': 'sample-dev, libsample0, sample-udeb' } + 'with deb' | :deb | :deb | 'amd64' | { 'Multi-Arch': 'same' } + 'with udeb' | :udeb | :udeb | 'amd64' | { 'Package': 'sample-udeb' } + 'with buildinfo' | :buildinfo | :buildinfo | nil | { 'Architecture': 'amd64 source', 'Build-Architecture': 'amd64' } + 'with changes' | :changes | :changes | nil | { 'Architecture': 'source amd64', 'Binary': 'libsample0 sample-dev sample-udeb' } + end + + with_them do + include_context 'Debian ExtractMetadata Service', params[:trait] do + it_behaves_like 'Test Debian ExtractMetadata Service', + params[:expected_file_type], + params[:expected_architecture], + params[:expected_fields] + end + end + + context 'with invalid package file' do + let(:package_file) { create(:conan_package_file) } + + it 'raise error' do + expect { subject }.to raise_error(described_class::ExtractionError, 'invalid package file') + end + end +end