Skip to content

Commit

Permalink
Fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
lvoloshyn-sekoia committed Aug 5, 2024
1 parent fd90341 commit f909125
Show file tree
Hide file tree
Showing 16 changed files with 59 additions and 72 deletions.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,14 @@ testpaths = [
]

[tool.ruff]
select = ["A", "ARG", "E", "F", "I", "N", "RUF", "UP", "W"]
lint.select = ["A", "ARG", "E", "F", "I", "N", "RUF", "UP", "W"]
exclude = [
"tests/expectations/sample_module/main.py",
"tests/aio/",
"sekoia_automation/scripts/new_module/template/"
]

[tool.ruff.per-file-ignores]
[tool.ruff.lint.per-file-ignores]
"tests/*" = ["ARG"] # Ignore unusued args because of pytest fixtures

[tool.mypy]
Expand Down
2 changes: 1 addition & 1 deletion sekoia_automation/cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
from pathlib import Path
from typing import Optional, Annotated
from typing import Optional

import typer
from cookiecutter.main import cookiecutter
Expand Down
18 changes: 8 additions & 10 deletions sekoia_automation/scripts/check_compliance.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def format_errors(self, mod_val: ModuleValidator, ignored_paths: set[Path]) -> s
errors = mod_val.result.errors
module_name = mod_val.path.name
return "\n".join(
"%s:%s:%s" % (module_name, error.filepath.name, error.error)
f"{module_name}:{error.filepath.name}:{error.error}"
for error in errors
if error.filepath not in ignored_paths
)
Expand All @@ -121,12 +121,12 @@ def find_modules(self, root_path: Path) -> list[Path]:
return result

def fix_set_uuid(self, file_path: Path, uuid: str) -> None:
with open(file_path, "rt") as file:
with open(file_path) as file:
manifest = json.load(file)

manifest["uuid"] = uuid

with open(file_path, "wt") as file:
with open(file_path, "w") as file:
json.dump(manifest, file, indent=2)

def check_uniqueness(self, items, error_msg: str):
Expand Down Expand Up @@ -175,13 +175,12 @@ def check_docker_params(self, validators: list[ModuleValidator]):
continue

if data["connector"] != data["trigger"]:
filename_to_fix = "connector_%s" % suffix
filename_to_fix = f"connector_{suffix}"
filepath = module_path / filename_to_fix
validator.result.errors.append(
CheckError(
filepath=filepath,
error=f"`docker_parameters` is not consistent with trigger_%s"
% suffix,
error=f"`docker_parameters` is not consistent with trigger_{suffix}",

Check failure on line 183 in sekoia_automation/scripts/check_compliance.py

View workflow job for this annotation

GitHub Actions / Lint

Ruff (E501)

sekoia_automation/scripts/check_compliance.py:183:89: E501 Line too long (97 > 88)
)
)
# We don't want to check these further
Expand Down Expand Up @@ -236,14 +235,13 @@ def check_uuids_and_slugs(self, validators: list[ModuleValidator]):
continue

if data["connector"] != data["trigger"]:
filename_to_fix = "connector_%s" % suffix
filename_to_fix = f"connector_{suffix}"
filepath = module_path / filename_to_fix
validator.result.errors.append(
CheckError(
filepath=filepath,
error=f"UUID is not consistent with trigger_%s" % suffix,
fix_label="Set the same UUID for trigger_%s and connector_%s"
% (suffix, suffix),
error=f"UUID is not consistent with trigger_{suffix}",
fix_label=f"Set the same UUID for trigger_{suffix} and connector_{suffix}",

Check failure on line 244 in sekoia_automation/scripts/check_compliance.py

View workflow job for this annotation

GitHub Actions / Lint

Ruff (E501)

sekoia_automation/scripts/check_compliance.py:244:89: E501 Line too long (103 > 88)
fix=partial(
self.fix_set_uuid,
file_path=filepath,
Expand Down
7 changes: 5 additions & 2 deletions sekoia_automation/scripts/compliance/validators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
from .logo import LogoValidator
from .main import MainPYValidator
from .manifest import ManifestValidator
from .module import ModuleValidator
from .tests import TestsValidator
from .triggers_json import TriggersJSONValidator

from .module import ModuleValidator

MODULES_PATH = Path(__file__).parent.parent.parent.parent

__all__ = (
ActionsJSONValidator, ChangelogValidator, ConnectorsJSONValidator, DependenciesValidator, DockerfileValidator,

Check failure on line 18 in sekoia_automation/scripts/compliance/validators/__init__.py

View workflow job for this annotation

GitHub Actions / Lint

Ruff (E501)

sekoia_automation/scripts/compliance/validators/__init__.py:18:89: E501 Line too long (110 > 88)
LogoValidator, MainPYValidator, ManifestValidator, ModuleValidator, TestsValidator, TriggersJSONValidator)

Check failure on line 19 in sekoia_automation/scripts/compliance/validators/__init__.py

View workflow job for this annotation

GitHub Actions / Lint

Ruff (E501)

sekoia_automation/scripts/compliance/validators/__init__.py:19:89: E501 Line too long (106 > 88)
27 changes: 13 additions & 14 deletions sekoia_automation/scripts/compliance/validators/actions_json.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import argparse
import json
import uuid
from functools import partial
Expand Down Expand Up @@ -27,23 +26,23 @@ def validate(cls, result: CheckResult) -> None:
@classmethod
def validate_action_json(cls, path: Path, result: CheckResult):
try:
with open(path, "rt") as file:
with open(path) as file:
raw = json.load(file)

except ValueError:
result.errors.append(CheckError(filepath=path, error=f"can't load JSON"))
result.errors.append(CheckError(filepath=path, error="can't load JSON"))
return

if not isinstance(raw.get("name"), str):
result.errors.append(
CheckError(filepath=path, error=f"`name` is not present")
CheckError(filepath=path, error="`name` is not present")
)

if not isinstance(raw.get("uuid"), str):
result.errors.append(
CheckError(
filepath=path,
error=f"`uuid` is not present",
error="`uuid` is not present",
fix_label="Generate random UUID",
fix=partial(cls.set_random_uuid, path=path),
)
Expand All @@ -57,13 +56,13 @@ def validate_action_json(cls, path: Path, result: CheckResult):

if not isinstance(raw.get("description"), str):
result.errors.append(
CheckError(filepath=path, error=f"`description` is not present")
CheckError(filepath=path, error="`description` is not present")
)

# @todo track this to main.py ?
if not isinstance(raw.get("docker_parameters"), str):
result.errors.append(
CheckError(filepath=path, error=f"`docker_parameters` is not present")
CheckError(filepath=path, error="`docker_parameters` is not present")
)

else:
Expand All @@ -78,38 +77,38 @@ def validate_action_json(cls, path: Path, result: CheckResult):

if not isinstance(raw.get("arguments"), dict):
result.errors.append(
CheckError(filepath=path, error=f"`arguments` is not present")
CheckError(filepath=path, error="`arguments` is not present")
)

elif not cls.is_valid_json_schema(raw["arguments"]):
result.errors.append(
CheckError(filepath=path, error=f"`arguments` is not valid JSON schema")
CheckError(filepath=path, error="`arguments` is not valid JSON schema")
)

if not isinstance(raw.get("results"), dict):
result.errors.append(
CheckError(filepath=path, error=f"`results` is not present")
CheckError(filepath=path, error="`results` is not present")
)

elif not cls.is_valid_json_schema(raw["results"]):
result.errors.append(
CheckError(filepath=path, error=f"`results` is not valid JSON schema")
CheckError(filepath=path, error="`results` is not valid JSON schema")
)

@staticmethod
def is_valid_json_schema(schema: dict) -> bool:
try:
Draft7Validator.check_schema(schema)
return True
except SchemaError as e:
except SchemaError:
return False

@staticmethod
def set_random_uuid(path: Path):
with open(path, "rt") as file:
with open(path) as file:
manifest = json.load(file)

manifest["uuid"] = str(uuid.uuid4())

with open(path, "wt") as file:
with open(path, "w") as file:
json.dump(manifest, file, indent=2)
1 change: 0 additions & 1 deletion sekoia_automation/scripts/compliance/validators/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import argparse
from abc import ABC

from .models import CheckResult
Expand Down
7 changes: 3 additions & 4 deletions sekoia_automation/scripts/compliance/validators/changelog.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import argparse
import re
from pathlib import Path

Expand All @@ -25,7 +24,7 @@ def validate(cls, result: CheckResult) -> None:

result.options["changelog_path"] = changelog_path

with open(changelog_path, "rt") as file:
with open(changelog_path) as file:
text = file.read()

cls.validate_changelog_content(text=text, path=changelog_path, result=result)
Expand Down Expand Up @@ -163,7 +162,7 @@ def validate(self, path: Path, result: CheckResult):
versions = self.versions()
if len(versions) == 0:
result.errors.append(
CheckError(filepath=path, error=f"No entries in changelog")
CheckError(filepath=path, error="No entries in changelog")
)

for v in versions:
Expand Down Expand Up @@ -256,7 +255,7 @@ def validate_version_sections(
if body and not body.startswith("###"):
result.errors.append(
CheckError(
filepath=path, error=f"Version has content outside of a section."
filepath=path, error="Version has content outside of a section."
)
)

Expand Down
23 changes: 11 additions & 12 deletions sekoia_automation/scripts/compliance/validators/connectors_json.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import argparse
import json
import uuid
from functools import partial
Expand Down Expand Up @@ -27,23 +26,23 @@ def validate(cls, result: CheckResult) -> None:
@classmethod
def validate_connector_json(cls, path: Path, result: CheckResult):
try:
with open(path, "rt") as file:
with open(path) as file:
raw = json.load(file)

except ValueError:
result.errors.append(CheckError(filepath=path, error=f"can't load JSON"))
result.errors.append(CheckError(filepath=path, error="can't load JSON"))
return

if not isinstance(raw.get("name"), str):
result.errors.append(
CheckError(filepath=path, error=f"`name` is not present")
CheckError(filepath=path, error="`name` is not present")
)

if not isinstance(raw.get("uuid"), str):
result.errors.append(
CheckError(
filepath=path,
error=f"`uuid` is not present",
error="`uuid` is not present",
fix_label="Generate random UUID",
fix=partial(cls.set_random_uuid, path=path),
)
Expand All @@ -57,12 +56,12 @@ def validate_connector_json(cls, path: Path, result: CheckResult):

if not isinstance(raw.get("description"), str):
result.errors.append(
CheckError(filepath=path, error=f"`description` is not present")
CheckError(filepath=path, error="`description` is not present")
)

if not isinstance(raw.get("docker_parameters"), str):
result.errors.append(
CheckError(filepath=path, error=f"`docker_parameters` is not present")
CheckError(filepath=path, error="`docker_parameters` is not present")
)
else:
if "docker_parameters" not in result.options:
Expand All @@ -76,12 +75,12 @@ def validate_connector_json(cls, path: Path, result: CheckResult):

if not isinstance(raw.get("arguments"), dict):
result.errors.append(
CheckError(filepath=path, error=f"`arguments` is not present")
CheckError(filepath=path, error="`arguments` is not present")
)

elif not cls.is_valid_json_schema(raw["arguments"]):
result.errors.append(
CheckError(filepath=path, error=f"`arguments` is not valid JSON schema")
CheckError(filepath=path, error="`arguments` is not valid JSON schema")
)

# @todo perhaps check if results present? (they shouldn't be)
Expand All @@ -91,15 +90,15 @@ def is_valid_json_schema(schema: dict) -> bool:
try:
Draft7Validator.check_schema(schema)
return True
except SchemaError as e:
except SchemaError:
return False

@staticmethod
def set_random_uuid(path: Path):
with open(path, "rt") as file:
with open(path) as file:
manifest = json.load(file)

manifest["uuid"] = str(uuid.uuid4())

with open(path, "wt") as file:
with open(path, "w") as file:
json.dump(manifest, file, indent=2)
2 changes: 0 additions & 2 deletions sekoia_automation/scripts/compliance/validators/deps.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import argparse
import os
from pathlib import Path

from .base import Validator
Expand Down
2 changes: 0 additions & 2 deletions sekoia_automation/scripts/compliance/validators/dockerfile.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import argparse
import os
from pathlib import Path

from .base import Validator
Expand Down
3 changes: 1 addition & 2 deletions sekoia_automation/scripts/compliance/validators/logo.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import argparse
import os
from functools import partial
from pathlib import Path
Expand Down Expand Up @@ -30,7 +29,7 @@ def check_logo_image(result: CheckResult) -> None:
image_path = module_dir / "logo.png"

if not image_path.is_file():
result.errors.append(CheckError(filepath=image_path, error=f"Logo is missing"))
result.errors.append(CheckError(filepath=image_path, error="Logo is missing"))
return

image = Image.open(image_path)
Expand Down
1 change: 0 additions & 1 deletion sekoia_automation/scripts/compliance/validators/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import argparse
from pathlib import Path

from pyastgrep.search import FileFinished, search_python_files
Expand Down
5 changes: 2 additions & 3 deletions sekoia_automation/scripts/compliance/validators/manifest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import argparse
import json
import re
from pathlib import Path
Expand Down Expand Up @@ -29,7 +28,7 @@ def validate(cls, result: CheckResult) -> None:
result.options["manifest_path"] = manifest_path

try:
with open(manifest_path, "rt") as f:
with open(manifest_path) as f:
manifest = json.load(f)

except ValueError:
Expand Down Expand Up @@ -123,5 +122,5 @@ def is_valid_json_schema(schema: dict) -> bool:
try:
Draft7Validator.check_schema(schema)
return True
except SchemaError as e:
except SchemaError:
return False
1 change: 0 additions & 1 deletion sekoia_automation/scripts/compliance/validators/module.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import argparse
from pathlib import Path

from . import (
Expand Down
1 change: 0 additions & 1 deletion sekoia_automation/scripts/compliance/validators/tests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import argparse
from pathlib import Path

from .base import Validator
Expand Down
Loading

0 comments on commit f909125

Please sign in to comment.