Skip to content

Commit 41195d9

Browse files
committed
Fixes #37946 - Add 'allow_other_types' option in errata filters
1 parent 9c98533 commit 41195d9

File tree

7 files changed

+111
-18
lines changed

7 files changed

+111
-18
lines changed

app/controllers/katello/api/v2/content_view_filter_rules_controller.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def resource_class
4040
param :types, Array, :desc => N_("erratum: types (enhancement, bugfix, security)")
4141
param :date_type, String, :desc => N_("erratum: search using the 'Issued On' or 'Updated On' column of the errata. Values are 'issued'/'updated'")
4242
param :module_stream_ids, Array, :desc => N_("module stream ids")
43+
param :allow_other_types, :bool, :desc => N_("erratum: allow types not matching a valid errata type")
4344
def create
4445
rule_clazz = ContentViewFilter.rule_class_for(@filter)
4546

@@ -89,6 +90,7 @@ def show
8990
param :start_date, String, :desc => N_("erratum: start date (YYYY-MM-DD)")
9091
param :end_date, String, :desc => N_("erratum: end date (YYYY-MM-DD)")
9192
param :types, Array, :desc => N_("erratum: types (enhancement, bugfix, security)")
93+
param :allow_other_types, :bool, :desc => N_("erratum: allow types not matching a valid errata type")
9294
def update
9395
update_params = rule_params
9496
update_params[:name] = update_params[:name].first if update_params[:name]
@@ -136,7 +138,7 @@ def rule_params
136138

137139
@rule_params ||= params.fetch(:content_view_filter_rule, {}).
138140
permit(:uuid, :version, :min_version, :max_version, :architecture,
139-
:errata_id, :start_date, :end_date, :date_type,
141+
:errata_id, :start_date, :end_date, :date_type, :allow_other_types,
140142
:types => [], :module_stream_ids => [], :errata_ids => [], name: [])
141143
end
142144

app/lib/katello/validators/content_view_erratum_filter_rule_validator.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ module Katello
22
module Validators
33
class ContentViewErratumFilterRuleValidator < ActiveModel::Validator
44
def validate(record)
5-
if record.errata_id.blank? && record.start_date.blank? && record.end_date.blank? && record.types.blank?
5+
if record.errata_id.blank? && record.start_date.blank? && record.end_date.blank? && record.types.blank? && record.allow_other_types == false
66
invalid_parameters = _("Invalid erratum filter rule specified, Must specify at least one of the following:" \
7-
" 'errata_id', 'start_date', 'end_date' or 'types'")
7+
" 'errata_id', 'start_date', 'end_date', 'types', or 'allow_other_types'")
88
record.errors.add(:base, invalid_parameters)
99
return
1010
end

app/models/katello/content_view_erratum_filter.rb

+18-2
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,21 @@ def erratum_arel
9292
end
9393

9494
def types_clause
95+
# Create an array to store output clauses for quick type filtering later
96+
conditions = []
97+
98+
# Add clauses for types in the filter
9599
types = erratum_rules.first.types
96-
return if types.blank?
97-
errata_types_in(types)
100+
conditions << errata_types_in(types) unless types.blank?
101+
102+
# Add clauses for 'other' types
103+
conditions << errata_types_not_in(Erratum::TYPES) if erratum_rules.first.allow_other_types?
104+
105+
# Reduce the array of clauses to a single clause and return
106+
return if conditions.empty?
107+
conditions.reduce(nil) do |combined_clause, condition|
108+
combined_clause ? combined_clause.or(condition) : condition
109+
end
98110
end
99111

100112
def filter_by_id?
@@ -105,6 +117,10 @@ def errata_types_in(types)
105117
erratum_arel[:errata_type].in(types)
106118
end
107119

120+
def errata_types_not_in(types)
121+
erratum_arel[:errata_type].not_in(types)
122+
end
123+
108124
def errata_in(ids)
109125
erratum_arel[:errata_id].in(ids)
110126
end

app/views/katello/api/v2/content_view_filter_rules/show.json.rabl

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ attributes :start_date, :if => lambda { |rule| rule.respond_to?(:start_date) &&
1313
attributes :end_date, :if => lambda { |rule| rule.respond_to?(:end_date) && !rule.end_date.blank? }
1414
attributes :architecture, :if => lambda { |rule| rule.respond_to?(:architecture) && !rule.architecture.blank? }
1515
attributes :types, :if => lambda { |rule| rule.respond_to?(:types) && !rule.types.blank? }
16+
attributes :allow_other_types, :if => lambda { |rule| rule.respond_to?(:allow_other_types) }
1617
attributes :date_type, :if => lambda { |rule| rule.respond_to?(:date_type) }
1718
attributes :module_stream_id, :if => lambda { |rule| rule.respond_to?(:module_stream_id) && !rule.module_stream_id.blank? }
1819
if @resource&.try(:module_stream)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class AddAllowOtherTypesToContentViewErratumFilterRules < ActiveRecord::Migration[6.1]
2+
def change
3+
add_column :katello_content_view_erratum_filter_rules, :allow_other_types, :boolean,
4+
:default => false, :null => false
5+
end
6+
end

test/models/content_view_erratum_filter_test.rb

+45-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ def setup
66
@repo = katello_repositories(:fedora_17_x86_64)
77
end
88

9+
TYPICAL_TYPES_RESPONSE =
10+
" AND \"katello_errata\".\"errata_type\" IN ('bugfix', 'enhancement', 'security')".freeze
11+
912
def test_erratum_by_id_returns_arel_for_specified_errata_id
1013
erratum = katello_errata(:security)
1114
@repo.errata = [erratum]
@@ -24,7 +27,7 @@ def test_errata_by_start_date_returns_arel_for_errata_by_updated_date_and_errata
2427
filter = id_rule.filter
2528
filter.reload
2629

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

@@ -35,7 +38,7 @@ def test_errata_by_start_date_returns_arel_for_errata_by_issued_date_and_errata_
3538
filter = id_rule.filter
3639
filter.reload
3740

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

@@ -45,7 +48,7 @@ def test_errata_by_end_date_returns_arel_for_errata_by_updated_date_and_errata_t
4548
filter = id_rule.filter
4649
filter.reload
4750

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

@@ -56,7 +59,7 @@ def test_errata_by_end_date_returns_arel_for_errata_by_issued_date_and_errata_ty
5659
filter = id_rule.filter
5760
filter.reload
5861

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

@@ -69,6 +72,16 @@ def test_errata_by_type_returns_arel_by_errata_type
6972
filter.generate_clauses(@repo).to_sql
7073
end
7174

75+
def test_errata_by_type_returns_arel_by_errata_type_other
76+
id_rule = FactoryBot.create(:katello_content_view_erratum_filter_rule, :allow_other_types => true)
77+
id_rule.update!(types: [])
78+
filter = id_rule.filter
79+
filter.reload
80+
81+
assert_equal "\"katello_errata\".\"errata_type\" NOT IN ('security', 'bugfix', 'recommended', 'enhancement', 'optional')",
82+
filter.generate_clauses(@repo).to_sql
83+
end
84+
7285
def test_content_unit_pulp_ids_with_empty_errata_list_returns_empty_result
7386
rpm1 = @repo.rpms.first
7487
rpm2 = @repo.rpms.last
@@ -209,13 +222,16 @@ def test_content_unit_pulp_ids_by_issued_end_date_returns_pulp_hrefs
209222
end
210223

211224
def test_content_unit_pulp_ids_by_errata_type
212-
rpm1 = @repo.rpms.first
213-
rpm2 = @repo.rpms.last
225+
rpm1 = @repo.rpms[0]
226+
rpm2 = @repo.rpms[1]
227+
rpm3 = @repo.rpms[2]
214228

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

220236
@repo.errata = [erratum2]
221237
@repo.save!
@@ -226,5 +242,28 @@ def test_content_unit_pulp_ids_by_errata_type
226242

227243
assert_equal [rpm2.pulp_id], filter.content_unit_pulp_ids(@repo)
228244
end
245+
246+
def test_content_unit_pulp_ids_by_errata_type_other
247+
rpm1 = @repo.rpms[0]
248+
rpm2 = @repo.rpms[1]
249+
rpm3 = @repo.rpms[2]
250+
251+
erratum1 = Katello::Erratum.new(:pulp_id => "one", :errata_id => "ERRATA1", :errata_type => 'bugfix')
252+
erratum1.packages << Katello::ErratumPackage.new(:filename => rpm1.filename, :name => "e1", :nvrea => "e1")
253+
erratum2 = Katello::Erratum.new(:pulp_id => "two", :errata_id => "ERRATA2", :errata_type => 'security')
254+
erratum2.packages << Katello::ErratumPackage.new(:filename => rpm2.filename, :name => "e2", :nvrea => "e2")
255+
erratum3 = Katello::Erratum.new(:pulp_id => "three", :errata_id => "ERRATA3", :errata_type => 'not_recognized')
256+
erratum3.packages << Katello::ErratumPackage.new(:filename => rpm3.filename, :name => "e3", :nvrea => "e3")
257+
258+
@repo.errata = [erratum3]
259+
@repo.save!
260+
261+
id_rule = FactoryBot.create(:katello_content_view_erratum_filter_rule, :allow_other_types => true)
262+
id_rule.update!(types: [])
263+
filter = id_rule.filter
264+
filter.reload
265+
266+
assert_equal [rpm3.pulp_id], filter.content_unit_pulp_ids(@repo)
267+
end
229268
end
230269
end

webpack/scenes/ContentViews/Details/Filters/CVErrataDateFilterContent.js

+36-7
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,33 @@ const CVErrataDateFilterContent = ({
4646
selectCVFilterDetails(state, cvId, filterId), shallowEqual);
4747
const { repositories = [], rules } = filterDetails;
4848
const [{
49-
id, types, start_date: ruleStartDate, end_date: ruleEndDate, date_type: ruleDateType,
49+
id,
50+
types,
51+
allow_other_types: ruleAllowOtherTypes,
52+
start_date: ruleStartDate,
53+
end_date: ruleEndDate,
54+
date_type: ruleDateType,
5055
} = {}] = rules;
5156
const { permissions } = details;
5257
const [startDate, setStartDate] = useState(convertAPIDateToUIFormat(ruleStartDate));
5358
const [endDate, setEndDate] = useState(convertAPIDateToUIFormat(ruleEndDate));
5459
const [dateType, setDateType] = useState(ruleDateType);
5560
const [dateTypeSelectOpen, setDateTypeSelectOpen] = useState(false);
5661
const [typeSelectOpen, setTypeSelectOpen] = useState(false);
57-
const [selectedTypes, setSelectedTypes] = useState(types);
5862
const dispatch = useDispatch();
5963
const [activeTabKey, setActiveTabKey] = useState(0);
6064
const [startEntry, setStartEntry] = useState(false);
6165
const [endEntry, setEndEntry] = useState(false);
6266

67+
const getInitialSelectedTypes = () => {
68+
if (ruleAllowOtherTypes) {
69+
return [...types, 'other'];
70+
}
71+
return types;
72+
};
73+
74+
const [selectedTypes, setSelectedTypes] = useState(getInitialSelectedTypes());
75+
6376
const onSave = () => {
6477
dispatch(editCVFilterRule(
6578
filterId,
@@ -68,8 +81,9 @@ const CVErrataDateFilterContent = ({
6881
content_view_filter_id: filterId,
6982
start_date: startDate && startDate !== '' ? dateParse(startDate) : null,
7083
end_date: endDate && endDate !== '' ? dateParse(endDate) : null,
71-
types: selectedTypes,
84+
types: selectedTypes.filter(e => e !== 'other'),
7285
date_type: dateType,
86+
allow_other_types: selectedTypes.includes('other'),
7387
},
7488
() => {
7589
dispatch({ type: CONTENT_VIEW_NEEDS_PUBLISH });
@@ -81,15 +95,21 @@ const CVErrataDateFilterContent = ({
8195
const resetFilters = () => {
8296
setStartDate(convertAPIDateToUIFormat(ruleStartDate));
8397
setEndDate(convertAPIDateToUIFormat(ruleEndDate));
84-
setSelectedTypes(types);
8598
setDateType(ruleDateType);
99+
setSelectedTypes(getInitialSelectedTypes());
86100
};
87101

88102
const onTypeSelect = (selection) => {
89103
if (selectedTypes.includes(selection)) {
104+
// If the selection is the only selection remaining, do not allow it to be removed
90105
if (selectedTypes.length === 1) return;
106+
107+
// Filter out the current selection to deselect it
91108
setSelectedTypes(selectedTypes.filter(e => e !== selection));
92-
} else setSelectedTypes([...selectedTypes, selection]);
109+
} else {
110+
// Add the selection to the selected types
111+
setSelectedTypes([...selectedTypes, selection]);
112+
}
93113
};
94114

95115
const singleSelection = selection => (selectedTypes.length === 1
@@ -99,7 +119,7 @@ const CVErrataDateFilterContent = ({
99119
(
100120
isEqual(convertAPIDateToUIFormat(ruleStartDate), startDate) &&
101121
isEqual(convertAPIDateToUIFormat(ruleEndDate), endDate) &&
102-
isEqual(sortBy(types), sortBy(selectedTypes)) &&
122+
isEqual(sortBy(getInitialSelectedTypes()), sortBy(selectedTypes)) &&
103123
isEqual(ruleDateType, dateType)
104124
);
105125

@@ -171,14 +191,23 @@ const CVErrataDateFilterContent = ({
171191
{__('Bugfix')}
172192
</p>
173193
</SelectOption>
194+
<SelectOption
195+
isDisabled={singleSelection('other') || !hasPermission(permissions, 'edit_content_views')}
196+
key="other"
197+
value="other"
198+
>
199+
<p style={{ marginTop: '4px' }}>
200+
{__('Other')}
201+
</p>
202+
</SelectOption>
174203
</Select>
175204
</FlexItem>
176205
<FlexItem span={1} spacer={{ default: 'spacerNone' }}>
177206
{(selectedTypes.length === 1) &&
178207
<Tooltip
179208
position="top"
180209
content={
181-
__('Atleast one errata type needs to be selected.')
210+
__('At least one errata type option needs to be selected.')
182211
}
183212
>
184213
<OutlinedQuestionCircleIcon />

0 commit comments

Comments
 (0)