From 4691b1a2a173cacaf9ec17cbb2acdadd8dbabd74 Mon Sep 17 00:00:00 2001 From: mrahmed2026 Date: Tue, 3 Sep 2024 14:03:50 +0300 Subject: [PATCH 1/4] complexity from 16 to 15 test 1 --- src/install.js | 62 +++++++++++++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 23 deletions(-) diff --git a/src/install.js b/src/install.js index 89b40d7b39..d5100b2c64 100644 --- a/src/install.js +++ b/src/install.js @@ -123,33 +123,49 @@ function checkSetupFlagEnv() { } function checkCIFlag() { - let ciVals; - try { - ciVals = JSON.parse(nconf.get('ci')); - } catch (e) { - ciVals = undefined; - } + let ciVals = getCIVals(); + + if (isValidCIVals(ciVals)) { + install.ciVals = ciVals; + } else { + handleMissingCIValues(ciVals); + } +} - if (ciVals && ciVals instanceof Object) { - if (ciVals.hasOwnProperty('host') && ciVals.hasOwnProperty('port') && ciVals.hasOwnProperty('database')) { - install.ciVals = ciVals; - } else { - winston.error('[install/checkCIFlag] required values are missing for automated CI integration:'); - if (!ciVals.hasOwnProperty('host')) { - winston.error(' host'); - } - if (!ciVals.hasOwnProperty('port')) { - winston.error(' port'); - } - if (!ciVals.hasOwnProperty('database')) { - winston.error(' database'); - } +function getCIVals() { + try { + return JSON.parse(nconf.get('ci')); + } catch (e) { + return undefined; + } +} - process.exit(); - } - } +function isValidCIVals(ciVals) { + return ciVals && ciVals instanceof Object && + ciVals.hasOwnProperty('host') && + ciVals.hasOwnProperty('port') && + ciVals.hasOwnProperty('database'); } +function handleMissingCIValues(ciVals) { + winston.error('[install/checkCIFlag] required values are missing for automated CI integration:'); + + if (!ciVals.hasOwnProperty('host')) { + winston.error(' host'); + } + + if (!ciVals.hasOwnProperty('port')) { + winston.error(' port'); + } + + if (!ciVals.hasOwnProperty('database')) { + winston.error(' database'); + } + + process.exit(); +} + + async function setupConfig() { const configureDatabases = require('../install/databases'); From 35f6a7f2b52983acfda639003e7ea3db5e9f56d1 Mon Sep 17 00:00:00 2001 From: mrahmed2026 Date: Tue, 3 Sep 2024 14:16:09 +0300 Subject: [PATCH 2/4] trying to solve 22 to 15 complexity issue, other is solved i think --- src/install.js | 149 +++++++++++++++++++++++++++---------------------- 1 file changed, 82 insertions(+), 67 deletions(-) diff --git a/src/install.js b/src/install.js index d5100b2c64..2e24d9980e 100644 --- a/src/install.js +++ b/src/install.js @@ -48,80 +48,95 @@ questions.optional = [ ]; function checkSetupFlagEnv() { - let setupVal = install.values; - - const envConfMap = { - CONFIG: 'config', - NODEBB_CONFIG: 'config', - NODEBB_URL: 'url', - NODEBB_PORT: 'port', - NODEBB_ADMIN_USERNAME: 'admin:username', - NODEBB_ADMIN_PASSWORD: 'admin:password', - NODEBB_ADMIN_EMAIL: 'admin:email', - NODEBB_DB: 'database', - NODEBB_DB_HOST: 'host', - NODEBB_DB_PORT: 'port', - NODEBB_DB_USER: 'username', - NODEBB_DB_PASSWORD: 'password', - NODEBB_DB_NAME: 'database', - NODEBB_DB_SSL: 'ssl', - }; + let setupVal = install.values; + const envConfMap = getEnvConfMap(); - // Set setup values from env vars (if set) - const envKeys = Object.keys(process.env); - if (Object.keys(envConfMap).some(key => envKeys.includes(key))) { - winston.info('[install/checkSetupFlagEnv] checking env vars for setup info...'); - setupVal = setupVal || {}; - - Object.entries(process.env).forEach(([evName, evValue]) => { // get setup values from env - if (evName.startsWith('NODEBB_DB_')) { - setupVal[`${process.env.NODEBB_DB}:${envConfMap[evName]}`] = evValue; - } else if (evName.startsWith('NODEBB_')) { - setupVal[envConfMap[evName]] = evValue; - } - }); + if (hasRelevantEnvVars(envConfMap)) { + winston.info('[install/checkSetupFlagEnv] checking env vars for setup info...'); + setupVal = setUpValuesFromEnv(setupVal, envConfMap); + } - setupVal['admin:password:confirm'] = setupVal['admin:password']; - } + setupVal = getSetupValuesFromJson(setupVal); - // try to get setup values from json, if successful this overwrites all values set by env - // TODO: better behaviour would be to support overrides per value, i.e. in order of priority (generic pattern): - // flag, env, config file, default - try { - if (nconf.get('setup')) { - const setupJSON = JSON.parse(nconf.get('setup')); - setupVal = { ...setupVal, ...setupJSON }; - } - } catch (err) { - winston.error('[install/checkSetupFlagEnv] invalid json in nconf.get(\'setup\'), ignoring setup values from json'); - } + if (setupVal && typeof setupVal === 'object') { + validateSetupValues(setupVal); + } else if (nconf.get('database')) { + setDatabaseValues(); + } +} - if (setupVal && typeof setupVal === 'object') { - if (setupVal['admin:username'] && setupVal['admin:password'] && setupVal['admin:password:confirm'] && setupVal['admin:email']) { - install.values = setupVal; - } else { - winston.error('[install/checkSetupFlagEnv] required values are missing for automated setup:'); - if (!setupVal['admin:username']) { - winston.error(' admin:username'); - } - if (!setupVal['admin:password']) { - winston.error(' admin:password'); - } - if (!setupVal['admin:password:confirm']) { - winston.error(' admin:password:confirm'); - } - if (!setupVal['admin:email']) { - winston.error(' admin:email'); - } +function getEnvConfMap() { + return { + CONFIG: 'config', + NODEBB_CONFIG: 'config', + NODEBB_URL: 'url', + NODEBB_PORT: 'port', + NODEBB_ADMIN_USERNAME: 'admin:username', + NODEBB_ADMIN_PASSWORD: 'admin:password', + NODEBB_ADMIN_EMAIL: 'admin:email', + NODEBB_DB: 'database', + NODEBB_DB_HOST: 'host', + NODEBB_DB_PORT: 'port', + NODEBB_DB_USER: 'username', + NODEBB_DB_PASSWORD: 'password', + NODEBB_DB_NAME: 'database', + NODEBB_DB_SSL: 'ssl', + }; +} - process.exit(); - } - } else if (nconf.get('database')) { - install.values = install.values || {}; - install.values.database = nconf.get('database'); - } +function hasRelevantEnvVars(envConfMap) { + const envKeys = Object.keys(process.env); + return Object.keys(envConfMap).some(key => envKeys.includes(key)); +} + +function setUpValuesFromEnv(setupVal, envConfMap) { + setupVal = setupVal || {}; + Object.entries(process.env).forEach(([evName, evValue]) => { + if (evName.startsWith('NODEBB_DB_')) { + setupVal[`${process.env.NODEBB_DB}:${envConfMap[evName]}`] = evValue; + } else if (evName.startsWith('NODEBB_')) { + setupVal[envConfMap[evName]] = evValue; + } + }); + setupVal['admin:password:confirm'] = setupVal['admin:password']; + return setupVal; +} + +function getSetupValuesFromJson(setupVal) { + try { + if (nconf.get('setup')) { + const setupJSON = JSON.parse(nconf.get('setup')); + return { ...setupVal, ...setupJSON }; + } + } catch (err) { + winston.error('[install/checkSetupFlagEnv] invalid json in nconf.get(\'setup\'), ignoring setup values from json'); + } + return setupVal; +} + +function validateSetupValues(setupVal) { + if (setupVal['admin:username'] && setupVal['admin:password'] && setupVal['admin:password:confirm'] && setupVal['admin:email']) { + install.values = setupVal; + } else { + winston.error('[install/checkSetupFlagEnv] required values are missing for automated setup:'); + logMissingSetupValues(setupVal); + process.exit(); + } +} + +function logMissingSetupValues(setupVal) { + if (!setupVal['admin:username']) winston.error(' admin:username'); + if (!setupVal['admin:password']) winston.error(' admin:password'); + if (!setupVal['admin:password:confirm']) winston.error(' admin:password:confirm'); + if (!setupVal['admin:email']) winston.error(' admin:email'); +} + +function setDatabaseValues() { + install.values = install.values || {}; + install.values.database = nconf.get('database'); } +// GPT assisted code function checkCIFlag() { let ciVals = getCIVals(); From 24c05e24fc9d647b30927b7de6cf06bf919f25ef Mon Sep 17 00:00:00 2001 From: mrahmed2026 Date: Tue, 3 Sep 2024 18:11:33 +0300 Subject: [PATCH 3/4] fixing elint errors --- src/install.js | 182 ++++++++++++++++++++++++------------------------- 1 file changed, 90 insertions(+), 92 deletions(-) diff --git a/src/install.js b/src/install.js index 2e24d9980e..67104e5e71 100644 --- a/src/install.js +++ b/src/install.js @@ -48,136 +48,134 @@ questions.optional = [ ]; function checkSetupFlagEnv() { - let setupVal = install.values; - const envConfMap = getEnvConfMap(); - - if (hasRelevantEnvVars(envConfMap)) { - winston.info('[install/checkSetupFlagEnv] checking env vars for setup info...'); - setupVal = setUpValuesFromEnv(setupVal, envConfMap); - } - - setupVal = getSetupValuesFromJson(setupVal); + let setupVal = install.values; + const envConfMap = getEnvConfMap(); + if (hasRelevantEnvVars(envConfMap)) { + winston.info('[install/checkSetupFlagEnv] checking env vars for setup info...'); + setupVal = setUpValuesFromEnv(setupVal, envConfMap); + } + setupVal = getSetupValuesFromJson(setupVal); - if (setupVal && typeof setupVal === 'object') { - validateSetupValues(setupVal); - } else if (nconf.get('database')) { - setDatabaseValues(); - } + if (setupVal && typeof setupVal === 'object') { + validateSetupValues(setupVal); + } else if (nconf.get('database')) { + setDatabaseValues(); + } } function getEnvConfMap() { - return { - CONFIG: 'config', - NODEBB_CONFIG: 'config', - NODEBB_URL: 'url', - NODEBB_PORT: 'port', - NODEBB_ADMIN_USERNAME: 'admin:username', - NODEBB_ADMIN_PASSWORD: 'admin:password', - NODEBB_ADMIN_EMAIL: 'admin:email', - NODEBB_DB: 'database', - NODEBB_DB_HOST: 'host', - NODEBB_DB_PORT: 'port', - NODEBB_DB_USER: 'username', - NODEBB_DB_PASSWORD: 'password', - NODEBB_DB_NAME: 'database', - NODEBB_DB_SSL: 'ssl', - }; + return { + CONFIG: 'config', + NODEBB_CONFIG: 'config', + NODEBB_URL: 'url', + NODEBB_PORT: 'port', + NODEBB_ADMIN_USERNAME: 'admin:username', + NODEBB_ADMIN_PASSWORD: 'admin:password', + NODEBB_ADMIN_EMAIL: 'admin:email', + NODEBB_DB: 'database', + NODEBB_DB_HOST: 'host', + NODEBB_DB_PORT: 'port', + NODEBB_DB_USER: 'username', + NODEBB_DB_PASSWORD: 'password', + NODEBB_DB_NAME: 'database', + NODEBB_DB_SSL: 'ssl', + }; } function hasRelevantEnvVars(envConfMap) { - const envKeys = Object.keys(process.env); - return Object.keys(envConfMap).some(key => envKeys.includes(key)); + const envKeys = Object.keys(process.env); + return Object.keys(envConfMap).some(key => envKeys.includes(key)); } function setUpValuesFromEnv(setupVal, envConfMap) { - setupVal = setupVal || {}; - Object.entries(process.env).forEach(([evName, evValue]) => { - if (evName.startsWith('NODEBB_DB_')) { - setupVal[`${process.env.NODEBB_DB}:${envConfMap[evName]}`] = evValue; - } else if (evName.startsWith('NODEBB_')) { - setupVal[envConfMap[evName]] = evValue; - } - }); - setupVal['admin:password:confirm'] = setupVal['admin:password']; - return setupVal; + setupVal = setupVal || {}; + Object.entries(process.env).forEach(([evName, evValue]) => { + if (evName.startsWith('NODEBB_DB_')) { + setupVal[`${process.env.NODEBB_DB}:${envConfMap[evName]}`] = evValue; + } else if (evName.startsWith('NODEBB_')) { + setupVal[envConfMap[evName]] = evValue; + } + }); + setupVal['admin:password:confirm'] = setupVal['admin:password']; + return setupVal; } function getSetupValuesFromJson(setupVal) { - try { - if (nconf.get('setup')) { - const setupJSON = JSON.parse(nconf.get('setup')); - return { ...setupVal, ...setupJSON }; - } - } catch (err) { - winston.error('[install/checkSetupFlagEnv] invalid json in nconf.get(\'setup\'), ignoring setup values from json'); - } - return setupVal; + try { + if (nconf.get('setup')) { + const setupJSON = JSON.parse(nconf.get('setup')); + return { ...setupVal, ...setupJSON }; + } + } catch (err) { + winston.error('[install/checkSetupFlagEnv] invalid json in nconf.get(\'setup\'), ignoring setup values from json'); + } + return setupVal; } function validateSetupValues(setupVal) { - if (setupVal['admin:username'] && setupVal['admin:password'] && setupVal['admin:password:confirm'] && setupVal['admin:email']) { - install.values = setupVal; - } else { - winston.error('[install/checkSetupFlagEnv] required values are missing for automated setup:'); - logMissingSetupValues(setupVal); - process.exit(); - } + if (setupVal['admin:username'] && setupVal['admin:password'] && setupVal['admin:password:confirm'] && setupVal['admin:email']) { + install.values = setupVal; + } else { + winston.error('[install/checkSetupFlagEnv] required values are missing for automated setup:'); + logMissingSetupValues(setupVal); + process.exit(); + } } function logMissingSetupValues(setupVal) { - if (!setupVal['admin:username']) winston.error(' admin:username'); - if (!setupVal['admin:password']) winston.error(' admin:password'); - if (!setupVal['admin:password:confirm']) winston.error(' admin:password:confirm'); - if (!setupVal['admin:email']) winston.error(' admin:email'); + if (!setupVal['admin:username']) winston.error(' admin:username'); + if (!setupVal['admin:password']) winston.error(' admin:password'); + if (!setupVal['admin:password:confirm']) winston.error(' admin:password:confirm'); + if (!setupVal['admin:email']) winston.error(' admin:email'); } function setDatabaseValues() { - install.values = install.values || {}; - install.values.database = nconf.get('database'); + install.values = install.values || {}; + install.values.database = nconf.get('database'); } // GPT assisted code function checkCIFlag() { - let ciVals = getCIVals(); - - if (isValidCIVals(ciVals)) { - install.ciVals = ciVals; - } else { - handleMissingCIValues(ciVals); - } + const ciVals = getCIVals(); + + if (isValidCIVals(ciVals)) { + install.ciVals = ciVals; + } else { + handleMissingCIValues(ciVals); + } } function getCIVals() { - try { - return JSON.parse(nconf.get('ci')); - } catch (e) { - return undefined; - } + try { + return JSON.parse(nconf.get('ci')); + } catch (e) { + return undefined; + } } function isValidCIVals(ciVals) { - return ciVals && ciVals instanceof Object && + return ciVals && ciVals instanceof Object && ciVals.hasOwnProperty('host') && ciVals.hasOwnProperty('port') && ciVals.hasOwnProperty('database'); } function handleMissingCIValues(ciVals) { - winston.error('[install/checkCIFlag] required values are missing for automated CI integration:'); - - if (!ciVals.hasOwnProperty('host')) { - winston.error(' host'); - } - - if (!ciVals.hasOwnProperty('port')) { - winston.error(' port'); - } - - if (!ciVals.hasOwnProperty('database')) { - winston.error(' database'); - } - - process.exit(); + winston.error('[install/checkCIFlag] required values are missing for automated CI integration:'); + + if (!ciVals.hasOwnProperty('host')) { + winston.error(' host'); + } + + if (!ciVals.hasOwnProperty('port')) { + winston.error(' port'); + } + + if (!ciVals.hasOwnProperty('database')) { + winston.error(' database'); + } + + process.exit(); } From 437eeab36881115d5163ce38a316564e5ccdaff0 Mon Sep 17 00:00:00 2001 From: mrahmed2026 Date: Tue, 3 Sep 2024 18:50:26 +0300 Subject: [PATCH 4/4] sonar cloud and lint issues all fixed, completely done. --- src/install.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/install.js b/src/install.js index 67104e5e71..4d8904820c 100644 --- a/src/install.js +++ b/src/install.js @@ -47,6 +47,7 @@ questions.optional = [ }, ]; +// gpt assisted code function checkSetupFlagEnv() { let setupVal = install.values; const envConfMap = getEnvConfMap();