Skip to content

Commit 412abdd

Browse files
committed
Phase 10B: Repositories and Code Reviews pages, commits/issues/PR endpoints
1 parent 979a829 commit 412abdd

38 files changed

Lines changed: 2315 additions & 53 deletions

‎apps/api/app/api/v1/issues.py‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import uuid
2+
from typing import Annotated, Literal
3+
4+
from fastapi import APIRouter, Query
5+
from sqlalchemy import func, select
6+
7+
from app.api.deps import AnyMember, DbSession
8+
from app.db.models import GithubInstallation, Issue, Repository
9+
from app.schemas import IssueOut
10+
11+
router = APIRouter(tags=["issues"])
12+
13+
14+
@router.get("/orgs/{org_id}/issues", response_model=list[IssueOut])
15+
def list_issues(
16+
ctx: AnyMember,
17+
db: DbSession,
18+
repo_id: uuid.UUID | None = None,
19+
state: Literal["open", "closed", "all"] = "open",
20+
q: Annotated[str | None, Query(max_length=100)] = None,
21+
limit: Annotated[int, Query(ge=1, le=100)] = 30,
22+
offset: Annotated[int, Query(ge=0, le=100_000)] = 0,
23+
) -> list[IssueOut]:
24+
"""Issues GitHub has told us about, most recently updated first. `q` matches the title."""
25+
stmt = (
26+
select(Issue, Repository.full_name)
27+
.join(Repository, Repository.id == Issue.repo_id)
28+
.join(GithubInstallation, GithubInstallation.id == Repository.installation_id)
29+
.where(GithubInstallation.org_id == ctx.org.id)
30+
.order_by(Issue.gh_updated_at.desc().nulls_last(), Issue.number.desc())
31+
.limit(limit)
32+
.offset(offset)
33+
)
34+
if repo_id is not None:
35+
stmt = stmt.where(Issue.repo_id == repo_id)
36+
if state != "all":
37+
stmt = stmt.where(Issue.state == state)
38+
if q and q.strip():
39+
stmt = stmt.where(func.lower(Issue.title).contains(q.strip().lower(), autoescape=True))
40+
return [
41+
IssueOut(
42+
id=i.id,
43+
repository_id=i.repo_id,
44+
repository=full_name,
45+
number=i.number,
46+
title=i.title,
47+
state=i.state,
48+
author=i.author_login,
49+
comments=i.comments,
50+
html_url=i.html_url,
51+
created_at=i.gh_created_at,
52+
updated_at=i.gh_updated_at,
53+
)
54+
for i, full_name in db.execute(stmt).all()
55+
]

‎apps/api/app/api/v1/repos.py‎

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import uuid
2-
from typing import Any
2+
from typing import Annotated, Any
33

4-
from fastapi import APIRouter
4+
from fastapi import APIRouter, HTTPException, Query, status
55
from sqlalchemy import case, func, select
66
from sqlalchemy.orm import Session
77

@@ -103,6 +103,8 @@ def list_repos(ctx: AnyMember, db: DbSession) -> list[RepoOut]:
103103
RepoOut(
104104
id=r.id,
105105
full_name=r.full_name,
106+
owner=r.owner_login,
107+
description=r.description,
106108
private=r.private,
107109
archived=r.archived,
108110
language=r.language,
@@ -159,3 +161,39 @@ def overview(ctx: AnyMember, db: DbSession) -> OverviewOut:
159161
),
160162
recent_commits=commits,
161163
)
164+
165+
166+
@router.get("/orgs/{org_id}/repos/{repo_id}/commits", response_model=list[CommitOut])
167+
def repo_commits(
168+
repo_id: uuid.UUID,
169+
ctx: AnyMember,
170+
db: DbSession,
171+
limit: Annotated[int, Query(ge=1, le=100)] = 30,
172+
offset: Annotated[int, Query(ge=0, le=100_000)] = 0,
173+
) -> list[CommitOut]:
174+
"""The stored commits of one repository, newest first."""
175+
repo = db.scalar(
176+
select(Repository)
177+
.join(GithubInstallation, GithubInstallation.id == Repository.installation_id)
178+
.where(Repository.id == repo_id, GithubInstallation.org_id == ctx.org.id)
179+
)
180+
if repo is None:
181+
raise HTTPException(status.HTTP_404_NOT_FOUND, "Repository not found")
182+
rows = db.scalars(
183+
select(Commit)
184+
.where(Commit.repo_id == repo.id)
185+
.order_by(Commit.committed_at.desc().nulls_last(), Commit.sha)
186+
.limit(limit)
187+
.offset(offset)
188+
)
189+
return [
190+
CommitOut(
191+
sha=c.sha,
192+
message=c.message_headline,
193+
author=c.author_login or c.author_name,
194+
repository=repo.full_name,
195+
committed_at=c.committed_at,
196+
url=c.html_url,
197+
)
198+
for c in rows
199+
]

‎apps/api/app/api/v1/reviews.py‎

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -118,24 +118,8 @@ def _check_review_rate(db: Session, user_id: uuid.UUID, per_minute: int) -> None
118118
# ---------- routes ----------
119119

120120

121-
@router.get("/orgs/{org_id}/pull-requests", response_model=list[PullRequestOut])
122-
def list_pull_requests(
123-
ctx: AnyMember,
124-
db: DbSession,
125-
state: Literal["open", "all"] = "open",
126-
limit: Annotated[int, Query(ge=1, le=100)] = 50,
127-
) -> list[PullRequestOut]:
128-
stmt = (
129-
select(PullRequest, Repository.full_name)
130-
.join(Repository, Repository.id == PullRequest.repo_id)
131-
.join(GithubInstallation, GithubInstallation.id == Repository.installation_id)
132-
.where(GithubInstallation.org_id == ctx.org.id)
133-
.order_by(PullRequest.gh_updated_at.desc().nulls_last(), PullRequest.number.desc())
134-
.limit(limit)
135-
)
136-
if state == "open":
137-
stmt = stmt.where(PullRequest.state == "open")
138-
rows = db.execute(stmt).all()
121+
def _pull_request_rows(db: Session, rows: list[tuple[PullRequest, str]]) -> list[PullRequestOut]:
122+
"""Pull requests with a summary of their newest AI review."""
139123
ids = [pr.id for pr, _ in rows]
140124

141125
newest: dict[uuid.UUID, PullRequestReview] = {}
@@ -192,6 +176,44 @@ def list_pull_requests(
192176
return out
193177

194178

179+
@router.get("/orgs/{org_id}/pull-requests", response_model=list[PullRequestOut])
180+
def list_pull_requests(
181+
ctx: AnyMember,
182+
db: DbSession,
183+
state: Literal["open", "all"] = "open",
184+
repo_id: uuid.UUID | None = None,
185+
q: Annotated[str | None, Query(max_length=100)] = None,
186+
limit: Annotated[int, Query(ge=1, le=100)] = 50,
187+
offset: Annotated[int, Query(ge=0, le=100_000)] = 0,
188+
) -> list[PullRequestOut]:
189+
"""Pull requests, most recently updated first. `q` matches the title."""
190+
stmt = (
191+
select(PullRequest, Repository.full_name)
192+
.join(Repository, Repository.id == PullRequest.repo_id)
193+
.join(GithubInstallation, GithubInstallation.id == Repository.installation_id)
194+
.where(GithubInstallation.org_id == ctx.org.id)
195+
.order_by(PullRequest.gh_updated_at.desc().nulls_last(), PullRequest.number.desc())
196+
.limit(limit)
197+
.offset(offset)
198+
)
199+
if state == "open":
200+
stmt = stmt.where(PullRequest.state == "open")
201+
if repo_id is not None:
202+
stmt = stmt.where(PullRequest.repo_id == repo_id)
203+
if q and q.strip():
204+
needle = q.strip().lower()
205+
stmt = stmt.where(func.lower(PullRequest.title).contains(needle, autoescape=True))
206+
return _pull_request_rows(db, [(pr, name) for pr, name in db.execute(stmt).all()])
207+
208+
209+
@router.get("/orgs/{org_id}/pull-requests/{pr_id}", response_model=PullRequestOut)
210+
def get_pull_request(pr_id: uuid.UUID, ctx: AnyMember, db: DbSession) -> PullRequestOut:
211+
pr = _pr_in_org(db, ctx.org.id, pr_id)
212+
repo = db.get(Repository, pr.repo_id)
213+
assert repo is not None # the foreign key guarantees it
214+
return _pull_request_rows(db, [(pr, repo.full_name)])[0]
215+
216+
195217
@router.get("/orgs/{org_id}/pull-requests/{pr_id}/review", response_model=ReviewOut | None)
196218
def get_review(pr_id: uuid.UUID, ctx: AnyMember, db: DbSession) -> ReviewOut | None:
197219
pr = _pr_in_org(db, ctx.org.id, pr_id)

‎apps/api/app/api/v1/router.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
github,
99
health,
1010
indexing,
11+
issues,
1112
me,
1213
orgs,
1314
repos,
@@ -27,6 +28,7 @@
2728
api_router.include_router(indexing.router)
2829
api_router.include_router(reviews.router)
2930
api_router.include_router(ci.router)
31+
api_router.include_router(issues.router)
3032
api_router.include_router(dashboard.router)
3133
api_router.include_router(repos.router)
3234
api_router.include_router(webhooks.router)

‎apps/api/app/schemas.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ class IndexOut(BaseModel):
141141
class RepoOut(BaseModel):
142142
id: uuid.UUID
143143
full_name: str
144+
owner: str
145+
description: str | None
144146
private: bool
145147
archived: bool
146148
language: str | None
@@ -531,3 +533,20 @@ class DashboardOut(BaseModel):
531533
recent_reviews: list[DashboardReviewOut]
532534
recent_failed_runs: list[DashboardRunOut]
533535
open_issues: list[DashboardIssueOut]
536+
537+
538+
# ---------- issues ----------
539+
540+
541+
class IssueOut(BaseModel):
542+
id: uuid.UUID
543+
repository_id: uuid.UUID
544+
repository: str
545+
number: int
546+
title: str
547+
state: str # open | closed
548+
author: str | None
549+
comments: int
550+
html_url: str
551+
created_at: datetime | None
552+
updated_at: datetime | None

0 commit comments

Comments
 (0)