Class: ContainerRepository

Inherits:
ApplicationRecord show all
Includes:
Gitlab::SQL::Pattern, Gitlab::Utils::StrongMemoize
Defined in:
app/models/container_repository.rb

Constant Summary

Constants included from Gitlab::SQL::Pattern

Gitlab::SQL::Pattern::MIN_CHARS_FOR_PARTIAL_MATCHING, Gitlab::SQL::Pattern::REGEX_QUOTED_WORD

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Methods inherited from ApplicationRecord

at_most, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, underscore, without_order

Class Method Details

.build_from_path(path) ⇒ Object


110
111
112
113
# File 'app/models/container_repository.rb', line 110

def self.build_from_path(path)
  self.new(project: path.repository_project,
           name: path.repository_name)
end

.build_root_repository(project) ⇒ Object


119
120
121
# File 'app/models/container_repository.rb', line 119

def self.build_root_repository(project)
  self.new(project: project, name: '')
end

.create_from_path!(path) ⇒ Object


115
116
117
# File 'app/models/container_repository.rb', line 115

def self.create_from_path!(path)
  build_from_path(path).tap(&:save!)
end

.exists_by_path?(path) ⇒ Boolean

Returns:

  • (Boolean)

29
30
31
32
33
34
# File 'app/models/container_repository.rb', line 29

def self.exists_by_path?(path)
  where(
    project: path.repository_project,
    name: path.repository_name
  ).exists?
end

.find_by_path!(path) ⇒ Object


123
124
125
126
# File 'app/models/container_repository.rb', line 123

def self.find_by_path!(path)
  self.find_by!(project: path.repository_project,
                name: path.repository_name)
end

Instance Method Details

#blob(config) ⇒ Object


82
83
84
# File 'app/models/container_repository.rb', line 82

def blob(config)
  ContainerRegistry::Blob.new(self, config)
end

#delete_tag_by_digest(digest) ⇒ Object


102
103
104
# File 'app/models/container_repository.rb', line 102

def delete_tag_by_digest(digest)
  client.delete_repository_tag_by_digest(self.path, digest)
end

#delete_tag_by_name(name) ⇒ Object


106
107
108
# File 'app/models/container_repository.rb', line 106

def delete_tag_by_name(name)
  client.delete_repository_tag_by_name(self.path, name)
end

#delete_tags!Object


94
95
96
97
98
99
100
# File 'app/models/container_repository.rb', line 94

def delete_tags!
  return unless has_tags?

  digests = tags.map { |tag| tag.digest }.compact.to_set

  digests.map(&method(:delete_tag_by_digest)).all?
end

#has_tags?Boolean

Returns:

  • (Boolean)

86
87
88
# File 'app/models/container_repository.rb', line 86

def has_tags?
  tags.any?
end

#locationObject


54
55
56
# File 'app/models/container_repository.rb', line 54

def location
  File.join(registry.path, path)
end

#manifestObject


62
63
64
# File 'app/models/container_repository.rb', line 62

def manifest
  @manifest ||= client.repository_tags(path)
end

#pathObject

rubocop: enable CodeReuse/ServiceClass


49
50
51
52
# File 'app/models/container_repository.rb', line 49

def path
  @path ||= [project.full_path, name]
    .select(&:present?).join('/').downcase
end

#registryObject

rubocop: disable CodeReuse/ServiceClass


37
38
39
40
41
42
43
44
45
46
# File 'app/models/container_repository.rb', line 37

def registry
  @registry ||= begin
    token = Auth::ContainerRegistryAuthenticationService.full_access_token(path)

    url = Gitlab.config.registry.api_url
    host_port = Gitlab.config.registry.host_port

    ContainerRegistry::Registry.new(url, token: token, path: host_port)
  end
end

#root_repository?Boolean

Returns:

  • (Boolean)

90
91
92
# File 'app/models/container_repository.rb', line 90

def root_repository?
  name.empty?
end

#tag(tag) ⇒ Object


58
59
60
# File 'app/models/container_repository.rb', line 58

def tag(tag)
  ContainerRegistry::Tag.new(self, tag)
end

#tagsObject


66
67
68
69
70
71
72
73
74
# File 'app/models/container_repository.rb', line 66

def tags
  return [] unless manifest && manifest['tags']

  strong_memoize(:tags) do
    manifest['tags'].sort.map do |tag|
      ContainerRegistry::Tag.new(self, tag)
    end
  end
end

#tags_countObject


76
77
78
79
80
# File 'app/models/container_repository.rb', line 76

def tags_count
  return 0 unless manifest && manifest['tags']

  manifest['tags'].size
end