Skip to content

Commit 5b4b7ff

Browse files
Merge pull request #354 from codeflash-ai/chore/get-pr-number-from-gh-action-event-file
[Chore] get pr number from gh action event json file, fallback to old behavior
2 parents 03f3c2d + 5a12b7c commit 5b4b7ff

File tree

7 files changed

+34
-39
lines changed

7 files changed

+34
-39
lines changed

.github/workflows/codeflash-optimize.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ jobs:
2020
CODEFLASH_AIS_SERVER: prod
2121
POSTHOG_API_KEY: ${{ secrets.POSTHOG_API_KEY }}
2222
CODEFLASH_API_KEY: ${{ secrets.CODEFLASH_API_KEY }}
23-
CODEFLASH_PR_NUMBER: ${{ github.event.number }}
2423
COLUMNS: 110
2524
steps:
2625
- name: 🛎️ Checkout

codeflash/api/cfapi.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,10 @@ def get_blocklisted_functions() -> dict[str, set[str]] | dict[str, Any]:
205205
if pr_number is None:
206206
return {}
207207

208-
owner, repo = get_repo_owner_and_name()
209-
information = {"pr_number": pr_number, "repo_owner": owner, "repo_name": repo}
210208
try:
209+
owner, repo = get_repo_owner_and_name()
210+
information = {"pr_number": pr_number, "repo_owner": owner, "repo_name": repo}
211+
211212
req = make_cfapi_request(endpoint="/verify-existing-optimizations", method="POST", payload=information)
212213
req.raise_for_status()
213214
content: dict[str, list[str]] = req.json()

codeflash/cli_cmds/workflows/codeflash-optimize.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ jobs:
2121
runs-on: ubuntu-latest
2222
env:
2323
CODEFLASH_API_KEY: ${{ secrets.CODEFLASH_API_KEY }}
24-
CODEFLASH_PR_NUMBER: ${{ github.event.number }}
2524
{{ working_directory }}
2625
steps:
2726
- name: 🛎️ Checkout

codeflash/code_utils/env_utils.py

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,22 @@ def ensure_codeflash_api_key() -> bool:
7474

7575
@lru_cache(maxsize=1)
7676
def get_pr_number() -> Optional[int]:
77+
event_data = get_cached_gh_event_data()
78+
pr_number = event_data.get("number")
79+
if pr_number:
80+
return int(pr_number)
81+
7782
pr_number = os.environ.get("CODEFLASH_PR_NUMBER")
78-
if not pr_number:
79-
return None
80-
return int(pr_number)
83+
if pr_number:
84+
return int(pr_number)
85+
return None
8186

8287

8388
def ensure_pr_number() -> bool:
8489
if not get_pr_number():
8590
msg = (
86-
"CODEFLASH_PR_NUMBER not found in environment variables; make sure the Github Action is setting this so "
87-
"Codeflash can comment on the right PR"
91+
"Codeflash couldn't detect your pull request number. Are you running Codeflash within a GitHub Action?"
92+
"If not, please set the CODEFLASH_PR_NUMBER environment variable to ensure Codeflash can comment on the correct PR."
8893
)
8994
raise OSError(msg)
9095
return True
@@ -96,22 +101,19 @@ def is_end_to_end() -> bool:
96101

97102

98103
@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:
104+
def get_cached_gh_event_data() -> dict[str, Any]:
108105
event_path = os.getenv("GITHUB_EVENT_PATH")
109106
if not event_path:
110-
return None
107+
return {}
111108
with Path(event_path).open() as f:
112109
return json.load(f) # type: ignore # noqa
113110

114111

112+
def is_repo_a_fork() -> bool:
113+
event = get_cached_gh_event_data()
114+
return bool(event.get("repository", {}).get("fork", False))
115+
116+
115117
@lru_cache(maxsize=1)
116118
def is_ci() -> bool:
117119
"""Check if running in a CI environment."""
@@ -125,14 +127,5 @@ def is_LSP_enabled() -> bool:
125127

126128
def is_pr_draft() -> bool:
127129
"""Check if the PR is draft. in the github action context."""
128-
try:
129-
event_path = os.getenv("GITHUB_EVENT_PATH")
130-
pr_number = get_pr_number()
131-
if pr_number is not None and event_path:
132-
with Path(event_path).open() as f:
133-
event_data = json.load(f)
134-
return bool(event_data["pull_request"]["draft"])
135-
return False # noqa
136-
except Exception as e:
137-
logger.warning(f"Error checking if PR is draft: {e}")
138-
return False
130+
event = get_cached_gh_event_data()
131+
return bool(event.get("pull_request", {}).get("draft", False))

codeflash/result/critic.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from typing import TYPE_CHECKING
3+
from typing import TYPE_CHECKING, Optional
44

55
from codeflash.cli_cmds.console import logger
66
from codeflash.code_utils import env_utils
@@ -26,7 +26,10 @@ def performance_gain(*, original_runtime_ns: int, optimized_runtime_ns: int) ->
2626

2727

2828
def speedup_critic(
29-
candidate_result: OptimizedCandidateResult, original_code_runtime: int, best_runtime_until_now: int
29+
candidate_result: OptimizedCandidateResult,
30+
original_code_runtime: int,
31+
best_runtime_until_now: int,
32+
disable_gh_action_noise: Optional[bool] = None,
3033
) -> bool:
3134
"""Take in a correct optimized Test Result and decide if the optimization should actually be surfaced to the user.
3235
@@ -35,10 +38,11 @@ def speedup_critic(
3538
when the original runtime is less than 10 microseconds, and becomes MIN_IMPROVEMENT_THRESHOLD for any higher runtime.
3639
The noise floor is doubled when benchmarking on a (noisy) GitHub Action virtual instance, also we want to be more confident there.
3740
"""
38-
in_github_actions_mode = bool(env_utils.get_pr_number())
3941
noise_floor = 3 * MIN_IMPROVEMENT_THRESHOLD if original_code_runtime < 10000 else MIN_IMPROVEMENT_THRESHOLD
40-
if in_github_actions_mode:
41-
noise_floor = noise_floor * 2 # Increase the noise floor in GitHub Actions mode
42+
if not disable_gh_action_noise:
43+
in_github_actions_mode = bool(env_utils.get_pr_number())
44+
if in_github_actions_mode:
45+
noise_floor = noise_floor * 2 # Increase the noise floor in GitHub Actions mode
4246

4347
perf_gain = performance_gain(
4448
original_runtime_ns=original_code_runtime, optimized_runtime_ns=candidate_result.best_test_runtime

docs/docs/getting-started/codeflash-github-actions.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ jobs:
5757
runs-on: ubuntu-latest
5858
env:
5959
CODEFLASH_API_KEY: ${{ secrets.CODEFLASH_API_KEY }}
60-
CODEFLASH_PR_NUMBER: ${{ github.event.number }}
6160
steps:
6261
- uses: actions/checkout@v4
6362
with:

tests/test_critic.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def test_speedup_critic() -> None:
4141
total_candidate_timing=12,
4242
)
4343

44-
assert speedup_critic(candidate_result, original_code_runtime, best_runtime_until_now) # 20% improvement
44+
assert speedup_critic(candidate_result, original_code_runtime, best_runtime_until_now, True) # 20% improvement
4545

4646
candidate_result = OptimizedCandidateResult(
4747
max_loop_count=5,
@@ -52,7 +52,7 @@ def test_speedup_critic() -> None:
5252
optimization_candidate_index=0,
5353
)
5454

55-
assert not speedup_critic(candidate_result, original_code_runtime, best_runtime_until_now) # 6% improvement
55+
assert not speedup_critic(candidate_result, original_code_runtime, best_runtime_until_now, True) # 6% improvement
5656

5757
original_code_runtime = 100000
5858
best_runtime_until_now = 100000
@@ -66,7 +66,7 @@ def test_speedup_critic() -> None:
6666
optimization_candidate_index=0,
6767
)
6868

69-
assert speedup_critic(candidate_result, original_code_runtime, best_runtime_until_now) # 6% improvement
69+
assert speedup_critic(candidate_result, original_code_runtime, best_runtime_until_now, True) # 6% improvement
7070

7171

7272
def test_generated_test_critic() -> None:

0 commit comments

Comments
 (0)