Class: Gitlab::Ci::Build::Artifacts::Metadata

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/ci/build/artifacts/metadata.rb,
lib/gitlab/ci/build/artifacts/metadata/entry.rb

Defined Under Namespace

Classes: Entry

Constant Summary collapse

ParserError =
Class.new(StandardError)
InvalidStreamError =
Class.new(StandardError)
VERSION_PATTERN =
/^[\w\s]+(\d+\.\d+\.\d+)/
INVALID_PATH_PATTERN =
%r{(^\.?\.?/)|(/\.?\.?/)}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream, path, **opts) ⇒ Metadata

Returns a new instance of Metadata.



19
20
21
22
23
24
25
26
27
28
# File 'lib/gitlab/ci/build/artifacts/metadata.rb', line 19

def initialize(stream, path, **opts)
  @stream = stream

  # Ensure to remove any ./ prefix from the path
  # so that the pattern matching will work as expected
  @path = path.gsub(%r{^\./}, '')

  @opts = opts
  @full_version = read_version
end

Instance Attribute Details

#full_versionObject (readonly)

Returns the value of attribute full_version.



17
18
19
# File 'lib/gitlab/ci/build/artifacts/metadata.rb', line 17

def full_version
  @full_version
end

#pathObject (readonly)

Returns the value of attribute path.



17
18
19
# File 'lib/gitlab/ci/build/artifacts/metadata.rb', line 17

def path
  @path
end

#streamObject (readonly)

Returns the value of attribute stream.



17
18
19
# File 'lib/gitlab/ci/build/artifacts/metadata.rb', line 17

def stream
  @stream
end

Instance Method Details

#errorsObject



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gitlab/ci/build/artifacts/metadata.rb', line 34

def errors
  gzip do |gz|
    read_string(gz) # version
    errors = read_string(gz)
    raise ParserError, 'Errors field not found!' unless errors

    begin
      Gitlab::Json.parse(errors)
    rescue JSON::ParserError
      raise ParserError, 'Invalid errors field!'
    end
  end
end

#find_entries!Object



48
49
50
51
52
53
# File 'lib/gitlab/ci/build/artifacts/metadata.rb', line 48

def find_entries!
  gzip do |gz|
    2.times { read_string(gz) } # version and errors fields
    match_entries(gz)
  end
end

#to_entryObject



55
56
57
58
# File 'lib/gitlab/ci/build/artifacts/metadata.rb', line 55

def to_entry
  entries = find_entries!
  Entry.new(@path, entries)
end

#versionObject



30
31
32
# File 'lib/gitlab/ci/build/artifacts/metadata.rb', line 30

def version
  @full_version.match(VERSION_PATTERN)[1]
end