Skip to content

Commit 47969c6

Browse files
error message on missing api key for forks
1 parent 859d50b commit 47969c6

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

codeflash/code_utils/env_utils.py

Lines changed: 28 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,19 @@ 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+
)
4251
raise OSError(msg)
4352
if not api_key.startswith("cf-"):
4453
msg = (
@@ -83,3 +92,20 @@ def ensure_pr_number() -> bool:
8392
@lru_cache(maxsize=1)
8493
def is_end_to_end() -> bool:
8594
return bool(os.environ.get("CODEFLASH_END_TO_END"))
95+
96+
97+
@lru_cache(maxsize=1)
98+
def is_repo_a_fork() -> bool:
99+
event = get_cached_gh_event_data()
100+
if event is None:
101+
return False
102+
return bool(event["repository"]["fork"])
103+
104+
105+
@lru_cache(maxsize=1)
106+
def get_cached_gh_event_data() -> dict[str, Any] | None:
107+
event_path = os.getenv("GITHUB_EVENT_PATH")
108+
if not event_path:
109+
return None
110+
with Path(event_path).open() as f:
111+
return json.load(f) # type: ignore # noqa

0 commit comments

Comments
 (0)