11import git
22from fastapi import APIRouter , Request
33
4+ from prometheus .app .decorators .require_login import requireLogin
45from prometheus .app .models .response .response import Response
56from prometheus .app .services .knowledge_graph_service import KnowledgeGraphService
67from prometheus .app .services .repository_service import RepositoryService
8+ from prometheus .app .services .user_service import UserService
79from prometheus .exceptions .server_exception import ServerException
810
911router = APIRouter ()
1012
1113
14+ def get_github_token (request : Request , github_token : str ) -> str :
15+ """Retrieve GitHub token from the request or user profile."""
16+ # If the token is provided in the request, use it directly
17+ if github_token :
18+ return github_token
19+ # If the token is not provided, fetch it from the user profile if logged in
20+ # Check if the user is authenticated
21+ if not request .state .user_id :
22+ # If the user is not authenticated, raise an exception
23+ raise ServerException (
24+ code = 401 , message = "GitHub token is required, please provide it or log in"
25+ )
26+ # If the user is authenticated, get the user service and fetch the token
27+ user_service : UserService = request .app .state .service ["user_service" ]
28+ user = user_service .get_user_by_id (request .state .user_id )
29+ github_token = user .github_token if user else None
30+
31+ # If the token is still not available, raise an exception
32+ if not github_token :
33+ raise ServerException (
34+ code = 423 , message = "Either provide a GitHub token or set it in your user profile"
35+ )
36+ return github_token
37+
38+
1239@router .get (
1340 "/github/" ,
1441 description = """
1542 Upload a GitHub repository to Prometheus, default to the latest commit in the main branch.
1643 """ ,
1744 response_model = Response ,
1845)
46+ @requireLogin
1947def upload_github_repository (github_token : str , https_url : str , request : Request ):
2048 # Get the repository and knowledge graph services
2149 repository_service : RepositoryService = request .app .state .service ["repository_service" ]
2250 knowledge_graph_service : KnowledgeGraphService = request .app .state .service [
2351 "knowledge_graph_service"
2452 ]
53+ github_token = get_github_token (request , github_token )
2554
2655 # Clean the services to ensure no previous data is present
2756 repository_service .clean ()
2857 knowledge_graph_service .clear ()
58+
2959 try :
3060 # Clone the repository
3161 saved_path = repository_service .clone_github_repo (github_token , https_url )
@@ -43,6 +73,7 @@ def upload_github_repository(github_token: str, https_url: str, request: Request
4373 """ ,
4474 response_model = Response ,
4575)
76+ @requireLogin
4677def upload_github_repository_at_commit (
4778 github_token , https_url : str , commit_id : str , request : Request
4879):
@@ -52,6 +83,8 @@ def upload_github_repository_at_commit(
5283 "knowledge_graph_service"
5384 ]
5485
86+ github_token = get_github_token (request , github_token )
87+
5588 # Clean the services to ensure no previous data is present
5689 repository_service .clean ()
5790 knowledge_graph_service .clear ()
@@ -73,6 +106,7 @@ def upload_github_repository_at_commit(
73106 """ ,
74107 response_model = Response ,
75108)
109+ @requireLogin
76110def delete (request : Request ):
77111 knowledge_graph_service : KnowledgeGraphService = request .app .state .service [
78112 "knowledge_graph_service"
@@ -88,6 +122,7 @@ def delete(request: Request):
88122 return Response ()
89123
90124
125+ @requireLogin
91126@router .get (
92127 "/exists/" ,
93128 description = """
0 commit comments