11#!/usr/bin/env python3
22import re
33import sys
4+ import os
45from pathlib import Path
56from sumologic_client import SumoLogicClient
67
78def find_sql_blocks_in_pr ():
8- """Detect changed SQL blocks without file modifications"""
9- changed_files = sys .argv [1 :] if len (sys .argv ) > 1 else [
10- str (p ) for p in Path ("docs" ).rglob ("*.md" )
11- if "search-query-language" in str (p )
12- ]
13-
14- sql_blocks = []
15- for file in changed_files :
16- content = Path (file ).read_text ()
17- sql_blocks .extend ([
18- (file , sql .strip ())
19- for sql in re .findall (r'```sql\n(.*?)```' , content , re .DOTALL )
20- ])
21- return sql_blocks
9+ """Detect changed SQL blocks with better debugging"""
10+ print ("::group::Detecting SQL blocks" ) # GitHub Actions log grouping
11+
12+ # Get changed files from environment if running in GitHub Actions
13+ changed_files = sys .argv [1 :] if len (sys .argv ) > 1 else []
14+ if not changed_files and "GITHUB_ACTIONS" in os .environ :
15+ try :
16+ with open (os .environ ["GITHUB_EVENT_PATH" ]) as f :
17+ event_data = json .load (f )
18+ changed_files = [
19+ f"docs/{ f ['filename' ]} " for f in
20+ event_data .get ("pull_request" , {}).get ("files" , [])
21+ if f ['filename' ].endswith ('.md' )
22+ ]
23+ except Exception as e :
24+ print (f"::warning::Couldn't get changed files: { str (e )} " )
25+
26+ if not changed_files :
27+ changed_files = [
28+ str (p ) for p in Path ("docs" ).rglob ("*.md" )
29+ if "search-query-language" in str (p )
30+ ]
31+
32+ print (f"Files to scan: { changed_files } " )
33+ return changed_files
2234
2335def validate_queries ():
36+ print ("::group::Starting validation" )
2437 client = SumoLogicClient ()
2538 failed = False
2639
27- for file , query in find_sql_blocks_in_pr ():
28- print (f"Validating SQL in { file } ..." )
40+ for file , query in find_sql_blocks_with_content ():
41+ print (f"\n 🔍 Validating query in { file } " )
42+ print (f"Query sample:\n { query [:200 ]} ..." ) # Show first 200 chars
43+
2944 try :
45+ print ("Calling Sumo Logic API..." )
3046 if not client .test_query (query ):
3147 print (f"::error file={ file } ,title=Query Validation Failed::Query returned no results" )
3248 failed = True
49+ else :
50+ print ("✅ Query validated successfully" )
3351 except Exception as e :
3452 print (f"::error file={ file } ,title=Query Execution Failed::{ str (e )} " )
3553 failed = True
3654
55+ print ("::endgroup::" )
3756 if failed :
3857 sys .exit (1 )
3958
59+ def find_sql_blocks_with_content ():
60+ """Yields (file_path, query) tuples with better error handling"""
61+ for file in find_sql_blocks_in_pr ():
62+ try :
63+ content = Path (file ).read_text ()
64+ queries = re .findall (r'```sql\n(.*?)```' , content , re .DOTALL )
65+ for query in queries :
66+ query = query .strip ()
67+ if query : # Skip empty queries
68+ yield (file , query )
69+ except Exception as e :
70+ print (f"::warning file={ file } ::Error processing file: { str (e )} " )
71+
4072if __name__ == "__main__" :
41- validate_queries ()
73+ validate_queries ()
0 commit comments