debian-mirror-gitlab/lib/gitlab/gl_repository/identifier.rb

88 lines
2.5 KiB
Ruby
Raw Normal View History

2020-06-23 00:09:42 +05:30
# frozen_string_literal: true
module Gitlab
class GlRepository
class Identifier
2020-07-28 23:09:34 +05:30
include Gitlab::Utils::StrongMemoize
2020-06-23 00:09:42 +05:30
2020-07-28 23:09:34 +05:30
InvalidIdentifier = Class.new(ArgumentError)
2020-06-23 00:09:42 +05:30
2020-07-28 23:09:34 +05:30
def self.parse(gl_repository)
segments = gl_repository&.split('-')
2020-06-23 00:09:42 +05:30
2020-07-28 23:09:34 +05:30
# gl_repository can either have 2 or 3 segments:
#
# TODO: convert all 2-segment format to 3-segment:
# https://gitlab.com/gitlab-org/gitlab/-/issues/219192
identifier = case segments&.size
when 2
TwoPartIdentifier.new(*segments)
when 3
ThreePartIdentifier.new(*segments)
end
return identifier if identifier&.valid?
2020-06-23 00:09:42 +05:30
2020-07-28 23:09:34 +05:30
raise InvalidIdentifier, %Q(Invalid GL Repository "#{gl_repository}")
2020-06-23 00:09:42 +05:30
end
2020-07-28 23:09:34 +05:30
# The older 2-segment format, where the container is implied.
# eg. project-1, wiki-1
class TwoPartIdentifier < Identifier
def initialize(repo_type_name, container_id_str)
@container_id_str = container_id_str
@repo_type_name = repo_type_name
end
2020-06-23 00:09:42 +05:30
2020-07-28 23:09:34 +05:30
private
2020-06-23 00:09:42 +05:30
2020-07-28 23:09:34 +05:30
def container_class
repo_type.container_class
end
end
2020-06-23 00:09:42 +05:30
2020-07-28 23:09:34 +05:30
# The newer 3-segment format, where the container is explicit
# eg. group-1-wiki, project-1-wiki
class ThreePartIdentifier < Identifier
def initialize(container_type, container_id_str, repo_type_name)
@container_id_str = container_id_str
@container_type = container_type
@repo_type_name = repo_type_name
end
2020-06-23 00:09:42 +05:30
2020-07-28 23:09:34 +05:30
private
2020-06-23 00:09:42 +05:30
2020-07-28 23:09:34 +05:30
def container_class
2021-01-03 14:25:43 +05:30
# NOTE: This is currently only used and supported for group wikis
# https://gitlab.com/gitlab-org/gitlab/-/issues/219192
return unless @repo_type_name == 'wiki'
"#{@container_type}_#{@repo_type_name}".classify.constantize
rescue NameError
nil
2020-06-23 00:09:42 +05:30
end
end
2020-07-28 23:09:34 +05:30
def repo_type
strong_memoize(:repo_type) { Gitlab::GlRepository.types[repo_type_name] }
end
2020-06-23 00:09:42 +05:30
2020-07-28 23:09:34 +05:30
def container
strong_memoize(:container) { container_class.find_by_id(container_id) }
2020-06-23 00:09:42 +05:30
end
2020-07-28 23:09:34 +05:30
def valid?
repo_type.present? && container_class.present? && container_id&.positive?
2020-06-23 00:09:42 +05:30
end
2020-07-28 23:09:34 +05:30
private
attr_reader :container_id_str, :repo_type_name
def container_id
strong_memoize(:container_id) { Integer(container_id_str, 10, exception: false) }
2020-06-23 00:09:42 +05:30
end
end
end
end