feat: load complete mobile conversations (#209)
This commit is contained in:
1 parent
e04d6753ac
commit
c63b23865c
9 files changed
+528
-30
No files matched your search
+56
-15
@@ -942,10 +942,58 @@ async def update_assigned_issue_milestone(
|
||||
}
|
||||
|
||||
|
||||
async def issue_conversation_page(
|
||||
repository: str,
|
||||
number: int,
|
||||
page: int | None = None,
|
||||
limit: int = 20,
|
||||
) -> dict:
|
||||
"""Return one bounded comment page, opening on the newest page by default."""
|
||||
bounded_limit = min(50, max(1, limit))
|
||||
requested_page = max(1, page or 1)
|
||||
path = f"/api/v1/repos/{repository}/issues/{number}/comments"
|
||||
|
||||
async def load(selected_page: int) -> tuple[list, int]:
|
||||
response = await _get_client().get(
|
||||
path,
|
||||
headers=_auth(),
|
||||
params={"limit": bounded_limit, "page": selected_page},
|
||||
)
|
||||
response.raise_for_status()
|
||||
payload = response.json()
|
||||
if not isinstance(payload, list):
|
||||
raise ValueError("Gitea issue comments response was not a list")
|
||||
comments = [item for item in payload if isinstance(item, dict)]
|
||||
try:
|
||||
total = max(len(comments), int(response.headers.get("X-Total-Count", len(comments))))
|
||||
except (TypeError, ValueError):
|
||||
total = len(comments)
|
||||
return comments, total
|
||||
|
||||
comments, total = await load(requested_page)
|
||||
newest_page = max(1, (total + bounded_limit - 1) // bounded_limit)
|
||||
if len(comments) >= total:
|
||||
selected_page = newest_page if page is None else min(requested_page, newest_page)
|
||||
start = (selected_page - 1) * bounded_limit
|
||||
comments = comments[start:start + bounded_limit]
|
||||
else:
|
||||
selected_page = requested_page
|
||||
if page is None and newest_page != requested_page:
|
||||
selected_page = newest_page
|
||||
comments, confirmed_total = await load(selected_page)
|
||||
total = max(total, confirmed_total)
|
||||
return {
|
||||
"comments": [_normalize_issue_comment(item) for item in comments],
|
||||
"page": selected_page,
|
||||
"older_page": selected_page - 1 if selected_page > 1 else None,
|
||||
"total": total,
|
||||
}
|
||||
|
||||
|
||||
async def issue_detail(repository: str, number: int) -> dict:
|
||||
base = f"repos/{repository}/issues/{number}"
|
||||
issue, comments = await asyncio.gather(
|
||||
fetch(base), fetch(f"{base}/comments?limit=20&page=1")
|
||||
issue, conversation = await asyncio.gather(
|
||||
fetch(base), issue_conversation_page(repository, number)
|
||||
)
|
||||
if not isinstance(issue, dict):
|
||||
raise ValueError("Gitea issue response was not an object")
|
||||
@@ -953,12 +1001,7 @@ async def issue_detail(repository: str, number: int) -> dict:
|
||||
labels: list = labels_value if isinstance(labels_value, list) else []
|
||||
assignees_value = issue.get("assignees")
|
||||
assignees: list = assignees_value if isinstance(assignees_value, list) else []
|
||||
comments_value: list = comments if isinstance(comments, list) else []
|
||||
normalized_comments = [
|
||||
_normalize_issue_comment(comment)
|
||||
for comment in comments_value
|
||||
if isinstance(comment, dict)
|
||||
]
|
||||
normalized_comments = conversation["comments"]
|
||||
return {
|
||||
"repository": repository,
|
||||
"number": number,
|
||||
@@ -984,6 +1027,7 @@ async def issue_detail(repository: str, number: int) -> dict:
|
||||
if isinstance(assignee, dict) and isinstance(assignee.get("login"), str)
|
||||
],
|
||||
"comments": normalized_comments,
|
||||
"conversation": conversation,
|
||||
}
|
||||
|
||||
|
||||
@@ -1159,10 +1203,10 @@ async def pull_completion_detail(repository: str, number: int) -> dict:
|
||||
raise ValueError("Gitea pull request response was not an object")
|
||||
head = pull.get("head") if isinstance(pull.get("head"), dict) else {}
|
||||
sha = head.get("sha") if isinstance(head.get("sha"), str) else ""
|
||||
files, status, comments, diff_result = await asyncio.gather(
|
||||
files, status, conversation, diff_result = await asyncio.gather(
|
||||
fetch(f"{base}/files"),
|
||||
fetch(f"repos/{repository}/commits/{sha}/status"),
|
||||
fetch(f"repos/{repository}/issues/{number}/comments?limit=20&page=1"),
|
||||
issue_conversation_page(repository, number),
|
||||
fetch_text(
|
||||
f"repos/{repository}/pulls/{number}.diff", REVIEW_DIFF_MAX_BYTES
|
||||
),
|
||||
@@ -1202,11 +1246,8 @@ async def pull_completion_detail(repository: str, number: int) -> dict:
|
||||
for item in (files if isinstance(files, list) else [])[:100]
|
||||
if isinstance(item, dict) and isinstance(item.get("filename"), str)
|
||||
],
|
||||
"comments": [
|
||||
_normalize_issue_comment(item)
|
||||
for item in (comments if isinstance(comments, list) else [])[:20]
|
||||
if isinstance(item, dict)
|
||||
],
|
||||
"comments": conversation["comments"],
|
||||
"conversation": conversation,
|
||||
}
|
||||
|
||||
|
||||
|
||||
+58
@@ -1415,6 +1415,35 @@ async def create_assigned_issue(
|
||||
return JSONResponse(result, status_code=201)
|
||||
|
||||
|
||||
@app.get("/api/v1/repos/{owner}/{repo}/issues/{number}/comments")
|
||||
async def assigned_issue_conversation(
|
||||
owner: str,
|
||||
repo: str,
|
||||
number: int = PathParam(gt=0),
|
||||
page: int | None = Query(default=None, ge=1, le=100),
|
||||
limit: int = Query(default=20, ge=1, le=50),
|
||||
):
|
||||
repository = f"{owner}/{repo}"
|
||||
|
||||
async def load_conversation():
|
||||
if not await gitea_proxy.is_assigned_issue(repository, number):
|
||||
raise HTTPException(status_code=404, detail="Assigned issue not found")
|
||||
return await gitea_proxy.issue_conversation_page(repository, number, page, limit)
|
||||
|
||||
try:
|
||||
return await asyncio.wait_for(
|
||||
load_conversation(), timeout=ISSUE_ACTION_TIMEOUT_SECONDS
|
||||
)
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception:
|
||||
return JSONResponse(
|
||||
{"error": "The conversation could not be loaded. Your draft is safe; please retry."},
|
||||
status_code=503,
|
||||
headers={"Retry-After": "1"},
|
||||
)
|
||||
|
||||
|
||||
@app.post("/api/v1/repos/{owner}/{repo}/issues/{number}/comments", status_code=201)
|
||||
async def comment_on_assigned_issue(
|
||||
comment: IssueComment,
|
||||
@@ -1587,6 +1616,35 @@ async def assigned_pull_detail(owner: str, repo: str, number: int = PathParam(gt
|
||||
)
|
||||
|
||||
|
||||
@app.get("/api/v1/repos/{owner}/{repo}/pulls/{number}/comments")
|
||||
async def assigned_pull_conversation(
|
||||
owner: str,
|
||||
repo: str,
|
||||
number: int = PathParam(gt=0),
|
||||
page: int | None = Query(default=None, ge=1, le=100),
|
||||
limit: int = Query(default=20, ge=1, le=50),
|
||||
):
|
||||
repository = f"{owner}/{repo}"
|
||||
|
||||
async def load_conversation():
|
||||
if not await gitea_proxy.is_assigned_pull(repository, number):
|
||||
raise HTTPException(status_code=404, detail="Assigned pull request not found")
|
||||
return await gitea_proxy.issue_conversation_page(repository, number, page, limit)
|
||||
|
||||
try:
|
||||
return await asyncio.wait_for(
|
||||
load_conversation(), timeout=ISSUE_ACTION_TIMEOUT_SECONDS
|
||||
)
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception:
|
||||
return JSONResponse(
|
||||
{"error": "The conversation could not be loaded. Your draft is safe; please retry."},
|
||||
status_code=503,
|
||||
headers={"Retry-After": "1"},
|
||||
)
|
||||
|
||||
|
||||
@app.post("/api/v1/repos/{owner}/{repo}/pulls/{number}/comments", status_code=201)
|
||||
async def comment_on_assigned_pull(
|
||||
comment: IssueComment,
|
||||
|
||||
Reference in new issue
Block a user