Class: Gitlab::Ci::Config::External::Context

Inherits:
Object
  • Object
show all
Includes:
Utils::StrongMemoize
Defined in:
lib/gitlab/ci/config/external/context.rb

Constant Summary collapse

TimeoutError =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project: nil, pipeline: nil, sha: nil, user: nil, parent_pipeline: nil, variables: nil, pipeline_config: nil, logger: nil) {|_self| ... } ⇒ Context

Returns a new instance of Context.

Yields:

  • (_self)

Yield Parameters:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/gitlab/ci/config/external/context.rb', line 21

def initialize(
  project: nil, pipeline: nil, sha: nil, user: nil, parent_pipeline: nil, variables: nil,
  pipeline_config: nil, logger: nil
)
  @project = project
  @pipeline = pipeline
  @sha = sha
  @user = user
  @parent_pipeline = parent_pipeline
  @variables = variables || Ci::Variables::Collection.new
  @pipeline_config = pipeline_config
  @expandset = []
  @execution_deadline = 0
  @logger = logger || Gitlab::Ci::Pipeline::Logger.new(project: project)
  @max_includes = Gitlab::CurrentSettings.current_application_settings.ci_max_includes
  @max_total_yaml_size_bytes =
    Gitlab::CurrentSettings.current_application_settings.ci_max_total_yaml_size_bytes
  @total_file_size_in_bytes = 0
  yield self if block_given?
end

Instance Attribute Details

#execution_deadlineObject

Returns the value of attribute execution_deadline.



15
16
17
# File 'lib/gitlab/ci/config/external/context.rb', line 15

def execution_deadline
  @execution_deadline
end

#expandsetObject

Returns the value of attribute expandset.



15
16
17
# File 'lib/gitlab/ci/config/external/context.rb', line 15

def expandset
  @expandset
end

#loggerObject

Returns the value of attribute logger.



15
16
17
# File 'lib/gitlab/ci/config/external/context.rb', line 15

def logger
  @logger
end

#max_includesObject

Returns the value of attribute max_includes.



15
16
17
# File 'lib/gitlab/ci/config/external/context.rb', line 15

def max_includes
  @max_includes
end

#max_total_yaml_size_bytesObject

Returns the value of attribute max_total_yaml_size_bytes.



15
16
17
# File 'lib/gitlab/ci/config/external/context.rb', line 15

def max_total_yaml_size_bytes
  @max_total_yaml_size_bytes
end

#parent_pipelineObject (readonly)

Returns the value of attribute parent_pipeline.



14
15
16
# File 'lib/gitlab/ci/config/external/context.rb', line 14

def parent_pipeline
  @parent_pipeline
end

#pipelineObject

Returns the value of attribute pipeline.



15
16
17
# File 'lib/gitlab/ci/config/external/context.rb', line 15

def pipeline
  @pipeline
end

#pipeline_configObject (readonly)

Returns the value of attribute pipeline_config.



14
15
16
# File 'lib/gitlab/ci/config/external/context.rb', line 14

def pipeline_config
  @pipeline_config
end

#projectObject (readonly)

Returns the value of attribute project.



14
15
16
# File 'lib/gitlab/ci/config/external/context.rb', line 14

def project
  @project
end

#shaObject (readonly)

Returns the value of attribute sha.



14
15
16
# File 'lib/gitlab/ci/config/external/context.rb', line 14

def sha
  @sha
end

#total_file_size_in_bytesObject

Returns the value of attribute total_file_size_in_bytes.



17
18
19
# File 'lib/gitlab/ci/config/external/context.rb', line 17

def total_file_size_in_bytes
  @total_file_size_in_bytes
end

#userObject (readonly)

Returns the value of attribute user.



14
15
16
# File 'lib/gitlab/ci/config/external/context.rb', line 14

def user
  @user
end

#variablesObject (readonly)

Returns the value of attribute variables.



14
15
16
# File 'lib/gitlab/ci/config/external/context.rb', line 14

def variables
  @variables
end

Instance Method Details

#all_worktree_pathsObject



48
49
50
51
52
# File 'lib/gitlab/ci/config/external/context.rb', line 48

def all_worktree_paths
  strong_memoize(:all_worktree_paths) do
    project.repository.ls_files(sha)
  end
end

#check_execution_time!Object

Raises:



75
76
77
# File 'lib/gitlab/ci/config/external/context.rb', line 75

def check_execution_time!
  raise TimeoutError if execution_expired?
end

#includesObject



96
97
98
# File 'lib/gitlab/ci/config/external/context.rb', line 96

def includes
  expandset.map(&:metadata)
end

#internal_include?Boolean

Some Ci::ProjectConfig sources prepend the config content with an “internal” ‘include`, which becomes the first included file. When running a pipeline, we pass pipeline_config into the context of the first included file, which we use in this method to determine if the file is an “internal” one.

Returns:

  • (Boolean)


103
104
105
# File 'lib/gitlab/ci/config/external/context.rb', line 103

def internal_include?
  !!pipeline_config&.internal_include_prepended?
end

#mask_variables_from(string) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/gitlab/ci/config/external/context.rb', line 86

def mask_variables_from(string)
  variables.reduce(string.dup) do |str, variable|
    if variable[:masked]
      Gitlab::Ci::MaskSecret.mask!(str, variable[:value])
    else
      str
    end
  end
end

#mutate(attrs = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/gitlab/ci/config/external/context.rb', line 60

def mutate(attrs = {})
  self.class.new(**attrs) do |ctx|
    ctx.pipeline = pipeline
    ctx.expandset = expandset
    ctx.execution_deadline = execution_deadline
    ctx.logger = logger
    ctx.max_includes = max_includes
    ctx.max_total_yaml_size_bytes = max_total_yaml_size_bytes
  end
end

#sentry_payloadObject



79
80
81
82
83
84
# File 'lib/gitlab/ci/config/external/context.rb', line 79

def sentry_payload
  {
    user: user.inspect,
    project: project.inspect
  }
end

#set_deadline(timeout_seconds) ⇒ Object



71
72
73
# File 'lib/gitlab/ci/config/external/context.rb', line 71

def set_deadline(timeout_seconds)
  @execution_deadline = current_monotonic_time + timeout_seconds.to_f
end

#top_level_worktree_pathsObject



42
43
44
45
46
# File 'lib/gitlab/ci/config/external/context.rb', line 42

def top_level_worktree_paths
  strong_memoize(:top_level_worktree_paths) do
    project.repository.tree(sha).blobs.map(&:path)
  end
end

#variables_hashObject



54
55
56
57
58
# File 'lib/gitlab/ci/config/external/context.rb', line 54

def variables_hash
  strong_memoize(:variables_hash) do
    variables.to_hash
  end
end