summaryrefslogtreecommitdiff
path: root/lib/container_registry/path.rb
blob: d3f8bf74e141b4d462fcc432731f4db53eb68abd (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
module ContainerRegistry
  ##
  # Class reponsible for extracting project and repository name from
  # image repository path provided by a containers registry API response.
  #
  # Example:
  #
  # some/group/my_project/my/image ->
  #   project: some/group/my_project
  #   repository: my/image
  #
  class Path
    InvalidRegistryPathError = Class.new(StandardError)

    def initialize(path)
      @path = path
      @nodes = path.to_s.split('/')
    end

    def to_s
      @path
    end

    def valid?
      @nodes.size > 1 && @nodes.size < Namespace::NUMBER_OF_ANCESTORS_ALLOWED
    end

    def components
      raise InvalidRegistryPathError unless valid?

      @components ||= @nodes.size.downto(2).map do |length|
        @nodes.take(length).join('/')
      end
    end

    def has_repository?
      # ContainerRepository.find_by_full_path(@path).present?
    end

    def repository_project
      @project ||= Project.where_full_path_in(components.first(3))&.first
    end

    def repository_name
      return unless repository_project

      @path.remove(%r(^?#{Regexp.escape(repository_project.full_path)}/?))
    end
  end
end