1- import json
21from abc import ABCMeta , abstractmethod
32from collections import defaultdict
43from importlib .metadata import entry_points
54from pathlib import Path
65from typing import DefaultDict
76
7+ from pydantic import ValidationError
8+ from sarif_pydantic import Run , Sarif
9+
810from codemodder .logging import logger
911
1012
1113class AbstractSarifToolDetector (metaclass = ABCMeta ):
1214 @classmethod
1315 @abstractmethod
14- def detect (cls , run_data : dict ) -> bool :
16+ def detect (cls , run_data : Run ) -> bool :
1517 pass
1618
1719
@@ -24,18 +26,16 @@ def detect_sarif_tools(filenames: list[Path]) -> DefaultDict[str, list[Path]]:
2426 }
2527 for fname in filenames :
2628 try :
27- data = json . loads (fname .read_text (encoding = "utf-8-sig" ))
28- except json . JSONDecodeError :
29- logger .exception ("Malformed JSON file: %s" , fname )
29+ data = Sarif . model_validate_json (fname .read_text (encoding = "utf-8-sig" ))
30+ except ValidationError :
31+ logger .exception ("Invalid SARIF file: %s" , fname )
3032 raise
31- for name , det in detectors .items ():
32- try :
33- runs = data ["runs" ]
34- except KeyError :
35- logger .exception ("Sarif file without `runs` data: %s" , fname )
36- raise
3733
38- for run in runs :
34+ if not data .runs :
35+ raise ValueError (f"SARIF file without `runs` data: { fname } " )
36+
37+ for name , det in detectors .items ():
38+ for run in data .runs :
3939 try :
4040 if det .detect (run ):
4141 logger .debug ("detected %s sarif: %s" , name , fname )
0 commit comments