Skip to content

Commit ca8689b

Browse files
committed
Update validate solution script to fetch one solution
1 parent ac6a160 commit ca8689b

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

scripts/validate_solutions.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#!/usr/bin/env python3
22
import importlib.util
33
import json
4+
import os
45
import pathlib
56
from types import ModuleType
67
from typing import Dict, List
78

9+
import github
810
import pytest
911

1012
PROJECT_EULER_DIR_PATH = pathlib.Path.cwd().joinpath("project_euler")
@@ -24,7 +26,7 @@ def convert_path_to_module(file_path: pathlib.Path) -> ModuleType:
2426
return module
2527

2628

27-
def collect_solution_file_paths() -> List[pathlib.Path]:
29+
def all_solution_file_paths() -> List[pathlib.Path]:
2830
"""Collects all the solution file path in the Project Euler directory"""
2931
solution_file_paths = []
3032
for problem_dir_path in PROJECT_EULER_DIR_PATH.iterdir():
@@ -37,6 +39,40 @@ def collect_solution_file_paths() -> List[pathlib.Path]:
3739
return solution_file_paths
3840

3941

42+
def get_pull_number() -> int:
43+
"""Return the pull request number which triggered this action."""
44+
GITHUB_REF = os.environ["GITHUB_REF"]
45+
return int(GITHUB_REF.split("/")[2])
46+
47+
48+
def added_solution_file_path() -> List[pathlib.Path]:
49+
"""Collects only the solution file path which got added in the current
50+
pull request.
51+
52+
This will only be triggered if the script is ran from GitHub Actions.
53+
"""
54+
solution_file_paths = []
55+
# Direct fetching so that the error propagates, if any.
56+
login = github.Github(os.environ["GITHUB_TOKEN"])
57+
repo = login.get_repo(os.environ["GITHUB_REPOSITORY"])
58+
if pull_number := get_pull_number():
59+
pull = repo.get_pull(pull_number)
60+
for file in pull.get_files():
61+
file_path = pathlib.Path.cwd().joinpath(file.filename)
62+
if file_path.suffix != ".py" or file_path.name.startswith(("_", "test")):
63+
continue
64+
solution_file_paths.append(file_path)
65+
return solution_file_paths
66+
67+
68+
def collect_solution_file_paths() -> List[pathlib.Path]:
69+
if os.environ.get("CI") and os.environ.get("GITHUB_EVENT_NAME") == "pull_request":
70+
# Return only if there are any, otherwise default to all solutions
71+
if filepaths := added_solution_file_path():
72+
return filepaths
73+
return all_solution_file_paths()
74+
75+
4076
@pytest.mark.parametrize(
4177
"solution_path",
4278
collect_solution_file_paths(),

0 commit comments

Comments
 (0)