Skip to content

Commit 859d50b

Browse files
Merge pull request #322 from codeflash-ai/fix/skip-optimization-for-draft-prs
[FIX] skip draft pr optimization (CF-667)
2 parents 560a5f6 + 66e5efa commit 859d50b

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

codeflash/cli_cmds/cli.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ def parse_args() -> Namespace:
6262
type=str,
6363
help="Path to the directory of the project, where all the pytest-benchmark tests are located.",
6464
)
65+
parser.add_argument("--no-draft", default=False, action="store_true", help="Skip optimization for draft PRs")
66+
6567
args: Namespace = parser.parse_args()
6668
return process_and_validate_cmd_args(args)
6769

codeflash/optimization/optimizer.py

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

33
import ast
4+
import json
45
import os
56
import tempfile
67
import time
@@ -86,6 +87,11 @@ def run(self) -> None:
8687
return
8788
if not env_utils.check_formatter_installed(self.args.formatter_cmds):
8889
return
90+
91+
if self.args.no_draft and is_pr_draft():
92+
logger.warning("PR is in draft mode, skipping optimization")
93+
return
94+
8995
function_optimizer = None
9096
file_to_funcs_to_optimize: dict[Path, list[FunctionToOptimize]]
9197
num_optimizable_functions: int
@@ -301,3 +307,18 @@ def run_with_args(args: Namespace) -> None:
301307
optimizer.cleanup_temporary_paths()
302308

303309
raise SystemExit from None
310+
311+
312+
def is_pr_draft() -> bool:
313+
"""Check if the PR is draft. in the github action context."""
314+
try:
315+
event_path = os.getenv("GITHUB_EVENT_PATH")
316+
pr_number = get_pr_number()
317+
if pr_number is not None and event_path:
318+
with Path(event_path).open() as f:
319+
event_data = json.load(f)
320+
return bool(event_data["pull_request"]["draft"])
321+
return False # noqa
322+
except Exception as e:
323+
logger.warning(f"Error checking if PR is draft: {e}")
324+
return False

0 commit comments

Comments
 (0)