@@ -287,9 +287,10 @@ def check_create_pr(
287287 optimization_review : str = "" ,
288288 original_line_profiler : str | None = None ,
289289 optimized_line_profiler : str | None = None ,
290+ language : str = "python" ,
290291) -> None :
291292 pr_number : Optional [int ] = env_utils .get_pr_number ()
292- git_repo = git .Repo (search_parent_directories = True )
293+ git_repo = git .Repo (str ( root_dir ), search_parent_directories = True )
293294
294295 if pr_number is not None :
295296 logger .info (f"Suggesting changes to PR #{ pr_number } ..." )
@@ -323,6 +324,7 @@ def check_create_pr(
323324 benchmark_details = explanation .benchmark_details ,
324325 original_async_throughput = explanation .original_async_throughput ,
325326 best_async_throughput = explanation .best_async_throughput ,
327+ language = language ,
326328 ),
327329 existing_tests = existing_tests_source ,
328330 generated_tests = generated_original_test_source ,
@@ -351,7 +353,7 @@ def check_create_pr(
351353 logger .warning ("⏭️ Branch is not pushed, skipping PR creation..." )
352354 return
353355 relative_path = explanation .file_path .resolve ().relative_to (root_dir .resolve ()).as_posix ()
354- base_branch = get_current_branch ()
356+ base_branch = get_current_branch (git_repo )
355357 build_file_changes = {
356358 Path (p ).resolve ().relative_to (root_dir .resolve ()).as_posix (): FileDiffContent (
357359 oldContent = original_code [p ], newContent = new_code [p ]
@@ -377,6 +379,7 @@ def check_create_pr(
377379 benchmark_details = explanation .benchmark_details ,
378380 original_async_throughput = explanation .original_async_throughput ,
379381 best_async_throughput = explanation .best_async_throughput ,
382+ language = language ,
380383 ),
381384 existing_tests = existing_tests_source ,
382385 generated_tests = generated_original_test_source ,
@@ -389,9 +392,23 @@ def check_create_pr(
389392 optimized_line_profiler = optimized_line_profiler ,
390393 )
391394 if response .ok :
392- pr_id = response .text
393- pr_url = github_pr_url (owner , repo , pr_id )
394- logger .info (f"Successfully created a new PR #{ pr_id } with the optimized code: { pr_url } " )
395+ # The cf-api returns a PR number on success, or a JSON object when staging is used as fallback
396+ try :
397+ response_data = response .json ()
398+ if isinstance (response_data , int ):
399+ pr_url = github_pr_url (owner , repo , str (response_data ))
400+ logger .info (f"Successfully created a new PR #{ response_data } with the optimized code: { pr_url } " )
401+ elif isinstance (response_data , dict ) and "storageType" in response_data :
402+ logger .info (
403+ f"PR creation fell back to staging (storageType: { response_data .get ('storageType' )} ). "
404+ f"The optimization is saved and can be reviewed in the Codeflash dashboard."
405+ )
406+ else :
407+ logger .info (f"PR creation response: { response .text } " )
408+ except Exception :
409+ pr_id = response .text
410+ pr_url = github_pr_url (owner , repo , pr_id )
411+ logger .info (f"Successfully created a new PR #{ pr_id } with the optimized code: { pr_url } " )
395412 else :
396413 logger .error (
397414 f"Optimization was successful, but I failed to create a PR with the optimized code."
0 commit comments