Skip to content

Commit

Permalink
Merge pull request #26 from alanisaac/static-analysis
Browse files Browse the repository at this point in the history
Update GitHub actions to run static analysis
  • Loading branch information
alanisaac authored May 3, 2024
2 parents 76c0f30 + 6f2aa6d commit 72d762f
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 58 deletions.
18 changes: 18 additions & 0 deletions .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: "setup-ocsf-validator"
description: "Sets up the CI environment for the ocsf-validator"
inputs:
python-version:
description: Python version to use (e.g. "3.11")
required: true
runs:
using: "composite"
steps:
- name: Set up Python ${{ inputs.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ inputs.python-version }}
- name: Install Poetry
uses: snok/install-poetry@v1
- name: Install dependencies
shell: bash
run: poetry install
52 changes: 52 additions & 0 deletions .github/workflows/ocsf-validator.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: ocsf-validator

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
pytest:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.11", "3.12"]
steps:
- uses: actions/checkout@v4
- name: Set up environment
uses: "./.github/actions/setup"
with:
python-version: ${{ matrix.python-version }}
- name: pytest
run: poetry run pytest
black:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up environment
uses: "./.github/actions/setup"
with:
python-version: "3.12"
- name: black
run: poetry run black --check .
isort:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up environment
uses: "./.github/actions/setup"
with:
python-version: "3.12"
- name: isort
run: poetry run isort --check .
pyright:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up environment
uses: "./.github/actions/setup"
with:
python-version: "3.12"
- name: pyright
run: poetry run pyright
27 changes: 0 additions & 27 deletions .github/workflows/test.yml

This file was deleted.

15 changes: 6 additions & 9 deletions ocsf_validator/errors.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Iterable, Optional, TypeVar
from __future__ import annotations

TCollector = TypeVar("TCollector", bound="Collector")
from typing import Iterable, Optional


class Collector:
Expand All @@ -11,7 +11,7 @@ class Collector:
notice the collector until `throw` is `False`.
"""

default: TCollector # type: ignore
default: Collector
"""Simple singleton used whenever an Optional[Collector] parameter is None."""

def __init__(self, throw: bool = True):
Expand Down Expand Up @@ -52,16 +52,13 @@ class ValidationError(Exception):
...


class InvalidBasePathError(ValidationError):
...
class InvalidBasePathError(ValidationError): ...


class InvalidMetaSchemaError(ValidationError):
...
class InvalidMetaSchemaError(ValidationError): ...


class InvalidMetaSchemaFileError(ValidationError):
...
class InvalidMetaSchemaFileError(ValidationError): ...


class UnusedAttributeError(ValidationError):
Expand Down
6 changes: 5 additions & 1 deletion ocsf_validator/matchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ def match(self, value: str):
if matcher.match(value):
return True

return False

def add(self, matcher: Matcher):
self._matchers.append(matcher)

Expand Down Expand Up @@ -123,12 +125,14 @@ def __init__(self):
def get_type(self):
return OcsfCategories


class ExcludeMatcher(Matcher):
"""
A matcher that produces the opposite result of the matcher it's given.
"""

def __init__(self, matcher: Matcher):
self.matcher = matcher

def match(self, value: str) -> bool:
return not self.matcher.match(value)
return not self.matcher.match(value)
13 changes: 4 additions & 9 deletions ocsf_validator/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
from typing import Any, Callable, Optional

from ocsf_validator.errors import *
from ocsf_validator.matchers import (
CategoriesMatcher,
ExcludeMatcher
)
from ocsf_validator.matchers import CategoriesMatcher, ExcludeMatcher
from ocsf_validator.reader import Reader
from ocsf_validator.type_mapping import TypeMapping
from ocsf_validator.types import (
Expand Down Expand Up @@ -259,7 +256,7 @@ class ProfilesParser(MergeParser):
def applies_to(self, t: type) -> bool:
if hasattr(t, "__required_keys__") or hasattr(t, "__optional_keys"):
return (
PROFILES_KEY in t.__required_keys__
PROFILES_KEY in t.__required_keys__ # type: ignore
or PROFILES_KEY in t.__optional_keys__ # type: ignore
)
else:
Expand Down Expand Up @@ -289,7 +286,7 @@ class AttributesParser(MergeParser):
def applies_to(self, t: type) -> bool:
if hasattr(t, "__required_keys__") or hasattr(t, "__optional_keys"):
return (
ATTRIBUTES_KEY in t.__required_keys__
ATTRIBUTES_KEY in t.__required_keys__ # type: ignore
or ATTRIBUTES_KEY in t.__optional_keys__ # type: ignore
)
else:
Expand Down Expand Up @@ -468,9 +465,7 @@ def process_includes(

# categories cannot be extended with dependencies, and it causes problems
# if we try to include dictionary attributes in categories
matcher = ExcludeMatcher(
CategoriesMatcher()
)
matcher = ExcludeMatcher(CategoriesMatcher())

for path in reader.match(matcher):
for directive, parser in parsers.items():
Expand Down
8 changes: 4 additions & 4 deletions ocsf_validator/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@
leaf_type,
)


METASCHEMA_MATCHERS = {
METASCHEMA_MATCHERS = {
"event.schema.json": EventMatcher(),
"include.schema.json": IncludeMatcher(),
"object.schema.json": ObjectMatcher(),
Expand Down Expand Up @@ -269,10 +268,11 @@ def validate(reader: Reader, file: str):

def _default_get_registry(reader: Reader, base_uri: str) -> referencing.Registry:
registry: referencing.Registry = referencing.Registry()
for schema_file_path in reader.metaschema_path.glob("*.schema.json"):

for schema_file_path in reader.metaschema_path.glob("*.schema.json"): # type: ignore
with open(schema_file_path, "r") as file:
schema = json.load(file)
resource = referencing.Resource.from_contents(schema)
resource = referencing.Resource.from_contents(schema) # type: ignore
registry = registry.with_resource(
base_uri + schema_file_path.name, resource=resource
)
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ pyright = "^1.1.327"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[tool.isort]
profile = "black"
8 changes: 5 additions & 3 deletions tests/test_dependencies.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
from typing import Any

import pytest

from ocsf_validator.errors import *
from ocsf_validator.processor import *
from ocsf_validator.reader import DictReader, Reader


def attributes(attrs=[]):
def attributes(attrs: list = []) -> dict[str, Any]:
d = {}
for a in attrs:
d[a] = {"name": a}
return {"attributes": d}


def obj(name="object", attrs=[]):
def obj(name: str = "object", attrs: list = []) -> dict[str, Any]:
return {"name": name, "caption": ""} | attributes(attrs)


def event(name="event", attrs=[]):
def event(name: str = "event", attrs: list = []) -> dict[str, Any]:
return {"name": name, "caption": ""} | attributes(attrs)


Expand Down
4 changes: 1 addition & 3 deletions tests/test_matchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ def test_extension_matcher():


def test_exclude_matcher():
m = ExcludeMatcher(
ExtensionMatcher()
)
m = ExcludeMatcher(ExtensionMatcher())

assert m.match("/extensions/ext1/extension.json") is False
assert m.match("/extension.json") is True
Expand Down
4 changes: 2 additions & 2 deletions tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,11 @@ def test_validate_metaschemas():
def _get_registry(reader, base_uri) -> referencing.Registry:
registry: referencing.Registry = referencing.Registry()
for schema in METASCHEMA_MATCHERS.keys():
resource = referencing.Resource.from_contents(object_json_schema)
resource = referencing.Resource.from_contents(object_json_schema) # type: ignore
registry = registry.with_resource(base_uri + schema, resource=resource)
return registry

options = ReaderOptions(base_path='')
options = ReaderOptions(base_path=Path(""))

# test that a bad schema fails validation
r = DictReader(options)
Expand Down

0 comments on commit 72d762f

Please sign in to comment.