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 ...@@ -4,7 +4,7 @@ module Ci
class DailyBuildGroupReportResultsFinder class DailyBuildGroupReportResultsFinder
include Gitlab::Allowable 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 @current_user = current_user
@project = project @project = project
@ref_path = ref_path @ref_path = ref_path
...@@ -35,11 +35,18 @@ module Ci ...@@ -35,11 +35,18 @@ module Ci
end end
def query_params def query_params
{ params = {
project_id: project, project_id: project,
ref_path: ref_path,
date: start_date..end_date date: start_date..end_date
} }
if ref_path
params[:ref_path] = ref_path
else
params[:default_branch] = true
end
params
end end
def none def none
......
...@@ -25,7 +25,9 @@ To see the latest code coverage for each project in your group: ...@@ -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. > [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. Go to your group's **Analytics > Repositories** page
1. Click **Download historic test coverage data (.csv)**, 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 ...@@ -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. 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 <!-- ## Troubleshooting
Include any troubleshooting steps that you can foresee. If you know beforehand what issues 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 ...@@ -40,7 +40,7 @@ class Groups::Analytics::CoverageReportsController < Groups::Analytics::Applicat
current_user: current_user, current_user: current_user,
group: @group, group: @group,
project_ids: params.permit(project_ids: [])[:project_ids], 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)), start_date: Date.parse(params.require(:start_date)),
end_date: Date.parse(params.require(:end_date)) end_date: Date.parse(params.require(:end_date))
} }
......
...@@ -9,7 +9,7 @@ module Ci ...@@ -9,7 +9,7 @@ module Ci
# See thread: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/37768#note_386839633 # See thread: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/37768#note_386839633
GROUP_QUERY_RESULT_LIMIT = 1000.freeze 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) super(current_user: current_user, project: nil, ref_path: ref_path, start_date: start_date, end_date: end_date, limit: limit)
@group = group @group = group
......
...@@ -4,5 +4,5 @@ ...@@ -4,5 +4,5 @@
%h3 %h3
= _("Repositories Analytics") = _("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 } } 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 ...@@ -94,6 +94,17 @@ RSpec.describe Groups::Analytics::CoverageReportsController do
end end
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 it 'executes the same number of queries regardless of the number of records returned' do
control = ActiveRecord::QueryRecorder.new do control = ActiveRecord::QueryRecorder.new do
get :index, params: valid_request_params get :index, params: valid_request_params
......
...@@ -4,57 +4,77 @@ require 'spec_helper' ...@@ -4,57 +4,77 @@ require 'spec_helper'
RSpec.describe Ci::DailyBuildGroupReportResultsFinder do RSpec.describe Ci::DailyBuildGroupReportResultsFinder do
describe '#execute' do describe '#execute' do
let(:project) { create(:project, :private) } let_it_be(:project) { create(:project, :private) }
let(:ref_path) { 'refs/heads/master' } let_it_be(:current_user) { project.owner }
let_it_be(:ref_path) { 'refs/heads/master' }
let(:limit) { nil } let(:limit) { nil }
let_it_be(:default_branch) { false }
let!(:rspec_coverage_1) { create_daily_coverage('rspec', 79.0, '2020-03-09') } let_it_be(: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_it_be(: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_it_be(: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_it_be(: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_it_be(: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(:karma_coverage_3) { create_daily_coverage('karma', 99.0, '2020-03-11') }
subject do let(:attributes) do
described_class.new( {
current_user: current_user, current_user: current_user,
project: project, project: project,
ref_path: ref_path, ref_path: ref_path,
start_date: '2020-03-09', start_date: '2020-03-09',
end_date: '2020-03-10', end_date: '2020-03-10',
limit: limit limit: limit
).execute }
end end
context 'when current user is allowed to read build report results' do subject(:coverages) do
let(:current_user) { project.owner } 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 it 'returns limited number of matching results within the given date range' do
expect(subject).to match_array([ expect(coverages).to match_array([
karma_coverage_2, karma_coverage_2,
rspec_coverage_2, rspec_coverage_2
karma_coverage_1, ])
rspec_coverage_1 end
]) end
end end
context 'and limit is specified' do context 'when current user is not allowed to read build report results' do
let(:limit) { 2 } let(:current_user) { create(:user) }
it 'returns limited number of matching results within the given date range' do it 'returns an empty result' do
expect(subject).to match_array([ expect(coverages).to be_empty
karma_coverage_2,
rspec_coverage_2
])
end end
end end
end end
context 'when current user is not allowed to read build report results' do context 'when ref_path is not present' do
let(:current_user) { create(:user) } let(:ref_path) { nil }
it 'returns an empty result' do context 'when coverages exist for the default branch' do
expect(subject).to be_empty 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 end
end end
...@@ -65,10 +85,11 @@ RSpec.describe Ci::DailyBuildGroupReportResultsFinder do ...@@ -65,10 +85,11 @@ RSpec.describe Ci::DailyBuildGroupReportResultsFinder do
create( create(
:ci_daily_build_group_report_result, :ci_daily_build_group_report_result,
project: project, project: project,
ref_path: ref_path, ref_path: ref_path || 'feature-branch',
group_name: group_name, group_name: group_name,
data: { 'coverage' => coverage }, data: { 'coverage' => coverage },
date: date date: date,
default_branch: default_branch
) )
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