Class: Packages::Go::ModuleVersion

Inherits:
Object
  • Object
show all
Includes:
Gitlab::Golang, Gitlab::Utils::StrongMemoize
Defined in:
app/models/packages/go/module_version.rb

Constant Summary collapse

VALID_TYPES =
%i[ref commit pseudo].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Golang

#go_path, #local_module_prefix, #package_url, #parse_pseudo_version, #parse_semver, #pkg_go_dev_url, #pseudo_version?, #semver_tag?, #validate_pseudo_version

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Constructor Details

#initialize(mod, type, commit, name: nil, semver: nil, ref: nil) ⇒ ModuleVersion

Returns a new instance of ModuleVersion.

Raises:

  • (ArgumentError)

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/models/packages/go/module_version.rb', line 19

def initialize(mod, type, commit, name: nil, semver: nil, ref: nil)
  raise ArgumentError, "invalid type '#{type}'" unless VALID_TYPES.include? type
  raise ArgumentError, "mod is required" unless mod
  raise ArgumentError, "commit is required" unless commit

  if type == :ref
    raise ArgumentError, "ref is required" unless ref
  elsif type == :pseudo
    raise ArgumentError, "name is required" unless name
    raise ArgumentError, "semver is required" unless semver
  end

  @mod = mod
  @type = type
  @commit = commit
  @name = name if name
  @semver = semver if semver
  @ref = ref if ref
end

Instance Attribute Details

#commitObject (readonly)

Returns the value of attribute commit.


11
12
13
# File 'app/models/packages/go/module_version.rb', line 11

def commit
  @commit
end

#modObject (readonly)

Returns the value of attribute mod.


11
12
13
# File 'app/models/packages/go/module_version.rb', line 11

def mod
  @mod
end

#refObject (readonly)

Returns the value of attribute ref.


11
12
13
# File 'app/models/packages/go/module_version.rb', line 11

def ref
  @ref
end

#typeObject (readonly)

Returns the value of attribute type.


11
12
13
# File 'app/models/packages/go/module_version.rb', line 11

def type
  @type
end

Instance Method Details

#archiveObject


59
60
61
62
63
64
65
66
67
68
# File 'app/models/packages/go/module_version.rb', line 59

def archive
  suffix_len = @mod.path == '' ? 0 : @mod.path.length + 1

  Zip::OutputStream.write_buffer do |zip|
    files.each do |file|
      zip.put_next_entry "#{full_name}/#{file[suffix_len...]}"
      zip.write blob_at(file)
    end
  end
end

#excludedObject


76
77
78
79
80
81
82
# File 'app/models/packages/go/module_version.rb', line 76

def excluded
  strong_memoize(:excluded) do
    ls_tree
      .filter { |f| f.end_with?('/go.mod') && f != @mod.path + '/go.mod' }
      .map    { |f| f[0..-7] }
  end
end

#filesObject


70
71
72
73
74
# File 'app/models/packages/go/module_version.rb', line 70

def files
  strong_memoize(:files) do
    ls_tree.filter { |e| !excluded.any? { |n| e.start_with? n } }
  end
end

#full_nameObject


43
44
45
# File 'app/models/packages/go/module_version.rb', line 43

def full_name
  "#{mod.name}@#{name || commit.sha}"
end

#gomodObject


47
48
49
50
51
52
53
54
55
56
57
# File 'app/models/packages/go/module_version.rb', line 47

def gomod
  strong_memoize(:gomod) do
    if strong_memoized?(:blobs)
      blob_at(@mod.path + '/go.mod')
    elsif @mod.path.empty?
      @mod.project.repository.blob_at(@commit.sha, 'go.mod')&.data
    else
      @mod.project.repository.blob_at(@commit.sha, @mod.path + '/go.mod')&.data
    end
  end
end

#nameObject


39
40
41
# File 'app/models/packages/go/module_version.rb', line 39

def name
  @name || @ref&.name
end

#valid?Boolean

Returns:

  • (Boolean)

84
85
86
87
88
89
# File 'app/models/packages/go/module_version.rb', line 84

def valid?
  # assume the module version is valid if a corresponding Package exists
  return true if ::Packages::Go::PackageFinder.new(mod.project, mod.name, name).exists?

  @mod.path_valid?(major) && @mod.gomod_valid?(gomod)
end