From c817fb58f943e7ded80bac59416fec36220e9d66 Mon Sep 17 00:00:00 2001 From: Yue Pan <79363355+dcloud347@users.noreply.github.com> Date: Mon, 11 Aug 2025 01:51:35 +0800 Subject: [PATCH 1/3] Implement user issue credit management in issue processing --- prometheus/app/api/routes/issue.py | 19 +++++++++++++++- prometheus/app/services/user_service.py | 30 +++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/prometheus/app/api/routes/issue.py b/prometheus/app/api/routes/issue.py index e58582c6..05253252 100644 --- a/prometheus/app/api/routes/issue.py +++ b/prometheus/app/api/routes/issue.py @@ -7,6 +7,7 @@ from prometheus.app.services.issue_service import IssueService from prometheus.app.services.knowledge_graph_service import KnowledgeGraphService from prometheus.app.services.repository_service import RepositoryService +from prometheus.app.services.user_service import UserService from prometheus.configuration.config import settings from prometheus.exceptions.server_exception import ServerException @@ -24,19 +25,35 @@ @requireLogin async def answer_issue(issue: IssueRequest, request: Request) -> Response[IssueResponse]: repository_service: RepositoryService = request.app.state.service["repository_service"] + # Fetch the repository by ID repository = repository_service.get_repository_by_id(issue.repository_id) + # Ensure the repository exists if not repository: raise ServerException(code=404, message="Repository not found") + # Ensure the user has access to the repository if settings.ENABLE_AUTHENTICATION and repository.user_id != request.state.user_id: raise ServerException(code=403, message="You do not have access to this repository") + # Deduct issue credit if authentication is enabled + user_service: UserService = request.app.state.service["user_service"] + if settings.ENABLE_AUTHENTICATION: + # Check and deduct issue credit + user_issue_credit = user_service.get_issue_credit(request.state.user_id) + if user_issue_credit <= 0: + raise ServerException( + code=403, + message="Insufficient issue credits. Please purchase more to continue.", + ) + user_service.update_issue_credit(request.state.user_id, user_issue_credit - 1) + + # Validate Dockerfile and workdir inputs if issue.dockerfile_content or issue.image_name: if issue.workdir is None: raise ServerException( code=400, message="workdir must be provided for user defined environment", ) - + # Ensure the repository is not currently being used if repository.is_working: raise ServerException( code=400, diff --git a/prometheus/app/services/user_service.py b/prometheus/app/services/user_service.py index 49fa6030..92d5c628 100644 --- a/prometheus/app/services/user_service.py +++ b/prometheus/app/services/user_service.py @@ -120,3 +120,33 @@ def get_user_by_id(self, user_id: int) -> Optional[User]: with Session(self.engine) as session: statement = select(User).where(User.id == user_id) return session.exec(statement).first() + + def get_issue_credit(self, user_id: int) -> int: + """ + Retrieve the issue credit of a user by their ID. + + Args: + user_id (int): The ID of the user. + + Returns: + int: The issue credit of the user. + """ + with Session(self.engine) as session: + statement = select(User.issue_credit).where(User.id == user_id) + result = session.exec(statement).first() + return result + + def update_issue_credit(self, user_id: int, new_issue_credit) -> None: + """ + Update the issue credit of a user by their ID. + + Args: + user_id (int): The ID of the user. + new_issue_credit (int): The new issue credit. + """ + with Session(self.engine) as session: + statement = select(User).where(User.id == user_id) + user = session.exec(statement).first() + user.issue_credit = new_issue_credit + session.add(user) + session.commit() From c703dca51bf67b6940e3d3793367eb2014f4467f Mon Sep 17 00:00:00 2001 From: Yue Pan <79363355+dcloud347@users.noreply.github.com> Date: Mon, 11 Aug 2025 01:56:52 +0800 Subject: [PATCH 2/3] Fix error code for missing GitHub token in authentication --- prometheus/app/api/routes/repository.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prometheus/app/api/routes/repository.py b/prometheus/app/api/routes/repository.py index 7aff519f..a7bd15da 100644 --- a/prometheus/app/api/routes/repository.py +++ b/prometheus/app/api/routes/repository.py @@ -23,7 +23,7 @@ def get_github_token(request: Request, github_token: str) -> str: if not settings.ENABLE_AUTHENTICATION: # If the user is not authenticated, raise an exception raise ServerException( - code=401, message="GitHub token is required, please provide it or log in" + code=400, message="GitHub token is required, please provide it or log in" ) # If the user is authenticated, get the user service and fetch the token user_service: UserService = request.app.state.service["user_service"] From da885a342c7b2be9cf06519756ce8baa392d8204 Mon Sep 17 00:00:00 2001 From: Yue Pan <79363355+dcloud347@users.noreply.github.com> Date: Mon, 11 Aug 2025 02:01:22 +0800 Subject: [PATCH 3/3] Fix error code for missing GitHub token in authentication --- prometheus/app/api/routes/repository.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prometheus/app/api/routes/repository.py b/prometheus/app/api/routes/repository.py index a7bd15da..f78d44f4 100644 --- a/prometheus/app/api/routes/repository.py +++ b/prometheus/app/api/routes/repository.py @@ -33,7 +33,7 @@ def get_github_token(request: Request, github_token: str) -> str: # If the token is still not available, raise an exception if not github_token: raise ServerException( - code=423, message="Either provide a GitHub token or set it in your user profile" + code=400, message="Either provide a GitHub token or set it in your user profile" ) return github_token