Commit 0cc9e3b6 authored by drew's avatar drew Committed by Mayra Cabrera

Add project_id filtering to DailyBuildGroupReportResultsByGroupFinder

Optional parameter that allows for group-level coverage report data
to be returned for specific projects within it.
parent 408a48e5
...@@ -39,6 +39,7 @@ class Groups::Analytics::CoverageReportsController < Groups::Analytics::Applicat ...@@ -39,6 +39,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],
ref_path: params.require(:ref_path), ref_path: params.require(: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,10 +9,11 @@ module Ci ...@@ -9,10 +9,11 @@ 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:, 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) 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
@project_ids = Array(project_ids)
@limit = GROUP_QUERY_RESULT_LIMIT unless limit && limit < GROUP_QUERY_RESULT_LIMIT @limit = GROUP_QUERY_RESULT_LIMIT unless limit && limit < GROUP_QUERY_RESULT_LIMIT
end end
...@@ -23,7 +24,15 @@ module Ci ...@@ -23,7 +24,15 @@ module Ci
end end
def query_params 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 end
end end
...@@ -109,6 +109,7 @@ module EE ...@@ -109,6 +109,7 @@ module EE
.limit(limit) .limit(limit)
end end
scope :including_project, ->(project) { where(id: project) }
scope :with_wiki_enabled, -> { with_feature_enabled(:wiki) } scope :with_wiki_enabled, -> { with_feature_enabled(:wiki) }
scope :within_shards, -> (shard_names) { where(repository_storage: Array(shard_names)) } scope :within_shards, -> (shard_names) { where(repository_storage: Array(shard_names)) }
scope :verification_failed_repos, -> { joins(:repository_state).merge(ProjectRepositoryState.verification_failed_repos) } 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 ...@@ -8,8 +8,8 @@ RSpec.describe Groups::Analytics::CoverageReportsController do
let(:project) { create(:project, namespace: group) } let(:project) { create(:project, namespace: group) }
let(:ref_path) { 'refs/heads/master' } let(:ref_path) { 'refs/heads/master' }
let!(:first_coverage) { create_daily_coverage('rspec', 79.0, '2020-03-09') } let!(:first_coverage) { create_daily_coverage('rspec', project, 79.0, '2020-03-09') }
let!(:last_coverage) { create_daily_coverage('karma', 95.0, '2020-03-10') } let!(:last_coverage) { create_daily_coverage('karma', project, 95.0, '2020-03-10') }
let(:valid_request_params) do let(:valid_request_params) do
{ {
...@@ -93,6 +93,23 @@ RSpec.describe Groups::Analytics::CoverageReportsController do ...@@ -93,6 +93,23 @@ RSpec.describe Groups::Analytics::CoverageReportsController do
]) ])
end 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 context 'with an invalid format' do
it 'responds 404' do it 'responds 404' do
get :index, params: valid_request_params.merge(format: :json) get :index, params: valid_request_params.merge(format: :json)
...@@ -105,7 +122,7 @@ RSpec.describe Groups::Analytics::CoverageReportsController do ...@@ -105,7 +122,7 @@ RSpec.describe Groups::Analytics::CoverageReportsController do
private private
def create_daily_coverage(group_name, coverage, date) def create_daily_coverage(group_name, project, coverage, date)
create( create(
:ci_daily_build_group_report_result, :ci_daily_build_group_report_result,
project: project, project: project,
......
...@@ -19,29 +19,32 @@ RSpec.describe Ci::DailyBuildGroupReportResultsByGroupFinder do ...@@ -19,29 +19,32 @@ RSpec.describe Ci::DailyBuildGroupReportResultsByGroupFinder do
let(:ref_path) { 'refs/heads/master' } let(:ref_path) { 'refs/heads/master' }
let(:limit) { nil } let(:limit) { nil }
let(:project_ids) { nil }
subject do let(:attributes) do
described_class.new( {
current_user: user, current_user: user,
group: group, group: group,
project_ids: project_ids,
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
subject do
described_class.new(attributes).execute
end end
context 'when current user is allowed to :read_group_build_report_results' do 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 before do
group.add_reporter(user) group.add_reporter(user)
end 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 context 'with a limit below 1000' do
let(:limit) { 5 } let(:limit) { 5 }
...@@ -63,6 +66,32 @@ RSpec.describe Ci::DailyBuildGroupReportResultsByGroupFinder do ...@@ -63,6 +66,32 @@ RSpec.describe Ci::DailyBuildGroupReportResultsByGroupFinder do
expect(subject.limit_value).to eq(Ci::DailyBuildGroupReportResultsByGroupFinder::GROUP_QUERY_RESULT_LIMIT) expect(subject.limit_value).to eq(Ci::DailyBuildGroupReportResultsByGroupFinder::GROUP_QUERY_RESULT_LIMIT)
end end
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 end
context 'without permmissions' do 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