1515Recategorize CodeQL SARIF results according to coding standards.
1616"""
1717
18+ import json
19+ import re
1820import subprocess
1921import sys
2022from pathlib import Path
@@ -108,6 +110,65 @@ def recategorize_sarif():
108110 return False
109111
110112
113+ def filter_sarif_results ():
114+ """
115+ Filter SARIF results to only include entries with paths matching repos/*.
116+
117+ Returns:
118+ True if successful, False otherwise
119+ """
120+ sarif_path = Path (SARIF_FILE )
121+
122+ if not sarif_path .exists ():
123+ print (f"Warning: SARIF file not found: { SARIF_FILE } " , file = sys .stderr )
124+ return False
125+
126+ try :
127+ # Load SARIF file
128+ with open (sarif_path , "r" ) as f :
129+ sarif_data = json .load (f )
130+
131+ print ("Filtering SARIF results to only include entries with paths matching repos/* ..." )
132+
133+ # Filter runs and results
134+ if "runs" in sarif_data :
135+ for run in sarif_data ["runs" ]:
136+ if "results" in run :
137+ filtered_results = []
138+
139+ for result in run ["results" ]:
140+ # Check if result has locations
141+ locations = result .get ("locations" , [])
142+ if not locations :
143+ continue
144+
145+ # Check if first location URI matches repos/ pattern
146+ first_location = locations [0 ].get ("physicalLocation" , {})
147+ artifact_uri = first_location .get ("artifactLocation" , {}).get ("uri" , "" )
148+
149+ # Pattern: (^|/)repos/ - matches repos/ at start or after a /
150+ if artifact_uri and re .search (r"(^|/)repos/" , artifact_uri ):
151+ filtered_results .append (result )
152+
153+ # Update results with filtered list
154+ run ["results" ] = filtered_results
155+ print (
156+ f"Run '{ run .get ('tool' , {}).get ('driver' , {}).get ('name' , 'unknown' )} ' "
157+ f"now has { len (filtered_results )} results"
158+ )
159+
160+ # Write filtered SARIF back to file
161+ with open (sarif_path , "w" ) as f :
162+ json .dump (sarif_data , f , indent = 2 )
163+
164+ print (f"Filtered SARIF written to { SARIF_FILE } " )
165+ return True
166+
167+ except (json .JSONDecodeError , IOError ) as e :
168+ print (f"Error: Failed to filter SARIF file: { e } " , file = sys .stderr )
169+ return False
170+
171+
111172def main ():
112173 """Main entry point."""
113174 # Validate required files exist
@@ -118,6 +179,10 @@ def main():
118179 if not recategorize_sarif ():
119180 sys .exit (1 )
120181
182+ # Filter SARIF results to only include repos/*
183+ if not filter_sarif_results ():
184+ sys .exit (1 )
185+
121186 print ("Recategorization workflow completed successfully" )
122187 sys .exit (0 )
123188
0 commit comments