|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import json |
3 | 4 | import os |
4 | 5 | import tempfile |
5 | 6 | from functools import lru_cache |
6 | 7 | from pathlib import Path |
7 | | -from typing import Optional |
| 8 | +from typing import Any, Optional |
8 | 9 |
|
9 | 10 | from codeflash.cli_cmds.console import logger |
10 | 11 | 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 = |
34 | 35 | @lru_cache(maxsize=1) |
35 | 36 | def get_codeflash_api_key() -> str: |
36 | 37 | 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 |
37 | 39 | if not api_key: |
38 | 40 | msg = ( |
39 | 41 | "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}" |
41 | 44 | ) |
| 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 | + ) |
42 | 51 | raise OSError(msg) |
43 | 52 | if not api_key.startswith("cf-"): |
44 | 53 | msg = ( |
@@ -83,3 +92,20 @@ def ensure_pr_number() -> bool: |
83 | 92 | @lru_cache(maxsize=1) |
84 | 93 | def is_end_to_end() -> bool: |
85 | 94 | 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