debian-mirror-gitlab/lib/gitlab/ci/config/external/file/remote.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

72 lines
2.1 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
module Gitlab
module Ci
class Config
module External
module File
class Remote < Base
include Gitlab::Utils::StrongMemoize
2019-02-15 15:39:39 +05:30
def initialize(params, context)
@location = params[:remote]
super
end
2018-12-13 13:39:08 +05:30
def content
strong_memoize(:content) { fetch_remote_content }
end
2022-06-21 17:19:12 +05:30
def metadata
super.merge(
type: :remote,
location: masked_location,
2022-07-16 23:28:13 +05:30
blob: nil,
raw: masked_location,
2022-06-21 17:19:12 +05:30
extra: {}
)
end
2023-01-13 00:05:48 +05:30
def validate_context!
# no-op
end
2018-12-13 13:39:08 +05:30
def validate_location!
super
unless ::Gitlab::UrlSanitizer.valid?(location)
2022-04-01 21:47:47 +05:30
errors.push("Remote file `#{masked_location}` does not have a valid address!")
2018-12-13 13:39:08 +05:30
end
end
2023-04-23 21:23:45 +05:30
private
2018-12-13 13:39:08 +05:30
def fetch_remote_content
begin
2022-07-23 23:45:48 +05:30
response = context.logger.instrument(:config_file_fetch_remote_content) do
Gitlab::HTTP.get(location)
end
2018-12-13 13:39:08 +05:30
rescue SocketError
2022-04-01 21:47:47 +05:30
errors.push("Remote file `#{masked_location}` could not be fetched because of a socket error!")
2018-12-13 13:39:08 +05:30
rescue Timeout::Error
2022-04-01 21:47:47 +05:30
errors.push("Remote file `#{masked_location}` could not be fetched because of a timeout error!")
2018-12-13 13:39:08 +05:30
rescue Gitlab::HTTP::Error
2022-04-01 21:47:47 +05:30
errors.push("Remote file `#{masked_location}` could not be fetched because of HTTP error!")
2023-03-04 22:38:38 +05:30
rescue Errno::ECONNREFUSED, Gitlab::HTTP::BlockedUrlError => e
2018-12-13 13:39:08 +05:30
errors.push("Remote file could not be fetched because #{e}!")
end
if response&.code.to_i >= 400
2022-04-01 21:47:47 +05:30
errors.push("Remote file `#{masked_location}` could not be fetched because of HTTP code `#{response.code}` error!")
2018-12-13 13:39:08 +05:30
end
2021-10-27 15:23:28 +05:30
response.body if errors.none?
2018-12-13 13:39:08 +05:30
end
end
end
end
end
end
end