Skip to content
This repository has been archived by the owner on Feb 15, 2024. It is now read-only.

Commit

Permalink
Merge pull request #275 from RedHatProductSecurity/fix-non-strict-search
Browse files Browse the repository at this point in the history
GRIF-177: Fix non strict search
  • Loading branch information
JakubFrejlach authored Dec 22, 2023
2 parents 69fbe3f + 6818145 commit 4fceaf1
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 29 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* COMMUNITY_COMPONENTS_API_URL environment variable changed to
COMMUNITY_COMPONENTS_SERVER_URL
* Migrated from setup.py to pyproject.toml
* searched names regex escaped by default

### Added
* --search-provides in service products-contain-component which
Expand All @@ -22,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* example plugin introduced
* osidb-bindings and component-registry-bindings dependency versions
are now pinned in pyproject.toml
* -r option for regex name search without escaping the searched string

### Fixed
* standardized progress bar accross whole Griffon which fixes
Expand Down
46 changes: 42 additions & 4 deletions griffon/commands/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"""
import copy
import logging
import re
import subprocess
from json import loads

Expand Down Expand Up @@ -92,9 +93,11 @@ def queries_grp(ctx):
@click.option(
"-s",
"strict_name_search",
cls=GroupOption,
is_flag=True,
default=False,
help="Strict search, exact match of name.",
mutually_exclusive_group=["regex_name_search"],
)
@click.option(
"--all",
Expand All @@ -110,9 +113,20 @@ def queries_grp(ctx):
default=get_config_option("default", "verbosity", 0),
help="Verbose output, more detailed search results, can be used multiple times (e.g. -vvv).",
) # noqa
@click.option(
"-r",
"regex_name_search",
cls=GroupOption,
is_flag=True,
default=False,
help="Regex search.",
mutually_exclusive_group=["strict_name_search"],
)
@click.pass_context
@progress_bar
def get_product_summary(ctx, product_stream_name, strict_name_search, all, verbose):
def get_product_summary(
ctx, product_stream_name, strict_name_search, all, verbose, regex_name_search, operation_status
):
"""get product stream."""
if verbose:
ctx.obj["VERBOSE"] = verbose
Expand Down Expand Up @@ -182,9 +196,11 @@ def retrieve_component_summary(ctx, component_name, strict_name_search):
@click.option(
"-s",
"strict_name_search",
cls=GroupOption,
is_flag=True,
default=False,
help="Strict search, exact match of component name.",
mutually_exclusive_group=["regex_search"],
)
@click.option(
"--generate-affects",
Expand Down Expand Up @@ -334,7 +350,16 @@ def retrieve_component_summary(ctx, component_name, strict_name_search):
count=True,
default=get_config_option("default", "verbosity", 0),
help="Verbose output, more detailed search results, can be used multiple times (e.g. -vvv).",
) # noqa
)
@click.option(
"-r",
"regex_name_search",
cls=GroupOption,
is_flag=True,
default=False,
help="Regex search.",
mutually_exclusive_group=["strict_name_search"],
)
@click.pass_context
@progress_bar
def get_product_contain_component(
Expand Down Expand Up @@ -366,6 +391,7 @@ def get_product_contain_component(
output_type_filter,
verbose,
operation_status,
regex_name_search,
):
# with console_status(ctx) as operation_status:
"""List products of a latest component."""
Expand Down Expand Up @@ -408,7 +434,7 @@ def get_product_contain_component(
# Use split for users who runs middleware via python
mw_command = [
*MIDDLEWARE_CLI.split(),
component_name,
re.escape(component_name),
"-e",
"maven",
"-b",
Expand Down Expand Up @@ -631,17 +657,28 @@ def get_product_contain_component(
@click.option(
"-s",
"strict_name_search",
cls=GroupOption,
is_flag=True,
default=False,
help="Strict search, exact match of component name.",
mutually_exclusive_group=["regex_name_search"],
)
@click.option(
"-v",
"verbose",
count=True,
default=get_config_option("default", "verbosity", 0),
help="Verbose output, more detailed search results, can be used multiple times (e.g. -vvv).",
) # noqa
)
@click.option(
"-r",
"regex_name_search",
cls=GroupOption,
is_flag=True,
default=False,
help="Regex search.",
mutually_exclusive_group=["strict_name_search"],
)
@click.pass_context
@progress_bar
def get_component_contain_component(
Expand All @@ -655,6 +692,7 @@ def get_component_contain_component(
strict_name_search,
verbose,
operation_status,
regex_name_search,
):
"""List components that contain component."""
if verbose:
Expand Down
78 changes: 53 additions & 25 deletions griffon/services/core_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ class product_stream_summary:

name = "product_stream_summary"
description = "retrieve product_stream summary"
allowed_params = ["strict_name_search", "all", "product_stream_name", "ofuri", "verbose"]
allowed_params = [
"strict_name_search",
"all",
"product_stream_name",
"ofuri",
"verbose",
"regex_name_search",
]

def __init__(self, params: dict) -> None:
self.corgi_session = CorgiService.create_session()
Expand All @@ -37,18 +44,25 @@ def __init__(self, params: dict) -> None:
self.ofuri = self.params.get("ofuri")
self.strict_name_search = self.params.get("strict_name_search", None)
self.all = self.params.get("all", None)
self.regex_name_search = self.params.get("regex_name_search")

def execute(self, status=None) -> List[Dict[str, Any]]:
cond = {}

product_stream_name = (
re.escape(self.product_stream_name)
if not self.regex_name_search
else self.product_stream_name
)

if self.ofuri:
cond["ofuri"] = self.ofuri
elif not self.strict_name_search:
if not self.all:
cond["re_name"] = self.product_stream_name
cond["re_name"] = product_stream_name
else:
if not self.all:
cond["name"] = self.product_stream_name
cond["name"] = product_stream_name

# TODO - corgi bindings need to support ofuri in product_streams
product_streams = self.corgi_session.product_streams.retrieve_list(
Expand Down Expand Up @@ -184,6 +198,7 @@ class products_containing_specific_component_query:
"include_inactive_product_streams",
"include_product_stream_excluded_components",
"output_type_filter",
"regex_name_search",
]

def __init__(self, params: dict) -> None:
Expand Down Expand Up @@ -277,6 +292,7 @@ class products_containing_component_query:
"include_inactive_product_streams",
"include_product_stream_excluded_components",
"output_type_filter",
"regex_name_search",
]

def __init__(self, params: dict) -> None:
Expand All @@ -298,6 +314,7 @@ def __init__(self, params: dict) -> None:
self.no_community = self.params.get("no_community")
self.search_provides = self.params.get("search_provides")
self.search_upstreams = self.params.get("search_upstreams")
self.regex_name_search = self.params.get("regex_name_search")
if not self.no_community:
self.community_session = CommunityComponentService.create_session()
self.include_inactive_product_streams = self.params.get("include_inactive_product_streams")
Expand All @@ -309,12 +326,17 @@ def execute(self, status=None) -> List[Dict[str, Any]]:
"limit": 50,
"include_fields": "purl,type,name,related_url,namespace,software_build,nvr,release,version,arch,product_streams.product_versions,product_streams.name,product_streams.ofuri,product_streams.active,product_streams.exclude_components", # noqa
}

component_name = (
re.escape(self.component_name) if not self.regex_name_search else self.component_name
)

if self.search_latest:
search_latest_params = copy.deepcopy(params)
if not (self.strict_name_search):
search_latest_params["re_name"] = self.component_name
search_latest_params["re_name"] = component_name
else:
search_latest_params["name"] = self.component_name
search_latest_params["name"] = component_name
if self.ns:
search_latest_params["namespace"] = self.ns
if not (self.include_inactive_product_streams):
Expand Down Expand Up @@ -353,9 +375,9 @@ def execute(self, status=None) -> List[Dict[str, Any]]:
if self.search_provides:
search_provides_params = copy.deepcopy(params)
if not (self.strict_name_search):
search_provides_params["re_provides_name"] = self.component_name
search_provides_params["re_provides_name"] = component_name
else:
search_provides_params["provides_name"] = self.component_name
search_provides_params["provides_name"] = component_name
if self.ns:
search_provides_params["namespace"] = self.ns
if not (self.include_inactive_product_streams):
Expand Down Expand Up @@ -396,9 +418,9 @@ def execute(self, status=None) -> List[Dict[str, Any]]:
search_upstreams_params = copy.deepcopy(params)
search_upstreams_params["latest_components_by_streams"] = "True"
if not (self.strict_name_search):
search_upstreams_params["re_upstreams_name"] = self.component_name
search_upstreams_params["re_upstreams_name"] = component_name
else:
search_upstreams_params["upstreams_name"] = self.component_name
search_upstreams_params["upstreams_name"] = component_name
if self.ns:
search_upstreams_params["namespace"] = self.ns
if not (self.include_inactive_product_streams):
Expand Down Expand Up @@ -445,7 +467,7 @@ def execute(self, status=None) -> List[Dict[str, Any]]:
if self.search_related_url:
search_related_url_params = copy.deepcopy(params)
# Note: related_url filter has no concept of strict
search_related_url_params["related_url"] = self.component_name
search_related_url_params["related_url"] = component_name
if self.ns:
search_related_url_params["namespace"] = self.ns
if self.component_type:
Expand Down Expand Up @@ -493,9 +515,9 @@ def execute(self, status=None) -> List[Dict[str, Any]]:
if self.search_all:
search_all_params = copy.deepcopy(params)
if not (self.strict_name_search):
search_all_params["re_name"] = self.component_name
search_all_params["re_name"] = component_name
else:
search_all_params["name"] = self.component_name
search_all_params["name"] = component_name
if self.component_type:
search_all_params["type"] = self.component_type
if self.ns:
Expand Down Expand Up @@ -546,9 +568,9 @@ def execute(self, status=None) -> List[Dict[str, Any]]:
search_all_roots_params = copy.deepcopy(params)
search_all_roots_params["root_components"] = "True"
if not (self.strict_name_search):
search_all_roots_params["re_name"] = self.component_name
search_all_roots_params["re_name"] = component_name
else:
search_all_roots_params["name"] = self.component_name
search_all_roots_params["name"] = component_name
if self.ns:
search_all_roots_params["namespace"] = self.ns
if not (self.include_inactive_product_streams):
Expand Down Expand Up @@ -580,9 +602,9 @@ def execute(self, status=None) -> List[Dict[str, Any]]:
search_all_upstreams_params = copy.deepcopy(params)
search_all_upstreams_params["namespace"] = "UPSTREAM"
if not (self.strict_name_search):
search_all_upstreams_params["re_name"] = self.component_name
search_all_upstreams_params["re_name"] = component_name
else:
search_all_upstreams_params["name"] = self.component_name
search_all_upstreams_params["name"] = component_name
if self.component_type:
search_all_upstreams_params["type"] = self.component_type
if not (self.include_inactive_product_streams):
Expand Down Expand Up @@ -627,17 +649,17 @@ def execute(self, status=None) -> List[Dict[str, Any]]:
patterns = [
# binutils
re.compile(
f"(devtoolset\\-[0-9]+\\-|mingw\\-|gcc\\-toolset\\-[0-9]+\\-)?{self.component_name}[0-9\\.]*$", # noqa
f"(devtoolset\\-[0-9]+\\-|mingw\\-|gcc\\-toolset\\-[0-9]+\\-)?{component_name}[0-9\\.]*$", # noqa
flags=flags,
),
# compat-* style
re.compile(f"(compat\\-)?{self.component_name}[0-9\\.]*(\\-[0-9]+)?$", flags=flags),
re.compile(f"(compat\\-)?{component_name}[0-9\\.]*(\\-[0-9]+)?$", flags=flags),
# kernel
re.compile(f"^{self.component_name}(\\-rt)?$", flags=flags),
re.compile(f"^{component_name}(\\-rt)?$", flags=flags),
# qemu
re.compile(f"^{self.component_name}(\\-kvm(\\-rhev|\\-ma)?)?$", flags=flags),
re.compile(f"^{component_name}(\\-kvm(\\-rhev|\\-ma)?)?$", flags=flags),
# webkit
re.compile(f"^{self.component_name}([0-9])?(gtk)?([0-9])?$", flags=flags),
re.compile(f"^{component_name}([0-9])?(gtk)?([0-9])?$", flags=flags),
]

filtered_results = []
Expand All @@ -664,9 +686,9 @@ def execute(self, status=None) -> List[Dict[str, Any]]:
if self.search_community:
search_community_params = copy.deepcopy(params)
if not (self.strict_name_search):
search_community_params["re_name"] = self.component_name
search_community_params["re_name"] = component_name
else:
search_community_params["name"] = self.component_name
search_community_params["name"] = component_name
if self.ns:
search_community_params["namespace"] = self.ns
if not (self.include_inactive_product_streams):
Expand Down Expand Up @@ -749,6 +771,7 @@ class components_containing_component_query:
"namespace",
"strict_name_search",
"verbose",
"regex_name_search",
]

def __init__(self, params: dict) -> None:
Expand All @@ -760,13 +783,18 @@ def __init__(self, params: dict) -> None:
self.component_arch = self.params.get("component_arch")
self.namespace = self.params.get("namespace")
self.strict_name_search = self.params.get("strict_name_search")
self.regex_name_search = self.params.get("regex_name_search")

def execute(self, status=None) -> List[Dict[str, Any]]:
component_name = (
re.escape(self.component_name) if not self.regex_name_search else self.component_name
)

cond = {}
if not self.strict_name_search:
cond["re_name"] = self.component_name
cond["re_name"] = component_name
else:
cond["name"] = self.component_name
cond["name"] = component_name
if self.component_version:
cond["version"] = self.component_version
if self.component_arch:
Expand Down

0 comments on commit 4fceaf1

Please sign in to comment.