Extract `Ci::Build#parse_trace_sections!` into a service

This commit is contained in:
Alessio Caiazza 2017-10-06 12:33:10 +02:00
parent ea023138bf
commit c0cfc9ebd2
No known key found for this signature in database
GPG Key ID: 8655B9CB5B8B512E
4 changed files with 92 additions and 35 deletions

View File

@ -267,24 +267,7 @@ module Ci
end
def parse_trace_sections!
return false unless trace_sections.empty?
sections = trace.extract_sections.map do |attr|
name = attr.delete(:name)
name_record = begin
project.build_trace_section_names.find_or_create_by!(name: name)
rescue ActiveRecord::RecordInvalid
project.build_trace_section_names.find_by!(name: name)
end
attr.merge(
build_id: self.id,
project_id: self.project_id,
section_name_id: name_record.id)
end
Gitlab::Database.bulk_insert(Ci::BuildTraceSection.table_name, sections)
true
ExtractSectionsFromBuildTraceService.new(project, user).execute(self)
end
def trace

View File

@ -0,0 +1,30 @@
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

View File

@ -322,24 +322,13 @@ describe Ci::Build do
end
describe '#parse_trace_sections!' do
context "when the build trace has sections markers," do
before do
build.trace.set(File.read(expand_fixture_path('trace/trace_with_sections')))
end
it 'calls ExtractSectionsFromBuildTraceService' do
expect(Ci::ExtractSectionsFromBuildTraceService)
.to receive(:new).with(project, build.user).once.and_call_original
expect_any_instance_of(Ci::ExtractSectionsFromBuildTraceService)
.to receive(:execute).with(build).once
it "saves the correct extracted sections" do
expect(build.trace_sections).to be_empty
expect(build.parse_trace_sections!).to be(true)
expect(build.trace_sections).not_to be_empty
end
it "fails if trace_sections isn't empty" do
expect(build.parse_trace_sections!).to be(true)
expect(build.trace_sections).not_to be_empty
expect(build.parse_trace_sections!).to be(false)
expect(build.trace_sections).not_to be_empty
end
build.parse_trace_sections!
end
end

View File

@ -0,0 +1,55 @@
require 'spec_helper'
describe Ci::ExtractSectionsFromBuildTraceService, '#execute' do
let(:user) { create(:user) }
let(:project) { create(:project) }
let(:build) { create(:ci_build, project: project) }
subject { described_class.new(project, user) }
shared_examples 'build trace has sections markers' do
before do
build.trace.set(File.read(expand_fixture_path('trace/trace_with_sections')))
end
it 'saves the correct extracted sections' do
expect(build.trace_sections).to be_empty
expect(subject.execute(build)).to be(true)
expect(build.trace_sections).not_to be_empty
end
it "fails if trace_sections isn't empty" do
expect(subject.execute(build)).to be(true)
expect(build.trace_sections).not_to be_empty
expect(subject.execute(build)).to be(false)
expect(build.trace_sections).not_to be_empty
end
end
shared_examples 'build trace has no sections markers' do
before do
build.trace.set('no markerts')
end
it 'extracts no sections' do
expect(build.trace_sections).to be_empty
expect(subject.execute(build)).to be(true)
expect(build.trace_sections).to be_empty
end
end
context 'when the build has no user' do
it_behaves_like 'build trace has sections markers'
it_behaves_like 'build trace has no sections markers'
end
context 'when the build has a valid user' do
before do
build.user = user
end
it_behaves_like 'build trace has sections markers'
it_behaves_like 'build trace has no sections markers'
end
end