Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.

Commit 5f24cb4

Browse files
fix: Filter components by id in addition to name (#1013)
1 parent 1d7539e commit 5f24cb4

File tree

6 files changed

+75
-9
lines changed

6 files changed

+75
-9
lines changed

api/internal/coverage/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def get_object(self) -> ReportPaths:
4343
component_paths = []
4444
if components:
4545
all_components = components_service.commit_components(commit, self.owner)
46-
filtered_components = components_service.filter_components_by_name(
46+
filtered_components = components_service.filter_components_by_name_or_id(
4747
all_components, components
4848
)
4949

compare/commands/compare/interactors/fetch_impacted_files.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def _apply_filters(
4545
all_components = components.commit_components(
4646
comparison.head_commit, comparison.user
4747
)
48-
filtered_components = components.filter_components_by_name(
48+
filtered_components = components.filter_components_by_name_or_id(
4949
all_components, components_filter
5050
)
5151
for component in filtered_components:

graphql_api/types/commit/commit.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def resolve_path_contents(commit: Commit, info, path: str = None, filters=None):
178178

179179
if component_filter:
180180
all_components = components_service.commit_components(commit, current_owner)
181-
filtered_components = components_service.filter_components_by_name(
181+
filtered_components = components_service.filter_components_by_name_or_id(
182182
all_components, component_filter
183183
)
184184

@@ -268,7 +268,7 @@ def resolve_deprecated_path_contents(
268268

269269
if component_filter:
270270
all_components = components_service.commit_components(commit, current_owner)
271-
filtered_components = components_service.filter_components_by_name(
271+
filtered_components = components_service.filter_components_by_name_or_id(
272272
all_components, component_filter
273273
)
274274

@@ -394,7 +394,7 @@ def resolve_coverage_file(commit, info, path, flags=None, components=None):
394394
all_components = components_service.commit_components(
395395
commit, info.context["request"].current_owner
396396
)
397-
filtered_components = components_service.filter_components_by_name(
397+
filtered_components = components_service.filter_components_by_name_or_id(
398398
all_components, components
399399
)
400400
for fc in filtered_components:
@@ -423,7 +423,7 @@ def resolve_coverage_components(commit: Commit, info, filters=None) -> List[Comp
423423
all_components = components_service.commit_components(commit, current_owner)
424424

425425
if filters and filters.get("components"):
426-
return components_service.filter_components_by_name(
426+
return components_service.filter_components_by_name_or_id(
427427
all_components, filters["components"]
428428
)
429429

graphql_api/types/comparison/comparison.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def resolve_component_comparisons(
203203
list_components = comparison_report.commit_comparison.component_comparisons.all()
204204

205205
if filters and filters.get("components"):
206-
components = components_service.filter_components_by_name(
206+
components = components_service.filter_components_by_name_or_id(
207207
components, filters["components"]
208208
)
209209

services/components.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,20 @@ def component_filtered_report(
3939
return filtered_report
4040

4141

42-
def filter_components_by_name(
42+
def filter_components_by_name_or_id(
4343
components: List[Component], terms: List[str]
4444
) -> List[Component]:
4545
"""
4646
Given a list of Components and a list of strings (terms),
4747
return a new list of Components only including Components with names in terms (case insensitive)
48+
OR component_id in terms (case insensitive)
4849
"""
4950
terms = [v.lower() for v in terms]
50-
return list(filter(lambda c: c.name.lower() in terms, components))
51+
return [
52+
component
53+
for component in components
54+
if component.name.lower() in terms or component.component_id.lower() in terms
55+
]
5156

5257

5358
class ComponentComparison:

services/tests/test_components.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
ComponentComparison,
1818
commit_components,
1919
component_filtered_report,
20+
filter_components_by_name_or_id,
2021
)
2122

2223

@@ -221,3 +222,63 @@ def test_patch_totals(self, head_report_mock, git_comparison_mock):
221222

222223
# removed 1 tested line, added 1 tested and 1 untested line
223224
assert component_comparison.patch_totals.coverage == "50.00000"
225+
226+
def test_filter_components_by_name_or_id(self):
227+
components = [
228+
Component(
229+
name="ComponentA",
230+
component_id="123",
231+
paths=[],
232+
flag_regexes=[],
233+
statuses=[],
234+
),
235+
Component(
236+
name="ComponentB",
237+
component_id="456",
238+
paths=[],
239+
flag_regexes=[],
240+
statuses=[],
241+
),
242+
Component(
243+
name="ComponentC",
244+
component_id="789",
245+
paths=[],
246+
flag_regexes=[],
247+
statuses=[],
248+
),
249+
]
250+
terms = ["comPOnentA", "123", "456"]
251+
252+
filtered = filter_components_by_name_or_id(components, terms)
253+
self.assertEqual(len(filtered), 2)
254+
self.assertEqual(filtered[0].name, "ComponentA")
255+
self.assertEqual(filtered[1].component_id, "456")
256+
257+
def test_filter_components_by_name_or_id_no_matches(self):
258+
components = [
259+
Component(
260+
name="ComponentA",
261+
component_id="123",
262+
paths=[],
263+
flag_regexes=[],
264+
statuses=[],
265+
),
266+
Component(
267+
name="ComponentB",
268+
component_id="456",
269+
paths=[],
270+
flag_regexes=[],
271+
statuses=[],
272+
),
273+
Component(
274+
name="ComponentC",
275+
component_id="789",
276+
paths=[],
277+
flag_regexes=[],
278+
statuses=[],
279+
),
280+
]
281+
terms = ["nonexistent", "000"]
282+
283+
filtered = filter_components_by_name_or_id(components, terms)
284+
self.assertEqual(len(filtered), 0)

0 commit comments

Comments
 (0)