Skip to content

Commit

Permalink
Release v2.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
xkcm authored Jun 9, 2023
2 parents fb1019b + c3ebfbc commit 7b94274
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 24 deletions.
33 changes: 27 additions & 6 deletions core.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,52 @@
const _ = require("lodash");

const consts = require("./consts.json");
const helpers = require("./helpers");
const redaction = require("./secrets-redaction");
const autocomplete = require("./autocomplete");
const { loadMethodFromConfiguration } = require("./config-loader");
const {
loadMethodFromConfiguration,
loadConfiguration,
} = require("./config-loader");

function generatePluginMethod(method) {
return async (action, settings) => {
const methodDefinition = loadMethodFromConfiguration(action.method.name);
const parameters = await helpers.readActionArguments(action, settings, methodDefinition);
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 = shouldRedactSecrets
&& Object.values(await redaction.getVaultedParameters(parameters, methodDefinition));
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(parameters, { action, settings, utils });
result = await method(params, {
action,
settings,
utils,
parsedSettings,
});
} catch (error) {
throw shouldRedactSecrets ? redaction.redactSecrets(error, secrets) : error;
}

if (_.isNil(result) || _.isEmpty(result)) {
if (!allowEmptyResult && (_.isNil(result) || _.isEmpty(result))) {
return consts.OPERATION_FINISHED_SUCCESSFULLY_MESSAGE;
}

Expand Down
21 changes: 9 additions & 12 deletions helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async function readActionArguments(
throw new Error(`Could not find a method "${action.method.name}" in config.json`);
}

const settingsParamsDefinition = loadConfiguration()?.settings ?? [];
const settingsParamsDefinition = loadConfiguration().settings ?? [];
const settingsParsingPromises = settingsParamsDefinition.map(async (settingDefinition) => {
settingsValues[settingDefinition.name] = await parseParameter(
settingDefinition,
Expand All @@ -52,7 +52,6 @@ async function readActionArguments(
paramValues[paramDefinition.name] = await parseParameter(
paramDefinition,
paramValues[paramDefinition.name],
settingsValues[paramDefinition.name],
);

const { validationType } = paramDefinition;
Expand All @@ -70,7 +69,6 @@ async function readActionArguments(
paramValues[paramDefinition.name] = await parseParameter(
paramDefinition,
paramValues[paramDefinition.name],
settingsValues[paramDefinition.name],
);

const { validationType } = paramDefinition;
Expand All @@ -84,10 +82,10 @@ async function readActionArguments(
await Promise.all(accountParsingPromises);
}

return removeUndefinedAndEmpty({
...settingsValues,
...paramValues,
});
return {
params: removeUndefinedAndEmpty(paramValues),
settings: removeUndefinedAndEmpty(settingsValues),
};
}

async function temporaryFileSentinel(fileDataArray, functionToWatch) {
Expand Down Expand Up @@ -171,18 +169,17 @@ function removeUndefinedAndEmpty(object) {
return _.omitBy(object, (value) => value === "" || _.isNil(value) || (_.isObjectLike(value) && _.isEmpty(value)));
}

function parseParameter(paramDefinition, paramValue, settingsValue) {
const valueToParse = paramValue ?? settingsValue ?? paramDefinition.default;
if (_.isNil(valueToParse)) {
function parseParameter(paramDefinition, paramValue) {
if (_.isNil(paramValue)) {
if (paramDefinition.required) {
throw Error(`Missing required "${paramDefinition.name}" value`);
}
return valueToParse;
return paramValue;
}

const { parserOptions } = paramDefinition;
const parserToUse = paramDefinition.parserType || paramDefinition.type;
return parsers.resolveParser(parserToUse)(valueToParse, parserOptions);
return parsers.resolveParser(parserToUse)(paramValue, parserOptions);
}

function validateParamValue(
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kaholo/plugin-library",
"version": "2.2.1",
"version": "2.3.0",
"description": "Kaholo library for plugins",
"main": "kaholo-plugin-library.js",
"scripts": {
Expand Down
6 changes: 3 additions & 3 deletions secrets-redaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ function redactSecretsInPlainObject(input, secrets) {
);
}

async function getVaultedParameters(params, methodDefinition) {
const vaultParams = Object.entries(params).filter(([paramName]) => methodDefinition.params.some(
function filterVaultedParameters(params, paramsDefinition) {
const vaultParams = Object.entries(params).filter(([paramName]) => paramsDefinition.some(
(paramDefinition) => paramDefinition.type === "vault" && paramDefinition.name === paramName,
));
return Object.fromEntries(vaultParams);
Expand All @@ -93,7 +93,7 @@ function escapeRegExp(string) {
}

module.exports = {
getVaultedParameters,
filterVaultedParameters,
redactSecrets,
createRedactedLogger,
};

0 comments on commit 7b94274

Please sign in to comment.