Coverage for mindsdb / integrations / handlers / github_handler / github_handler.py: 0%
55 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-21 00:36 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-21 00:36 +0000
1import github
3from mindsdb_sql_parser import parse_sql
5from mindsdb.integrations.handlers.github_handler.github_tables import (
6 GithubIssuesTable,
7 GithubFilesTable
8)
9from mindsdb.integrations.handlers.github_handler.generate_api import get_github_types, get_github_methods, GHTable
10from mindsdb.integrations.libs.api_handler import APIHandler
11from mindsdb.integrations.libs.response import (
12 HandlerStatusResponse as StatusResponse,
13)
14from mindsdb.utilities import log
17logger = log.getLogger(__name__)
20class GithubHandler(APIHandler):
21 """The GitHub handler implementation"""
23 def __init__(self, name: str, **kwargs):
24 """Initialize the GitHub handler.
26 Parameters
27 ----------
28 name : str
29 name of a handler instance
30 """
31 super().__init__(name)
33 connection_data = kwargs.get("connection_data", {})
34 self.connection_data = connection_data
35 self.repository = connection_data["repository"]
36 self.kwargs = kwargs
38 self.connection = None
39 self.is_connected = False
41 # custom tables
42 self._register_table("issues", GithubIssuesTable(self))
43 self._register_table("files", GithubFilesTable(self))
45 # generated tables
46 github_types = get_github_types()
48 # generate tables from repository object
49 for method in get_github_methods(github.Repository.Repository):
50 if method.table_name in self._tables:
51 continue
53 table = GHTable(self, github_types=github_types, method=method)
54 self._register_table(method.table_name, table)
56 def connect(self) -> StatusResponse:
57 """Set up the connection required by the handler.
59 Returns
60 -------
61 StatusResponse
62 connection object
63 """
65 if self.is_connected is True:
66 return self.connection
68 connection_kwargs = {}
70 if self.connection_data.get("api_key", None):
71 connection_kwargs["login_or_token"] = self.connection_data["api_key"]
73 if self.connection_data.get("github_url", None):
74 connection_kwargs["base_url"] = self.connection_data["github_url"]
76 self.connection = github.Github(**connection_kwargs)
77 self.is_connected = True
79 return self.connection
81 def check_connection(self) -> StatusResponse:
82 """Check connection to the handler.
84 Returns
85 -------
86 StatusResponse
87 Status confirmation
88 """
89 response = StatusResponse(False)
91 try:
92 self.connect()
93 if self.connection_data.get("api_key", None):
94 current_user = self.connection.get_user().name
95 logger.info(f"Authenticated as user {current_user}")
96 else:
97 logger.info("Proceeding without an API key")
99 current_limit = self.connection.get_rate_limit()
100 logger.info(f"Current rate limit: {current_limit}")
101 response.success = True
102 except Exception as e:
103 logger.error(f"Error connecting to GitHub API: {e}!")
104 response.error_message = e
106 self.is_connected = response.success
108 return response
110 def native_query(self, query: str) -> StatusResponse:
111 """Receive and process a raw query.
113 Parameters
114 ----------
115 query : str
116 query in a native format
118 Returns
119 -------
120 StatusResponse
121 Request status
122 """
123 ast = parse_sql(query)
124 return self.query(ast)