Skip to content

Commit 6347a8a

Browse files
authored
Merge branch 'main' into updated-vsc-extension
2 parents d0e6284 + 2344c2c commit 6347a8a

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

codeflash/code_utils/env_utils.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from __future__ import annotations
22

3+
import json
34
import os
45
import tempfile
56
from functools import lru_cache
67
from pathlib import Path
7-
from typing import Optional
8+
from typing import Any, Optional
89

910
from codeflash.cli_cmds.console import logger
1011
from codeflash.code_utils.code_utils import exit_with_message
@@ -34,11 +35,20 @@ def check_formatter_installed(formatter_cmds: list[str], exit_on_failure: bool =
3435
@lru_cache(maxsize=1)
3536
def get_codeflash_api_key() -> str:
3637
api_key = os.environ.get("CODEFLASH_API_KEY") or read_api_key_from_shell_config()
38+
api_secret_docs_message = "For more information, refer to the documentation at [https://docs.codeflash.ai/getting-started/codeflash-github-actions#add-your-api-key-to-your-repository-secrets]." # noqa
3739
if not api_key:
3840
msg = (
3941
"I didn't find a Codeflash API key in your environment.\nYou can generate one at "
40-
"https://app.codeflash.ai/app/apikeys ,\nthen set it as a CODEFLASH_API_KEY environment variable."
42+
"https://app.codeflash.ai/app/apikeys ,\nthen set it as a CODEFLASH_API_KEY environment variable.\n"
43+
f"{api_secret_docs_message}"
4144
)
45+
if is_repo_a_fork():
46+
msg = (
47+
"Codeflash API key not detected in your environment. It appears you're running Codeflash from a GitHub fork.\n"
48+
"For external contributors, please ensure you've added your own API key to your fork's repository secrets and set it as the CODEFLASH_API_KEY environment variable.\n"
49+
f"{api_secret_docs_message}"
50+
)
51+
exit_with_message(msg)
4252
raise OSError(msg)
4353
if not api_key.startswith("cf-"):
4454
msg = (
@@ -83,3 +93,20 @@ def ensure_pr_number() -> bool:
8393
@lru_cache(maxsize=1)
8494
def is_end_to_end() -> bool:
8595
return bool(os.environ.get("CODEFLASH_END_TO_END"))
96+
97+
98+
@lru_cache(maxsize=1)
99+
def is_repo_a_fork() -> bool:
100+
event = get_cached_gh_event_data()
101+
if event is None:
102+
return False
103+
return bool(event["repository"]["fork"])
104+
105+
106+
@lru_cache(maxsize=1)
107+
def get_cached_gh_event_data() -> dict[str, Any] | None:
108+
event_path = os.getenv("GITHUB_EVENT_PATH")
109+
if not event_path:
110+
return None
111+
with Path(event_path).open() as f:
112+
return json.load(f) # type: ignore # noqa

0 commit comments

Comments
 (0)