Commit c3ba5646 authored by Erick Bajao's avatar Erick Bajao

Use average of coverage values to avoid upsert error

Given we use the build's group_name there are chances that
in some projects, each build group will still store its own
coverage. This would cause SQL errors when upserting because
it will violate the constraint when we try to upsert multiple
instances of the same group name.
parent d4083f52
...@@ -19,12 +19,21 @@ module Ci ...@@ -19,12 +19,21 @@ module Ci
last_pipeline_id: pipeline.id last_pipeline_id: pipeline.id
} }
pipeline.builds.with_coverage.map do |build| aggregate(pipeline.builds.with_coverage).map do |group_name, group|
base_attrs.merge( base_attrs.merge(
title: build.group_name, title: group_name,
value: build.coverage value: average_coverage(group)
) )
end end
end end
def aggregate(builds)
builds.group_by(&:group_name)
end
def average_coverage(group)
total_coverage = group.reduce(0.0) { |sum, build| sum + build.coverage }
(total_coverage / group.size).round(2)
end
end end
end end
---
title: Fix daily report result to use average of coverage values if there are multiple builds for a given group
name
merge_request: 28556
author:
type: fixed
...@@ -38,6 +38,27 @@ describe Ci::DailyReportResultService, '#execute' do ...@@ -38,6 +38,27 @@ describe Ci::DailyReportResultService, '#execute' do
expect(Ci::DailyReportResult.find_by(title: 'extra')).to be_nil expect(Ci::DailyReportResult.find_by(title: 'extra')).to be_nil
end end
context 'when there are multiple builds with the same group name that report coverage' do
let!(:test_job_1) { create(:ci_build, pipeline: pipeline, name: '1/2 test', coverage: 70) }
let!(:test_job_2) { create(:ci_build, pipeline: pipeline, name: '2/2 test', coverage: 80) }
it 'creates daily code coverage record with the average as the value' do
described_class.new.execute(pipeline)
Ci::DailyReportResult.find_by(title: 'test').tap do |coverage|
expect(coverage).to have_attributes(
project_id: pipeline.project.id,
last_pipeline_id: pipeline.id,
ref_path: pipeline.source_ref_path,
param_type: 'coverage',
title: test_job_2.group_name,
value: 75,
date: pipeline.created_at.to_date
)
end
end
end
context 'when there is an existing daily code coverage for the matching date, project, ref_path, and group name' do context 'when there is an existing daily code coverage for the matching date, project, ref_path, and group name' do
let!(:new_pipeline) do let!(:new_pipeline) do
create( create(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment