Skip to content

Commit

Permalink
first go at testing on ds changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ljstella committed Oct 7, 2024
1 parent 5488ca6 commit 1f15302
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
14 changes: 11 additions & 3 deletions contentctl/actions/detection_testing/GitService.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from contentctl.objects.macro import Macro
from contentctl.objects.lookup import Lookup
from contentctl.objects.detection import Detection
from contentctl.objects.data_source import DataSource
from contentctl.objects.security_content_object import SecurityContentObject
from contentctl.objects.config import test_common, All, Changes, Selected

Expand Down Expand Up @@ -70,6 +71,7 @@ def getChanges(self, target_branch:str)->List[Detection]:
updated_detections:List[Detection] = []
updated_macros:List[Macro] = []
updated_lookups:List[Lookup] =[]
updated_datasources:List[DataSource] = []

for diff in all_diffs:
if type(diff) == pygit2.Patch:
Expand All @@ -90,6 +92,13 @@ def getChanges(self, target_branch:str)->List[Detection]:
updated_macros.append(macroObject)
else:
raise Exception(f"Error getting macro object for file {str(decoded_path)}")

elif decoded_path.is_relative_to(self.config.path/"data_sources") and decoded_path.suffix == ".yml":
datasourceObject = filepath_to_content_map.get(decoded_path, None)
if isinstance(datasourceObject, DataSource):
updated_datasources.append(datasourceObject)
else:
raise Exception(f"Error getting data source object for file {str(decoded_path)}")

elif decoded_path.is_relative_to(self.config.path/"lookups"):
# We need to convert this to a yml. This means we will catch
Expand All @@ -115,7 +124,6 @@ def getChanges(self, target_branch:str)->List[Detection]:
# Detected a changed .mlmodel file. However, since we do not have testing for these detections at
# this time, we will ignore this change.
updatedLookup = None


else:
raise Exception(f"Detected a changed file in the lookups/ directory '{str(decoded_path)}'.\n"
Expand All @@ -136,15 +144,15 @@ def getChanges(self, target_branch:str)->List[Detection]:

# If a detection has at least one dependency on changed content,
# then we must test it again
changed_macros_and_lookups = updated_macros + updated_lookups
changed_macros_and_lookups_and_datasources = updated_macros + updated_lookups + updated_datasources

for detection in self.director.detections:
if detection in updated_detections:
# we are already planning to test it, don't need
# to add it again
continue

for obj in changed_macros_and_lookups:
for obj in changed_macros_and_lookups_and_datasources:
if obj in detection.get_content_dependencies():
updated_detections.append(detection)
break
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,7 @@ def get_content_dependencies(self) -> list[SecurityContentObject]:
objects: list[SecurityContentObject] = []
objects += self.macros
objects += self.lookups
objects += self.data_source_objects
return objects

@field_validator("deployment", mode="before")
Expand Down

0 comments on commit 1f15302

Please sign in to comment.