|
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,20 @@ 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 | + ) |
| 51 | + exit_with_message(msg) |
42 | 52 | raise OSError(msg)
|
43 | 53 | if not api_key.startswith("cf-"):
|
44 | 54 | msg = (
|
@@ -83,3 +93,20 @@ def ensure_pr_number() -> bool:
|
83 | 93 | @lru_cache(maxsize=1)
|
84 | 94 | def is_end_to_end() -> bool:
|
85 | 95 | 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