From 47969c61c9fb0a9da110c1f56c1e72f69bd4e7a6 Mon Sep 17 00:00:00 2001 From: mohammed Date: Tue, 24 Jun 2025 03:27:00 +0300 Subject: [PATCH 1/2] error message on missing api key for forks --- codeflash/code_utils/env_utils.py | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/codeflash/code_utils/env_utils.py b/codeflash/code_utils/env_utils.py index adca67df8..99fd1fb4c 100644 --- a/codeflash/code_utils/env_utils.py +++ b/codeflash/code_utils/env_utils.py @@ -1,10 +1,11 @@ from __future__ import annotations +import json import os import tempfile from functools import lru_cache from pathlib import Path -from typing import Optional +from typing import Any, Optional from codeflash.cli_cmds.console import logger 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 = @lru_cache(maxsize=1) def get_codeflash_api_key() -> str: api_key = os.environ.get("CODEFLASH_API_KEY") or read_api_key_from_shell_config() + 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 if not api_key: msg = ( "I didn't find a Codeflash API key in your environment.\nYou can generate one at " - "https://app.codeflash.ai/app/apikeys ,\nthen set it as a CODEFLASH_API_KEY environment variable." + "https://app.codeflash.ai/app/apikeys ,\nthen set it as a CODEFLASH_API_KEY environment variable.\n" + f"{api_secret_docs_message}" ) + if is_repo_a_fork(): + msg = ( + "Codeflash API key not detected in your environment. It appears you're running Codeflash from a GitHub fork.\n" + "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" + f"{api_secret_docs_message}" + ) raise OSError(msg) if not api_key.startswith("cf-"): msg = ( @@ -83,3 +92,20 @@ def ensure_pr_number() -> bool: @lru_cache(maxsize=1) def is_end_to_end() -> bool: return bool(os.environ.get("CODEFLASH_END_TO_END")) + + +@lru_cache(maxsize=1) +def is_repo_a_fork() -> bool: + event = get_cached_gh_event_data() + if event is None: + return False + return bool(event["repository"]["fork"]) + + +@lru_cache(maxsize=1) +def get_cached_gh_event_data() -> dict[str, Any] | None: + event_path = os.getenv("GITHUB_EVENT_PATH") + if not event_path: + return None + with Path(event_path).open() as f: + return json.load(f) # type: ignore # noqa From 4801bc688abf38a100a396b107e7216860d7b672 Mon Sep 17 00:00:00 2001 From: mohammed Date: Tue, 24 Jun 2025 03:50:15 +0300 Subject: [PATCH 2/2] exit with 0 --- codeflash/code_utils/env_utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/codeflash/code_utils/env_utils.py b/codeflash/code_utils/env_utils.py index 99fd1fb4c..97af0cc3d 100644 --- a/codeflash/code_utils/env_utils.py +++ b/codeflash/code_utils/env_utils.py @@ -48,6 +48,7 @@ def get_codeflash_api_key() -> str: "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" f"{api_secret_docs_message}" ) + exit_with_message(msg) raise OSError(msg) if not api_key.startswith("cf-"): msg = (