extract_sections_from_build_trace_service.rb 834 Bytes
Newer Older
1 2
# frozen_string_literal: true

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
module Ci
  class ExtractSectionsFromBuildTraceService < BaseService
    def execute(build)
      return false unless build.trace_sections.empty?

      Gitlab::Database.bulk_insert(BuildTraceSection.table_name, extract_sections(build))
      true
    end

    private

    def find_or_create_name(name)
      project.build_trace_section_names.find_or_create_by!(name: name)
    rescue ActiveRecord::RecordInvalid
      project.build_trace_section_names.find_by!(name: name)
    end

    def extract_sections(build)
      build.trace.extract_sections.map do |attr|
        name = attr.delete(:name)
        name_record = find_or_create_name(name)

        attr.merge(
          build_id: build.id,
          project_id: project.id,
          section_name_id: name_record.id)
      end
    end
  end
end