-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
115 lines (100 loc) · 3.7 KB
/
index.js
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const core = require('@actions/core');
const { CreateGithubClient } = require('./src/GithubClient');
const { GetEnterpriseScopedIpAllowListEntriesCommand } = require('./src/EnterpriseCommands');
const {
getMetaCidrEntries,
getAdditionalCidrEntries,
getToDeleteIpAllowListEntries,
getToCreateIpAllowListEntries,
getToUpdateIpAllowListEntries,
createIpAllowListEntries,
deleteIpAllowListEntries,
updateIpAllowListEntries,
} = require('./src/util');
const { IpAllowListEntry } = require('./src/models/IpAllowListEntry');
const expectCidrEntries = [];
async function run() {
try {
const githubToken = core.getInput('github_token', { required: true });
const enterpriseSlug = core.getInput('enterprise_slug', { required: true });
const metadataKey = core.getInput('metadata_key');
const additionalCidrEntries = core.getInput('additional_cidr_entries');
const scope = core.getInput('scope');
const mode = core.getInput('mode');
const octokit = CreateGithubClient(githubToken);
if (!metadataKey && !additionalCidrEntries) {
throw new Error('A set of additionalCidrEntries or GitHub metadataKey must be specified.');
}
const { enterprise, ipAllowListEntries: existScopedIpAllowListEntries } =
await GetEnterpriseScopedIpAllowListEntriesCommand({
enterpriseSlug,
octokit,
scope,
});
if (metadataKey) {
const cidrEntries = await getMetaCidrEntries({ metadataKey, scope });
if (cidrEntries) {
expectCidrEntries.push(...cidrEntries);
} else {
throw new Error(
`The metadata cidrEntries for '${metadataKey}' were unable to be resolved.`,
);
}
}
if (additionalCidrEntries) {
const cidrEntries = getAdditionalCidrEntries({ additionalCidrEntries, scope });
expectCidrEntries.push(...cidrEntries);
}
if (mode === 'delete') {
expectCidrEntries.splice(0);
}
core.info(`number of existScopedIpAllowListEntries: ${existScopedIpAllowListEntries.length}`);
core.info(`number of expectCidrEntries: ${expectCidrEntries.length}`);
core.startGroup(`Delete IpAllowListEntries`);
const toDelete = getToDeleteIpAllowListEntries({
existScopedIpAllowListEntries,
expectCidrEntries,
});
const toDeleteResult = await deleteIpAllowListEntries({
ipAllowListEntries: toDelete,
octokit,
});
core.info(`number of toDelete: ${toDelete.length}`);
core.info(`number of toDeleteResult: ${toDeleteResult.length}`);
core.endGroup();
core.startGroup(`Create IpAllowListEntries`);
const toCreate = getToCreateIpAllowListEntries({
existScopedIpAllowListEntries,
expectCidrEntries,
});
const toCreateResult = await createIpAllowListEntries({
enterprise,
cidrEntries: toCreate,
octokit,
});
core.info(`number of toCreate: ${toCreate.length}`);
core.info(`number of toCreateResult: ${toCreateResult.length}`);
core.endGroup();
core.startGroup(`Update IpAllowListEntries`);
const toUpdateMergedIpAllowListEntries = getToUpdateIpAllowListEntries({
existScopedIpAllowListEntries,
expectCidrEntries,
}).map(([cidrEntry, ipAllowListEntry]) => {
return new IpAllowListEntry({
...ipAllowListEntry.toDictionary(),
name: cidrEntry.name,
isActive: cidrEntry.isActive,
});
});
const toUpdateResult = await updateIpAllowListEntries({
ipAllowListEntries: toUpdateMergedIpAllowListEntries,
octokit,
});
core.info(`number of toUpdate: ${toUpdateMergedIpAllowListEntries.length}`);
core.info(`number of toUpdateResult: ${toUpdateResult.length}`);
core.endGroup();
} catch (err) {
core.setFailed(err);
}
}
run();