Commit 56100117 authored by Yannis Roussos's avatar Yannis Roussos

Merge branch 'mo-coverage-default-branch' into 'master'

Persist coverage default branch

See merge request gitlab-org/gitlab!45702
parents f48c6c15 ed396469
......@@ -14,6 +14,7 @@ module Ci
scope :with_included_projects, -> { includes(:project) }
scope :by_projects, -> (ids) { where(project_id: ids) }
scope :with_coverage, -> { where("(data->'coverage') IS NOT NULL") }
scope :with_default_branch, -> { where(default_branch: true) }
store_accessor :data, :coverage
......
......@@ -13,7 +13,8 @@ module Ci
project_id: pipeline.project_id,
ref_path: pipeline.source_ref_path,
date: pipeline.created_at.to_date,
last_pipeline_id: pipeline.id
last_pipeline_id: pipeline.id,
default_branch: pipeline.default_branch?
}
aggregate(pipeline.builds.with_coverage).map do |group_name, group|
......
---
title: Add default_branch to ci_daily_build_group_report_result
merge_request: 45702
author:
type: performance
# frozen_string_literal: true
class AddDefaultBranchToDailyBuildGroupReportResult < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
def change
add_column :ci_daily_build_group_report_results, :default_branch, :boolean, default: false, null: false
end
end
# frozen_string_literal: true
class AddIndexToCiDailyBuildGroupReportResults < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
INDEX_NAME = 'index_ci_daily_build_group_report_results_on_project_and_date'
disable_ddl_transaction!
def up
add_concurrent_index(
:ci_daily_build_group_report_results,
[:project_id, :date],
order: { date: :desc },
where: "default_branch = TRUE AND (data -> 'coverage') IS NOT NULL",
name: INDEX_NAME
)
end
def down
remove_concurrent_index_by_name(:ci_daily_build_group_report_results, INDEX_NAME)
end
end
e5b3bcac7150df4443879db05b18b6aeb01271d99965b2468278954dedf8413b
\ No newline at end of file
81b9b79f2ca8830b9d9e9315d93421875dfe44cfa0da6f4e9166567452a2363b
\ No newline at end of file
......@@ -10132,7 +10132,8 @@ CREATE TABLE ci_daily_build_group_report_results (
last_pipeline_id bigint NOT NULL,
ref_path text NOT NULL,
group_name text NOT NULL,
data jsonb NOT NULL
data jsonb NOT NULL,
default_branch boolean DEFAULT false NOT NULL
);
CREATE SEQUENCE ci_daily_build_group_report_results_id_seq
......@@ -20164,6 +20165,8 @@ CREATE UNIQUE INDEX index_ci_builds_runner_session_on_build_id ON ci_builds_runn
CREATE INDEX index_ci_daily_build_group_report_results_on_last_pipeline_id ON ci_daily_build_group_report_results USING btree (last_pipeline_id);
CREATE INDEX index_ci_daily_build_group_report_results_on_project_and_date ON ci_daily_build_group_report_results USING btree (project_id, date DESC) WHERE ((default_branch = true) AND ((data -> 'coverage'::text) IS NOT NULL));
CREATE INDEX index_ci_deleted_objects_on_pick_up_at ON ci_deleted_objects USING btree (pick_up_at);
CREATE INDEX index_ci_freeze_periods_on_project_id ON ci_freeze_periods USING btree (project_id);
......
......@@ -10,5 +10,11 @@ FactoryBot.define do
data do
{ 'coverage' => 77.0 }
end
default_branch { true }
trait :on_feature_branch do
ref_path { Gitlab::Git::BRANCH_REF_PREFIX + 'feature' }
default_branch { false }
end
end
end
......@@ -104,5 +104,24 @@ RSpec.describe Ci::DailyBuildGroupReportResult do
expect(subject).to contain_exactly(recent_build_group_report_result, old_build_group_report_result)
end
end
describe '.with_default_branch' do
subject(:coverages) { described_class.with_default_branch }
context 'when coverage for the default branch exist' do
let!(:recent_build_group_report_result) { create(:ci_daily_build_group_report_result, project: project) }
let!(:coverage_feature_branch) { create(:ci_daily_build_group_report_result, :on_feature_branch, project: project) }
it 'returns coverage with the default branch' do
expect(coverages).to contain_exactly(recent_build_group_report_result)
end
end
context 'when coverage for the default branch does not exist' do
it 'returns an empty collection' do
expect(coverages).to be_empty
end
end
end
end
end
......@@ -7,6 +7,7 @@ RSpec.describe Ci::DailyBuildGroupReportResultService, '#execute' do
let!(:rspec_job) { create(:ci_build, pipeline: pipeline, name: '3/3 rspec', coverage: 80) }
let!(:karma_job) { create(:ci_build, pipeline: pipeline, name: '2/2 karma', coverage: 90) }
let!(:extra_job) { create(:ci_build, pipeline: pipeline, name: 'extra', coverage: nil) }
let(:coverages) { Ci::DailyBuildGroupReportResult.all }
it 'creates daily code coverage record for each job in the pipeline that has coverage value' do
described_class.new.execute(pipeline)
......@@ -158,4 +159,30 @@ RSpec.describe Ci::DailyBuildGroupReportResultService, '#execute' do
expect { described_class.new.execute(new_pipeline) }.not_to raise_error
end
end
context 'when pipeline ref_path is the project default branch' do
let(:default_branch) { 'master' }
before do
allow(pipeline.project).to receive(:default_branch).and_return(default_branch)
end
it 'sets default branch to true' do
described_class.new.execute(pipeline)
coverages.each do |coverage|
expect(coverage.default_branch).to be_truthy
end
end
end
context 'when pipeline ref_path is not the project default branch' do
it 'sets default branch to false' do
described_class.new.execute(pipeline)
coverages.each do |coverage|
expect(coverage.default_branch).to be_falsey
end
end
end
end
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