feat: resolve shared My Work routes
This commit is contained in:
1 parent
2fecccfbde
commit
b5545e380e
6 files changed
+328
-3
No files matched your search
@@ -22,6 +22,10 @@ class WorkItems(list[dict]):
|
||||
self.pagination = pagination
|
||||
|
||||
|
||||
class WorkRouteUnavailableError(ValueError):
|
||||
"""Raised when a shared route no longer belongs in the current user's queue."""
|
||||
|
||||
|
||||
class StaleReviewError(ValueError):
|
||||
"""Raised before mutation when a pull request head changed during review."""
|
||||
|
||||
@@ -1040,6 +1044,57 @@ def _login_in_users(login: str, value: object) -> bool:
|
||||
)
|
||||
|
||||
|
||||
async def resolve_work_route(
|
||||
kind: str,
|
||||
repository: str | None,
|
||||
number: int | None,
|
||||
notification_id: int | None,
|
||||
) -> dict:
|
||||
if kind == "update":
|
||||
if notification_id is None:
|
||||
raise WorkRouteUnavailableError("Notification identity is missing")
|
||||
thread = await fetch(f"notifications/threads/{notification_id}")
|
||||
if not isinstance(thread, dict) or thread.get("unread") is not True:
|
||||
raise WorkRouteUnavailableError("Notification is no longer unread")
|
||||
detail = await notification_detail(notification_id)
|
||||
return {
|
||||
"kind": "update",
|
||||
"notification_id": notification_id,
|
||||
"has_update": True,
|
||||
"repository": detail.get("repository", ""),
|
||||
"title": detail.get("title", ""),
|
||||
"url": detail.get("url", ""),
|
||||
}
|
||||
if repository is None or number is None or kind not in {"issue", "pull", "review"}:
|
||||
raise WorkRouteUnavailableError("Work identity is missing")
|
||||
|
||||
target_path = "issues" if kind == "issue" else "pulls"
|
||||
login, target = await _current_login_and_target(
|
||||
f"repos/{repository}/{target_path}/{number}"
|
||||
)
|
||||
assigned = _login_in_users(login, target.get("assignees"))
|
||||
requested = _login_in_users(login, target.get("requested_reviewers"))
|
||||
eligible = (
|
||||
target.get("state") == "open"
|
||||
and (
|
||||
(kind == "issue" and not isinstance(target.get("pull_request"), dict) and assigned)
|
||||
or (kind == "pull" and assigned)
|
||||
or (kind == "review" and requested)
|
||||
)
|
||||
)
|
||||
if not eligible:
|
||||
raise WorkRouteUnavailableError("Work item is no longer in My Work")
|
||||
return {
|
||||
"kind": kind,
|
||||
"repository": repository,
|
||||
"number": number,
|
||||
"title": target.get("title", "") if isinstance(target.get("title"), str) else "",
|
||||
"state": "open",
|
||||
"url": _safe_web_url(target.get("html_url")),
|
||||
**({"is_review": True, "work_reasons": ["review_requested"]} if kind == "review" else {}),
|
||||
}
|
||||
|
||||
|
||||
async def is_assigned_issue(repository: str, number: int) -> bool:
|
||||
login, issue = await _current_login_and_target(
|
||||
f"repos/{repository}/issues/{number}"
|
||||
|
||||
+28
-1
@@ -397,7 +397,7 @@ app.include_router(frontend_router)
|
||||
@app.middleware("http")
|
||||
async def prevent_live_api_caching(request, call_next):
|
||||
response = await call_next(request)
|
||||
if request.url.path in {"/api/v1/context", "/api/v1/events", "/api/v1/live", "/api/v1/available-issues", "/api/v1/search"} or request.url.path.startswith("/api/v1/work/") or (
|
||||
if request.url.path in {"/api/v1/context", "/api/v1/events", "/api/v1/live", "/api/v1/available-issues", "/api/v1/search", "/api/v1/work-route"} or request.url.path.startswith("/api/v1/work/") or (
|
||||
request.url.path.startswith("/api/v1/repos/")
|
||||
and request.url.path.endswith("/review")
|
||||
) or request.url.path.startswith("/api/v1/notifications") or (
|
||||
@@ -547,6 +547,33 @@ async def global_search_preview(
|
||||
return JSONResponse(preview)
|
||||
|
||||
|
||||
@app.get("/api/v1/work-route")
|
||||
async def resolve_work_route(
|
||||
kind: Literal["issue", "pull", "review", "update"] = Query(),
|
||||
repository: str | None = Query(default=None, pattern=r"^[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+$"),
|
||||
number: int | None = Query(default=None, gt=0),
|
||||
notification_id: int | None = Query(default=None, gt=0),
|
||||
) -> JSONResponse:
|
||||
repository_route = repository is not None and number is not None and notification_id is None
|
||||
update_route = kind == "update" and notification_id is not None and repository is None and number is None
|
||||
if (kind == "update" and not update_route) or (kind != "update" and not repository_route):
|
||||
raise HTTPException(status_code=422, detail="Invalid work route identity")
|
||||
try:
|
||||
result = await asyncio.wait_for(
|
||||
gitea_proxy.resolve_work_route(kind, repository, number, notification_id),
|
||||
timeout=WORK_PAGE_TIMEOUT_SECONDS,
|
||||
)
|
||||
except gitea_proxy.WorkRouteUnavailableError:
|
||||
raise HTTPException(status_code=404, detail="Work item is no longer in My Work")
|
||||
except Exception:
|
||||
return JSONResponse(
|
||||
{"error": "The shared work item is temporarily unavailable. Please retry."},
|
||||
status_code=503,
|
||||
headers={"Retry-After": "1"},
|
||||
)
|
||||
return JSONResponse(result)
|
||||
|
||||
|
||||
@app.get("/api/v1/work/{stream}")
|
||||
async def paged_work(
|
||||
stream: Literal["issue", "pull", "review"],
|
||||
|
||||
Reference in new issue
Block a user