diff --git a/app/controllers/projects/artifacts_controller.rb b/app/controllers/projects/artifacts_controller.rb index 3a112587f720ef4c6e6d89b589981c91397b781e..5bd0c8cd78078ac63f88930a14d96382cc93e2d4 100644 --- a/app/controllers/projects/artifacts_controller.rb +++ b/app/controllers/projects/artifacts_controller.rb @@ -18,8 +18,8 @@ class Projects::ArtifactsController < Projects::ApplicationController return render_404 unless build.artifacts? current_path = params[:path] ? "./#{params[:path]}/" : './' - metadata = build.artifacts_metadata_for_path(current_path) - @path = Gitlab::StringPath.new(current_path, metadata) + paths, metadata = build.artifacts_metadata_for_path(current_path) + @path = Gitlab::StringPath.new(current_path, paths, metadata) end private diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb index 2c389bbdf613e466b62e6606b85a7e3a770148a0..f6783e21d90de36a9df7f84a9848290cce09bb08 100644 --- a/app/models/ci/build.rb +++ b/app/models/ci/build.rb @@ -350,22 +350,23 @@ module Ci end def artifacts_metadata_for_path(path) - return {} unless artifacts_metadata.exists? - metadata = [] + return [] unless artifacts_metadata.exists? + paths, metadata = [], [] meta_path = path.sub(/^\.\//, '') File.open(artifacts_metadata.path) do |file| gzip = Zlib::GzipReader.new(file) gzip.each_line do |line| if line =~ %r{^#{meta_path}[^/]+/?\s} - path, meta = line.split(' ') - metadata << path + path, meta = line.split(' ') + paths << path + metadata << JSON.parse(meta) end end gzip.close end - metadata + [paths, metadata] end private diff --git a/lib/gitlab/string_path.rb b/lib/gitlab/string_path.rb index bfeb0f852f02bd831855e0dafa9308f536f6825f..ad68a8ff2d60a3756ea80e2f9d3282240fee2a9a 100644 --- a/lib/gitlab/string_path.rb +++ b/lib/gitlab/string_path.rb @@ -10,10 +10,11 @@ module Gitlab class StringPath attr_reader :path, :universe - def initialize(path, universe) + def initialize(path, universe, metadata = []) @path = prepare(path) - @universe = Set.new(universe.map { |entry| prepare(entry) }) - @universe.add('./') + @universe = universe.map { |entry| prepare(entry) } + @universe << './' unless @universe.include?('./') + @metadata = metadata end def to_s @@ -84,6 +85,11 @@ module Gitlab children.select(&:file?) end + def metadata + index = @universe.index(@path) + @metadata[index] + end + def ==(other) @path == other.path && @universe == other.universe end diff --git a/spec/lib/gitlab/string_path_spec.rb b/spec/lib/gitlab/string_path_spec.rb index 8290aab7701b0160ab84e1d2fc7d2fe73e79f640..af46f9754ac3aef0cde84e19fbde42da33684291 100644 --- a/spec/lib/gitlab/string_path_spec.rb +++ b/spec/lib/gitlab/string_path_spec.rb @@ -140,4 +140,20 @@ describe Gitlab::StringPath do it { expect(subject.count).to eq 3 } end end + + describe '#metadata' do + let(:universe) do + ['path/', 'path/file1', 'path/file2'] + end + + let(:metadata) do + [{ name: '/path/'}, { name: '/path/file1' }, { name: '/path/file2' }] + end + + subject do + described_class.new('path/file1', universe, metadata).metadata[:name] + end + + it { is_expected.to eq '/path/file1' } + end end