Commit 23e9a90f authored by Maxime Orefice's avatar Maxime Orefice Committed by Robert Speicher

Fix group coverage for default branch

This commits fixes a bug where the group code coverage data was not
being rendered correctly if the default branch was not master.
parent c0f74cfd
......@@ -4,7 +4,7 @@ module Ci
class DailyBuildGroupReportResultsFinder
include Gitlab::Allowable
def initialize(current_user:, project:, ref_path:, start_date:, end_date:, limit: nil)
def initialize(current_user:, project:, ref_path: nil, start_date:, end_date:, limit: nil)
@current_user = current_user
@project = project
@ref_path = ref_path
......@@ -35,11 +35,18 @@ module Ci
end
def query_params
{
params = {
project_id: project,
ref_path: ref_path,
date: start_date..end_date
}
if ref_path
params[:ref_path] = ref_path
else
params[:default_branch] = true
end
params
end
def none
......
......@@ -25,7 +25,9 @@ To see the latest code coverage for each project in your group:
> [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/215104) in [GitLab Premium](https://about.gitlab.com/pricing/) 13.4.
You can get a CSV of the code coverage data for all of the projects in your group. This report has a maximum of 1000 records. To get the report:
You can get a CSV of the code coverage data for all of the projects in your group. This report has a maximum of 1000 records. The code coverage data is from the default branch in each project.
To get the report:
1. Go to your group's **Analytics > Repositories** page
1. Click **Download historic test coverage data (.csv)**,
......@@ -44,6 +46,9 @@ For each day that a coverage report was generated by a job in a project's pipeli
If the project's code coverage was calculated more than once in a day, we will take the last value from that day.
NOTE:
[In GitLab 13.7 and later](https://gitlab.com/gitlab-org/gitlab/-/issues/270102), group code coverage data is taken from the configured [default branch](../../project/repository/branches/index.md#default-branch). In earlier versions, it is taken from the `master` branch.
<!-- ## Troubleshooting
Include any troubleshooting steps that you can foresee. If you know beforehand what issues
......
......@@ -40,7 +40,7 @@ class Groups::Analytics::CoverageReportsController < Groups::Analytics::Applicat
current_user: current_user,
group: @group,
project_ids: params.permit(project_ids: [])[:project_ids],
ref_path: params.require(:ref_path),
ref_path: params[:ref_path],
start_date: Date.parse(params.require(:start_date)),
end_date: Date.parse(params.require(:end_date))
}
......
......@@ -9,7 +9,7 @@ module Ci
# See thread: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/37768#note_386839633
GROUP_QUERY_RESULT_LIMIT = 1000.freeze
def initialize(current_user:, group:, project_ids: [], ref_path:, start_date:, end_date:, limit: nil)
def initialize(current_user:, group:, project_ids: [], ref_path: nil, start_date:, end_date:, limit: nil)
super(current_user: current_user, project: nil, ref_path: ref_path, start_date: start_date, end_date: end_date, limit: limit)
@group = group
......
......@@ -4,5 +4,5 @@
%h3
= _("Repositories Analytics")
#js-group-repository-analytics{ data: { group_analytics_coverage_reports_path: group_analytics_coverage_reports_path(@group, format: :csv, ref_path: "refs/heads/master"),
#js-group-repository-analytics{ data: { group_analytics_coverage_reports_path: group_analytics_coverage_reports_path(@group, format: :csv),
group_full_path: @group.full_path } }
---
title: Fix group code coverage for default branch
merge_request: 49630
author:
type: fixed
......@@ -94,6 +94,17 @@ RSpec.describe Groups::Analytics::CoverageReportsController do
end
end
context 'when ref_path is nil' do
let(:ref_path) { nil }
it 'responds HTTP 200' do
get :index, params: valid_request_params
expect(response).to have_gitlab_http_status(:ok)
expect(csv_response.size).to eq(3)
end
end
it 'executes the same number of queries regardless of the number of records returned' do
control = ActiveRecord::QueryRecorder.new do
get :index, params: valid_request_params
......
......@@ -4,57 +4,77 @@ require 'spec_helper'
RSpec.describe Ci::DailyBuildGroupReportResultsFinder do
describe '#execute' do
let(:project) { create(:project, :private) }
let(:ref_path) { 'refs/heads/master' }
let_it_be(:project) { create(:project, :private) }
let_it_be(:current_user) { project.owner }
let_it_be(:ref_path) { 'refs/heads/master' }
let(:limit) { nil }
let_it_be(:default_branch) { false }
let!(:rspec_coverage_1) { create_daily_coverage('rspec', 79.0, '2020-03-09') }
let!(:karma_coverage_1) { create_daily_coverage('karma', 89.0, '2020-03-09') }
let!(:rspec_coverage_2) { create_daily_coverage('rspec', 95.0, '2020-03-10') }
let!(:karma_coverage_2) { create_daily_coverage('karma', 92.0, '2020-03-10') }
let!(:rspec_coverage_3) { create_daily_coverage('rspec', 97.0, '2020-03-11') }
let!(:karma_coverage_3) { create_daily_coverage('karma', 99.0, '2020-03-11') }
let_it_be(:rspec_coverage_1) { create_daily_coverage('rspec', 79.0, '2020-03-09') }
let_it_be(:karma_coverage_1) { create_daily_coverage('karma', 89.0, '2020-03-09') }
let_it_be(:rspec_coverage_2) { create_daily_coverage('rspec', 95.0, '2020-03-10') }
let_it_be(:karma_coverage_2) { create_daily_coverage('karma', 92.0, '2020-03-10') }
let_it_be(:rspec_coverage_3) { create_daily_coverage('rspec', 97.0, '2020-03-11') }
let_it_be(:karma_coverage_3) { create_daily_coverage('karma', 99.0, '2020-03-11') }
subject do
described_class.new(
let(:attributes) do
{
current_user: current_user,
project: project,
ref_path: ref_path,
start_date: '2020-03-09',
end_date: '2020-03-10',
limit: limit
).execute
}
end
context 'when current user is allowed to read build report results' do
let(:current_user) { project.owner }
subject(:coverages) do
described_class.new(**attributes).execute
end
context 'when ref_path is present' do
context 'when current user is allowed to read build report results' do
it 'returns all matching results within the given date range' do
expect(coverages).to match_array([
karma_coverage_2,
rspec_coverage_2,
karma_coverage_1,
rspec_coverage_1
])
end
context 'and limit is specified' do
let(:limit) { 2 }
it 'returns all matching results within the given date range' do
expect(subject).to match_array([
karma_coverage_2,
rspec_coverage_2,
karma_coverage_1,
rspec_coverage_1
])
it 'returns limited number of matching results within the given date range' do
expect(coverages).to match_array([
karma_coverage_2,
rspec_coverage_2
])
end
end
end
context 'and limit is specified' do
let(:limit) { 2 }
context 'when current user is not allowed to read build report results' do
let(:current_user) { create(:user) }
it 'returns limited number of matching results within the given date range' do
expect(subject).to match_array([
karma_coverage_2,
rspec_coverage_2
])
it 'returns an empty result' do
expect(coverages).to be_empty
end
end
end
context 'when current user is not allowed to read build report results' do
let(:current_user) { create(:user) }
context 'when ref_path is not present' do
let(:ref_path) { nil }
it 'returns an empty result' do
expect(subject).to be_empty
context 'when coverages exist for the default branch' do
let(:default_branch) { true }
it 'returns coverage for the default branch' do
rspec_coverage_4 = create_daily_coverage('rspec', 66.0, '2020-03-10')
expect(coverages).to contain_exactly(rspec_coverage_4)
end
end
end
end
......@@ -65,10 +85,11 @@ RSpec.describe Ci::DailyBuildGroupReportResultsFinder do
create(
:ci_daily_build_group_report_result,
project: project,
ref_path: ref_path,
ref_path: ref_path || 'feature-branch',
group_name: group_name,
data: { 'coverage' => coverage },
date: date
date: date,
default_branch: default_branch
)
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