Class: Ci::Components::FetchService

Inherits:
Object
  • Object
show all
Includes:
Gitlab::Utils::StrongMemoize
Defined in:
app/services/ci/components/fetch_service.rb

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

#initialize(address:, current_user:) ⇒ FetchService

Returns a new instance of FetchService.



12
13
14
15
# File 'app/services/ci/components/fetch_service.rb', line 12

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

Instance Method Details

#executeObject



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
# File 'app/services/ci/components/fetch_service.rb', line 17

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&.content
    ServiceResponse.success(payload: {
      content: result.content,
      path: result.path,
      project: component_path.project,
      sha: component_path.sha,
      name: component_path.component_name,
      version: component_path.matched_version,
      reference: component_path.reference
    })
  elsif component_path.invalid_usage_for_latest?
    ServiceResponse.error(
      message: "#{error_prefix} The ~latest version reference is not supported for non-catalog resources. " \
               'Use a tag, branch, or commit SHA instead.',
      reason: :invalid_usage)
  elsif component_path.invalid_usage_for_partial_semver?
    ServiceResponse.error(
      message: "#{error_prefix} The partial semantic version reference is not supported for " \
               'non-catalog resources. Use a tag, branch, or commit SHA instead.',
      reason: :invalid_usage)
  else
    ServiceResponse.error(message: "#{error_prefix} content not found", reason: :content_not_found)
  end
rescue Gitlab::Access::AccessDeniedError
  if current_user.external? && component_path.project.internal?
    ServiceResponse.error(
      message: "#{error_prefix} project is `Internal`, it cannot be accessed by an External User",
      reason: :not_allowed)
  else
    ServiceResponse.error(
      message: "#{error_prefix} project does not exist or you don't have sufficient permissions",
      reason: :not_allowed)
  end
end