Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions utils/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,30 +104,38 @@ export const folderCleanup = (folder) => {

export const reportMapper = (inputElement, parsedInput, reportPairsMapper) => {
const mapper = {};
// eslint-disable-next-line no-unused-vars
const escapeForReplace = (s) => String(s).replace(/\$/g, '$$$$'); // <-- added
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can remove comments like: // <-- added, // <-- wrapped to increase readability


for (const [reportKey, reportValue] of Object.entries(reportPairsMapper)) {
let firstPass = false;
const reportInputVariablesFetcher = [...reportValue.match(/\{{(.*?)\}}/g) ?? []];
// If there are no fields to replace, map the value instantly
const reportInputVariablesFetcher = [...reportValue.match(/\{{(.*?)\}}/g)];
if (reportInputVariablesFetcher.length === 0) {
mapper[reportKey] = reportValue;
continue;
}

for (let keyPosition = 0; keyPosition < reportInputVariablesFetcher.length; keyPosition++) {
const keyName = reportInputVariablesFetcher[keyPosition].replace('{{', '').replace('}}', '');
if (reportInputVariablesFetcher.length === 1 || firstPass === false) {
mapper[reportKey] = reportPairsMapper[reportKey].replace(`{{${keyName}}}`, `${parsedInput[inputElement][keyName]}`);
mapper[reportKey] = reportPairsMapper[reportKey].replace(
`{{${keyName}}}`,
escapeForReplace(parsedInput[inputElement][keyName]) // <-- wrapped
);
} else {
const parsedInputValue = parsedInput[inputElement][keyName];
if (typeof parsedInputValue === 'object') {
let formattedInput = '';
parsedInputValue.forEach((singleParsedInputValue) => {
formattedInput += `\u2022 ${singleParsedInputValue}\n`;
});
mapper[reportKey] = mapper[reportKey].replace(`{{${keyName}}}`, formattedInput);
mapper[reportKey] = mapper[reportKey].replace(
`{{${keyName}}}`,
formattedInput
);
} else {
mapper[reportKey] = mapper[reportKey].replace(`{{${keyName}}}`, `${parsedInputValue}`);
mapper[reportKey] = mapper[reportKey].replace(
`{{${keyName}}}`,
escapeForReplace(parsedInputValue) // <-- wrapped
);
}
}
firstPass = true;
Expand Down
45 changes: 33 additions & 12 deletions utils/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,53 @@ export const createFromTemplate = (templateBluePrint) => {
const extraFieldsAtomicView = {};

fieldKeys.forEach((field) => {
// excluding the description field since it's considered one of the basic fields we use in the action
const raw = templateBluePrint[field];

// Keep description fields as-is and mark type as 'object'
if (field.includes('description')) {
_.set(template, field, templateBluePrint[field]);
_.set(template, field, raw);
_.set(extraFieldsAtomicView, field, { type: 'object' });
return;
}
// For the extra fields supplied, we need to determine the data type in order to properly construct the JSON
// schema to be used. Array types are special data types used in that module
const fieldType = templateBluePrint[field].match(/\[.+\]/g) !== null ? 'array' : typeof templateBluePrint[field];
if (fieldType === 'array') {
let formattedValue = templateBluePrint[field].replace(/\[|\]/g, '').replace(/,\s/g, ',').split(',');

// Deconstruct field into array and gets final value
// Safer array detection: real arrays OR whole-string bracketed list (e.g., "[a,b]")
const isArrayValue =
Array.isArray(raw) ||
(typeof raw === 'string' && /^\s*\[[^[\]]*]\s*$/.test(raw));

const fieldType = isArrayValue ? 'array' : typeof raw;

if (isArrayValue) {
// Normalize into an array
let formattedValue;
if (Array.isArray(raw)) {
formattedValue = raw;
} else {
// Remove ONLY the outer [ ] and split by commas
const inner = raw.trim().slice(1, -1);
formattedValue = inner
.split(',')
.map(v => v.trim())
.filter(v => v.length > 0);
}

// Deconstruct field into array and get final value
const deconstructedField = field.split('.');
const finalField = deconstructedField.pop();
const remainingField = deconstructedField.join('.');

if (finalField.includes('{')) {
formattedValue = formattedValue.map(value => _.set({}, finalField.replace(/\{|\}/g, ''), value));
if (finalField && finalField.includes('{')) {
const key = finalField.replace(/\{|\}/g, '');
formattedValue = formattedValue.map(value => _.set({}, key, value));
}
const remainingField = deconstructedField.join('.');

_.set(template, remainingField, formattedValue);
} else {
_.set(template, field, templateBluePrint[field]);
_.set(template, field, raw);
}

_.set(extraFieldsAtomicView, field, { type: fieldType });
});

return { template, extraFieldsAtomicView };
};
Loading