# Path: cardinal/decorators.py
# def command(triggers):
#     if isinstance(triggers, str):
#         triggers = [triggers]
# 
#     if not isinstance(triggers, list):
#         raise TypeError("Command must be a trigger string or list of triggers")
# 
#     def wrap(f):
#         f.commands = triggers
#         return f
# 
#     return wrap
# 
# def event(triggers):
#     if isinstance(triggers, str):
#         triggers = [triggers]
# 
#     if not isinstance(triggers, list):
#         raise TypeError("Event must be a trigger string or list of triggers")
# 
#     def wrap(f):
#         f.events = triggers
#         return f
# 
#     return wrap
# 
# def help(lines):
#     # For backwards compatibility
#     if isinstance(lines, str):
#         lines = [lines]
# 
#     if not isinstance(lines, list):
#         raise TypeError("Help must be a help string or list of help strings")
# 
#     def wrap(f):
#         # Create help list or prepend to it
#         if not hasattr(f, 'help'):
#             f.help = lines
#         else:
#             f.help = lines + f.help
# 
#         return f
# 
#     return wrap
# 
# Path: cardinal/exceptions.py
# class EventRejectedMessage(CardinalException):
#     """Raised when an event callback wants to reject an event."""


import re
import logging
import requests
from cardinal.decorators import command, event, help
from cardinal.exceptions import EventRejectedMessage
from twisted.internet import defer
from twisted.internet.threads import deferToThread
ISSUE_URL_REGEX = re.compile(
    r'https://(?:www\.)?github\..{2,4}/([^/]+)/([^/]+)/(?:issues|pull)/([0-9]+)',  # noqa: E501
    flags=re.IGNORECASE)
REPO_NAME_REGEX = re.compile(
    r'^[a-z0-9-]+/[a-z0-9_-]+$',
    flags=re.IGNORECASE)


class GithubPlugin:
    logger = None
    """Logging object for GithubPlugin"""

    default_repo = None
    """Default repository to select the issues from"""

    max_show_issues = 1
    """Max number of issues to show for a search. -1 means all"""

    def __init__(self, cardinal, config):
        # Initialize logging
        self.logger = logging.getLogger(__name__)

        if 'default_repo' in config and config['default_repo']:
            self.default_repo = config['default_repo']

        if 'max_show_issues' in config and config['max_show_issues']:
            self.max_show_issues = config['max_show_issues']

    @command('issue')
    @help("Find a Github repo or issue (or combination thereof)")

# Path: cardinal/decorators.py
# def command(triggers):
#     if isinstance(triggers, str):
#         triggers = [triggers]
# 
#     if not isinstance(triggers, list):
#         raise TypeError("Command must be a trigger string or list of triggers")
# 
#     def wrap(f):
#         f.commands = triggers
#         return f
# 
#     return wrap
# 
# def event(triggers):
#     if isinstance(triggers, str):
#         triggers = [triggers]
# 
#     if not isinstance(triggers, list):
#         raise TypeError("Event must be a trigger string or list of triggers")
# 
#     def wrap(f):
#         f.events = triggers
#         return f
# 
#     return wrap
# 
# def help(lines):
#     # For backwards compatibility
#     if isinstance(lines, str):
#         lines = [lines]
# 
#     if not isinstance(lines, list):
#         raise TypeError("Help must be a help string or list of help strings")
# 
#     def wrap(f):
#         # Create help list or prepend to it
#         if not hasattr(f, 'help'):
#             f.help = lines
#         else:
#             f.help = lines + f.help
# 
#         return f
# 
#     return wrap
# 
# Path: cardinal/exceptions.py
# class EventRejectedMessage(CardinalException):
#     """Raised when an event callback wants to reject an event."""


# Path: plugins/github/plugin.py
import re
import logging
import requests
from cardinal.decorators import command, event, help
from cardinal.exceptions import EventRejectedMessage
from twisted.internet import defer
from twisted.internet.threads import deferToThread

ISSUE_URL_REGEX = re.compile(
    r'https://(?:www\.)?github\..{2,4}/([^/]+)/([^/]+)/(?:issues|pull)/([0-9]+)',  # noqa: E501
    flags=re.IGNORECASE)
REPO_NAME_REGEX = re.compile(
    r'^[a-z0-9-]+/[a-z0-9_-]+$',
    flags=re.IGNORECASE)


class GithubPlugin:
    logger = None
    """Logging object for GithubPlugin"""

    default_repo = None
    """Default repository to select the issues from"""

    max_show_issues = 1
    """Max number of issues to show for a search. -1 means all"""

    def __init__(self, cardinal, config):
        # Initialize logging
        self.logger = logging.getLogger(__name__)

        if 'default_repo' in config and config['default_repo']:
            self.default_repo = config['default_repo']

        if 'max_show_issues' in config and config['max_show_issues']:
            self.max_show_issues = config['max_show_issues']

    @command('issue')
    @help("Find a Github repo or issue (or combination thereof)")

    @help("Syntax: .issue [username/repository] <id or search query>")