77from typing import Dict , List
88
99import pytest
10-
11- try :
12- import github
13- except ImportError :
14- pass
10+ import requests
1511
1612PROJECT_EULER_DIR_PATH = pathlib .Path .cwd ().joinpath ("project_euler" )
1713PROJECT_EULER_ANSWERS_PATH = pathlib .Path .cwd ().joinpath (
@@ -43,10 +39,11 @@ def all_solution_file_paths() -> List[pathlib.Path]:
4339 return solution_file_paths
4440
4541
46- def get_pull_number () -> int :
42+ def get_files_url () -> str :
4743 """Return the pull request number which triggered this action."""
48- GITHUB_REF = os .environ ["GITHUB_REF" ]
49- return int (GITHUB_REF .split ("/" )[2 ])
44+ with open (os .environ ["GITHUB_EVENT_PATH" ]) as file :
45+ event = json .load (file )
46+ return event ["pull_request" ]["url" ] + "/files"
5047
5148
5249def added_solution_file_path () -> List [pathlib .Path ]:
@@ -56,20 +53,20 @@ def added_solution_file_path() -> List[pathlib.Path]:
5653 This will only be triggered if the script is ran from GitHub Actions.
5754 """
5855 solution_file_paths = []
59- # Direct fetching so that the error propagates, if any.
60- login = github .Github ( os . environ [ "GITHUB_TOKEN" ])
61- repo = login . get_repo ( os .environ ["GITHUB_REPOSITORY" ])
62- if pull_number := get_pull_number ():
63- pull = repo . get_pull ( pull_number )
64- for file in pull . get_files () :
65- file_path = pathlib .Path .cwd ().joinpath (file . filename )
66- if (
67- file_path .suffix != ".py"
68- or file_path .name .startswith (("_" , "test" ))
69- or not file_path .name .startswith ("sol" )
70- ):
71- continue
72- solution_file_paths .append (file_path )
56+ headers = {
57+ "Accept" : "application/vnd. github.v3+json" ,
58+ "Authorization" : "token " + os .environ ["GITHUB_TOKEN" ],
59+ }
60+ files = requests . get ( get_files_url (), headers = headers ). json ( )
61+ for file in files :
62+ filepath = pathlib .Path .cwd ().joinpath (file [ " filename" ] )
63+ if (
64+ filepath .suffix != ".py"
65+ or filepath .name .startswith (("_" , "test" ))
66+ or not filepath .name .startswith ("sol" )
67+ ):
68+ continue
69+ solution_file_paths .append (filepath )
7370 return solution_file_paths
7471
7572
0 commit comments