Skip to content

Commit a46b79b

Browse files
Saga4claude
andcommitted
fix: pass git_repo for branch detection and handle staging response
Fixes two bugs in PR creation for external projects: 1. get_current_branch() was using CWD (CLI directory) instead of the target project's git repo, causing branch 404 errors on GitHub. 2. cf-api staging fallback returns a JSON object, not a PR number — adds response type detection to handle both cases. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 250d4b1 commit a46b79b

2 files changed

Lines changed: 26 additions & 7 deletions

File tree

codeflash/api/cfapi.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,8 @@ def create_staging(
285285
optimization_review: str = "",
286286
original_line_profiler: str | None = None,
287287
optimized_line_profiler: str | None = None,
288-
**_kwargs: Any, # ignores the language argument TODO Hesham: staging for all langs
288+
language: str = "python",
289+
**_kwargs: Any,
289290
) -> Response:
290291
"""Create a staging pull request, targeting the specified branch. (usually 'staging').
291292
@@ -308,7 +309,7 @@ def create_staging(
308309
}
309310

310311
payload = {
311-
"baseBranch": get_current_branch(),
312+
"baseBranch": get_current_branch(git.Repo(str(root_dir), search_parent_directories=True)),
312313
"diffContents": build_file_changes,
313314
"prCommentFields": PrComment(
314315
optimization_explanation=explanation.explanation_message(),
@@ -321,6 +322,7 @@ def create_staging(
321322
winning_behavior_test_results=explanation.winning_behavior_test_results,
322323
winning_benchmarking_test_results=explanation.winning_benchmarking_test_results,
323324
benchmark_details=explanation.benchmark_details,
325+
language=language,
324326
).to_json(),
325327
"existingTests": existing_tests_source,
326328
"generatedTests": generated_original_test_source,

codeflash/result/create_pr.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)