Commit c98721bf authored by Mikołaj Wawrzyniak's avatar Mikołaj Wawrzyniak

Merge branch 'eb-projects-with-coverage-new-query' into 'master'

Query new ci feature usage table for coverages

See merge request gitlab-org/gitlab!69890
parents 6d274826 bff44cbe
...@@ -380,6 +380,7 @@ class Project < ApplicationRecord ...@@ -380,6 +380,7 @@ class Project < ApplicationRecord
has_many :jira_imports, -> { order 'jira_imports.created_at' }, class_name: 'JiraImportState', inverse_of: :project has_many :jira_imports, -> { order 'jira_imports.created_at' }, class_name: 'JiraImportState', inverse_of: :project
has_many :daily_build_group_report_results, class_name: 'Ci::DailyBuildGroupReportResult' has_many :daily_build_group_report_results, class_name: 'Ci::DailyBuildGroupReportResult'
has_many :ci_feature_usages, class_name: 'Projects::CiFeatureUsage'
has_many :repository_storage_moves, class_name: 'Projects::RepositoryStorageMove', inverse_of: :container has_many :repository_storage_moves, class_name: 'Projects::RepositoryStorageMove', inverse_of: :container
......
---
name: query_project_ci_feature_usages_for_coverage
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/69890
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/339974
milestone: '14.3'
type: development
group: group::testing
default_enabled: false
...@@ -39,7 +39,11 @@ module EE ...@@ -39,7 +39,11 @@ module EE
def with_code_coverage(items) def with_code_coverage(items)
return items unless params[:has_code_coverage].present? return items unless params[:has_code_coverage].present?
items.with_code_coverage if ::Feature.enabled?(:query_project_ci_feature_usages_for_coverage, namespace, default_enabled: :yaml)
items.with_coverage_feature_usage(default_branch: true)
else
items.with_code_coverage
end
end end
end end
end end
......
...@@ -125,6 +125,13 @@ module EE ...@@ -125,6 +125,13 @@ module EE
joins(:daily_build_group_report_results).merge(::Ci::DailyBuildGroupReportResult.with_coverage.with_default_branch).group(:id) joins(:daily_build_group_report_results).merge(::Ci::DailyBuildGroupReportResult.with_coverage.with_default_branch).group(:id)
end end
scope :with_coverage_feature_usage, ->(default_branch: nil) do
join_conditions = { feature: :code_coverage }
join_conditions[:default_branch] = default_branch unless default_branch.nil?
joins(:ci_feature_usages).where(ci_feature_usages: join_conditions).group(:id)
end
scope :including_project, ->(project) { where(id: project) } 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)) }
......
...@@ -59,13 +59,46 @@ RSpec.describe Namespaces::ProjectsFinder do ...@@ -59,13 +59,46 @@ RSpec.describe Namespaces::ProjectsFinder do
end end
context 'has_code_coverage' do context 'has_code_coverage' do
let_it_be(:coverage_1) { create(:ci_daily_build_group_report_result, project: project_1) }
context 'when has_code_coverage is provided' do context 'when has_code_coverage is provided' do
let(:params) { { has_code_coverage: true } } let(:params) { { has_code_coverage: true } }
it 'returns projects with code coverage' do before_all do
expect(projects).to contain_exactly(project_1) create(:project_ci_feature_usage, feature: :code_coverage, project: project_1, default_branch: true)
create(:ci_daily_build_group_report_result, project: project_1, default_branch: true)
create(:project_ci_feature_usage, feature: :code_coverage, project: project_2, default_branch: false)
create(:ci_daily_build_group_report_result, project: project_2, default_branch: false)
end
context 'and query_project_ci_feature_usages_for_coverage flag is enabled for the given group' do
before do
stub_feature_flags(query_project_ci_feature_usages_for_coverage: namespace)
end
it 'returns projects with code coverage on default branch based on ci feature usages' do
record = ActiveRecord::QueryRecorder.new do
expect(projects).to contain_exactly(project_1)
end
queried_ci_table = record.log.any? {|l| l.include?('ci_daily_build_group_report_results')}
expect(queried_ci_table).to eq(false)
end
end
context 'and query_project_ci_feature_usages_for_coverage flag is disabled for the given group' do
before do
stub_feature_flags(query_project_ci_feature_usages_for_coverage: create(:group))
end
it 'returns projects with code coverage on default branch based on daily build group report results' do
record = ActiveRecord::QueryRecorder.new do
expect(projects).to contain_exactly(project_1)
end
queried_ci_table = record.log.any? {|l| l.include?('ci_daily_build_group_report_results')}
expect(queried_ci_table).to eq(true)
end
end end
end end
......
...@@ -61,9 +61,8 @@ RSpec.describe Resolvers::NamespaceProjectsResolver do ...@@ -61,9 +61,8 @@ RSpec.describe Resolvers::NamespaceProjectsResolver do
context 'has_code_coverage' do context 'has_code_coverage' do
subject(:projects) { resolve_projects(has_code_coverage: has_code_coverage) } subject(:projects) { resolve_projects(has_code_coverage: has_code_coverage) }
let!(:coverage_1) { create(:ci_daily_build_group_report_result, project: project_1) }
context 'when has_code_coverage is false' do context 'when has_code_coverage is false' do
let!(:coverage_1) { create(:ci_daily_build_group_report_result, project: project_1) }
let(:has_code_coverage) { false } let(:has_code_coverage) { false }
it { is_expected.to contain_exactly(project_1, project_2) } it { is_expected.to contain_exactly(project_1, project_2) }
...@@ -72,7 +71,44 @@ RSpec.describe Resolvers::NamespaceProjectsResolver do ...@@ -72,7 +71,44 @@ RSpec.describe Resolvers::NamespaceProjectsResolver do
context 'when has_code_coverage is true' do context 'when has_code_coverage is true' do
let(:has_code_coverage) { true } let(:has_code_coverage) { true }
it { is_expected.to contain_exactly(project_1) } before do
create(:project_ci_feature_usage, feature: :code_coverage, project: project_1, default_branch: true)
create(:ci_daily_build_group_report_result, project: project_1, default_branch: true)
create(:project_ci_feature_usage, feature: :code_coverage, project: project_2, default_branch: false)
create(:ci_daily_build_group_report_result, project: project_2, default_branch: false)
end
context 'and query_project_ci_feature_usages_for_coverage flag is enabled for the given group' do
before do
stub_feature_flags(query_project_ci_feature_usages_for_coverage: group)
end
it 'returns projects with code coverage on default branch based on ci feature usages' do
record = ActiveRecord::QueryRecorder.new do
expect(projects).to contain_exactly(project_1)
end
queried_ci_table = record.log.any? {|l| l.include?('ci_daily_build_group_report_results')}
expect(queried_ci_table).to eq(false)
end
end
context 'and query_project_ci_feature_usages_for_coverage flag is disabled for the given group' do
before do
stub_feature_flags(query_project_ci_feature_usages_for_coverage: false)
end
it 'returns projects with code coverage on default branch based on daily build group report results' do
record = ActiveRecord::QueryRecorder.new do
expect(projects).to contain_exactly(project_1)
end
queried_ci_table = record.log.any? {|l| l.include?('ci_daily_build_group_report_results')}
expect(queried_ci_table).to eq(true)
end
end
end end
end end
end end
......
...@@ -404,6 +404,37 @@ RSpec.describe Project do ...@@ -404,6 +404,37 @@ RSpec.describe Project do
it { is_expected.to contain_exactly(project_1, project_2) } it { is_expected.to contain_exactly(project_1, project_2) }
end end
describe '.with_coverage_feature_usage' do
let_it_be(:project_1) { create(:project) }
let_it_be(:project_2) { create(:project) }
let_it_be(:project_3) { create(:project) }
before_all do
create(:project_ci_feature_usage, feature: :code_coverage, project: project_1, default_branch: true)
create(:project_ci_feature_usage, feature: :code_coverage, project: project_1, default_branch: false)
create(:project_ci_feature_usage, feature: :code_coverage, project: project_2, default_branch: false)
create(:project_ci_feature_usage, feature: :security_report, project: project_3, default_branch: true)
end
context 'when default_branch is not specified' do
subject { described_class.with_coverage_feature_usage }
it { is_expected.to contain_exactly(project_1, project_2) }
end
context 'when default_branch is set to true' do
subject { described_class.with_coverage_feature_usage(default_branch: true) }
it { is_expected.to contain_exactly(project_1) }
end
context 'when default_branch is set to false' do
subject { described_class.with_coverage_feature_usage(default_branch: false) }
it { is_expected.to contain_exactly(project_1, project_2) }
end
end
end end
describe 'validations' do describe 'validations' do
......
...@@ -592,6 +592,7 @@ project: ...@@ -592,6 +592,7 @@ project:
- error_tracking_client_keys - error_tracking_client_keys
- pending_builds - pending_builds
- security_scans - security_scans
- ci_feature_usages
award_emoji: award_emoji:
- awardable - awardable
- user - user
......
...@@ -139,6 +139,7 @@ RSpec.describe Project, factory_default: :keep do ...@@ -139,6 +139,7 @@ RSpec.describe Project, factory_default: :keep do
it { is_expected.to have_many(:error_tracking_errors).class_name('ErrorTracking::Error') } it { is_expected.to have_many(:error_tracking_errors).class_name('ErrorTracking::Error') }
it { is_expected.to have_many(:error_tracking_client_keys).class_name('ErrorTracking::ClientKey') } it { is_expected.to have_many(:error_tracking_client_keys).class_name('ErrorTracking::ClientKey') }
it { is_expected.to have_many(:pending_builds).class_name('Ci::PendingBuild') } it { is_expected.to have_many(:pending_builds).class_name('Ci::PendingBuild') }
it { is_expected.to have_many(:ci_feature_usages).class_name('Projects::CiFeatureUsage') }
# GitLab Pages # GitLab Pages
it { is_expected.to have_many(:pages_domains) } it { is_expected.to have_many(:pages_domains) }
......
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