Skip to content

Commit 9b74487

Browse files
committed
.github/workflows: copy utils folder from berty
Signed-off-by: Jeff Thompson <jeff@thefirst.org>
1 parent 93c3083 commit 9b74487

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#!/usr/bin/env node
2+
const https = require("https");
3+
4+
// Function that request Github API to get self-hosted runners status
5+
async function getSelfHostedRunnersStatus() {
6+
const options = {
7+
hostname: "ghrunners-lambda.berty.io",
8+
port: 443,
9+
method: "GET",
10+
timeout: 30000,
11+
};
12+
13+
return new Promise((resolve, reject) => {
14+
const req = https.request(options, (res) => {
15+
const body = [];
16+
res.on("data", (chunk) => body.push(chunk));
17+
res.on("end", () => {
18+
const resString = Buffer.concat(body).toString();
19+
20+
if (res.statusCode < 200 || res.statusCode > 299) {
21+
return reject(
22+
new Error(`HTTP status code ${res.statusCode}: ${resString}`)
23+
);
24+
}
25+
resolve(resString);
26+
});
27+
});
28+
29+
req.on("error", (err) => {
30+
reject(err);
31+
});
32+
33+
req.on("timeout", () => {
34+
req.destroy();
35+
reject(new Error("Request timed out"));
36+
});
37+
38+
req.end();
39+
});
40+
}
41+
42+
// Return hom many self-hosted runners are online
43+
async function getSelfHostedRunnersOnlineCount() {
44+
return new Promise((resolve, reject) => {
45+
getSelfHostedRunnersStatus()
46+
.then((json) => {
47+
const stat = JSON.parse(json);
48+
const total = stat.runners.length;
49+
var count = 0;
50+
var desc = "None";
51+
52+
for (const runner of stat.runners) {
53+
if (runner.status === "online") count++;
54+
}
55+
56+
if (count === total && total > 0) desc = "All";
57+
else if (count > 0) desc = "Partially";
58+
59+
resolve(`${desc} (${count}/${total})`);
60+
})
61+
.catch((err) => {
62+
reject(err);
63+
});
64+
});
65+
}
66+
67+
// Returns true if at least one self-hosted runner is available
68+
async function isSelfHostedRunnerAvailable() {
69+
try {
70+
const json = await getSelfHostedRunnersStatus();
71+
const stat = JSON.parse(json);
72+
73+
for (const runner of stat.runners) {
74+
if (runner.status === "online" && runner.busy === false) return true;
75+
}
76+
} catch (err) {
77+
console.error("Github API request self-hosted status error:", err);
78+
}
79+
80+
return false;
81+
}
82+
83+
// If executed as a script
84+
if (require.main === module) {
85+
const onlineCheck = process.argv[2];
86+
87+
if (process.argv.length > 3 || (onlineCheck && onlineCheck !== "online")) {
88+
console.error(`Usage: ${process.argv[1]} [online]`);
89+
process.exit(1);
90+
}
91+
92+
if (onlineCheck) {
93+
getSelfHostedRunnersOnlineCount()
94+
.then((count) => console.log(count))
95+
.catch((err) => {
96+
console.error(err);
97+
process.exit(1);
98+
});
99+
} else {
100+
getSelfHostedRunnersStatus()
101+
.then((json) => {
102+
console.log(JSON.stringify(JSON.parse(json), undefined, 2));
103+
})
104+
.catch((err) => {
105+
console.error(err);
106+
process.exit(1);
107+
});
108+
}
109+
}
110+
111+
// Export module functions
112+
module.exports = {
113+
getSelfHostedRunnersStatus: getSelfHostedRunnersStatus,
114+
isSelfHostedRunnerAvailable: isSelfHostedRunnerAvailable,
115+
};
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env node
2+
const statusGetter = require("./get-self-hosted-runner-status");
3+
4+
// Check parameters
5+
const mode = process.argv[2] || "self-hosted";
6+
7+
if (
8+
!["self-hosted", "github", "optimized"].includes(mode) ||
9+
process.argv.length > 3
10+
) {
11+
console.error(`Usage: ${process.argv[1]} [self-hosted | github | optimized]`);
12+
process.exit(1);
13+
}
14+
15+
// Returns matrix according to parameters and self-hosted runner availability
16+
async function getMatrix() {
17+
const runners = {
18+
selfhosted: {
19+
name: "self-hosted",
20+
runner: ["self-hosted", "macOS", "public"],
21+
selfhosted: true,
22+
},
23+
github: {
24+
name: "github",
25+
runner: ["macos-12"],
26+
selfhosted: false,
27+
},
28+
};
29+
30+
switch (mode) {
31+
case "self-hosted":
32+
return [runners.selfhosted];
33+
case "github":
34+
return [runners.github];
35+
case "optimized":
36+
return (await statusGetter.isSelfHostedRunnerAvailable())
37+
? [runners.selfhosted]
38+
: [runners.github];
39+
}
40+
}
41+
42+
getMatrix().then((matrix) => {
43+
console.debug("matrix: " + JSON.stringify(matrix, undefined, 2));
44+
console.log(
45+
"::set-output name=matrix::" + JSON.stringify({ include: matrix })
46+
);
47+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"cache-versions": {
3+
"node": "3",
4+
"nodeweb": "1",
5+
"go": "1",
6+
"ruby": "1",
7+
"bridgeframework": "1",
8+
"pushframework": "1",
9+
"xcodeproject": "1",
10+
"xcodeworkspace": "1"
11+
}
12+
}

0 commit comments

Comments
 (0)