-
Notifications
You must be signed in to change notification settings - Fork 1
/
core.js
87 lines (74 loc) · 2.62 KB
/
core.js
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
const _ = require("lodash");
const consts = require("./consts.json");
const helpers = require("./helpers");
const redaction = require("./secrets-redaction");
const autocomplete = require("./autocomplete");
const {
loadMethodFromConfiguration,
loadConfiguration,
} = require("./config-loader");
function generatePluginMethod(method) {
return async (action, settings) => {
const methodDefinition = loadMethodFromConfiguration(action.method.name);
const pluginDefinition = loadConfiguration();
const {
params,
settings: parsedSettings,
} = await helpers.readActionArguments(action, settings, methodDefinition);
const allowEmptyResult = methodDefinition.allowEmptyResult ?? false;
const shouldRedactSecrets = methodDefinition.redactSecrets ?? consts.DEFAULT_REDACT_SECRETS;
const secrets = [];
if (shouldRedactSecrets) {
const paramsDefinition = [
...(methodDefinition.params ?? []),
...(pluginDefinition.auth?.params ?? []),
];
const secretsObject = redaction.filterVaultedParameters(params, paramsDefinition);
secrets.push(...Object.values(secretsObject));
}
const utils = {
logger: shouldRedactSecrets ? redaction.createRedactedLogger(secrets) : console,
};
let result;
try {
result = await method(params, {
action,
settings,
utils,
parsedSettings,
});
} catch (error) {
throw shouldRedactSecrets ? redaction.redactSecrets(error, secrets) : error;
}
if (!allowEmptyResult && helpers.isResultEmpty(result)) {
return consts.OPERATION_FINISHED_SUCCESSFULLY_MESSAGE;
}
return shouldRedactSecrets ? redaction.redactSecrets(result, secrets) : result;
};
}
function generateAutocompleteFunction(autocompleteFunction) {
return async (query, settings, params) => {
const {
params: parsedParams,
settings: parsedSettings,
} = await autocomplete.readAutocompleteFunctionArguments(
params,
settings,
);
return autocompleteFunction(query, parsedParams, { settings, params, parsedSettings });
};
}
function bootstrap(pluginMethods, autocompleteFunctions) {
const bootstrappedPluginMethods = _.entries(pluginMethods)
.map(([methodName, method]) => ({
[methodName]: generatePluginMethod(method),
}));
const bootstrappedAutocompleteFuncs = _.entries(autocompleteFunctions)
.map(([functionName, autocompleteFunction]) => ({
[functionName]: generateAutocompleteFunction(autocompleteFunction),
}));
return _.merge(...bootstrappedPluginMethods, ...bootstrappedAutocompleteFuncs);
}
module.exports = {
bootstrap,
};