From 539312645c9472be710ce2cd4ae36bc7cdd9979b Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Tue, 1 Sep 2020 16:40:06 +0200
Subject: [PATCH] refactor: better init config names

---
 lib/workers/repository/init/common.ts      |  9 ++--
 lib/workers/repository/init/config.spec.ts |  2 +-
 lib/workers/repository/init/config.ts      | 62 ++++++++++++++--------
 3 files changed, 43 insertions(+), 30 deletions(-)

diff --git a/lib/workers/repository/init/common.ts b/lib/workers/repository/init/common.ts
index 4424d679c2..cd1cbab024 100644
--- a/lib/workers/repository/init/common.ts
+++ b/lib/workers/repository/init/common.ts
@@ -1,18 +1,15 @@
-import { RenovateConfig } from '../../../config';
-
 export type RepoConfigError = {
   validationError: string;
   validationMessage: string;
 };
 
 export interface RepoFileConfig {
-  fileName?: string;
-  config?: any;
-  error?: RepoConfigError;
+  configFileName?: string;
+  configFileParsed?: any;
+  configFileParseError?: RepoConfigError;
 }
 
 export interface RepoInitConfig {
   defaultBranchSha?: string;
   repoConfig?: RepoFileConfig;
-  resolvedConfig?: RenovateConfig;
 }
diff --git a/lib/workers/repository/init/config.spec.ts b/lib/workers/repository/init/config.spec.ts
index fb393aeb1f..d5bb81a07a 100644
--- a/lib/workers/repository/init/config.spec.ts
+++ b/lib/workers/repository/init/config.spec.ts
@@ -93,7 +93,7 @@ describe('workers/repository/init/config', () => {
     it('throws on error', () => {
       expect(() =>
         checkForRepoConfigError({
-          error: { validationError: '', validationMessage: '' },
+          configFileParseError: { validationError: '', validationMessage: '' },
         })
       ).toThrow();
     });
diff --git a/lib/workers/repository/init/config.ts b/lib/workers/repository/init/config.ts
index efe4f53f8e..4133d79116 100644
--- a/lib/workers/repository/init/config.ts
+++ b/lib/workers/repository/init/config.ts
@@ -42,29 +42,30 @@ export async function detectRepoFileConfig(): Promise<RepoFileConfig> {
     }
     return null;
   }
-  const fileName = await detectConfigFile();
-  if (!fileName) {
+  const configFileName = await detectConfigFile();
+  if (!configFileName) {
     logger.debug('No renovate config file found');
     return {};
   }
-  logger.debug(`Found ${fileName} config file`);
-  let config;
-  if (fileName === 'package.json') {
+  logger.debug(`Found ${configFileName} config file`);
+  let configFileParsed;
+  if (configFileName === 'package.json') {
     // We already know it parses
-    config = JSON.parse(await readLocalFile('package.json', 'utf8')).renovate;
-    logger.debug({ config }, 'package.json>renovate config');
+    configFileParsed = JSON.parse(await readLocalFile('package.json', 'utf8'))
+      .renovate;
+    logger.debug({ config: configFileParsed }, 'package.json>renovate config');
   } else {
-    let rawFileContents = await readLocalFile(fileName, 'utf8');
+    let rawFileContents = await readLocalFile(configFileName, 'utf8');
     // istanbul ignore if
     if (!rawFileContents.length) {
       rawFileContents = '{}';
     }
 
-    const fileType = path.extname(fileName);
+    const fileType = path.extname(configFileName);
 
     if (fileType === '.json5') {
       try {
-        config = JSON5.parse(rawFileContents);
+        configFileParsed = JSON5.parse(rawFileContents);
       } catch (err) /* istanbul ignore next */ {
         logger.debug(
           { renovateConfig: rawFileContents },
@@ -72,7 +73,10 @@ export async function detectRepoFileConfig(): Promise<RepoFileConfig> {
         );
         const validationError = 'Invalid JSON5 (parsing failed)';
         const validationMessage = `JSON5.parse error:  ${String(err.message)}`;
-        return { fileName, error: { validationError, validationMessage } };
+        return {
+          configFileName,
+          configFileParseError: { validationError, validationMessage },
+        };
       }
     } else {
       let allowDuplicateKeys = true;
@@ -83,7 +87,10 @@ export async function detectRepoFileConfig(): Promise<RepoFileConfig> {
       if (jsonValidationError) {
         const validationError = 'Invalid JSON (parsing failed)';
         const validationMessage = jsonValidationError;
-        return { fileName, error: { validationError, validationMessage } };
+        return {
+          configFileName,
+          configFileParseError: { validationError, validationMessage },
+        };
       }
       allowDuplicateKeys = false;
       jsonValidationError = jsonValidator.validate(
@@ -93,10 +100,13 @@ export async function detectRepoFileConfig(): Promise<RepoFileConfig> {
       if (jsonValidationError) {
         const validationError = 'Duplicate keys in JSON';
         const validationMessage = JSON.stringify(jsonValidationError);
-        return { fileName, error: { validationError, validationMessage } };
+        return {
+          configFileName,
+          configFileParseError: { validationError, validationMessage },
+        };
       }
       try {
-        config = JSON.parse(rawFileContents);
+        configFileParsed = JSON.parse(rawFileContents);
       } catch (err) /* istanbul ignore next */ {
         logger.debug(
           { renovateConfig: rawFileContents },
@@ -104,22 +114,28 @@ export async function detectRepoFileConfig(): Promise<RepoFileConfig> {
         );
         const validationError = 'Invalid JSON (parsing failed)';
         const validationMessage = `JSON.parse error:  ${String(err.message)}`;
-        return { fileName, error: { validationError, validationMessage } };
+        return {
+          configFileName,
+          configFileParseError: { validationError, validationMessage },
+        };
       }
     }
-    logger.debug({ fileName, config }, 'Repository config');
+    logger.debug(
+      { fileName: configFileName, config: configFileParsed },
+      'Repository config'
+    );
   }
-  return { fileName, config };
+  return { configFileName, configFileParsed };
 }
 
 export function checkForRepoConfigError(repoConfig: RepoFileConfig): void {
-  if (!repoConfig.error) {
+  if (!repoConfig.configFileParseError) {
     return;
   }
   const error = new Error(CONFIG_VALIDATION);
-  error.configFile = repoConfig.fileName;
-  error.validationError = repoConfig.error.validationError;
-  error.validationMessage = repoConfig.error.validationMessage;
+  error.configFile = repoConfig.configFileName;
+  error.validationError = repoConfig.configFileParseError.validationError;
+  error.validationMessage = repoConfig.configFileParseError.validationMessage;
   throw error;
 }
 
@@ -132,11 +148,11 @@ export async function mergeRenovateConfig(
   checkForRepoConfigError(repoConfig);
   const migratedConfig = await migrateAndValidate(
     config,
-    repoConfig?.config || {}
+    repoConfig?.configFileParsed || {}
   );
   if (migratedConfig.errors.length) {
     const error = new Error(CONFIG_VALIDATION);
-    error.configFile = repoConfig.fileName;
+    error.configFile = repoConfig.configFileName;
     error.validationError =
       'The renovate configuration file contains some invalid settings';
     error.validationMessage = migratedConfig.errors
-- 
GitLab