Skip to content

Commit

Permalink
Fixes #37946 - Add 'allow_other_types' option in errata filters
Browse files Browse the repository at this point in the history
  • Loading branch information
qcjames53 committed Nov 21, 2024
1 parent c08f449 commit 6202076
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def resource_class
param :types, Array, :desc => N_("erratum: types (enhancement, bugfix, security)")
param :date_type, String, :desc => N_("erratum: search using the 'Issued On' or 'Updated On' column of the errata. Values are 'issued'/'updated'")
param :module_stream_ids, Array, :desc => N_("module stream ids")
param :allow_other_types, :bool, :desc => N_("erratum: allow types not matching a valid errata type")
def create
rule_clazz = ContentViewFilter.rule_class_for(@filter)

Expand Down Expand Up @@ -89,6 +90,7 @@ def show
param :start_date, String, :desc => N_("erratum: start date (YYYY-MM-DD)")
param :end_date, String, :desc => N_("erratum: end date (YYYY-MM-DD)")
param :types, Array, :desc => N_("erratum: types (enhancement, bugfix, security)")
param :allow_other_types, :bool, :desc => N_("erratum: allow types not matching a valid errata type")
def update
update_params = rule_params
update_params[:name] = update_params[:name].first if update_params[:name]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def remove_filter_rules
param :errata_ids, Array, :desc => N_("erratum: IDs or a select all object")
param :start_date, String, :desc => N_("erratum: start date (YYYY-MM-DD)")
param :end_date, String, :desc => N_("erratum: end date (YYYY-MM-DD)")
param :types, Array, :desc => N_("erratum: types (enhancement, bugfix, security)")
param :types, Array, :desc => N_("erratum: types (enhancement, bugfix, security, other)")
param :date_type, String, :desc => N_("erratum: search using the 'Issued On' or 'Updated On' column of the errata. Values are 'issued'/'updated'")
param :module_stream_ids, Array, :desc => N_("module stream ids")
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ module Katello
module Validators
class ContentViewErratumFilterRuleValidator < ActiveModel::Validator
def validate(record)
if record.errata_id.blank? && record.start_date.blank? && record.end_date.blank? && record.types.blank?
if record.errata_id.blank? && record.start_date.blank? && record.end_date.blank? && record.types.blank? && record.allow_other_types == false
invalid_parameters = _("Invalid erratum filter rule specified, Must specify at least one of the following:" \
" 'errata_id', 'start_date', 'end_date' or 'types'")
" 'errata_id', 'start_date', 'end_date', 'types', or 'allow_other_types'")
record.errors[:base] << invalid_parameters
return
end
Expand Down
20 changes: 18 additions & 2 deletions app/models/katello/content_view_erratum_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,21 @@ def erratum_arel
end

def types_clause
# Create an array to store output clauses for quick type filtering later
conditions = []

# Add clauses for types in the filter
types = erratum_rules.first.types
return if types.blank?
errata_types_in(types)
conditions << errata_types_in(types) unless types.blank?

# Add clauses for 'other' types
conditions << errata_types_not_in(Erratum::TYPES) if erratum_rules.first.allow_other_types?

# Reduce the array of clauses to a single clause and return
return if conditions.empty?
conditions.reduce(nil) do |combined_clause, condition|
combined_clause ? combined_clause.or(condition) : condition
end
end

def filter_by_id?
Expand All @@ -105,6 +117,10 @@ def errata_types_in(types)
erratum_arel[:errata_type].in(types)
end

def errata_types_not_in(types)
erratum_arel[:errata_type].not_in(types)
end

def errata_in(ids)
erratum_arel[:errata_id].in(ids)
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddAllowOtherTypesToContentViewErratumFilterRules < ActiveRecord::Migration[6.1]
def change
add_column :katello_content_view_erratum_filter_rules, :allow_other_types, :boolean,
:default => false, :null => false
end
end
17 changes: 10 additions & 7 deletions test/actions/katello/repository_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ class DestroyTest < TestBase
let(:in_use_repository) { katello_repositories(:fedora_17_no_arch) }
let(:published_repository) { katello_repositories(:rhel_6_x86_64) }
let(:published_fedora_repository) { katello_repositories(:fedora_17_x86_64) }
let(:published_rhel7_repository) { katello_repositories(:rhel_7_no_arch) }
let(:simplified_acs) { katello_alternate_content_sources(:yum_alternate_content_source) }
def setup
simplified_acs.products << published_repository.product
Expand Down Expand Up @@ -378,22 +379,24 @@ def setup
assert_not_equal(0, simplified_acs.products.length)
end

it 'plans ACS product removal when removing the deleting the last repo with URL' do
::Katello::SmartProxyAlternateContentSource.create!(alternate_content_source_id: simplified_acs.id, smart_proxy_id: proxy.id, repository_id: published_repository.id)
it 'plans ACS product removal when deleting the last repo with URL' do
::Katello::SmartProxyAlternateContentSource.create!(alternate_content_source_id: simplified_acs.id, smart_proxy_id: proxy.id, repository_id: published_rhel7_repository.id)
action = create_action action_class
action.stubs(:action_subject).with(published_repository)
action.stubs(:action_subject).with(published_rhel7_repository)

# manually remove the URLs from all repos in product except repository
repository.product.repositories.each do |repo|
repo.root.url = nil unless repo.id == repository.id
testing_repo = repository.product.repositories[4]
testing_repo.product.repositories.each do |repo|
repo.root.url = nil unless repo.id == testing_repo.id || repo.root.id == testing_repo.root.id
repo.root.save!(validate: false)
end
url_sum = repository.product.repositories.count do |repo|
url_sum = testing_repo.product.repositories.count do |repo|
repo.root.url.present?
end
assert_equal(1, url_sum) # double check there's only one URL left

# Since there is only one URL remaining, the product should be removed
plan_action action, published_repository, remove_from_content_view_versions: true
plan_action action, published_rhel7_repository, remove_from_content_view_versions: true
simplified_acs.reload
assert_equal(0, simplified_acs.products.length)
end
Expand Down
51 changes: 45 additions & 6 deletions test/models/content_view_erratum_filter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ def setup
@repo = katello_repositories(:fedora_17_x86_64)
end

TYPICAL_TYPES_RESPONSE =
" AND \"katello_errata\".\"errata_type\" IN ('bugfix', 'enhancement', 'security')".freeze

def test_erratum_by_id_returns_arel_for_specified_errata_id
erratum = katello_errata(:security)
@repo.errata = [erratum]
Expand All @@ -24,7 +27,7 @@ def test_errata_by_start_date_returns_arel_for_errata_by_updated_date_and_errata
filter = id_rule.filter
filter.reload

assert_equal "\"katello_errata\".\"updated\" >= '#{start_date}' AND \"katello_errata\".\"errata_type\" IN ('bugfix', 'enhancement', 'security')",
assert_equal "\"katello_errata\".\"updated\" >= '#{start_date}'" + TYPICAL_TYPES_RESPONSE,
filter.generate_clauses(@repo).to_sql
end

Expand All @@ -35,7 +38,7 @@ def test_errata_by_start_date_returns_arel_for_errata_by_issued_date_and_errata_
filter = id_rule.filter
filter.reload

assert_equal "\"katello_errata\".\"issued\" >= '#{start_date}' AND \"katello_errata\".\"errata_type\" IN ('bugfix', 'enhancement', 'security')",
assert_equal "\"katello_errata\".\"issued\" >= '#{start_date}'" + TYPICAL_TYPES_RESPONSE,
filter.generate_clauses(@repo).to_sql
end

Expand All @@ -45,7 +48,7 @@ def test_errata_by_end_date_returns_arel_for_errata_by_updated_date_and_errata_t
filter = id_rule.filter
filter.reload

assert_equal "\"katello_errata\".\"updated\" <= '#{end_date}' AND \"katello_errata\".\"errata_type\" IN ('bugfix', 'enhancement', 'security')",
assert_equal "\"katello_errata\".\"updated\" <= '#{end_date}'" + TYPICAL_TYPES_RESPONSE,
filter.generate_clauses(@repo).to_sql
end

Expand All @@ -56,7 +59,7 @@ def test_errata_by_end_date_returns_arel_for_errata_by_issued_date_and_errata_ty
filter = id_rule.filter
filter.reload

assert_equal "\"katello_errata\".\"issued\" <= '#{end_date}' AND \"katello_errata\".\"errata_type\" IN ('bugfix', 'enhancement', 'security')",
assert_equal "\"katello_errata\".\"issued\" <= '#{end_date}'" + TYPICAL_TYPES_RESPONSE,
filter.generate_clauses(@repo).to_sql
end

Expand All @@ -69,6 +72,16 @@ def test_errata_by_type_returns_arel_by_errata_type
filter.generate_clauses(@repo).to_sql
end

def test_errata_by_type_returns_arel_by_errata_type_other
id_rule = FactoryBot.create(:katello_content_view_erratum_filter_rule, :allow_other_types => true)
id_rule.update!(types: [])
filter = id_rule.filter
filter.reload

assert_equal "\"katello_errata\".\"errata_type\" NOT IN ('security', 'bugfix', 'recommended', 'enhancement', 'optional')",
filter.generate_clauses(@repo).to_sql
end

def test_content_unit_pulp_ids_with_empty_errata_list_returns_empty_result
rpm1 = @repo.rpms.first
rpm2 = @repo.rpms.last
Expand Down Expand Up @@ -209,13 +222,16 @@ def test_content_unit_pulp_ids_by_issued_end_date_returns_pulp_hrefs
end

def test_content_unit_pulp_ids_by_errata_type
rpm1 = @repo.rpms.first
rpm2 = @repo.rpms.last
rpm1 = @repo.rpms[0]
rpm2 = @repo.rpms[1]
rpm3 = @repo.rpms[2]

erratum1 = Katello::Erratum.new(:pulp_id => "one", :errata_id => "ERRATA1", :errata_type => 'bugfix')
erratum1.packages << Katello::ErratumPackage.new(:filename => rpm1.filename, :name => "e1", :nvrea => "e1")
erratum2 = Katello::Erratum.new(:pulp_id => "two", :errata_id => "ERRATA2", :errata_type => 'security')
erratum2.packages << Katello::ErratumPackage.new(:filename => rpm2.filename, :name => "e2", :nvrea => "e2")
erratum3 = Katello::Erratum.new(:pulp_id => "three", :errata_id => "ERRATA3", :errata_type => 'not_recognized')
erratum3.packages << Katello::ErratumPackage.new(:filename => rpm3.filename, :name => "e3", :nvrea => "e3")

@repo.errata = [erratum2]
@repo.save!
Expand All @@ -226,5 +242,28 @@ def test_content_unit_pulp_ids_by_errata_type

assert_equal [rpm2.pulp_id], filter.content_unit_pulp_ids(@repo)
end

def test_content_unit_pulp_ids_by_errata_type_other
rpm1 = @repo.rpms[0]
rpm2 = @repo.rpms[1]
rpm3 = @repo.rpms[2]

erratum1 = Katello::Erratum.new(:pulp_id => "one", :errata_id => "ERRATA1", :errata_type => 'bugfix')
erratum1.packages << Katello::ErratumPackage.new(:filename => rpm1.filename, :name => "e1", :nvrea => "e1")
erratum2 = Katello::Erratum.new(:pulp_id => "two", :errata_id => "ERRATA2", :errata_type => 'security')
erratum2.packages << Katello::ErratumPackage.new(:filename => rpm2.filename, :name => "e2", :nvrea => "e2")
erratum3 = Katello::Erratum.new(:pulp_id => "three", :errata_id => "ERRATA3", :errata_type => 'not_recognized')
erratum3.packages << Katello::ErratumPackage.new(:filename => rpm3.filename, :name => "e3", :nvrea => "e3")

@repo.errata = [erratum3]
@repo.save!

id_rule = FactoryBot.create(:katello_content_view_erratum_filter_rule, :allow_other_types => true)
id_rule.update!(types: [])
filter = id_rule.filter
filter.reload

assert_equal [rpm3.pulp_id], filter.content_unit_pulp_ids(@repo)
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,15 @@ const CVErrataDateFilterContent = ({
{__('Bugfix')}
</p>
</SelectOption>
<SelectOption
isDisabled={!hasPermission(permissions, 'edit_content_views')}
key="other"
value="other"
>
<p style={{ marginTop: '4px' }}>
{__('Other')}
</p>
</SelectOption>
</Select>
</FlexItem>
<FlexItem span={1} spacer={{ default: 'spacerNone' }}>
Expand Down

0 comments on commit 6202076

Please sign in to comment.