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

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

98 lines
2.9 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 Local < Base
2019-12-21 20:55:43 +05:30
extend ::Gitlab::Utils::Override
2018-12-13 13:39:08 +05:30
include Gitlab::Utils::StrongMemoize
2019-02-15 15:39:39 +05:30
def initialize(params, context)
2023-04-23 21:23:45 +05:30
# `Repository#blobs_at` does not support files with the `/` prefix.
@location = Gitlab::Utils.remove_leading_slashes(params[:local])
2018-12-13 13:39:08 +05:30
super
end
def content
strong_memoize(:content) { fetch_local_content }
end
2022-06-21 17:19:12 +05:30
def metadata
super.merge(
type: :local,
location: masked_location,
2022-07-16 23:28:13 +05:30
blob: masked_blob,
raw: masked_raw,
2022-06-21 17:19:12 +05:30
extra: {}
)
end
2023-01-13 00:05:48 +05:30
def validate_context!
return if context.project&.repository
errors.push("Local file `#{masked_location}` does not have project!")
end
2018-12-13 13:39:08 +05:30
def validate_content!
2023-01-13 00:05:48 +05:30
if content.nil?
2022-04-01 21:47:47 +05:30
errors.push("Local file `#{masked_location}` does not exist!")
2018-12-13 13:39:08 +05:30
elsif content.blank?
2022-04-01 21:47:47 +05:30
errors.push("Local file `#{masked_location}` is empty!")
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_local_content
2023-04-23 21:23:45 +05:30
BatchLoader.for([context.sha, location])
.batch(key: context.project) do |locations, loader, args|
context.logger.instrument(:config_file_fetch_local_content) do
args[:key].repository.blobs_at(locations).each do |blob|
loader.call([blob.commit_id, blob.path], blob.data)
end
end
rescue GRPC::InvalidArgument
# no-op
2022-07-23 23:45:48 +05:30
end
2018-12-13 13:39:08 +05:30
end
2019-07-07 11:18:12 +05:30
2019-12-21 20:55:43 +05:30
override :expand_context_attrs
def expand_context_attrs
{
2019-07-07 11:18:12 +05:30
project: context.project,
sha: context.sha,
2020-04-08 14:13:33 +05:30
user: context.user,
2021-03-08 18:12:59 +05:30
parent_pipeline: context.parent_pipeline,
variables: context.variables
2019-12-21 20:55:43 +05:30
}
2019-07-07 11:18:12 +05:30
end
2022-07-16 23:28:13 +05:30
def masked_blob
2023-04-23 21:23:45 +05:30
return unless valid?
2022-07-16 23:28:13 +05:30
strong_memoize(:masked_blob) do
context.mask_variables_from(
Gitlab::Routing.url_helpers.project_blob_url(context.project, ::File.join(context.sha, location))
)
end
end
def masked_raw
2023-04-23 21:23:45 +05:30
return unless valid?
2022-07-16 23:28:13 +05:30
strong_memoize(:masked_raw) do
context.mask_variables_from(
Gitlab::Routing.url_helpers.project_raw_url(context.project, ::File.join(context.sha, location))
)
end
end
2018-12-13 13:39:08 +05:30
end
end
end
end
end
end