Welcome to mirror list, hosted at ThFree Co, Russian Federation.

create.rb « chain « pipeline « ci « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 15b0ff3c04db68be27bf641de9ca285610051944 (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
# frozen_string_literal: true

module Gitlab
  module Ci
    module Pipeline
      module Chain
        class Create < Chain::Base
          include Chain::Helpers
          include Gitlab::Utils::StrongMemoize

          def perform!
            logger.instrument(:pipeline_save) do
              BulkInsertableAssociations.with_bulk_insert do
                tags = extract_tag_list_by_status

                pipeline.transaction do
                  pipeline.save!
                  CommitStatus.bulk_insert_tags!(statuses, tags) if bulk_insert_tags?
                end
              end
            end
          rescue ActiveRecord::RecordInvalid => e
            error("Failed to persist the pipeline: #{e}")
          end

          def break?
            !pipeline.persisted?
          end

          private

          def statuses
            strong_memoize(:statuses) do
              pipeline.stages.flat_map(&:statuses)
            end
          end

          # We call `job.tag_list=` to assign tags to the jobs from the
          # Chain::Seed step which uses the `@tag_list` instance variable to
          # store them on the record. We remove them here because we want to
          # bulk insert them, otherwise they would be inserted and assigned one
          # by one with callbacks. We must use `remove_instance_variable`
          # because having the instance variable defined would still run the callbacks
          def extract_tag_list_by_status
            return {} unless bulk_insert_tags?

            statuses.each.with_object({}) do |job, acc|
              tag_list = job.clear_memoization(:tag_list)
              next unless tag_list

              acc[job.name] = tag_list
            end
          end

          def bulk_insert_tags?
            strong_memoize(:bulk_insert_tags) do
              ::Feature.enabled?(:ci_bulk_insert_tags, project, default_enabled: :yaml)
            end
          end
        end
      end
    end
  end
end