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 ...@@ -18,8 +18,8 @@ class Projects::ArtifactsController < Projects::ApplicationController
return render_404 unless build.artifacts? return render_404 unless build.artifacts?
current_path = params[:path] ? "./#{params[:path]}/" : './' current_path = params[:path] ? "./#{params[:path]}/" : './'
metadata = build.artifacts_metadata_for_path(current_path) paths, metadata = build.artifacts_metadata_for_path(current_path)
@path = Gitlab::StringPath.new(current_path, metadata) @path = Gitlab::StringPath.new(current_path, paths, metadata)
end end
private private
......
...@@ -350,22 +350,23 @@ module Ci ...@@ -350,22 +350,23 @@ module Ci
end end
def artifacts_metadata_for_path(path) def artifacts_metadata_for_path(path)
return {} unless artifacts_metadata.exists? return [] unless artifacts_metadata.exists?
metadata = [] paths, metadata = [], []
meta_path = path.sub(/^\.\//, '') meta_path = path.sub(/^\.\//, '')
File.open(artifacts_metadata.path) do |file| File.open(artifacts_metadata.path) do |file|
gzip = Zlib::GzipReader.new(file) gzip = Zlib::GzipReader.new(file)
gzip.each_line do |line| gzip.each_line do |line|
if line =~ %r{^#{meta_path}[^/]+/?\s} if line =~ %r{^#{meta_path}[^/]+/?\s}
path, meta = line.split(' ') path, meta = line.split(' ')
metadata << path paths << path
metadata << JSON.parse(meta)
end end
end end
gzip.close gzip.close
end end
metadata [paths, metadata]
end end
private private
......
...@@ -10,10 +10,11 @@ module Gitlab ...@@ -10,10 +10,11 @@ module Gitlab
class StringPath class StringPath
attr_reader :path, :universe attr_reader :path, :universe
def initialize(path, universe) def initialize(path, universe, metadata = [])
@path = prepare(path) @path = prepare(path)
@universe = Set.new(universe.map { |entry| prepare(entry) }) @universe = universe.map { |entry| prepare(entry) }
@universe.add('./') @universe << './' unless @universe.include?('./')
@metadata = metadata
end end
def to_s def to_s
...@@ -84,6 +85,11 @@ module Gitlab ...@@ -84,6 +85,11 @@ module Gitlab
children.select(&:file?) children.select(&:file?)
end end
def metadata
index = @universe.index(@path)
@metadata[index]
end
def ==(other) def ==(other)
@path == other.path && @universe == other.universe @path == other.path && @universe == other.universe
end end
......
...@@ -140,4 +140,20 @@ describe Gitlab::StringPath do ...@@ -140,4 +140,20 @@ describe Gitlab::StringPath do
it { expect(subject.count).to eq 3 } it { expect(subject.count).to eq 3 }
end end
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 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