Welcome to mirror list, hosted at ThFree Co, Russian Federation.

path.rb « container_registry « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a4b5f2aba6cf14372e49f623af76172d016914f7 (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
69
70
module ContainerRegistry
  ##
  # Class responsible 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)

    LEVELS_SUPPORTED = 3

    def initialize(path)
      @path = path
    end

    def valid?
      @path =~ Gitlab::Regex.container_repository_name_regex &&
        components.size > 1 &&
        components.size < Namespace::NUMBER_OF_ANCESTORS_ALLOWED
    end

    def components
      @components ||= @path.to_s.split('/')
    end

    def nodes
      raise InvalidRegistryPathError unless valid?

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

    def has_project?
      repository_project.present?
    end

    def has_repository?
      return false unless has_project?

      repository_project.container_repositories
        .where(name: repository_name).any?
    end

    def root_repository?
      @path == repository_project.full_path
    end

    def repository_project
      @project ||= Project
        .where_full_path_in(nodes.first(LEVELS_SUPPORTED))
        .first
    end

    def repository_name
      return unless has_project?

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

    def to_s
      @path
    end
  end
end