from typing import List
from typing import Tuple
import re


def get_issues_in_text(repo_name: str, text: str) -> List[Tuple[str, int]]:
    """
    See https://docs.github.com/en/free-pro-team@latest/github/writing-on-github/
            autolinked-references-and-urls#issues-and-pull-requests
    :param repo_name: full repo name like "username/repository_name"
    :param text: the text to extract linked issues (e.g. commits)
    :return: Tuples of (repo_name, issue_number)
    """
    pattern = r"([\w\.\-_]+/[\w\.\-_]+)#(\d+)|#(\d+)|GH-(\d+)"
    result = []
    for match in re.finditer(pattern, text):
        # print(match.string)
        # print(text[match.start():match.end()])
        # print(match.groups())
        groups = match.groups()
        if groups[0] is not None and groups[1] is not None:
            result.append((groups[0], int(groups[1])))
        elif groups[2] is not None:
            result.append((repo_name, int(groups[2])))
        elif groups[3] is not None:
            result.append((repo_name, int(groups[3])))
    url_pattern = r"https://github\.com/([\w\.\-_]+/[\w\.\-_]+)/(issues|pull)/(\d+)"
    for match in re.finditer(url_pattern, text):
        result.append((match.groups()[0], int(match.groups()[2])))
    return result