2020-05-05 18:37:31 -04:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2020-05-05 18:37:31 -04:00
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2020-08-14 19:49:07 -04:00
|
|
|
import {params, types} from 'typed-graphqlify';
|
2020-05-05 18:37:31 -04:00
|
|
|
|
2020-08-14 19:49:07 -04:00
|
|
|
import {GitClient} from './git';
|
2020-05-05 18:37:31 -04:00
|
|
|
|
2020-05-11 18:21:18 -04:00
|
|
|
/** Get a PR from github */
|
2020-08-14 19:49:07 -04:00
|
|
|
export async function getPr<PrSchema>(prSchema: PrSchema, prNumber: number, git: GitClient) {
|
|
|
|
/** The owner and name of the repository */
|
|
|
|
const {owner, name} = git.remoteConfig;
|
|
|
|
/** The GraphQL query object to get a the PR */
|
2020-05-11 18:21:18 -04:00
|
|
|
const PR_QUERY = params(
|
|
|
|
{
|
|
|
|
$number: 'Int!', // The PR number
|
|
|
|
$owner: 'String!', // The organization to query for
|
|
|
|
$name: 'String!', // The organization to query for
|
|
|
|
},
|
|
|
|
{
|
|
|
|
repository: params({owner: '$owner', name: '$name'}, {
|
|
|
|
pullRequest: params({number: '$number'}, prSchema),
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2020-08-14 19:49:07 -04:00
|
|
|
const result = (await git.github.graphql.query(PR_QUERY, {number: prNumber, owner, name}));
|
2020-05-11 18:21:18 -04:00
|
|
|
return result.repository.pullRequest;
|
|
|
|
}
|
|
|
|
|
2020-05-05 18:37:31 -04:00
|
|
|
/** Get all pending PRs from github */
|
2020-08-14 19:49:07 -04:00
|
|
|
export async function getPendingPrs<PrSchema>(prSchema: PrSchema, git: GitClient) {
|
|
|
|
/** The owner and name of the repository */
|
|
|
|
const {owner, name} = git.remoteConfig;
|
|
|
|
/** The GraphQL query object to get a page of pending PRs */
|
2020-05-05 18:37:31 -04:00
|
|
|
const PRS_QUERY = params(
|
|
|
|
{
|
|
|
|
$first: 'Int', // How many entries to get with each request
|
|
|
|
$after: 'String', // The cursor to start the page at
|
|
|
|
$owner: 'String!', // The organization to query for
|
|
|
|
$name: 'String!', // The repository to query for
|
|
|
|
},
|
|
|
|
{
|
|
|
|
repository: params({owner: '$owner', name: '$name'}, {
|
|
|
|
pullRequests: params(
|
|
|
|
{
|
|
|
|
first: '$first',
|
|
|
|
after: '$after',
|
|
|
|
states: `OPEN`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
nodes: [prSchema],
|
|
|
|
pageInfo: {
|
|
|
|
hasNextPage: types.boolean,
|
|
|
|
endCursor: types.string,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
});
|
2020-08-14 19:49:07 -04:00
|
|
|
/** The current cursor */
|
2020-05-05 18:37:31 -04:00
|
|
|
let cursor: string|undefined;
|
2020-08-14 19:49:07 -04:00
|
|
|
/** If an additional page of members is expected */
|
2020-05-05 18:37:31 -04:00
|
|
|
let hasNextPage = true;
|
2020-08-14 19:49:07 -04:00
|
|
|
/** Array of pending PRs */
|
2020-05-05 18:37:31 -04:00
|
|
|
const prs: Array<PrSchema> = [];
|
|
|
|
|
2020-08-14 19:49:07 -04:00
|
|
|
// For each page of the response, get the page and add it to the list of PRs
|
2020-05-05 18:37:31 -04:00
|
|
|
while (hasNextPage) {
|
2020-08-14 19:49:07 -04:00
|
|
|
const params = {
|
|
|
|
after: cursor || null,
|
|
|
|
first: 100,
|
|
|
|
owner,
|
|
|
|
name,
|
|
|
|
};
|
|
|
|
const results = await git.github.graphql.query(PRS_QUERY, params) as typeof PRS_QUERY;
|
2020-05-05 18:37:31 -04:00
|
|
|
prs.push(...results.repository.pullRequests.nodes);
|
|
|
|
hasNextPage = results.repository.pullRequests.pageInfo.hasNextPage;
|
|
|
|
cursor = results.repository.pullRequests.pageInfo.endCursor;
|
|
|
|
}
|
|
|
|
return prs;
|
|
|
|
}
|