summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/config/external/file/artifact.rb
blob: 140cbfac5c175a2b09ae6fcd20669f44866558a9 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# frozen_string_literal: true

module Gitlab
  module Ci
    class Config
      module External
        module File
          class Artifact < Base
            extend ::Gitlab::Utils::Override
            include Gitlab::Utils::StrongMemoize

            attr_reader :job_name

            def initialize(params, context)
              @location = params[:artifact]
              @job_name = params[:job]

              super
            end

            def content
              strong_memoize(:content) do
                next unless artifact_job

                Gitlab::Ci::ArtifactFileReader.new(artifact_job).read(location)
              rescue Gitlab::Ci::ArtifactFileReader::Error => error
                errors.push(error.message)
              end
            end

            def metadata
              super.merge(
                type: :artifact,
                location: masked_location,
                extra: { job_name: masked_job_name }
              )
            end

            private

            def validate_context!
              context.logger.instrument(:config_file_artifact_validate_context) do
                if !creating_child_pipeline?
                  errors.push('Including configs from artifacts is only allowed when triggering child pipelines')
                elsif !job_name.present?
                  errors.push("Job must be provided when including configs from artifacts")
                elsif !artifact_job.present?
                  errors.push("Job `#{masked_job_name}` not found in parent pipeline or does not have artifacts!")
                end
              end
            end

            def validate_content!
              errors.push("File `#{masked_location}` is empty!") unless content.present?
            end

            def artifact_job
              strong_memoize(:artifact_job) do
                next unless creating_child_pipeline?

                context.parent_pipeline.find_job_with_archive_artifacts(job_name)
              end
            end

            def creating_child_pipeline?
              context.parent_pipeline.present?
            end

            override :expand_context_attrs
            def expand_context_attrs
              {
                project: context.project,
                sha: context.sha,
                user: context.user,
                parent_pipeline: context.parent_pipeline
              }
            end

            def masked_job_name
              strong_memoize(:masked_job_name) do
                context.mask_variables_from(job_name)
              end
            end
          end
        end
      end
    end
  end
end