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

1import github 

2 

3from mindsdb_sql_parser import parse_sql 

4 

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 

15 

16 

17logger = log.getLogger(__name__) 

18 

19 

20class GithubHandler(APIHandler): 

21 """The GitHub handler implementation""" 

22 

23 def __init__(self, name: str, **kwargs): 

24 """Initialize the GitHub handler. 

25 

26 Parameters 

27 ---------- 

28 name : str 

29 name of a handler instance 

30 """ 

31 super().__init__(name) 

32 

33 connection_data = kwargs.get("connection_data", {}) 

34 self.connection_data = connection_data 

35 self.repository = connection_data["repository"] 

36 self.kwargs = kwargs 

37 

38 self.connection = None 

39 self.is_connected = False 

40 

41 # custom tables 

42 self._register_table("issues", GithubIssuesTable(self)) 

43 self._register_table("files", GithubFilesTable(self)) 

44 

45 # generated tables 

46 github_types = get_github_types() 

47 

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 

52 

53 table = GHTable(self, github_types=github_types, method=method) 

54 self._register_table(method.table_name, table) 

55 

56 def connect(self) -> StatusResponse: 

57 """Set up the connection required by the handler. 

58 

59 Returns 

60 ------- 

61 StatusResponse 

62 connection object 

63 """ 

64 

65 if self.is_connected is True: 

66 return self.connection 

67 

68 connection_kwargs = {} 

69 

70 if self.connection_data.get("api_key", None): 

71 connection_kwargs["login_or_token"] = self.connection_data["api_key"] 

72 

73 if self.connection_data.get("github_url", None): 

74 connection_kwargs["base_url"] = self.connection_data["github_url"] 

75 

76 self.connection = github.Github(**connection_kwargs) 

77 self.is_connected = True 

78 

79 return self.connection 

80 

81 def check_connection(self) -> StatusResponse: 

82 """Check connection to the handler. 

83 

84 Returns 

85 ------- 

86 StatusResponse 

87 Status confirmation 

88 """ 

89 response = StatusResponse(False) 

90 

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") 

98 

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 

105 

106 self.is_connected = response.success 

107 

108 return response 

109 

110 def native_query(self, query: str) -> StatusResponse: 

111 """Receive and process a raw query. 

112 

113 Parameters 

114 ---------- 

115 query : str 

116 query in a native format 

117 

118 Returns 

119 ------- 

120 StatusResponse 

121 Request status 

122 """ 

123 ast = parse_sql(query) 

124 return self.query(ast)