2017-03-28 12:34:56 +00:00
|
|
|
module ContainerRegistry
|
2017-03-29 10:53:02 +00:00
|
|
|
##
|
|
|
|
# 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
|
|
|
|
#
|
2017-03-28 12:34:56 +00:00
|
|
|
class Path
|
|
|
|
InvalidRegistryPathError = Class.new(StandardError)
|
|
|
|
|
2017-03-29 10:53:02 +00:00
|
|
|
def initialize(path)
|
|
|
|
@path = path
|
|
|
|
@nodes = path.to_s.split('/')
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
@path
|
2017-03-28 12:34:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def valid?
|
2017-03-28 14:20:16 +00:00
|
|
|
@nodes.size > 1 && @nodes.size < Namespace::NUMBER_OF_ANCESTORS_ALLOWED
|
2017-03-28 12:34:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def components
|
|
|
|
raise InvalidRegistryPathError unless valid?
|
|
|
|
|
|
|
|
@components ||= @nodes.size.downto(2).map do |length|
|
|
|
|
@nodes.take(length).join('/')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-30 11:45:54 +00:00
|
|
|
def has_project?
|
|
|
|
repository_project.present?
|
|
|
|
end
|
|
|
|
|
2017-03-28 12:57:22 +00:00
|
|
|
def has_repository?
|
2017-03-30 11:45:54 +00:00
|
|
|
return false unless has_project?
|
|
|
|
|
|
|
|
repository_project.container_repositories
|
|
|
|
.where(name: repository_name).any?
|
2017-03-28 12:57:22 +00:00
|
|
|
end
|
|
|
|
|
2017-03-28 12:34:56 +00:00
|
|
|
def repository_project
|
|
|
|
@project ||= Project.where_full_path_in(components.first(3))&.first
|
|
|
|
end
|
|
|
|
|
|
|
|
def repository_name
|
2017-03-30 11:45:54 +00:00
|
|
|
return unless has_project?
|
2017-03-28 12:57:22 +00:00
|
|
|
|
2017-03-29 10:53:02 +00:00
|
|
|
@path.remove(%r(^?#{Regexp.escape(repository_project.full_path)}/?))
|
2017-03-28 12:34:56 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|