blob: 57350b1edb016b9fcc337a3501a12229330e87ca (
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# frozen_string_literal: true
module Gitlab
class GlRepository
class Identifier
include Gitlab::Utils::StrongMemoize
InvalidIdentifier = Class.new(ArgumentError)
def self.parse(gl_repository)
segments = gl_repository&.split('-')
# 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?
raise InvalidIdentifier, %Q(Invalid GL Repository "#{gl_repository}")
end
# 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
private
def container_class
repo_type.container_class
end
end
# 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
private
def container_class
case @container_type
when 'project'
Project
when 'group'
Group
end
end
end
def repo_type
strong_memoize(:repo_type) { Gitlab::GlRepository.types[repo_type_name] }
end
def container
strong_memoize(:container) { container_class.find_by_id(container_id) }
end
def valid?
repo_type.present? && container_class.present? && container_id&.positive?
end
private
attr_reader :container_id_str, :repo_type_name
def container_id
strong_memoize(:container_id) { Integer(container_id_str, 10, exception: false) }
end
end
end
end
|