forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub.ts
84 lines (78 loc) · 2.74 KB
/
github.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* 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
*/
import {params, types} from 'typed-graphqlify';
import {GitClient} from './git/index';
/** Get a PR from github */
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 */
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),
})
});
const result = (await git.github.graphql.query(PR_QUERY, {number: prNumber, owner, name}));
return result.repository.pullRequest;
}
/** Get all pending PRs from github */
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 */
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,
},
}),
})
});
/** The current cursor */
let cursor: string|undefined;
/** If an additional page of members is expected */
let hasNextPage = true;
/** Array of pending PRs */
const prs: Array<PrSchema> = [];
// For each page of the response, get the page and add it to the list of PRs
while (hasNextPage) {
const params = {
after: cursor || null,
first: 100,
owner,
name,
};
const results = await git.github.graphql.query(PRS_QUERY, params) as typeof PRS_QUERY;
prs.push(...results.repository.pullRequests.nodes);
hasNextPage = results.repository.pullRequests.pageInfo.hasNextPage;
cursor = results.repository.pullRequests.pageInfo.endCursor;
}
return prs;
}