1
1
#!/usr/bin/env python3
2
2
import importlib .util
3
3
import json
4
+ import os
4
5
import pathlib
5
6
from types import ModuleType
6
7
from typing import Dict , List
7
8
9
+ import github
8
10
import pytest
9
11
10
12
PROJECT_EULER_DIR_PATH = pathlib .Path .cwd ().joinpath ("project_euler" )
@@ -24,7 +26,7 @@ def convert_path_to_module(file_path: pathlib.Path) -> ModuleType:
24
26
return module
25
27
26
28
27
- def collect_solution_file_paths () -> List [pathlib .Path ]:
29
+ def all_solution_file_paths () -> List [pathlib .Path ]:
28
30
"""Collects all the solution file path in the Project Euler directory"""
29
31
solution_file_paths = []
30
32
for problem_dir_path in PROJECT_EULER_DIR_PATH .iterdir ():
@@ -37,6 +39,40 @@ def collect_solution_file_paths() -> List[pathlib.Path]:
37
39
return solution_file_paths
38
40
39
41
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
+
40
76
@pytest .mark .parametrize (
41
77
"solution_path" ,
42
78
collect_solution_file_paths (),
0 commit comments