-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.mjs
59 lines (47 loc) · 1.71 KB
/
app.mjs
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 'dotenv/config';
import fetch from 'node-fetch';
import * as fse from 'fs-extra';
async function main() {
// validate environment
const envRequired = ['GH_PAT', 'ORG', 'SCHEMA_URL'];
for (const item of envRequired) {
if (!process.env[item]) {
console.error(`Error: ${item} is not defined in .env`);
process.exitCode = 1;
return;
}
if (!process.env.OUTPUT_PATH) {
process.env.OUTPUT_PATH = `outputs/${process.env.SCHEMA_URL.substring(process.env.SCHEMA_URL.lastIndexOf('/') + 1)}`;
}
}
async function getSchemaJSON(url) {
const response = await fetch(url);
const schema = await response.json();
return schema;
}
async function getRunnerLabels(schema) {
const headers = { 'Content-Type': 'application/json', Authorization: `token ${process.env.GH_PAT}` };
const url = `https://api.github.com/orgs/${process.env.ORG}/actions/runners`;
const response = await fetch(url, { method: 'GET', headers });
const runners = await response.json();
const labels = new Set(schema.definitions.normalJob.properties['runs-on'].oneOf[0].enum);
for (const runner of runners.runners) {
for (const label of runner.labels) {
labels.add(label.name);
}
}
return Array.from(labels);
}
async function addRunnerLabels(schema, labels) {
schema.definitions.normalJob.properties['runs-on'].oneOf[0].enum = labels;
fse.outputFile(process.env.OUTPUT_PATH, JSON.stringify(schema, null, 2), (err) => {
if (err) {
console.error(err);
}
});
}
const schema = await getSchemaJSON(process.env.SCHEMA_URL);
const labels = await getRunnerLabels(schema);
addRunnerLabels(schema, labels);
}
main().catch(console.log);