Skip to content

Commit c627d2e

Browse files
committed
In rare cases, if there is a new piece of
content that has already been committed to the current branch AND there are local, uncommitted changes to that content, GitService will pick up BOTH the fact that this is new content AND the fact that it has been modified. This will result in double-testing the content. This commit removes that as a possibility by adding content to be tested to a SET instead of appending it to a LIST, which couild have included duplicates.
1 parent 3a4be5d commit c627d2e

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

contentctl/actions/detection_testing/GitService.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ def getChanges(self, target_branch:str)->List[Detection]:
6767

6868
#Make a filename to content map
6969
filepath_to_content_map = { obj.file_path:obj for (_,obj) in self.director.name_to_content_map.items()}
70-
updated_detections:List[Detection] = []
71-
updated_macros:List[Macro] = []
72-
updated_lookups:List[Lookup] =[]
70+
updated_detections:set[Detection] = set()
71+
updated_macros:set[Macro] = set()
72+
updated_lookups:set[Lookup] = set()
7373

7474
for diff in all_diffs:
7575
if type(diff) == pygit2.Patch:
@@ -80,14 +80,14 @@ def getChanges(self, target_branch:str)->List[Detection]:
8080
if decoded_path.is_relative_to(self.config.path/"detections") and decoded_path.suffix == ".yml":
8181
detectionObject = filepath_to_content_map.get(decoded_path, None)
8282
if isinstance(detectionObject, Detection):
83-
updated_detections.append(detectionObject)
83+
updated_detections.add(detectionObject)
8484
else:
8585
raise Exception(f"Error getting detection object for file {str(decoded_path)}")
8686

8787
elif decoded_path.is_relative_to(self.config.path/"macros") and decoded_path.suffix == ".yml":
8888
macroObject = filepath_to_content_map.get(decoded_path, None)
8989
if isinstance(macroObject, Macro):
90-
updated_macros.append(macroObject)
90+
updated_macros.add(macroObject)
9191
else:
9292
raise Exception(f"Error getting macro object for file {str(decoded_path)}")
9393

@@ -98,7 +98,7 @@ def getChanges(self, target_branch:str)->List[Detection]:
9898
updatedLookup = filepath_to_content_map.get(decoded_path, None)
9999
if not isinstance(updatedLookup,Lookup):
100100
raise Exception(f"Expected {decoded_path} to be type {type(Lookup)}, but instead if was {(type(updatedLookup))}")
101-
updated_lookups.append(updatedLookup)
101+
updated_lookups.add(updatedLookup)
102102

103103
elif decoded_path.suffix == ".csv":
104104
# If the CSV was updated, we want to make sure that we
@@ -125,7 +125,7 @@ def getChanges(self, target_branch:str)->List[Detection]:
125125
if updatedLookup is not None and updatedLookup not in updated_lookups:
126126
# It is possible that both the CSV and YML have been modified for the same lookup,
127127
# and we do not want to add it twice.
128-
updated_lookups.append(updatedLookup)
128+
updated_lookups.add(updatedLookup)
129129

130130
else:
131131
pass
@@ -136,7 +136,7 @@ def getChanges(self, target_branch:str)->List[Detection]:
136136

137137
# If a detection has at least one dependency on changed content,
138138
# then we must test it again
139-
changed_macros_and_lookups = updated_macros + updated_lookups
139+
changed_macros_and_lookups:set[SecurityContentObject] = updated_macros.union(updated_lookups)
140140

141141
for detection in self.director.detections:
142142
if detection in updated_detections:
@@ -146,14 +146,14 @@ def getChanges(self, target_branch:str)->List[Detection]:
146146

147147
for obj in changed_macros_and_lookups:
148148
if obj in detection.get_content_dependencies():
149-
updated_detections.append(detection)
149+
updated_detections.add(detection)
150150
break
151151

152152
#Print out the names of all modified/new content
153153
modifiedAndNewContentString = "\n - ".join(sorted([d.name for d in updated_detections]))
154154

155155
print(f"[{len(updated_detections)}] Pieces of modifed and new content (this may include experimental/deprecated/manual_test content):\n - {modifiedAndNewContentString}")
156-
return updated_detections
156+
return sorted(list(updated_detections))
157157

158158
def getSelected(self, detectionFilenames: List[FilePath]) -> List[Detection]:
159159
filepath_to_content_map: dict[FilePath, SecurityContentObject] = {

0 commit comments

Comments
 (0)