Skip to content
Merged
5 changes: 5 additions & 0 deletions codeflash/api/aiservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from codeflash.cli_cmds.console import console, logger
from codeflash.code_utils.env_utils import get_codeflash_api_key
from codeflash.code_utils.git_utils import get_last_commit_author_if_pr_exists, get_repo_owner_and_name
from codeflash.models.models import OptimizedCandidate
from codeflash.telemetry.posthog_cf import ph
from codeflash.version import __version__ as codeflash_version
Expand Down Expand Up @@ -98,6 +99,7 @@ def optimize_python_code(

"""
start_time = time.perf_counter()
git_repo_owner, git_repo_name = get_repo_owner_and_name()
payload = {
"source_code": source_code,
"dependency_code": dependency_code,
Expand All @@ -106,6 +108,9 @@ def optimize_python_code(
"python_version": platform.python_version(),
"experiment_metadata": experiment_metadata,
"codeflash_version": codeflash_version,
"current_username" : get_last_commit_author_if_pr_exists(None),
"repo_owner": git_repo_owner,
"repo_name": git_repo_name,
}

logger.info("Generating optimized candidates…")
Expand Down
14 changes: 14 additions & 0 deletions codeflash/code_utils/git_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import os
import shutil
import subprocess
import sys
Expand Down Expand Up @@ -174,3 +175,16 @@ def remove_git_worktrees(worktree_root: Path | None, worktrees: list[Path]) -> N
logger.warning(f"Error removing worktrees: {e}")
if worktree_root:
shutil.rmtree(worktree_root)


def get_last_commit_author_if_pr_exists(repo: Repo | None = None) -> str | None:
"""
Return the author's name of the last commit in the current branch if PR_NUMBER is set.
Otherwise, return None.
"""
if "PR_NUMBER" not in os.environ:
return None

repository: Repo = repo if repo else git.Repo(search_parent_directories=True)
last_commit = repository.head.commit
return last_commit.author.name
Loading