summaryrefslogtreecommitdiff
path: root/app/models/container_repository.rb
blob: 149d65ddbff452c2349f55b02034638a51137e8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class ContainerRepository < ActiveRecord::Base
  belongs_to :project

  validates :name, length: { minimum: 0, allow_nil: false }

  delegate :client, to: :registry
  before_destroy :delete_tags

  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

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

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

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

  def tags
    return @tags if defined?(@tags)
    return [] unless manifest && manifest['tags']

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

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

  # TODO, add bang to this method
  #
  def delete_tags
    return unless tags

    digests = tags.map {|tag| tag.digest }.to_set
    digests.all? do |digest|
      client.delete_repository_tag(self.path, digest)
    end
  end

  # TODO, specs needed
  #
  def empty?
    tags.none?
  end

  # TODO, we will return a new ContainerRepository object here
  #
  def self.project_from_path(repository_path)
    ContainerRegistry::Path.new(repository_path)
      .repository_project
  end
end