cyyeh/py-code-analyzer
0
1"""CodeFetcher deals with every detail of2how to get all python files in the given directory3"""4import requests5 6 7def construct_fetch_repo_content_api_url(owner, repo, tree_sha, recursive):8 import os9 10 # to increase api rate limiting11 # https://docs.github.com/en/rest/overview/resources-in-the-rest-api#rate-limiting12 USER = os.environ.get("USER", "")13 PERSONAL_ACCESS_TOKEN = os.environ.get("PERSONAL_ACCESS_TOKEN", "")14 15 api_url = f"api.github.com/repos/{owner}/{repo}/git/trees/{tree_sha}"16 if USER and PERSONAL_ACCESS_TOKEN:17 api_url = f"{USER}:{PERSONAL_ACCESS_TOKEN}@{api_url}"18 if recursive:19 api_url += "?recursive=1"20 21 return "https://" + api_url22 23 24class CodeFetcher:25 @classmethod26 def get_python_files(27 cls,28 owner: str,29 repo: str,30 tree_sha: str,31 recursive: bool = True,32 ):33 """https://docs.github.com/en/rest/git/trees#get-a-tree"""34 # TODO: deal with truncated api results35 36 api_url = construct_fetch_repo_content_api_url(owner, repo, tree_sha, recursive)37 38 response = requests.get(39 api_url, headers={"Accept": "application/vnd.github.v3+json"}40 )41 42 api_results = response.json()43 if response.status_code == requests.codes.ok:44 python_files = [45 result46 for result in api_results["tree"]47 if type(result) is dict48 and result["type"] == "blob"49 and result["path"].endswith(".py")50 ]51 return python_files52 else:53 print(api_results)54 55 return []56 