Commit 3de8a462 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Parse artifacts metadata stored in JSON format

parent 447f5603
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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
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