Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Filter components by id in addition to name #1013

Merged
merged 5 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/internal/coverage/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def get_object(self) -> ReportPaths:
component_paths = []
if components:
all_components = components_service.commit_components(commit, self.owner)
filtered_components = components_service.filter_components_by_name(
filtered_components = components_service.filter_components_by_name_or_id(
all_components, components
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def _apply_filters(
all_components = components.commit_components(
comparison.head_commit, comparison.user
)
filtered_components = components.filter_components_by_name(
filtered_components = components.filter_components_by_name_or_id(
all_components, components_filter
)
for component in filtered_components:
Expand Down
8 changes: 4 additions & 4 deletions graphql_api/types/commit/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def resolve_path_contents(commit: Commit, info, path: str = None, filters=None):

if component_filter:
all_components = components_service.commit_components(commit, current_owner)
filtered_components = components_service.filter_components_by_name(
filtered_components = components_service.filter_components_by_name_or_id(
all_components, component_filter
)

Expand Down Expand Up @@ -268,7 +268,7 @@ def resolve_deprecated_path_contents(

if component_filter:
all_components = components_service.commit_components(commit, current_owner)
filtered_components = components_service.filter_components_by_name(
filtered_components = components_service.filter_components_by_name_or_id(
all_components, component_filter
)

Expand Down Expand Up @@ -394,7 +394,7 @@ def resolve_coverage_file(commit, info, path, flags=None, components=None):
all_components = components_service.commit_components(
commit, info.context["request"].current_owner
)
filtered_components = components_service.filter_components_by_name(
filtered_components = components_service.filter_components_by_name_or_id(
all_components, components
)
for fc in filtered_components:
Expand Down Expand Up @@ -423,7 +423,7 @@ def resolve_coverage_components(commit: Commit, info, filters=None) -> List[Comp
all_components = components_service.commit_components(commit, current_owner)

if filters and filters.get("components"):
return components_service.filter_components_by_name(
return components_service.filter_components_by_name_or_id(
all_components, filters["components"]
)

Expand Down
2 changes: 1 addition & 1 deletion graphql_api/types/comparison/comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def resolve_component_comparisons(
list_components = comparison_report.commit_comparison.component_comparisons.all()

if filters and filters.get("components"):
components = components_service.filter_components_by_name(
components = components_service.filter_components_by_name_or_id(
components, filters["components"]
)

Expand Down
9 changes: 7 additions & 2 deletions services/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,20 @@ def component_filtered_report(
return filtered_report


def filter_components_by_name(
def filter_components_by_name_or_id(
components: List[Component], terms: List[str]
) -> List[Component]:
"""
Given a list of Components and a list of strings (terms),
return a new list of Components only including Components with names in terms (case insensitive)
OR component_id in terms (case insensitive)
"""
terms = [v.lower() for v in terms]
return list(filter(lambda c: c.name.lower() in terms, components))
return [
component
for component in components
if component.name.lower() in terms or component.component_id.lower() in terms
]


class ComponentComparison:
Expand Down
61 changes: 61 additions & 0 deletions services/tests/test_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
ComponentComparison,
commit_components,
component_filtered_report,
filter_components_by_name_or_id,
)


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

# removed 1 tested line, added 1 tested and 1 untested line
assert component_comparison.patch_totals.coverage == "50.00000"

def test_filter_components_by_name_or_id(self):
components = [
Component(
name="ComponentA",
component_id="123",
paths=[],
flag_regexes=[],
statuses=[],
),
Component(
name="ComponentB",
component_id="456",
paths=[],
flag_regexes=[],
statuses=[],
),
Component(
name="ComponentC",
component_id="789",
paths=[],
flag_regexes=[],
statuses=[],
),
]
terms = ["comPOnentA", "123", "456"]

filtered = filter_components_by_name_or_id(components, terms)
self.assertEqual(len(filtered), 2)
self.assertEqual(filtered[0].name, "ComponentA")
self.assertEqual(filtered[1].component_id, "456")

def test_filter_components_by_name_or_id_no_matches(self):
components = [
Component(
name="ComponentA",
component_id="123",
paths=[],
flag_regexes=[],
statuses=[],
),
Component(
name="ComponentB",
component_id="456",
paths=[],
flag_regexes=[],
statuses=[],
),
Component(
name="ComponentC",
component_id="789",
paths=[],
flag_regexes=[],
statuses=[],
),
]
terms = ["nonexistent", "000"]

filtered = filter_components_by_name_or_id(components, terms)
self.assertEqual(len(filtered), 0)
Loading