|
15 | 15 | from prometheus.app.services.user_service import UserService |
16 | 16 | from prometheus.configuration.config import settings |
17 | 17 | from prometheus.exceptions.server_exception import ServerException |
| 18 | +from prometheus.utils.github_utils import is_repository_public |
18 | 19 |
|
19 | 20 | router = APIRouter() |
20 | 21 |
|
21 | 22 |
|
22 | | -async def get_github_token(request: Request, github_token: str) -> str: |
23 | | - """Retrieve GitHub token from the request or user profile.""" |
| 23 | +async def get_github_token(request: Request, github_token: str | None = None) -> str | None: |
| 24 | + """Retrieve GitHub token from the request or user profile. |
| 25 | +
|
| 26 | + Returns: |
| 27 | + str | None: GitHub token if available, None for public repositories |
| 28 | + """ |
24 | 29 | # If the token is provided in the request, use it directly |
25 | 30 | if github_token: |
26 | 31 | return github_token |
27 | | - # If the token is not provided, fetch it from the user profile if logged in |
28 | | - # Check if the user is authenticated |
29 | | - if not settings.ENABLE_AUTHENTICATION: |
30 | | - # If the user is not authenticated, raise an exception |
31 | | - raise ServerException( |
32 | | - code=400, message="GitHub token is required, please provide it or log in" |
33 | | - ) |
34 | | - # If the user is authenticated, get the user service and fetch the token |
35 | | - user_service: UserService = request.app.state.service["user_service"] |
36 | | - user = await user_service.get_user_by_id(request.state.user_id) |
37 | | - github_token = user.github_token if user else None |
38 | 32 |
|
39 | | - # If the token is still not available, raise an exception |
40 | | - if not github_token: |
41 | | - raise ServerException( |
42 | | - code=400, message="Either provide a GitHub token or set it in your user profile" |
43 | | - ) |
| 33 | + # If the user is authenticated, get the user service and fetch the token |
| 34 | + if settings.ENABLE_AUTHENTICATION: |
| 35 | + user_service: UserService = request.app.state.service["user_service"] |
| 36 | + user = await user_service.get_user_by_id(request.state.user_id) |
| 37 | + github_token = user.github_token if user else None |
44 | 38 | return github_token |
45 | 39 |
|
46 | 40 |
|
@@ -86,17 +80,25 @@ async def upload_github_repository( |
86 | 80 | message=f"You have reached the maximum number of repositories ({settings.DEFAULT_USER_REPOSITORY_LIMIT}). Please delete some repositories before uploading new ones.", |
87 | 81 | ) |
88 | 82 |
|
89 | | - # Get the GitHub token |
| 83 | + # Get the GitHub token (may be None for public repositories) |
90 | 84 | github_token = await get_github_token(request, upload_repository_request.github_token) |
91 | 85 |
|
| 86 | + # Check if the repository is public or private |
| 87 | + is_repository_public_ = await is_repository_public(upload_repository_request.https_url) |
| 88 | + if not is_repository_public_ and not github_token: |
| 89 | + raise ServerException( |
| 90 | + code=400, |
| 91 | + message="This appears to be a private repository. Please provide a GitHub token.", |
| 92 | + ) |
| 93 | + |
92 | 94 | # Clone the repository |
93 | 95 | try: |
94 | 96 | saved_path = await repository_service.clone_github_repo( |
95 | 97 | github_token, upload_repository_request.https_url, upload_repository_request.commit_id |
96 | 98 | ) |
97 | 99 | except git.exc.GitCommandError: |
98 | 100 | raise ServerException( |
99 | | - code=400, message=f"Unable to clone {upload_repository_request.https_url}." |
| 101 | + code=400, message=f"Unable to clone {upload_repository_request.https_url}" |
100 | 102 | ) |
101 | 103 |
|
102 | 104 | # Build and save the knowledge graph from the cloned repository |
|
0 commit comments