Commit ad2b0358 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Improve readability of artifacts `Metadata` related code

parent 6b0a43af
...@@ -603,15 +603,14 @@ Rails.application.routes.draw do ...@@ -603,15 +603,14 @@ Rails.application.routes.draw do
member do member do
get :status get :status
get :download
post :cancel post :cancel
post :retry post :retry
end end
resource :artifacts, only: [] do resource :artifacts, only: [] do
get :download get :download
get 'browse(/*path)', action: :browse, as: :browse, format: false get :browse, path: 'browse(/*path)', action: :browse, format: false
get 'file/*path', action: :file, as: :file, format: false get :file, path: 'file/*path', action: :file, format: false
end end
end end
......
...@@ -6,7 +6,9 @@ module Gitlab ...@@ -6,7 +6,9 @@ module Gitlab
module Build module Build
module Artifacts module Artifacts
class Metadata class Metadata
VERSION_PATTERN = '[\w\s]+(\d+\.\d+\.\d+)' VERSION_PATTERN = /^[\w\s]+(\d+\.\d+\.\d+)/
INVALID_PATH_PATTERN = %r{(^\.?\.?/)|(/\.?\.?/)}
attr_reader :file, :path, :full_version attr_reader :file, :path, :full_version
def initialize(file, path) def initialize(file, path)
...@@ -15,7 +17,7 @@ module Gitlab ...@@ -15,7 +17,7 @@ module Gitlab
end end
def version def version
@full_version.match(/#{VERSION_PATTERN}/).captures.first @full_version.match(VERSION_PATTERN)[1]
end end
def errors def errors
...@@ -27,7 +29,7 @@ module Gitlab ...@@ -27,7 +29,7 @@ module Gitlab
end end
end end
def match! def find_entries!
gzip do |gz| gzip do |gz|
2.times { read_string(gz) } # version and errors fields 2.times { read_string(gz) } # version and errors fields
match_entries(gz) match_entries(gz)
...@@ -35,8 +37,8 @@ module Gitlab ...@@ -35,8 +37,8 @@ module Gitlab
end end
def to_entry def to_entry
entires, metadata = match! entries, metadata = find_entries!
Entry.new(@path, entires, metadata) Entry.new(@path, entries, metadata)
end end
private private
...@@ -44,7 +46,6 @@ module Gitlab ...@@ -44,7 +46,6 @@ module Gitlab
def match_entries(gz) def match_entries(gz)
paths, metadata = [], [] paths, metadata = [], []
match_pattern = %r{^#{Regexp.escape(@path)}[^/]*/?$} match_pattern = %r{^#{Regexp.escape(@path)}[^/]*/?$}
invalid_pattern = %r{(^\.?\.?/)|(/\.?\.?/)}
until gz.eof? do until gz.eof? do
begin begin
...@@ -53,7 +54,7 @@ module Gitlab ...@@ -53,7 +54,7 @@ module Gitlab
next unless path.valid_encoding? && meta.valid_encoding? next unless path.valid_encoding? && meta.valid_encoding?
next unless path =~ match_pattern next unless path =~ match_pattern
next if path =~ invalid_pattern next if path =~ INVALID_PATH_PATTERN
paths.push(path) paths.push(path)
metadata.push(JSON.parse(meta, symbolize_names: true)) metadata.push(JSON.parse(meta, symbolize_names: true))
...@@ -73,7 +74,7 @@ module Gitlab ...@@ -73,7 +74,7 @@ module Gitlab
raise StandardError, 'Artifacts metadata file empty!' raise StandardError, 'Artifacts metadata file empty!'
end end
unless version_string =~ /^#{VERSION_PATTERN}/ unless version_string =~ VERSION_PATTERN
raise StandardError, 'Invalid version!' raise StandardError, 'Invalid version!'
end end
...@@ -92,19 +93,8 @@ module Gitlab ...@@ -92,19 +93,8 @@ module Gitlab
gz.read(string_size) gz.read(string_size)
end end
def gzip def gzip(&block)
open do |file| Zlib::GzipReader.open(@file, &block)
gzip = Zlib::GzipReader.new(file)
begin
yield gzip
ensure
gzip.close
end
end
end
def open
File.open(@file) { |file| yield file }
end end
end end
end end
......
...@@ -11,12 +11,12 @@ module Gitlab ...@@ -11,12 +11,12 @@ module Gitlab
# This class is working only with UTF-8 encoded paths. # This class is working only with UTF-8 encoded paths.
# #
class Entry class Entry
attr_reader :path, :entires attr_reader :path, :entries
attr_accessor :name attr_accessor :name
def initialize(path, entires, metadata = []) def initialize(path, entries, metadata = [])
@path = path.force_encoding('UTF-8') @path = path.force_encoding('UTF-8')
@entires = entires @entries = entries
@metadata = metadata @metadata = metadata
if path.include?("\0") if path.include?("\0")
...@@ -42,7 +42,7 @@ module Gitlab ...@@ -42,7 +42,7 @@ module Gitlab
def parent def parent
return nil unless has_parent? return nil unless has_parent?
new(@path.chomp(basename)) new_entry(@path.chomp(basename))
end end
def basename def basename
...@@ -58,7 +58,7 @@ module Gitlab ...@@ -58,7 +58,7 @@ module Gitlab
return @children if @children return @children if @children
child_pattern = %r{^#{Regexp.escape(@path)}[^/]+/?$} child_pattern = %r{^#{Regexp.escape(@path)}[^/]+/?$}
@children = select_entires { |entry| entry =~ child_pattern } @children = select_entries { |entry| entry =~ child_pattern }
end end
def directories(opts = {}) def directories(opts = {})
...@@ -77,7 +77,7 @@ module Gitlab ...@@ -77,7 +77,7 @@ module Gitlab
end end
def metadata def metadata
@index ||= @entires.index(@path) @index ||= @entries.index(@path)
@metadata[@index] || {} @metadata[@index] || {}
end end
...@@ -90,7 +90,7 @@ module Gitlab ...@@ -90,7 +90,7 @@ module Gitlab
end end
def exists? def exists?
blank_node? || @entires.include?(@path) blank_node? || @entries.include?(@path)
end end
def empty? def empty?
...@@ -102,7 +102,7 @@ module Gitlab ...@@ -102,7 +102,7 @@ module Gitlab
end end
def ==(other) def ==(other)
@path == other.path && @entires == other.entires @path == other.path && @entries == other.entries
end end
def inspect def inspect
...@@ -111,13 +111,13 @@ module Gitlab ...@@ -111,13 +111,13 @@ module Gitlab
private private
def new(path) def new_entry(path)
self.class.new(path, @entires, @metadata) self.class.new(path, @entries, @metadata)
end end
def select_entires def select_entries
selected = @entires.select { |entry| yield entry } selected = @entries.select { |entry| yield entry }
selected.map { |path| new(path) } selected.map { |path| new_entry(path) }
end end
end end
end end
......
...@@ -10,8 +10,8 @@ describe Gitlab::Ci::Build::Artifacts::Metadata do ...@@ -10,8 +10,8 @@ describe Gitlab::Ci::Build::Artifacts::Metadata do
end end
context 'metadata file exists' do context 'metadata file exists' do
describe '#match! empty string' do describe '#find_entries! empty string' do
subject { metadata('').match! } subject { metadata('').find_entries! }
it 'matches correct paths' do it 'matches correct paths' do
expect(subject.first).to contain_exactly 'ci_artifacts.txt', expect(subject.first).to contain_exactly 'ci_artifacts.txt',
...@@ -29,8 +29,8 @@ describe Gitlab::Ci::Build::Artifacts::Metadata do ...@@ -29,8 +29,8 @@ describe Gitlab::Ci::Build::Artifacts::Metadata do
end end
end end
describe '#match! other_artifacts_0.1.2/' do describe '#find_entries! other_artifacts_0.1.2/' do
subject { metadata('other_artifacts_0.1.2/').match! } subject { metadata('other_artifacts_0.1.2/').find_entries! }
it 'matches correct paths' do it 'matches correct paths' do
expect(subject.first). expect(subject.first).
...@@ -40,8 +40,8 @@ describe Gitlab::Ci::Build::Artifacts::Metadata do ...@@ -40,8 +40,8 @@ describe Gitlab::Ci::Build::Artifacts::Metadata do
end end
end end
describe '#match! other_artifacts_0.1.2/another-subdirectory/' do describe '#find_entries! other_artifacts_0.1.2/another-subdirectory/' do
subject { metadata('other_artifacts_0.1.2/another-subdirectory/').match! } subject { metadata('other_artifacts_0.1.2/another-subdirectory/').find_entries! }
it 'matches correct paths' do it 'matches correct paths' do
expect(subject.first). expect(subject.first).
...@@ -75,9 +75,9 @@ describe Gitlab::Ci::Build::Artifacts::Metadata do ...@@ -75,9 +75,9 @@ describe Gitlab::Ci::Build::Artifacts::Metadata do
context 'metadata file does not exist' do context 'metadata file does not exist' do
let(:metadata_file_path) { '' } let(:metadata_file_path) { '' }
describe '#match!' do describe '#find_entries!' do
it 'raises error' do it 'raises error' do
expect { metadata.match! }.to raise_error(Errno::ENOENT) expect { metadata.find_entries! }.to raise_error(Errno::ENOENT)
end end
end 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