@@ -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 )
196218def get_review (pr_id : uuid .UUID , ctx : AnyMember , db : DbSession ) -> ReviewOut | None :
197219 pr = _pr_in_org (db , ctx .org .id , pr_id )
0 commit comments