# Path: autorelease/github.py
# _GITHUB_ROOT: str = "https://api.github.com"
# _MAGIC_GITHUB_PROXY_ROOT: str = (
#     "https://magic-github-proxy.endpoints.devrel-prod.cloud.goog"
# )
# def _find_devrel_api_key() -> str:
#     def __init__(self, token: str, use_proxy: bool = False) -> None:
#     def get_url(self, url: str = None, **kwargs) -> dict:
#     def list_org_repos(self, org: str, type: str = None) -> Generator[dict, None, None]:
#     def list_org_issues(
#         self, org: str, state: str = None, labels: str = None, created_after: str = None
#     ) -> Generator[dict, None, None]:
#     def list_pull_requests(self, repository: str, **kwargs) -> Sequence[Dict]:
#     def create_pull_request(
#         self, repository: str, branch: str, title: str, body: str = None
#     ) -> dict:
#     def replace_issue_labels(
#         self, owner: str, repository: str, number: str, labels: Sequence[str]
#     ) -> dict:
#     def update_pull_labels(
#         self, pull: dict, add: Sequence[str] = None, remove: Sequence[str] = None
#     ) -> dict:
#     def get_tree(self, repository: str, tree_sha: str = "master") -> Sequence[dict]:
#     def get_contents(self, repository: str, path: str, ref: str = None) -> bytes:
#     def get_issue(self, repository: str, number: int) -> dict:
#     def list_files(self, repository: str, path: str, ref: str = None) -> Sequence[dict]:
#     def check_for_file(self, repository: str, path: str, ref: str = None) -> bool:
#     def create_release(
#         self,
#         repository: str,
#         tag_name: str,
#         target_commitish: str,
#         name: str,
#         body: str,
#     ) -> Dict:
#     def create_pull_request_comment(
#         self, repository: str, pull_request_number: int, comment: str
#     ) -> Dict:
#     def get_languages(self, repository) -> Dict[str, int]:
# class GitHub:
# 
# Path: autorelease/common.py
# def _determine_language(
#     fetch_repos_json: Callable[[], str], repo_full_name: str
# ) -> str:
#     python_tools = ["synthtool"]
# 
#     if repo_full_name.split("/")[1] in python_tools:
#         return "python_tool"
# 
#     repos = json.loads(fetch_repos_json())["repos"]
#     for repo in repos:
#         if repo_full_name == repo["repo"]:
#             return repo["language"]
# 
#     raise Exception("Unable to determine repository language.")
# 
# def guess_language(gh: GitHub, repo_full_name: str) -> str:
#     special_cases = {
#         # 1 special case inherited from the original determine_language() code.
#         "googleapis/synthtool": "python_tool",
#         # 2 more special cases where the most prevalent language is not the same as
#         # what was declared in the old repos.json.
#         "GoogleCloudPlatform/cloud-code-samples": "dotnet",
#         "googleapis/doc-templates": "python",
#     }
#     special_case = special_cases.get(repo_full_name)
#     if special_case:
#         return special_case
# 
#     # Does the repo name have a language name in it?
#     lang_names = {
#         "cpp",
#         "dotnet",
#         "elixir",
#         "go",
#         "java",
#         "nodejs",
#         "php",
#         "python",
#         "python_tool",
#         "ruby",
#     }
#     chunks = set(re.split("/|-", repo_full_name))
#     x = lang_names.intersection(chunks)
#     if 1 == len(x):
#         return x.pop()  # Found the language name in the repo name
# 
#     # Fetch how many lines of each language are found in the repo.
#     languages = gh.get_languages(repo_full_name)
#     ranks = [
#         (count, lang)
#         for (lang, count) in languages.items()
#         # Ignore languages we don't care about, like Shell.
#         if lang in _SILVER_LANGUAGE_NAMES
#     ]
#     ranks.sort(reverse=True)
#     if ranks:
#         # Return the most prevalent language in the repo.
#         return _SILVER_LANGUAGE_NAMES[ranks[0][1]]
#     else:
#         raise Exception("Unable to determine repository language.")


import json
import os
import pathlib
import pytest
import string
from autorelease import github
from autorelease.common import _determine_language, guess_language

def test_determine_language():
    # determine_language() is the old function that depends on sloth's repo.json.
    # Use it to generate the code for test_guess_language() so we can confirm
    # the output is 100% the same.
    repos_json = (pathlib.Path(__file__).parent / "testdata" / "repos.json").read_text()
    repos = json.loads(repos_json)["repos"]

    python_tools_repo_names = [
        "googleapis/releasetool",
        "googleapis/synthtool",
    ]

    repo_names = python_tools_repo_names + [repo["repo"] for repo in repos]
    languages = set()
    for name in repo_names:
        language = _determine_language(lambda: repos_json, name)
        languages.add((language, name))
    for language, name in sorted(languages):
        test_name = repo_name_to_test_name(name)
        print(
            f"""def {test_name}():
    gh = github.GitHub(os.environ["GITHUB_TOKEN"])
    assert {repr(language)} == guess_language(gh, {repr(name)})

"""
        )


def test_guess_google_cloud_platform_cpp_docs_samples():

# Path: autorelease/github.py
# _GITHUB_ROOT: str = "https://api.github.com"
# _MAGIC_GITHUB_PROXY_ROOT: str = (
#     "https://magic-github-proxy.endpoints.devrel-prod.cloud.goog"
# )
# def _find_devrel_api_key() -> str:
#     def __init__(self, token: str, use_proxy: bool = False) -> None:
#     def get_url(self, url: str = None, **kwargs) -> dict:
#     def list_org_repos(self, org: str, type: str = None) -> Generator[dict, None, None]:
#     def list_org_issues(
#         self, org: str, state: str = None, labels: str = None, created_after: str = None
#     ) -> Generator[dict, None, None]:
#     def list_pull_requests(self, repository: str, **kwargs) -> Sequence[Dict]:
#     def create_pull_request(
#         self, repository: str, branch: str, title: str, body: str = None
#     ) -> dict:
#     def replace_issue_labels(
#         self, owner: str, repository: str, number: str, labels: Sequence[str]
#     ) -> dict:
#     def update_pull_labels(
#         self, pull: dict, add: Sequence[str] = None, remove: Sequence[str] = None
#     ) -> dict:
#     def get_tree(self, repository: str, tree_sha: str = "master") -> Sequence[dict]:
#     def get_contents(self, repository: str, path: str, ref: str = None) -> bytes:
#     def get_issue(self, repository: str, number: int) -> dict:
#     def list_files(self, repository: str, path: str, ref: str = None) -> Sequence[dict]:
#     def check_for_file(self, repository: str, path: str, ref: str = None) -> bool:
#     def create_release(
#         self,
#         repository: str,
#         tag_name: str,
#         target_commitish: str,
#         name: str,
#         body: str,
#     ) -> Dict:
#     def create_pull_request_comment(
#         self, repository: str, pull_request_number: int, comment: str
#     ) -> Dict:
#     def get_languages(self, repository) -> Dict[str, int]:
# class GitHub:
# 
# Path: autorelease/common.py
# def _determine_language(
#     fetch_repos_json: Callable[[], str], repo_full_name: str
# ) -> str:
#     python_tools = ["synthtool"]
# 
#     if repo_full_name.split("/")[1] in python_tools:
#         return "python_tool"
# 
#     repos = json.loads(fetch_repos_json())["repos"]
#     for repo in repos:
#         if repo_full_name == repo["repo"]:
#             return repo["language"]
# 
#     raise Exception("Unable to determine repository language.")
# 
# def guess_language(gh: GitHub, repo_full_name: str) -> str:
#     special_cases = {
#         # 1 special case inherited from the original determine_language() code.
#         "googleapis/synthtool": "python_tool",
#         # 2 more special cases where the most prevalent language is not the same as
#         # what was declared in the old repos.json.
#         "GoogleCloudPlatform/cloud-code-samples": "dotnet",
#         "googleapis/doc-templates": "python",
#     }
#     special_case = special_cases.get(repo_full_name)
#     if special_case:
#         return special_case
# 
#     # Does the repo name have a language name in it?
#     lang_names = {
#         "cpp",
#         "dotnet",
#         "elixir",
#         "go",
#         "java",
#         "nodejs",
#         "php",
#         "python",
#         "python_tool",
#         "ruby",
#     }
#     chunks = set(re.split("/|-", repo_full_name))
#     x = lang_names.intersection(chunks)
#     if 1 == len(x):
#         return x.pop()  # Found the language name in the repo name
# 
#     # Fetch how many lines of each language are found in the repo.
#     languages = gh.get_languages(repo_full_name)
#     ranks = [
#         (count, lang)
#         for (lang, count) in languages.items()
#         # Ignore languages we don't care about, like Shell.
#         if lang in _SILVER_LANGUAGE_NAMES
#     ]
#     ranks.sort(reverse=True)
#     if ranks:
#         # Return the most prevalent language in the repo.
#         return _SILVER_LANGUAGE_NAMES[ranks[0][1]]
#     else:
#         raise Exception("Unable to determine repository language.")


# Path: tests/common_test.py
import json
import os
import pathlib
import pytest
import string
from autorelease import github
from autorelease.common import _determine_language, guess_language


def test_determine_language():
    # determine_language() is the old function that depends on sloth's repo.json.
    # Use it to generate the code for test_guess_language() so we can confirm
    # the output is 100% the same.
    repos_json = (pathlib.Path(__file__).parent / "testdata" / "repos.json").read_text()
    repos = json.loads(repos_json)["repos"]

    python_tools_repo_names = [
        "googleapis/releasetool",
        "googleapis/synthtool",
    ]

    repo_names = python_tools_repo_names + [repo["repo"] for repo in repos]
    languages = set()
    for name in repo_names:
        language = _determine_language(lambda: repos_json, name)
        languages.add((language, name))
    for language, name in sorted(languages):
        test_name = repo_name_to_test_name(name)
        print(
            f"""def {test_name}():
    gh = github.GitHub(os.environ["GITHUB_TOKEN"])
    assert {repr(language)} == guess_language(gh, {repr(name)})

"""
        )


def test_guess_google_cloud_platform_cpp_docs_samples():

    gh = github.GitHub(os.environ["GITHUB_TOKEN"])