Skip to content
Snippets Groups Projects
Commit 3de8a462 authored by Grzegorz Bizon's avatar Grzegorz Bizon
Browse files

Parse artifacts metadata stored in JSON format

parent 447f5603
No related branches found
No related tags found
No related merge requests found
Loading
@@ -18,8 +18,8 @@ class Projects::ArtifactsController < Projects::ApplicationController
Loading
@@ -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
Loading
Loading
Loading
@@ -350,22 +350,23 @@ module Ci
Loading
@@ -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
Loading
Loading
Loading
@@ -10,10 +10,11 @@ module Gitlab
Loading
@@ -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
Loading
@@ -84,6 +85,11 @@ module Gitlab
Loading
@@ -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
Loading
Loading
Loading
@@ -140,4 +140,20 @@ describe Gitlab::StringPath do
Loading
@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment