debian-mirror-gitlab/lib/gitlab/ci/pipeline/seed/build/cache.rb

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

82 lines
2.3 KiB
Ruby
Raw Normal View History

2019-12-26 22:10:19 +05:30
# frozen_string_literal: true
module Gitlab
module Ci
module Pipeline
module Seed
class Build
class Cache
2023-03-04 22:38:38 +05:30
def initialize(pipeline, cache, custom_key_prefix)
2019-12-26 22:10:19 +05:30
@pipeline = pipeline
local_cache = cache.to_h.deep_dup
@key = local_cache.delete(:key)
@paths = local_cache.delete(:paths)
@policy = local_cache.delete(:policy)
@untracked = local_cache.delete(:untracked)
2021-01-03 14:25:43 +05:30
@when = local_cache.delete(:when)
2023-03-17 16:20:25 +05:30
@unprotect = local_cache.delete(:unprotect)
2023-03-04 22:38:38 +05:30
@custom_key_prefix = custom_key_prefix
2019-12-26 22:10:19 +05:30
raise ArgumentError, "unknown cache keys: #{local_cache.keys}" if local_cache.any?
end
2021-04-17 20:07:23 +05:30
def attributes
2019-12-26 22:10:19 +05:30
{
2021-04-17 20:07:23 +05:30
key: key_string,
paths: @paths,
policy: @policy,
untracked: @untracked,
2023-03-17 16:20:25 +05:30
when: @when,
unprotect: @unprotect
2021-04-17 20:07:23 +05:30
}.compact
end
def build_attributes
{ options: { cache: attributes.presence }.compact }
2019-12-26 22:10:19 +05:30
end
private
def key_string
key_from_string || key_from_files
end
def key_from_string
@key.to_s if @key.is_a?(String) || @key.is_a?(Symbol)
end
def key_from_files
return unless @key.is_a?(Hash)
2023-03-04 22:38:38 +05:30
@key[:prefix] ||= @custom_key_prefix.to_s
2019-12-26 22:10:19 +05:30
[@key[:prefix], files_digest].select(&:present?).join('-')
end
def files_digest
hash_of_the_latest_changes || 'default'
end
def hash_of_the_latest_changes
ids = files.map { |path| last_commit_id_for_path(path) }
ids = ids.compact.sort.uniq
Digest::SHA1.hexdigest(ids.join('-')) if ids.any?
end
def files
@key[:files]
.to_a
.select(&:present?)
.uniq
end
def last_commit_id_for_path(path)
@pipeline.project.repository.last_commit_id_for_path(@pipeline.sha, path)
end
end
end
end
end
end
end