import { GithubPullRequestsApi } from './GithubPullRequestsApi' ; import { Octokit } from '@octokit/rest' ; import { PullsListResponseData } from '@octokit/types' ; import { PullRequestState } from '../types' ; export class GithubPullRequestsClient implements GithubPullRequestsApi { async listPullRequests ( { token , owner , repo , pageSize = 5 , page , state = 'all' , baseUrl , } ) { const pullRequestResponse = await new Octokit ( { auth : token , ... ( baseUrl && { baseUrl } ) , } ) . pulls . list ( { repo , state , per_page : pageSize , page , owner , } ) ; const paginationLinks = pullRequestResponse . headers . link ; const lastPage = paginationLinks ?. match ( /\d+(?!.*page=\d*)/g ) || [ '1' ] ; const maxTotalItems = paginationLinks ?. endsWith ( 'rel="last"' ) ? parseInt ( lastPage [ 0 ] , 10 ) * pageSize : undefined ; return { maxTotalItems , pullRequestsData : ( pullRequestResponse . data as any ) as PullsListResponseData , } ; } }