Commit 54a61279 authored by Mayra Cabrera's avatar Mayra Cabrera

Merge branch 'group-coverage-project-filter' into 'master'

Group coverage project filter

See merge request gitlab-org/gitlab!40547
parents 1de46233 0cc9e3b6
......@@ -39,6 +39,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),
start_date: Date.parse(params.require(:start_date)),
end_date: Date.parse(params.require(:end_date))
......
......@@ -9,10 +9,11 @@ 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:, ref_path:, start_date:, end_date:, limit: nil)
def initialize(current_user:, group:, project_ids: [], ref_path:, 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
@project_ids = Array(project_ids)
@limit = GROUP_QUERY_RESULT_LIMIT unless limit && limit < GROUP_QUERY_RESULT_LIMIT
end
......@@ -23,7 +24,15 @@ module Ci
end
def query_params
super.merge(project_id: @group.projects.select(:id))
super.merge(project_id: project_id_subquery)
end
def project_id_subquery
if @project_ids.empty?
@group.projects.select(:id)
else
@group.projects.including_project(@project_ids).select(:id)
end
end
end
end
......@@ -109,6 +109,7 @@ module EE
.limit(limit)
end
scope :including_project, ->(project) { where(id: project) }
scope :with_wiki_enabled, -> { with_feature_enabled(:wiki) }
scope :within_shards, -> (shard_names) { where(repository_storage: Array(shard_names)) }
scope :verification_failed_repos, -> { joins(:repository_state).merge(ProjectRepositoryState.verification_failed_repos) }
......
---
title: 'Allow for project filtering in Group code coverage Finder class'
merge_request: 40547
author:
type: changed
......@@ -8,8 +8,8 @@ RSpec.describe Groups::Analytics::CoverageReportsController do
let(:project) { create(:project, namespace: group) }
let(:ref_path) { 'refs/heads/master' }
let!(:first_coverage) { create_daily_coverage('rspec', 79.0, '2020-03-09') }
let!(:last_coverage) { create_daily_coverage('karma', 95.0, '2020-03-10') }
let!(:first_coverage) { create_daily_coverage('rspec', project, 79.0, '2020-03-09') }
let!(:last_coverage) { create_daily_coverage('karma', project, 95.0, '2020-03-10') }
let(:valid_request_params) do
{
......@@ -93,6 +93,23 @@ RSpec.describe Groups::Analytics::CoverageReportsController do
])
end
context 'with a project_id filter' do
let(:params) { valid_request_params.merge(project_ids: [project.id]) }
it 'responds 200 with CSV coverage data' do
expect(Ci::DailyBuildGroupReportResultsByGroupFinder).to receive(:new).with({
group: group,
current_user: user,
project_ids: [project.id.to_s],
start_date: Date.parse('2020-03-01'),
end_date: Date.parse('2020-03-31'),
ref_path: ref_path
}).and_call_original
get :index, params: params
end
end
context 'with an invalid format' do
it 'responds 404' do
get :index, params: valid_request_params.merge(format: :json)
......@@ -105,7 +122,7 @@ RSpec.describe Groups::Analytics::CoverageReportsController do
private
def create_daily_coverage(group_name, coverage, date)
def create_daily_coverage(group_name, project, coverage, date)
create(
:ci_daily_build_group_report_result,
project: project,
......
......@@ -19,29 +19,32 @@ RSpec.describe Ci::DailyBuildGroupReportResultsByGroupFinder do
let(:ref_path) { 'refs/heads/master' }
let(:limit) { nil }
let(:project_ids) { nil }
subject do
described_class.new(
let(:attributes) do
{
current_user: user,
group: group,
project_ids: project_ids,
ref_path: ref_path,
start_date: '2020-03-09',
end_date: '2020-03-10',
limit: limit
).execute
}
end
subject do
described_class.new(attributes).execute
end
context 'when current user is allowed to :read_group_build_report_results' do
let(:excluded_group_project) { create(:project, namespace: group) }
let!(:excluded_coverage) { create_daily_coverage('unreported', 95.0, '2020-03-10', excluded_group_project) }
before do
group.add_reporter(user)
end
it 'returns only coverages belonging to the passed group' do
expect(subject).to include(group_project_coverage)
expect(subject).not_to include(project_coverage)
expect(subject).not_to include(subgroup_project_coverage)
end
context 'with a limit below 1000' do
let(:limit) { 5 }
......@@ -63,6 +66,32 @@ RSpec.describe Ci::DailyBuildGroupReportResultsByGroupFinder do
expect(subject.limit_value).to eq(Ci::DailyBuildGroupReportResultsByGroupFinder::GROUP_QUERY_RESULT_LIMIT)
end
end
context 'with nil project_ids' do
it 'returns only coverages belonging to the passed group' do
expect(subject).to include(group_project_coverage)
expect(subject).not_to include(project_coverage)
expect(subject).not_to include(subgroup_project_coverage)
end
end
context 'with passed project_ids' do
let(:project_ids) { [group_project.id] }
it 'filters out non-specified projects' do
expect(subject).to include(group_project_coverage)
expect(subject).not_to include(excluded_coverage)
end
end
context 'with empty project_ids' do
let(:project_ids) { [] }
it 'returns all projects' do
expect(subject).to include(group_project_coverage)
expect(subject).to include(excluded_coverage)
end
end
end
context 'without permmissions' do
......
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