55from typing import Optional
66from typing import Sequence
77
8+ from datapilot .clients .altimate .utils import get_all_dbt_configs
89from datapilot .config .config import load_config
910from datapilot .core .platforms .dbt .constants import MODEL
1011from datapilot .core .platforms .dbt .constants import PROJECT
@@ -34,6 +35,8 @@ def validate_config_file(config_path: str) -> bool:
3435
3536def main (argv : Optional [Sequence [str ]] = None ):
3637 start_time = time .time ()
38+ print ("Starting DataPilot pre-commit hook..." , file = sys .stderr )
39+
3740 parser = argparse .ArgumentParser ()
3841 parser .add_argument (
3942 "--config-path" ,
@@ -66,18 +69,60 @@ def main(argv: Optional[Sequence[str]] = None):
6669
6770 args = parser .parse_known_args (argv )
6871
69- # Validate config file if provided
70- config = {}
72+ # Handle config loading like in project_health command
73+ config = None
7174 if hasattr (args [0 ], "config_path" ) and args [0 ].config_path :
7275 config_path = args [0 ].config_path [0 ]
76+ print (f"Loading config from file: { config_path } " , file = sys .stderr )
7377 if not validate_config_file (config_path ):
7478 print ("Pre-commit hook failed: Invalid config file." , file = sys .stderr )
7579 sys .exit (1 )
7680 config = load_config (config_path )
81+ print ("Config loaded successfully from file" , file = sys .stderr )
82+ elif (
83+ hasattr (args [0 ], "config_name" )
84+ and args [0 ].config_name
85+ and hasattr (args [0 ], "token" )
86+ and args [0 ].token
87+ and hasattr (args [0 ], "instance_name" )
88+ and args [0 ].instance_name
89+ ):
90+ config_name = args [0 ].config_name
91+ token = args [0 ].token
92+ instance_name = args [0 ].instance_name
93+ backend_url = getattr (args [0 ], "backend_url" , "https://api.myaltimate.com" )
94+
95+ print (f"Fetching config '{ config_name } ' from API..." , file = sys .stderr )
96+ try :
97+ # Get configs from API
98+ configs = get_all_dbt_configs (token , instance_name , backend_url )
99+ if configs and "items" in configs :
100+ # Find config by name
101+ matching_configs = [c for c in configs ["items" ] if c ["name" ] == config_name ]
102+ if matching_configs :
103+ # Get the config directly from the API response
104+ print (f"Using config from API: { config_name } " , file = sys .stderr )
105+ config = matching_configs [0 ].get ("config" , {})
106+ else :
107+ print (f"No config found with name: { config_name } " , file = sys .stderr )
108+ print ("Pre-commit hook failed: Config not found." , file = sys .stderr )
109+ sys .exit (1 )
110+ else :
111+ print ("Failed to fetch configs from API" , file = sys .stderr )
112+ print ("Pre-commit hook failed: Unable to fetch configs." , file = sys .stderr )
113+ sys .exit (1 )
114+ except Exception as e :
115+ print (f"Error fetching config from API: { e } " , file = sys .stderr )
116+ print ("Pre-commit hook failed: API error." , file = sys .stderr )
117+ sys .exit (1 )
118+ else :
119+ print ("No config provided. Using default configuration." , file = sys .stderr )
120+ config = {}
77121
78122 base_path = "./"
79123 if hasattr (args [0 ], "base_path" ) and args [0 ].base_path :
80124 base_path = args [0 ].base_path [0 ]
125+ print (f"Using base path: { base_path } " , file = sys .stderr )
81126
82127 # Get authentication parameters
83128 token = getattr (args [0 ], "token" , None )
@@ -99,9 +144,19 @@ def main(argv: Optional[Sequence[str]] = None):
99144 print ("No changed files detected. Skipping datapilot checks." , file = sys .stderr )
100145 return
101146
147+ print (f"Processing { len (changed_files )} changed files..." , file = sys .stderr )
148+ print (f"Changed files: { ', ' .join (changed_files )} " , file = sys .stderr )
149+
102150 try :
151+ print ("Generating partial manifest and catalog from changed files..." , file = sys .stderr )
103152 selected_models , manifest , catalog = generate_partial_manifest_catalog (changed_files , base_path = base_path )
153+ print (f"Generated manifest with { len (manifest .get ('nodes' , {}))} nodes" , file = sys .stderr )
154+ if catalog :
155+ print (f"Generated catalog with { len (catalog .get ('nodes' , {}))} nodes" , file = sys .stderr )
156+ else :
157+ print ("No catalog generated (catalog file not available)" , file = sys .stderr )
104158
159+ print ("Initializing DBT Insight Generator..." , file = sys .stderr )
105160 insight_generator = DBTInsightGenerator (
106161 manifest = manifest ,
107162 catalog = catalog ,
@@ -112,9 +167,11 @@ def main(argv: Optional[Sequence[str]] = None):
112167 backend_url = backend_url ,
113168 )
114169
170+ print ("Running insight generation..." , file = sys .stderr )
115171 reports = insight_generator .run ()
116172
117173 if reports :
174+ print ("Insights generated successfully. Analyzing results..." , file = sys .stderr )
118175 model_report = generate_model_insights_table (reports [MODEL ])
119176 if len (model_report ) > 0 :
120177 print ("--" * 50 , file = sys .stderr )
@@ -135,6 +192,8 @@ def main(argv: Optional[Sequence[str]] = None):
135192
136193 print ("\n Pre-commit hook failed: DataPilot found issues that need to be addressed." , file = sys .stderr )
137194 sys .exit (1 )
195+ else :
196+ print ("No insights generated. All checks passed!" , file = sys .stderr )
138197
139198 except Exception as e :
140199 print (f"Error running DataPilot checks: { e } " , file = sys .stderr )
0 commit comments