-
Notifications
You must be signed in to change notification settings - Fork 31
/
listBranchProtectionRules.ts
59 lines (52 loc) · 1.57 KB
/
listBranchProtectionRules.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
import { getOctokit } from '@actions/github';
import { GraphQlQueryResponseData } from '@octokit/graphql';
import { IterableList, makeGraphqlIterator } from './makeGraphqlIterator';
const listBranchProtectionRulesQuery = `
query($endCursor: String, $pageSize: Int!, $repositoryName: String!, $repositoryOwner: String!) {
repository(name: $repositoryName, owner: $repositoryOwner) {
branchProtectionRules(first: $pageSize, after: $endCursor) {
edges {
node {
pattern
requiresStrictStatusChecks
}
}
pageInfo {
endCursor
hasNextPage
}
}
}
}
`;
export interface BranchProtectionRule {
pattern: string;
requiresStrictStatusChecks: boolean;
}
/**
* Returns an array containing a repository's configured partial branch
* protection rules.
*/
export const listBranchProtectionRules = async (
octokit: ReturnType<typeof getOctokit>,
repositoryOwner: string,
repositoryName: string,
): Promise<BranchProtectionRule[]> => {
const iterator = makeGraphqlIterator<BranchProtectionRule>(octokit, {
extractListFunction: (
response: GraphQlQueryResponseData,
): IterableList<BranchProtectionRule> =>
response.repository.branchProtectionRules,
parameters: {
pageSize: 100,
repositoryName,
repositoryOwner,
},
query: listBranchProtectionRulesQuery,
});
const branchProtectionRules: BranchProtectionRule[] = [];
for await (const node of iterator) {
branchProtectionRules.push(node);
}
return branchProtectionRules;
};