( prSchema: PrSchema, prNumber: number, git: AuthenticatedGitClient, )
| 14 | * Gets the given pull request from Github using the GraphQL API endpoint. |
| 15 | */ |
| 16 | export async function getPr<PrSchema>( |
| 17 | prSchema: PrSchema, |
| 18 | prNumber: number, |
| 19 | git: AuthenticatedGitClient, |
| 20 | ): Promise<PrSchema | null> { |
| 21 | /** The owner and name of the repository */ |
| 22 | const {owner, name} = git.remoteConfig; |
| 23 | /** The Graphql query object to get a the PR */ |
| 24 | const PR_QUERY = params( |
| 25 | { |
| 26 | $number: 'Int!', // The PR number |
| 27 | $owner: 'String!', // The organization to query for |
| 28 | $name: 'String!', // The organization to query for |
| 29 | }, |
| 30 | { |
| 31 | repository: params( |
| 32 | {owner: '$owner', name: '$name'}, |
| 33 | { |
| 34 | pullRequest: params({number: '$number'}, prSchema), |
| 35 | }, |
| 36 | ), |
| 37 | }, |
| 38 | ); |
| 39 | |
| 40 | try { |
| 41 | const result = await git.github.graphql(PR_QUERY, {number: prNumber, owner, name}); |
| 42 | return result.repository.pullRequest; |
| 43 | } catch (e) { |
| 44 | // If we know the error is just about the pull request not being found, we explicitly |
| 45 | // return `null`. This allows convenient and graceful handling if a PR does not exist. |
| 46 | if (e instanceof GraphqlResponseError && e.errors?.every((e) => e.type === 'NOT_FOUND')) { |
| 47 | return null; |
| 48 | } |
| 49 | throw e; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /** Get all pending PRs from github */ |
| 54 | export async function getPendingPrs<PrSchema>(prSchema: PrSchema, git: AuthenticatedGitClient) { |
no test coverage detected