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

fetch_service.rb « components « ci « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4f09d47b5302e458f7bee9844e41d74e30d303f3 (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
# frozen_string_literal: true

module Ci
  module Components
    class FetchService
      include Gitlab::Utils::StrongMemoize

      COMPONENT_PATHS = [
        ::Gitlab::Ci::Components::InstancePath
      ].freeze

      def initialize(address:, current_user:)
        @address = address
        @current_user = current_user
      end

      def execute
        unless component_path_class
          return ServiceResponse.error(
            message: "#{error_prefix} the component path is not supported",
            reason: :unsupported_path)
        end

        component_path = component_path_class.new(address: address)
        result = component_path.fetch_content!(current_user: current_user)

        if result
          ServiceResponse.success(payload: {
            content: result.content,
            path: result.path,
            project: component_path.project,
            sha: component_path.sha
          })
        else
          ServiceResponse.error(message: "#{error_prefix} content not found", reason: :content_not_found)
        end
      rescue Gitlab::Access::AccessDeniedError
        ServiceResponse.error(
          message: "#{error_prefix} project does not exist or you don't have sufficient permissions",
          reason: :not_allowed)
      end

      private

      attr_reader :current_user, :address

      def component_path_class
        COMPONENT_PATHS.find { |klass| klass.match?(address) }
      end
      strong_memoize_attr :component_path_class

      def error_prefix
        "component '#{address}' -"
      end
    end
  end
end