Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions codeflash/code_utils/env_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from codeflash.code_utils.formatter import format_code
from codeflash.code_utils.shell_utils import read_api_key_from_shell_config

_cached_gh_event_data: dict[str, Any] | None = None


def check_formatter_installed(formatter_cmds: list[str], exit_on_failure: bool = True) -> bool: # noqa
return_code = True
Expand Down Expand Up @@ -100,13 +102,20 @@ def is_end_to_end() -> bool:
return bool(os.environ.get("CODEFLASH_END_TO_END"))


@lru_cache(maxsize=1)
def get_cached_gh_event_data() -> dict[str, Any]:
global _cached_gh_event_data
if _cached_gh_event_data is not None:
return _cached_gh_event_data
event_path = os.getenv("GITHUB_EVENT_PATH")
if not event_path:
return {}
with Path(event_path).open() as f:
return json.load(f) # type: ignore # noqa
_cached_gh_event_data = {}
return _cached_gh_event_data
try:
with open(event_path) as f: # Faster than Path(event_path).open()
_cached_gh_event_data = json.load(f) # type: ignore # noqa
except Exception:
_cached_gh_event_data = {}
return _cached_gh_event_data


def is_repo_a_fork() -> bool:
Expand All @@ -128,4 +137,8 @@ def is_LSP_enabled() -> bool:
def is_pr_draft() -> bool:
"""Check if the PR is draft. in the github action context."""
event = get_cached_gh_event_data()
return bool(event.get("pull_request", {}).get("draft", False))
# Fast dict access; avoids redundant dict lookups.
pr = event.get("pull_request")
if not pr:
return False
return pr.get("draft", False)
Loading