-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.ts
161 lines (133 loc) · 3.76 KB
/
main.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import * as core from "@actions/core";
import * as exec from "@actions/exec";
import * as tc from "@actions/tool-cache";
import * as fs from "node:fs";
import * as path from "node:path";
const breakpointVersion = "0.0.18";
interface WaitConfig {
endpoint: string;
duration: string;
authorized_keys?: string[];
authorized_github_users?: string[];
shell?: string[];
allowed_ssh_users: string[];
webhooks?: Webhook[];
slack_bot?: SlackBot;
}
class Webhook {
url: string;
payload: unknown;
}
class SlackBot {
channel: string;
token: string;
}
async function run(): Promise<void> {
try {
await installBreakpoint();
await runBreakpoint();
} catch (err) {
core.setFailed(err.message);
}
}
async function installBreakpoint(): Promise<void> {
// Download the specific version of the tool, e.g. as a tarball.
const toolURL = await getDownloadURL();
core.info(`Downloading: ${toolURL}`);
const pathToTarball = await tc.downloadTool(toolURL, null, null, {
CI: process.env.CI,
"User-Agent": "breakpoint-action",
accept: "application/octet-stream",
});
// Extract the tarball onto the runner.
const pathToCLI = await tc.extractTar(pathToTarball);
// Expose the tool by adding it to the $PATH.
core.addPath(pathToCLI);
}
async function runBreakpoint(): Promise<void> {
const configFile = tmpFile("config.json");
const configData = jsonifyInput();
core.debug(`Configuration: ${configData}`);
fs.writeFile(configFile, configData, (err) => {
if (err) {
core.setFailed(`Failed to write config file: ${err.message}`);
return;
}
});
core.debug(new Date().toTimeString());
await exec.exec(`breakpoint wait --config=${configFile}`);
core.debug(new Date().toTimeString());
}
async function getDownloadURL(): Promise<string> {
const { RUNNER_ARCH, RUNNER_OS } = process.env;
let arch = "";
switch (RUNNER_ARCH) {
case "X64":
arch = "amd64";
break;
case "ARM64":
arch = "arm64";
break;
default:
throw new Error(`Unsupported architecture: ${RUNNER_ARCH}`);
}
let os = "";
switch (RUNNER_OS) {
case "macOS":
os = "darwin";
break;
case "Linux":
os = "linux";
break;
default:
throw new Error(`Unsupported operating system: ${RUNNER_OS}`);
}
return `https://github.com/namespacelabs/breakpoint/releases/download/v${breakpointVersion}/breakpoint_${os}_${arch}.tar.gz`;
}
function jsonifyInput(): string {
const config: WaitConfig = {
endpoint: core.getInput("endpoint"),
duration: core.getInput("duration"),
allowed_ssh_users: ["runner"],
};
let authorized = false;
const authorizedUsers: string = core.getInput("authorized-users");
if (authorizedUsers) {
config.authorized_github_users = authorizedUsers.split(",").map((u) => String(u).trim());
authorized = true;
}
const authorizedKeys: string = core.getInput("authorized-keys");
if (authorizedKeys) {
config.authorized_keys = authorizedKeys.split(",").map((k) => String(k).trim());
authorized = true;
}
if (!authorized) {
throw new Error("Neither 'authorized-users' nor 'authorized-keys' is provded.");
}
const webhookDefFile: string = core.getInput("webhook-definition");
if (webhookDefFile) {
const webhookDef: string = fs.readFileSync(webhookDefFile, "utf8");
config.webhooks = [JSON.parse(webhookDef)];
}
const shell: string = core.getInput("shell");
if (shell) {
config.shell = [shell];
}
const slackChannel: string = core.getInput("slack-announce-channel");
if (slackChannel) {
const slackBot: SlackBot = {
channel: slackChannel,
token: "${SLACK_BOT_TOKEN}",
};
config.slack_bot = slackBot;
}
return JSON.stringify(config);
}
function tmpFile(file: string): string {
const tmpDir = path.join(process.env.RUNNER_TEMP, "breakpoint");
if (!fs.existsSync(tmpDir)) {
fs.mkdirSync(tmpDir);
}
return path.join(tmpDir, file);
}
run();