2019-12-21 20:55:43 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Ci
|
|
|
|
class Config
|
|
|
|
module External
|
|
|
|
class Context
|
2021-12-11 22:18:48 +05:30
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
TimeoutError = Class.new(StandardError)
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
include ::Gitlab::Utils::StrongMemoize
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
attr_reader :project, :sha, :user, :parent_pipeline, :variables
|
2022-01-26 12:08:38 +05:30
|
|
|
attr_reader :expandset, :execution_deadline, :logger
|
|
|
|
|
|
|
|
delegate :instrument, to: :logger
|
2019-12-21 20:55:43 +05:30
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
def initialize(project: nil, sha: nil, user: nil, parent_pipeline: nil, variables: nil, logger: nil)
|
2019-12-21 20:55:43 +05:30
|
|
|
@project = project
|
|
|
|
@sha = sha
|
|
|
|
@user = user
|
2020-04-08 14:13:33 +05:30
|
|
|
@parent_pipeline = parent_pipeline
|
2022-01-26 12:08:38 +05:30
|
|
|
@variables = variables || Ci::Variables::Collection.new
|
2019-12-21 20:55:43 +05:30
|
|
|
@expandset = Set.new
|
|
|
|
@execution_deadline = 0
|
2022-01-26 12:08:38 +05:30
|
|
|
@logger = logger || Gitlab::Ci::Pipeline::Logger.new(project: project)
|
2019-12-21 20:55:43 +05:30
|
|
|
|
|
|
|
yield self if block_given?
|
|
|
|
end
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
def top_level_worktree_paths
|
|
|
|
strong_memoize(:top_level_worktree_paths) do
|
|
|
|
project.repository.tree(sha).blobs.map(&:path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def all_worktree_paths
|
|
|
|
strong_memoize(:all_worktree_paths) do
|
|
|
|
project.repository.ls_files(sha)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
def variables_hash
|
|
|
|
strong_memoize(:variables_hash) do
|
|
|
|
variables.to_hash
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
def mutate(attrs = {})
|
|
|
|
self.class.new(**attrs) do |ctx|
|
|
|
|
ctx.expandset = expandset
|
|
|
|
ctx.execution_deadline = execution_deadline
|
2022-01-26 12:08:38 +05:30
|
|
|
ctx.logger = logger
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_deadline(timeout_seconds)
|
|
|
|
@execution_deadline = current_monotonic_time + timeout_seconds.to_f
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_execution_time!
|
|
|
|
raise TimeoutError if execution_expired?
|
|
|
|
end
|
|
|
|
|
|
|
|
def sentry_payload
|
|
|
|
{
|
|
|
|
user: user.inspect,
|
|
|
|
project: project.inspect
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2022-04-01 21:47:47 +05:30
|
|
|
def mask_variables_from(location)
|
|
|
|
variables.reduce(location.dup) do |loc, variable|
|
|
|
|
if variable[:masked]
|
|
|
|
Gitlab::Ci::MaskSecret.mask!(loc, variable[:value])
|
|
|
|
else
|
|
|
|
loc
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
protected
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
attr_writer :expandset, :execution_deadline, :logger
|
2019-12-21 20:55:43 +05:30
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def current_monotonic_time
|
|
|
|
Gitlab::Metrics::System.monotonic_time
|
|
|
|
end
|
|
|
|
|
|
|
|
def execution_expired?
|
2020-10-24 23:57:45 +05:30
|
|
|
return false if execution_deadline == 0
|
2019-12-21 20:55:43 +05:30
|
|
|
|
|
|
|
current_monotonic_time > execution_deadline
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|