This repository has been archived by the owner on Apr 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
255 lines (236 loc) · 6.6 KB
/
index.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import { config } from 'dotenv';
import core = require('@actions/core');
import { WebClient } from '@slack/web-api';
import pty = require('node-pty');
import os from 'os';
import path from 'path';
import { promises as fs } from 'fs';
config();
const REQUIRED_ENV = [
'SLACK_TOKEN',
'CHANNEL_ID',
'PUBLISH_COMMAND',
'CODE_PATTERN',
];
REQUIRED_ENV.forEach((key) => {
const missing = [];
if (!process.env[key]) {
missing.push(key);
}
if (missing.length > 0) {
throw new Error(
`Missing required environment variables ${missing.join(', ')}`,
);
}
});
const getInputs = () => {
const {
SLACK_TOKEN = '',
CHANNEL_ID = '',
PUBLISH_COMMAND = '',
CODE_PATTERN = '',
TIMEOUT = `${20 * 60 * 1000}`,
} = process.env;
const parts = PUBLISH_COMMAND.split('\n');
const command = parts[0];
const args = parts.slice(1);
console.log(
`Received publish command of '${command}' with args '${args.join(',')}'`,
);
return {
token: SLACK_TOKEN,
channelId: CHANNEL_ID,
command,
args,
pattern: CODE_PATTERN,
timeout: parseInt(TIMEOUT),
};
};
const request2FACode = async (web: WebClient, channel: string) => {
console.log(`Requesting 2FA token from channel ${channel}`);
await web.conversations.join({ channel }).catch((e) => {
// the following error is expected for private channels
if (e.data.error === 'method_not_supported_for_channel_type') {
return;
}
throw e;
});
const res = await web.chat.postMessage({
channel,
text: 'Please respond with 2FA code',
});
const { message, ts } = res;
const { user } = message as { user: string };
console.log('Requested 2FA token:', { user, ts });
return { user, ts: ts as string };
};
const acknowledge2FACode = async (
web: WebClient,
channel: string,
code: string,
) => {
try {
console.log('Sending acknowledge message');
await web.chat.postMessage({
channel,
text: `Received 2FA code ${code}`,
});
console.log('Done sending acknowledge message');
} catch (e) {
console.log('Failed sending acknowledge message');
}
};
const reportExitMessage = async (
web: WebClient,
channel: string,
message: string,
) => {
try {
console.log('Sending exit message');
await web.chat.postMessage({
channel,
text: message,
});
console.log('Done sending exit message');
} catch (e) {
console.log('Failed sending exit message');
}
};
const getHistory = async (web: WebClient, channel: string, ts: string) => {
const res = await web.conversations.history({
channel,
oldest: ts,
inclusive: false,
});
return res;
};
const waitFor2FACode = async (
web: WebClient,
channel: string,
timeout: number,
ts: string,
user: string,
) => {
let timedout = false;
const timeoutId = setTimeout(() => {
timedout = true;
}, timeout);
while (!timedout) {
const res = await getHistory(web, channel, ts);
const messages = (res.messages as { text: string; type: string }[]).filter(
({ type }) => type === 'message',
);
for (const { text } of messages) {
const match = text.match(new RegExp(`^<@${user}>(.+)$`));
if (match && match[1]) {
const code = match[1].trim();
console.log(`Extracted 2FA code of length ${code.length}`);
clearTimeout(timeoutId);
return { error: null, code };
}
}
console.log('Waiting for 2FA code');
await new Promise((resolve) => setTimeout(resolve, 1000));
}
return {
error: new Error(`2FA message timed out after ${timeout} milliseconds`),
code: '',
};
};
const setupAuth = async () => {
if (process.env.NPM_TOKEN) {
const homeDir = os.homedir();
const npmrcPath = path.join(homeDir, '.npmrc');
await fs.writeFile(
npmrcPath,
`//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}`,
);
}
};
(async () => {
try {
const { token, channelId, command, args, pattern, timeout } = getInputs();
await setupAuth();
const regex = new RegExp(pattern);
const { error, message } = await new Promise<{
error: Error | null;
message?: string;
}>((resolve) => {
const publishProcess = pty.spawn(command, args, {});
const timeoutId = setTimeout(() => {
publishProcess.kill();
resolve({
error: new Error(
`publish command timed out after ${timeout} milliseconds`,
),
});
}, timeout);
const handleError = (error: Error) => {
publishProcess.kill();
clearTimeout(timeoutId);
resolve({ error });
};
let matched2fa = false;
let muteOutput = false;
let unmuteMessage = '';
let currentMessage = '';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const dataHandler = async (data: any) => {
if (!muteOutput) {
process.stdout.write(data);
} else if (currentMessage === unmuteMessage) {
muteOutput = false;
} else {
currentMessage = `${currentMessage}${data}`;
}
if (!matched2fa && regex.test(data.toString())) {
try {
console.log(`Matched 2FA code pattern ${regex.toString()}`);
matched2fa = true;
const web = new WebClient(token);
const { user, ts } = await request2FACode(web, channelId);
const { error, code } = await waitFor2FACode(
web,
channelId,
timeout / 2,
ts,
user,
);
if (error) {
handleError(error);
} else {
await acknowledge2FACode(web, channelId, code);
console.log(`Sending 2FA code to publish command`);
muteOutput = true;
unmuteMessage = code;
publishProcess.write(`${code}\n`);
}
} catch (error) {
handleError(error);
}
}
};
publishProcess.on('data', dataHandler);
publishProcess.on('exit', (code) => {
const message = `Publish command process exited with exit code '${code}'`;
if (code !== 0) {
handleError(new Error(message));
} else {
clearTimeout(timeoutId);
resolve({ error: null, message });
}
});
});
if (error) {
await reportExitMessage(new WebClient(token), channelId, error.message);
core.setFailed(error.message);
}
await reportExitMessage(
new WebClient(token),
channelId,
error?.message || message || 'Done running publish command',
);
} catch (error) {
core.setFailed(error.message);
}
})();