44 lines
1.2 KiB
JavaScript
44 lines
1.2 KiB
JavaScript
const config = require('./conf/transformConfig.json');
|
|
const functionsConfig = require('./conf/functionsConfig.json');
|
|
const loadedFunctions = {};
|
|
|
|
Object.keys(functionsConfig).forEach(funcName => {
|
|
loadedFunctions[funcName] = require(functionsConfig[funcName]);
|
|
});
|
|
|
|
function resolveField(source, fieldConfig) {
|
|
let result = null;
|
|
|
|
if (typeof fieldConfig === 'string' || typeof fieldConfig === 'boolean' || typeof fieldConfig === 'number') {
|
|
return fieldConfig;
|
|
}
|
|
|
|
if (fieldConfig.from && source.hasOwnProperty(fieldConfig.from)) {
|
|
result = source[fieldConfig.from];
|
|
}
|
|
|
|
if (result === null && fieldConfig.fallback) {
|
|
result = source[fieldConfig.fallback];
|
|
}
|
|
|
|
if (fieldConfig.template) {
|
|
result = fieldConfig.template.replace(/\$\{(.*?)\}/g, (match, key) => source[key] || '');
|
|
}
|
|
|
|
if (fieldConfig.func && typeof loadedFunctions[fieldConfig.func] === 'function') {
|
|
result = loadedFunctions[fieldConfig.func](fieldConfig.params || {});
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
function convertToGiteaFormat(originalData) {
|
|
const result = {};
|
|
Object.keys(config.fields).forEach((key) => {
|
|
result[key] = resolveField(originalData, config.fields[key]);
|
|
});
|
|
return result;
|
|
}
|
|
|
|
module.exports = { convertToGiteaFormat };
|