# Path: rebasehelper/exceptions.py
# class RebaseHelperError(Exception):
#     """Class representing Error raised inside rebase-helper after intentionally
#     catching some expected and well known exception/error.
# 
#     """
# 
#     def __init__(self, *args, **kwargs):
#         """Constructor of RebaseHelperError"""
#         super().__init__()
#         if not args:
#             self.msg = None
#         elif len(args) > 1:
#             self.msg = args[0] % args[1:]
#         else:
#             self.msg = args[0]
#         self.logfiles = kwargs.get('logfiles')
# 
#     def __str__(self):
#         return str(self.msg) if self.msg else ''
# 
# Path: rebasehelper/logger.py
# class CustomLogger(logging.Logger):
# 
#     TRACE: int = logging.DEBUG + 1
#     VERBOSE: int = logging.DEBUG + 2
#     SUCCESS: int = logging.INFO + 5
#     HEADING: int = logging.INFO + 6
#     IMPORTANT: int = logging.INFO + 7
# 
#     _nameToLevel: Dict[str, int] = {
#         'TRACE': TRACE,
#         'VERBOSE': VERBOSE,
#         'SUCCESS': SUCCESS,
#         'HEADING': HEADING,
#         'IMPORTANT': IMPORTANT,
#     }
# 
#     def __init__(self, name, level=logging.NOTSET):
#         super().__init__(name, level)
# 
#         for lev, severity in self._nameToLevel.items():
#             logging.addLevelName(severity, lev)
# 
#     def __getattr__(self, level):
#         severity = self._nameToLevel.get(level.upper())
# 
#         def log(message, *args, **kwargs):
#             if self.isEnabledFor(severity):
#                 self._log(severity, message, args, **kwargs)
# 
#         if severity:
#             return log
# 
#         raise AttributeError


import logging
import os
import re
import git  # type: ignore
import requests
from typing import cast, Optional, Tuple
from rebasehelper.exceptions import RebaseHelperError
from rebasehelper.logger import CustomLogger


logger: CustomLogger = cast(CustomLogger, logging.getLogger(__name__))


class BugzillaHelper:

    """Class for working with Upstream Release Monitoring on bugzilla."""

    DIST_GIT_REPO_URL = 'https://src.fedoraproject.org/rpms'
    BUGZILLA_REST_API_URL = 'https://bugzilla.redhat.com/rest'
    UPSTREAM_RELEASE_MONITORING_USERNAME = 'upstream-release-monitoring'

    @classmethod
    def get_bugzilla_component(cls, bugzilla_id: str) -> str:
        """Gets a component of the bugzilla.

        Args:
            bugzilla_id: ID of the bugzilla.

        Returns:
            Component of the bugzilla.

        Raises:
            RebaseHelperError: If no such bugzilla exists or if the bugzilla
            was not created by Upstream Release Monitoring.

        """
        r = requests.get('{}/bug/{}'.format(cls.BUGZILLA_REST_API_URL, bugzilla_id))
        if not r.ok:

# Path: rebasehelper/exceptions.py
# class RebaseHelperError(Exception):
#     """Class representing Error raised inside rebase-helper after intentionally
#     catching some expected and well known exception/error.
# 
#     """
# 
#     def __init__(self, *args, **kwargs):
#         """Constructor of RebaseHelperError"""
#         super().__init__()
#         if not args:
#             self.msg = None
#         elif len(args) > 1:
#             self.msg = args[0] % args[1:]
#         else:
#             self.msg = args[0]
#         self.logfiles = kwargs.get('logfiles')
# 
#     def __str__(self):
#         return str(self.msg) if self.msg else ''
# 
# Path: rebasehelper/logger.py
# class CustomLogger(logging.Logger):
# 
#     TRACE: int = logging.DEBUG + 1
#     VERBOSE: int = logging.DEBUG + 2
#     SUCCESS: int = logging.INFO + 5
#     HEADING: int = logging.INFO + 6
#     IMPORTANT: int = logging.INFO + 7
# 
#     _nameToLevel: Dict[str, int] = {
#         'TRACE': TRACE,
#         'VERBOSE': VERBOSE,
#         'SUCCESS': SUCCESS,
#         'HEADING': HEADING,
#         'IMPORTANT': IMPORTANT,
#     }
# 
#     def __init__(self, name, level=logging.NOTSET):
#         super().__init__(name, level)
# 
#         for lev, severity in self._nameToLevel.items():
#             logging.addLevelName(severity, lev)
# 
#     def __getattr__(self, level):
#         severity = self._nameToLevel.get(level.upper())
# 
#         def log(message, *args, **kwargs):
#             if self.isEnabledFor(severity):
#                 self._log(severity, message, args, **kwargs)
# 
#         if severity:
#             return log
# 
#         raise AttributeError


# Path: rebasehelper/helpers/bugzilla_helper.py
import logging
import os
import re
import git  # type: ignore
import requests
from typing import cast, Optional, Tuple
from rebasehelper.exceptions import RebaseHelperError
from rebasehelper.logger import CustomLogger



logger: CustomLogger = cast(CustomLogger, logging.getLogger(__name__))


class BugzillaHelper:

    """Class for working with Upstream Release Monitoring on bugzilla."""

    DIST_GIT_REPO_URL = 'https://src.fedoraproject.org/rpms'
    BUGZILLA_REST_API_URL = 'https://bugzilla.redhat.com/rest'
    UPSTREAM_RELEASE_MONITORING_USERNAME = 'upstream-release-monitoring'

    @classmethod
    def get_bugzilla_component(cls, bugzilla_id: str) -> str:
        """Gets a component of the bugzilla.

        Args:
            bugzilla_id: ID of the bugzilla.

        Returns:
            Component of the bugzilla.

        Raises:
            RebaseHelperError: If no such bugzilla exists or if the bugzilla
            was not created by Upstream Release Monitoring.

        """
        r = requests.get('{}/bug/{}'.format(cls.BUGZILLA_REST_API_URL, bugzilla_id))
        if not r.ok:

            raise RebaseHelperError('Could not obtain data from bugzilla')