2017-03-28 08:34:56 -04:00
|
|
|
module ContainerRegistry
|
2017-03-29 06:53:02 -04:00
|
|
|
##
|
2017-04-06 04:23:51 -04:00
|
|
|
# Class responsible for extracting project and repository name from
|
2017-03-29 06:53:02 -04:00
|
|
|
# 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
|
|
|
|
#
|
2017-03-28 08:34:56 -04:00
|
|
|
class Path
|
|
|
|
InvalidRegistryPathError = Class.new(StandardError)
|
|
|
|
|
2017-04-06 04:23:51 -04:00
|
|
|
LEVELS_SUPPORTED = 3
|
|
|
|
|
2017-03-29 06:53:02 -04:00
|
|
|
def initialize(path)
|
2017-04-13 05:54:02 -04:00
|
|
|
@path = path.to_s.downcase
|
2017-03-28 08:34:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def valid?
|
2017-04-03 05:38:39 -04:00
|
|
|
@path =~ Gitlab::Regex.container_repository_name_regex &&
|
2017-04-04 05:56:32 -04:00
|
|
|
components.size > 1 &&
|
|
|
|
components.size < Namespace::NUMBER_OF_ANCESTORS_ALLOWED
|
2017-04-03 05:42:37 -04:00
|
|
|
end
|
|
|
|
|
2017-04-04 05:56:32 -04:00
|
|
|
def components
|
2017-04-13 05:54:02 -04:00
|
|
|
@components ||= @path.split('/')
|
2017-03-28 08:34:56 -04:00
|
|
|
end
|
|
|
|
|
2017-04-04 05:56:32 -04:00
|
|
|
def nodes
|
2017-03-28 08:34:56 -04:00
|
|
|
raise InvalidRegistryPathError unless valid?
|
|
|
|
|
2017-04-04 05:56:32 -04:00
|
|
|
@nodes ||= components.size.downto(2).map do |length|
|
|
|
|
components.take(length).join('/')
|
2017-03-28 08:34:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-30 07:45:54 -04:00
|
|
|
def has_project?
|
|
|
|
repository_project.present?
|
|
|
|
end
|
|
|
|
|
2017-03-28 08:57:22 -04:00
|
|
|
def has_repository?
|
2017-03-30 07:45:54 -04:00
|
|
|
return false unless has_project?
|
|
|
|
|
|
|
|
repository_project.container_repositories
|
|
|
|
.where(name: repository_name).any?
|
2017-03-28 08:57:22 -04:00
|
|
|
end
|
|
|
|
|
2017-03-30 09:24:46 -04:00
|
|
|
def root_repository?
|
2017-04-18 16:20:47 -04:00
|
|
|
@path == project_path
|
2017-03-30 09:24:46 -04:00
|
|
|
end
|
|
|
|
|
2017-03-28 08:34:56 -04:00
|
|
|
def repository_project
|
2017-04-06 04:23:51 -04:00
|
|
|
@project ||= Project
|
|
|
|
.where_full_path_in(nodes.first(LEVELS_SUPPORTED))
|
|
|
|
.first
|
2017-03-28 08:34:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def repository_name
|
2017-03-30 07:45:54 -04:00
|
|
|
return unless has_project?
|
2017-03-28 08:57:22 -04:00
|
|
|
|
2017-04-18 16:20:47 -04:00
|
|
|
@path.remove(%r(^#{Regexp.escape(project_path)}/?))
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_path
|
|
|
|
return unless has_project?
|
|
|
|
|
|
|
|
repository_project.full_path.downcase
|
2017-03-28 08:34:56 -04:00
|
|
|
end
|
2017-04-03 05:42:37 -04:00
|
|
|
|
|
|
|
def to_s
|
|
|
|
@path
|
|
|
|
end
|
2017-03-28 08:34:56 -04:00
|
|
|
end
|
|
|
|
end
|