Commit 8bb83f5d authored by Thong Kuah's avatar Thong Kuah

Merge branch '321361-use-open-file' into 'master'

Use `#use_open_file` for NuGet metadata extraction [RUN ALL RSPEC] [RUN AS-IF-FOSS]

See merge request gitlab-org/gitlab!62471
parents c154e1a9 6c112a10
...@@ -28,7 +28,7 @@ module Packages ...@@ -28,7 +28,7 @@ module Packages
def execute def execute
raise ExtractionError, 'invalid package file' unless valid_package_file? raise ExtractionError, 'invalid package file' unless valid_package_file?
extract_metadata(nuspec_file) extract_metadata(nuspec_file_content)
end end
private private
...@@ -39,6 +39,10 @@ module Packages ...@@ -39,6 +39,10 @@ module Packages
end end
end end
def project
package_file.package.project
end
def valid_package_file? def valid_package_file?
package_file && package_file &&
package_file.package&.nuget? && package_file.package&.nuget? &&
...@@ -89,18 +93,39 @@ module Packages ...@@ -89,18 +93,39 @@ module Packages
tags.split(::Packages::Tag::NUGET_TAGS_SEPARATOR) tags.split(::Packages::Tag::NUGET_TAGS_SEPARATOR)
end end
def nuspec_file def nuspec_file_content
package_file.file.use_file do |file_path| with_zip_file do |zip_file|
Zip::File.open(file_path) do |zip_file| entry = zip_file.glob('*.nuspec').first
entry = zip_file.glob('*.nuspec').first
raise ExtractionError, 'nuspec file not found' unless entry raise ExtractionError, 'nuspec file not found' unless entry
raise ExtractionError, 'nuspec file too big' if entry.size > MAX_FILE_SIZE raise ExtractionError, 'nuspec file too big' if entry.size > MAX_FILE_SIZE
entry.get_input_stream.read entry.get_input_stream.read
end
end
def with_zip_file(&block)
if ::Feature.enabled?(:packages_nuget_archive_new_file_reader, project, default_enabled: :yaml)
with_new_file_reader(&block)
else
with_legacy_file_reader(&block)
end
end
def with_legacy_file_reader
package_file.file.use_file do |file_path|
Zip::File.open(file_path) do |zip_file|
yield(zip_file)
end end
end end
end end
def with_new_file_reader
package_file.file.use_open_file do |open_file|
zip_file = Zip::File.new(open_file, false, true)
yield(zip_file)
end
end
end end
end end
end end
---
name: packages_nuget_archive_new_file_reader
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/62471
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/331799
milestone: '13.13'
type: development
group: group::package
default_enabled: false
...@@ -3,7 +3,8 @@ ...@@ -3,7 +3,8 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Packages::Nuget::MetadataExtractionService do RSpec.describe Packages::Nuget::MetadataExtractionService do
let(:package_file) { create(:nuget_package).package_files.first } let_it_be(:package_file) { create(:nuget_package).package_files.first }
let(:service) { described_class.new(package_file.id) } let(:service) { described_class.new(package_file.id) }
describe '#execute' do describe '#execute' do
...@@ -23,12 +24,27 @@ RSpec.describe Packages::Nuget::MetadataExtractionService do ...@@ -23,12 +24,27 @@ RSpec.describe Packages::Nuget::MetadataExtractionService do
package_tags: [] package_tags: []
} }
it { is_expected.to eq(expected_metadata) } context 'with packages_nuget_archive_new_file_reader enabled' do
before do
expect(service).to receive(:with_new_file_reader).and_call_original
end
it { is_expected.to eq(expected_metadata) }
end
context 'with packages_nuget_archive_new_file_reader disabled' do
before do
stub_feature_flags(packages_nuget_archive_new_file_reader: false)
expect(service).to receive(:with_legacy_file_reader).and_call_original
end
it { is_expected.to eq(expected_metadata) }
end
end end
context 'with nuspec file' do context 'with nuspec file' do
before do before do
allow(service).to receive(:nuspec_file).and_return(fixture_file(nuspec_filepath)) allow(service).to receive(:nuspec_file_content).and_return(fixture_file(nuspec_filepath))
end end
context 'with dependencies' do context 'with dependencies' do
...@@ -57,7 +73,7 @@ RSpec.describe Packages::Nuget::MetadataExtractionService do ...@@ -57,7 +73,7 @@ RSpec.describe Packages::Nuget::MetadataExtractionService do
let_it_be(:nuspec_filepath) { 'packages/nuget/with_metadata.nuspec' } let_it_be(:nuspec_filepath) { 'packages/nuget/with_metadata.nuspec' }
before do before do
allow(service).to receive(:nuspec_file).and_return(fixture_file(nuspec_filepath)) allow(service).to receive(:nuspec_file_content).and_return(fixture_file(nuspec_filepath))
end end
it { expect(subject[:license_url]).to eq('https://opensource.org/licenses/MIT') } it { expect(subject[:license_url]).to eq('https://opensource.org/licenses/MIT') }
......
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