summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/pipeline/chain/populate_metadata.rb
blob: 89befb2a65b70067f4cf6735b36498d59af8cd7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# frozen_string_literal: true

module Gitlab
  module Ci
    module Pipeline
      module Chain
        class PopulateMetadata < Chain::Base
          include Chain::Helpers

          def perform!
            set_pipeline_name
            return if pipeline.pipeline_metadata.nil? || pipeline.pipeline_metadata.valid?

            message = pipeline.pipeline_metadata.errors.full_messages.join(', ')
            error("Failed to build pipeline metadata! #{message}")
          end

          def break?
            pipeline.pipeline_metadata&.errors&.any?
          end

          private

          def set_pipeline_name
            return if Feature.disabled?(:pipeline_name, pipeline.project) ||
              @command.yaml_processor_result.workflow_name.blank?

            name = @command.yaml_processor_result.workflow_name
            name = ExpandVariables.expand(name, -> { global_context.variables.sort_and_expand_all })

            return if name.blank?

            pipeline.build_pipeline_metadata(project: pipeline.project, name: name.strip)
          end

          def global_context
            Gitlab::Ci::Build::Context::Global.new(
              pipeline, yaml_variables: @command.pipeline_seed.root_variables)
          end
        end
      end
    end
  end
end