Add a /api/ticket endpoint for ticket info (#122)

* Add a /api/ticket endpoint!

* Add some validation and user-friendly error msgs
This commit is contained in:
Mish 2025-11-23 19:13:36 +00:00 committed by GitHub
parent df7ab83cb4
commit 5567cc7673
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 40 additions and 0 deletions

38
nephthys/api/ticket.py Normal file
View file

@ -0,0 +1,38 @@
from starlette.requests import Request
from starlette.responses import JSONResponse
from nephthys.utils.env import env
async def ticket_info(req: Request):
try:
ticket_id = int(req.query_params["id"])
except KeyError:
return JSONResponse({"error": "missing_ticket_id"}, status_code=400)
except ValueError:
return JSONResponse({"error": "invalid_ticket_id"}, status_code=400)
ticket = await env.db.ticket.find_unique(
where={"id": ticket_id},
include={"openedBy": True, "closedBy": True, "assignedTo": True},
)
if not ticket:
return JSONResponse({"error": "ticket_not_found"}, status_code=404)
return JSONResponse(
{
"title": ticket.title,
"description": ticket.description,
"status": ticket.status,
"opened_by": {"slack_id": ticket.openedBy.slackId}
if ticket.openedBy
else None,
"closed_by": {"slack_id": ticket.closedBy.slackId}
if ticket.closedBy
else None,
"assigned_to": {"slack_id": ticket.assignedTo.slackId}
if ticket.assignedTo
else None,
"created_at": ticket.createdAt.isoformat(),
}
)

View file

@ -11,6 +11,7 @@ from starlette_exporter import PrometheusMiddleware
from nephthys.__main__ import main
from nephthys.api.stats import stats
from nephthys.api.ticket import ticket_info
from nephthys.api.user import user_stats
from nephthys.utils.env import env
from nephthys.utils.slack import app as slack_app
@ -59,6 +60,7 @@ app = Starlette(
Route(path="/slack/events", endpoint=endpoint, methods=["POST"]),
Route(path="/api/stats", endpoint=stats, methods=["GET"]),
Route(path="/api/user", endpoint=user_stats, methods=["GET"]),
Route(path="/api/ticket", endpoint=ticket_info, methods=["GET"]),
Route(path="/health", endpoint=health, methods=["GET"]),
Route(path="/metrics", endpoint=metrics, methods=["GET"]),
],