Skip to content

Commit

Permalink
Add paths to inputs of ScanSemgrep step (patched-codes#952)
Browse files Browse the repository at this point in the history
* add the input

* fix

* bump version and fix split

* add path key
  • Loading branch information
CTY-git authored Oct 15, 2024
1 parent c6cfbfd commit c666d58
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
63 changes: 63 additions & 0 deletions patchwork/common/utils/input_parsing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from __future__ import annotations
from typing_extensions import Union, AnyStr
from collections.abc import Iterable, Mapping

__ITEM_TYPE = Union[AnyStr, Mapping]

def __parse_to_list_handle_str(input_value: AnyStr, possible_delimiters: Iterable[AnyStr | None]) -> list[str]:
for possible_delimiter in possible_delimiters:
if possible_delimiter is None:
return input_value.split()

if possible_delimiter in input_value:
return input_value.split(possible_delimiter)

return []

def __parse_to_list_handle_dict(input_value: Mapping, possible_keys: Iterable[AnyStr | None]) -> list[str]:
for possible_key in possible_keys:
if input_value.get(possible_key) is not None:
return input_value.get(possible_key)

return []

def __parse_to_list_handle_iterable(input_value: Iterable[__ITEM_TYPE], possible_keys: Iterable[AnyStr | None]) -> list[str]:
rv = []
for item in input_value:
if isinstance(item, dict):
for possible_key in possible_keys:
if item.get(possible_key) is not None:
rv.append(item.get(possible_key))
else:
rv.append(item)

return rv

def parse_to_list(
input_value: __ITEM_TYPE | Iterable[__ITEM_TYPE],
possible_delimiters: Iterable[AnyStr | None] | None = None ,
possible_keys: Iterable[AnyStr | None] | None = None
) -> list[str]:
if len(input_value) < 1:
return []

if possible_delimiters is None:
possible_delimiters = []
if possible_keys is None:
possible_keys = []

value_to_parse = []
if isinstance(input_value, dict):
value_to_parse = __parse_to_list_handle_dict(input_value, possible_keys)
elif isinstance(input_value, str):
value_to_parse = __parse_to_list_handle_str(input_value, possible_delimiters)
elif isinstance(input_value, Iterable):
value_to_parse = __parse_to_list_handle_iterable(input_value, possible_keys)

rv = []
for value in value_to_parse:
stripped_value = value.strip()
if stripped_value == "":
continue
rv.append(stripped_value)
return rv
9 changes: 8 additions & 1 deletion patchwork/steps/ScanSemgrep/ScanSemgrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
from pathlib import Path

from patchwork.common.utils.dependency import import_with_dependency_group
from patchwork.common.utils.input_parsing import parse_to_list
from patchwork.logger import logger
from patchwork.step import Step, StepStatus
from patchwork.steps.ScanSemgrep.typed import ScanSemgrepOutputs, ScanSemgrepInputs


class ScanSemgrep(Step):
class ScanSemgrep(Step, input_class=ScanSemgrepInputs, output_class=ScanSemgrepOutputs):
def __init__(self, inputs: dict):
super().__init__(inputs)

Expand All @@ -27,6 +29,9 @@ def __init__(self, inputs: dict):
else:
self.sarif_values = None

path_key = inputs.get("path_key", "path")
self.paths = parse_to_list(inputs.get("paths", ""), possible_delimiters=[",", None], possible_keys=[path_key])

def run(self) -> dict:
if self.sarif_values is not None:
self.set_status(StepStatus.SKIPPED, "Using provided SARIF")
Expand All @@ -37,6 +42,8 @@ def run(self) -> dict:

cmd = [
"semgrep",
"scan",
*self.paths,
*self.extra_args.split(),
"--sarif",
]
Expand Down
2 changes: 2 additions & 0 deletions patchwork/steps/ScanSemgrep/typed.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class ScanSemgrepInputs(TypedDict, total=False):
sarif_file_path: Annotated[str, StepTypeConfig(is_config=True, is_path=True)]
sarif_values: str
semgrep_extra_args: Annotated[str, StepTypeConfig(is_config=True)]
paths: str
path_key: str


class ScanSemgrepOutputs(TypedDict):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "patchwork-cli"
version = "0.0.68"
version = "0.0.69"
description = ""
authors = ["patched.codes"]
license = "AGPL"
Expand Down

0 comments on commit c666d58

Please sign in to comment.