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

identifier.rb « gl_repository « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dc3e7931696f83ae153508b7c88e5ec2adb5875f (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
# frozen_string_literal: true

module Gitlab
  class GlRepository
    class Identifier
      attr_reader :gl_repository, :repo_type

      def initialize(gl_repository)
        @gl_repository = gl_repository
        @segments = gl_repository.split('-')

        raise_error if segments.size > 3

        @repo_type = find_repo_type
        @container_id = find_container_id
        @container_class = find_container_class
      end

      def fetch_container!
        container_class.find_by_id(container_id)
      end

      private

      attr_reader :segments, :container_class, :container_id

      def find_repo_type
        type_name = three_segments_format? ? segments.last : segments.first
        type = Gitlab::GlRepository.types[type_name]

        raise_error unless type

        type
      end

      def find_container_class
        if three_segments_format?
          case segments[0]
          when 'project'
            Project
          when 'group'
            Group
          else
            raise_error
          end
        else
          repo_type.container_class
        end
      end

      def find_container_id
        id = Integer(segments[1], 10, exception: false)

        raise_error unless id

        id
      end

      # gl_repository can either have 2 or 3 segments:
      # "wiki-1" is the older 2-segment format, where container is implied.
      # "group-1-wiki" is the newer 3-segment format, including container information.
      #
      # TODO: convert all 2-segment format to 3-segment:
      # https://gitlab.com/gitlab-org/gitlab/-/issues/219192
      def three_segments_format?
        segments.size == 3
      end

      def raise_error
        raise ArgumentError, "Invalid GL Repository \"#{gl_repository}\""
      end
    end
  end
end