Skip to content

Commit 89410a5

Browse files
get pr number from gh action event json file, fallback to old behaviour
1 parent 5298a68 commit 89410a5

File tree

4 files changed

+16
-5
lines changed

4 files changed

+16
-5
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/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: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import json
34
import os
45
import tempfile
56
from functools import lru_cache
@@ -64,6 +65,10 @@ def ensure_codeflash_api_key() -> bool:
6465

6566
@lru_cache(maxsize=1)
6667
def get_pr_number() -> Optional[int]:
68+
event_data = get_cached_gh_event_data()
69+
gh_pr_number = event_data["number"]
70+
if gh_pr_number is not None:
71+
return int(gh_pr_number)
6772
pr_number = os.environ.get("CODEFLASH_PR_NUMBER")
6873
if not pr_number:
6974
return None
@@ -73,8 +78,8 @@ def get_pr_number() -> Optional[int]:
7378
def ensure_pr_number() -> bool:
7479
if not get_pr_number():
7580
msg = (
76-
"CODEFLASH_PR_NUMBER not found in environment variables; make sure the Github Action is setting this so "
77-
"Codeflash can comment on the right PR"
81+
"Codeflash couldn't detect your pull request number. Are you running Codeflash within a GitHub Action?"
82+
"If not, please set the CODEFLASH_PR_NUMBER environment variable to ensure Codeflash can comment on the correct PR."
7883
)
7984
raise OSError(msg)
8085
return True
@@ -83,3 +88,12 @@ def ensure_pr_number() -> bool:
8388
@lru_cache(maxsize=1)
8489
def is_end_to_end() -> bool:
8590
return bool(os.environ.get("CODEFLASH_END_TO_END"))
91+
92+
93+
@lru_cache(maxsize=1)
94+
def get_cached_gh_event_data() -> dict[str,]:
95+
event_path = os.getenv("GITHUB_EVENT_PATH")
96+
if not event_path:
97+
return {}
98+
with Path(event_path).open() as f:
99+
return json.load(f)

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:

0 commit comments

Comments
 (0)