@@ -14,6 +14,7 @@ type PullRequestsPayload struct {
1414type PullRequest struct {
1515 Number int
1616 Title string
17+ State string
1718 URL string
1819 HeadRefName string
1920}
@@ -275,3 +276,96 @@ func PullRequestsForBranch(client *Client, ghRepo Repo, branch string) ([]PullRe
275276
276277 return prs , nil
277278}
279+
280+ func PullRequestList (client * Client , vars map [string ]interface {}, limit int ) ([]PullRequest , error ) {
281+ type response struct {
282+ Repository struct {
283+ PullRequests struct {
284+ Edges []struct {
285+ Node PullRequest
286+ }
287+ PageInfo struct {
288+ HasNextPage bool
289+ EndCursor string
290+ }
291+ }
292+ }
293+ }
294+
295+ query := `
296+ query(
297+ $owner: String!,
298+ $repo: String!,
299+ $limit: Int!,
300+ $endCursor: String,
301+ $baseBranch: String,
302+ $labels: [String!],
303+ $state: [PullRequestState!] = OPEN
304+ ) {
305+ repository(owner: $owner, name: $repo) {
306+ pullRequests(
307+ states: $state,
308+ baseRefName: $baseBranch,
309+ labels: $labels,
310+ first: $limit,
311+ after: $endCursor,
312+ orderBy: {field: CREATED_AT, direction: DESC}
313+ ) {
314+ edges {
315+ node {
316+ number
317+ title
318+ state
319+ url
320+ headRefName
321+ }
322+ }
323+ pageInfo {
324+ hasNextPage
325+ endCursor
326+ }
327+ }
328+ }
329+ }`
330+
331+ prs := []PullRequest {}
332+ pageLimit := min (limit , 100 )
333+ variables := map [string ]interface {}{}
334+ for name , val := range vars {
335+ variables [name ] = val
336+ }
337+
338+ for {
339+ variables ["limit" ] = pageLimit
340+ var data response
341+ err := client .GraphQL (query , variables , & data )
342+ if err != nil {
343+ return nil , err
344+ }
345+ prData := data .Repository .PullRequests
346+
347+ for _ , edge := range prData .Edges {
348+ prs = append (prs , edge .Node )
349+ if len (prs ) == limit {
350+ goto done
351+ }
352+ }
353+
354+ if prData .PageInfo .HasNextPage {
355+ variables ["endCursor" ] = prData .PageInfo .EndCursor
356+ pageLimit = min (pageLimit , limit - len (prs ))
357+ continue
358+ }
359+ done:
360+ break
361+ }
362+
363+ return prs , nil
364+ }
365+
366+ func min (a , b int ) int {
367+ if a < b {
368+ return a
369+ }
370+ return b
371+ }
0 commit comments