-
Notifications
You must be signed in to change notification settings - Fork 368
/
dryrun.service.ts
216 lines (177 loc) · 7.22 KB
/
dryrun.service.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
import promptly from 'promptly';
import chalk from 'chalk';
import type { Metadata, NangoConnection } from '@nangohq/shared';
import { SyncConfigType, SyncType, syncRunService, cloudHost, stagingHost } from '@nangohq/shared';
import type { GlobalOptions } from '../types.js';
import { parseSecretKey, printDebug, hostport, getConnection, getConfig } from '../utils.js';
import configService from './config.service.js';
import compileService from './compile.service.js';
import integrationService from './local-integration.service.js';
interface RunArgs extends GlobalOptions {
sync: string;
connectionId: string;
lastSyncDate?: string;
useServerLastSyncDate?: boolean;
input?: object;
metadata?: Metadata;
}
class DryRunService {
public async run(options: RunArgs, environment: string, debug = false) {
let syncName = '';
let connectionId, suppliedLastSyncDate, actionInput, rawStubbedMetadata;
await parseSecretKey(environment, debug);
if (!process.env['NANGO_HOSTPORT']) {
if (debug) {
printDebug(`NANGO_HOSTPORT is not set. Setting the default to ${hostport}`);
}
process.env['NANGO_HOSTPORT'] = hostport;
}
if (debug) {
printDebug(`NANGO_HOSTPORT is set to ${process.env['NANGO_HOSTPORT']}`);
}
if (Object.keys(options).length > 0) {
({ sync: syncName, connectionId, lastSyncDate: suppliedLastSyncDate, input: actionInput, metadata: rawStubbedMetadata } = options);
}
if (!syncName) {
console.log(chalk.red('Sync name is required'));
return;
}
if (!connectionId) {
console.log(chalk.red('Connection id is required'));
return;
}
const { success, error, response: config } = await configService.load('', debug);
if (!success || !config) {
console.log(chalk.red(error?.message));
return;
}
const providerConfigKey = config.find((config) => [...config.syncs, ...config.actions].find((sync) => sync.name === syncName))?.providerConfigKey;
if (!providerConfigKey) {
console.log(chalk.red(`Provider config key not found, please check that the provider exists for this sync name: ${syncName}`));
return;
}
const foundConfig = config.find((configItem) => {
const syncsArray = configItem.syncs || [];
const actionsArray = configItem.actions || [];
return [...syncsArray, ...actionsArray].some((sync) => sync.name === syncName);
});
const syncInfo = foundConfig
? (foundConfig.syncs || []).find((sync) => sync.name === syncName) || (foundConfig.actions || []).find((action) => action.name === syncName)
: null;
if (debug) {
printDebug(`Provider config key found to be ${providerConfigKey}`);
}
const nangoConnection = (await getConnection(
providerConfigKey,
connectionId,
{
'Nango-Is-Sync': true,
'Nango-Is-Dry-Run': true
},
debug
)) as unknown as NangoConnection;
if (!nangoConnection) {
console.log(chalk.red('Connection not found'));
return;
}
if (debug) {
printDebug(`Connection found with ${JSON.stringify(nangoConnection, null, 2)}`);
}
const {
config: { provider }
} = await getConfig(providerConfigKey, debug);
if (!provider) {
console.log(chalk.red('Provider not found'));
return;
}
if (debug) {
printDebug(`Provider found: ${provider}`);
}
if (process.env['NANGO_HOSTPORT'] === cloudHost || process.env['NANGO_HOSTPORT'] === stagingHost) {
process.env['NANGO_CLOUD'] = 'true';
}
let lastSyncDate = null;
if (suppliedLastSyncDate) {
if (debug) {
printDebug(`Last sync date supplied as ${suppliedLastSyncDate}`);
}
lastSyncDate = new Date(suppliedLastSyncDate);
}
const result = await compileService.run(debug, syncName);
if (!result) {
console.log(chalk.red('The sync/action did not compile successfully. Exiting'));
return;
}
let normalizedInput;
let stubbedMetadata;
try {
normalizedInput = JSON.parse(actionInput as unknown as string);
} catch {
normalizedInput = actionInput;
}
try {
stubbedMetadata = JSON.parse(rawStubbedMetadata as unknown as string);
} catch {
stubbedMetadata = rawStubbedMetadata;
}
const logMessages = {
counts: { updated: 0, added: 0, deleted: 0 },
messages: []
};
const syncRun = new syncRunService({
bigQueryClient: { insert: async () => {} },
integrationService,
writeToDb: false,
nangoConnection,
provider,
input: normalizedInput as object,
isAction: syncInfo?.type === SyncConfigType.ACTION,
syncId: 'abc',
activityLogId: -1,
syncJobId: -1,
syncName,
syncType: SyncType.INITIAL,
loadLocation: './',
debug,
logMessages,
stubbedMetadata
});
try {
const secretKey = process.env['NANGO_SECRET_KEY'];
const results = await syncRun.run(lastSyncDate, true, secretKey, process.env['NANGO_HOSTPORT']);
if (results) {
console.log(JSON.stringify(results, null, 2));
}
if (syncRun.logMessages && syncRun.logMessages.messages.length > 0) {
const logMessages = syncRun.logMessages.messages;
let index = 0;
const batchCount = 10;
const displayBatch = () => {
for (let i = 0; i < batchCount && index < logMessages.length; i++, index++) {
const logs = logMessages[index];
console.log(chalk.yellow(JSON.stringify(logs, null, 2)));
}
};
console.log(chalk.yellow(`The dry run would produce the following results: ${JSON.stringify(syncRun.logMessages.counts, null, 2)}`));
console.log(chalk.yellow('The following log messages were generated:'));
displayBatch();
while (index < syncRun.logMessages.messages.length) {
const remaining = syncRun.logMessages.messages.length - index;
const confirmation = await promptly.confirm(
`There are ${remaining} logs messages remaining. Would you like to see the next 10 log messages? (y/n)`
);
if (confirmation) {
displayBatch();
} else {
break;
}
}
}
process.exit(0);
} catch {
process.exit(1);
}
}
}
const dryRunService = new DryRunService();
export default dryRunService;