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

eslint/detekt: Clean up type hints and lint warnings #354

Merged
merged 5 commits into from
Nov 13, 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
9 changes: 8 additions & 1 deletion backend/engine/plugins/detekt/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import json
import os
import subprocess
from typing import List
import xml.etree.ElementTree as ET

from engine.plugins.lib import utils
Expand Down Expand Up @@ -65,6 +66,7 @@ def run_detekt(path=None):
output["details"] = _parse_report(path, detekt_report)
# If there was an error
else:
logger.error(f"Detekt exited with code {result.returncode}: {stderr}")
output["success"] = False
output["errors"] = ["The detekt plugin encountered a fatal error"]

Expand Down Expand Up @@ -95,7 +97,9 @@ def _run_detekt_command(config_file: str, path: str, report_file: str):
)


def _parse_report(path: str, xml_report: str) -> dict:
# Note: Need to use typing.List here since the java:17 container ships
# with Python 3.8.
def _parse_report(path: str, xml_report: str) -> List[dict]:
"""
Parse the XML report producted by detekt into a Python dictionary
"""
Expand Down Expand Up @@ -135,6 +139,9 @@ def _parse_severity(detekt_severity: str) -> str:
severity = "medium"
elif detekt_severity == "info":
severity = "low"
else:
logger.error(f"Unrecognized severity: {detekt_severity}")
severity = ""

return severity

Expand Down
22 changes: 12 additions & 10 deletions backend/engine/plugins/eslint/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
module running eslint on javascript/typescript files
"""

from dataclasses import dataclass
import json
import subprocess

Expand All @@ -11,7 +12,13 @@
log = utils.setup_logging("eslint")


def run_eslint(path: str, config: str) -> dict:
@dataclass
class Result:
data: list[str]
info: list[str]


def run_eslint(path: str, config: str) -> Result:
"""
function args: path to directory where code is
and path to where eslint rcfile
Expand All @@ -22,7 +29,6 @@ def run_eslint(path: str, config: str) -> dict:
1 = successful run. linting errors.
2 = internal error.
"""
result = {}
cmd = ["eslint", "-f", "json", "--config", config, "--ext", ".js,.jsx,.ts,.tsx", "."]
completed = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=path, check=False)
info = []
Expand All @@ -33,14 +39,10 @@ def run_eslint(path: str, config: str) -> dict:

data = list(filter(None, completed.stdout.decode("utf-8").split("\n")))

if data:
result["data"] = data
if info:
result["info"] = info
return result
return Result(data=data, info=info)


def parse_details(scan_data: list, path: str) -> list or None:
def parse_details(scan_data: list[str], path: str) -> list[dict]:
"""
takes the argument of scan results and returns a list of results. severity does not align with finding
severity, but tied to exit code. eslint-plugin-security does not have
Expand Down Expand Up @@ -83,9 +85,9 @@ def main():
args.config = args.eslint_config

scan_results = run_eslint(args.path, args.config)
details = parse_details(scan_results.get("data"), args.path)
details = parse_details(scan_results.data, args.path)

print(json.dumps({"success": not details, "details": details, "info": scan_results.get("info")}))
print(json.dumps({"success": not details, "details": details, "info": scan_results.info}))


if __name__ == "__main__":
Expand Down