From 9c56d6f51c4223a2ced1f1a737efbc3a088484a8 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@keylocation.sg>
Date: Thu, 14 Sep 2017 07:21:22 +0200
Subject: [PATCH] fix: nrefactor config.logger -> logger (#794)

---
 lib/workers/branch/lock-files.js     |  6 ++-
 lib/workers/branch/package-files.js  |  3 +-
 lib/workers/branch/schedule.js       | 38 ++++++++-------
 lib/workers/branch/status-checks.js  |  5 +-
 lib/workers/global/versions.js       |  7 +--
 lib/workers/repository/apis.js       | 69 +++++++++++++---------------
 lib/workers/repository/onboarding.js | 33 ++++++-------
 lib/workers/repository/upgrades.js   |  4 +-
 8 files changed, 83 insertions(+), 82 deletions(-)

diff --git a/lib/workers/branch/lock-files.js b/lib/workers/branch/lock-files.js
index df205ed36a..5b66be1fcf 100644
--- a/lib/workers/branch/lock-files.js
+++ b/lib/workers/branch/lock-files.js
@@ -13,7 +13,8 @@ module.exports = {
 };
 
 function hasPackageLock(config, packageFile) {
-  config.logger.trace(
+  const { logger } = config;
+  logger.trace(
     { packageFiles: config.packageFiles, packageFile },
     'hasPackageLock'
   );
@@ -26,7 +27,8 @@ function hasPackageLock(config, packageFile) {
 }
 
 function hasYarnLock(config, packageFile) {
-  config.logger.trace(
+  const { logger } = config;
+  logger.trace(
     { packageFiles: config.packageFiles, packageFile },
     'hasYarnLock'
   );
diff --git a/lib/workers/branch/package-files.js b/lib/workers/branch/package-files.js
index cde5433b43..1620614db1 100644
--- a/lib/workers/branch/package-files.js
+++ b/lib/workers/branch/package-files.js
@@ -6,6 +6,7 @@ module.exports = {
 };
 
 async function getUpdatedPackageFiles(config) {
+  const { logger } = config;
   const updatedPackageFiles = {};
 
   for (const upgrade of config.upgrades) {
@@ -35,7 +36,7 @@ async function getUpdatedPackageFiles(config) {
         );
       }
       if (newContent !== existingContent) {
-        config.logger.debug('Updating packageFile content');
+        logger.debug('Updating packageFile content');
         updatedPackageFiles[upgrade.packageFile] = newContent;
       }
     }
diff --git a/lib/workers/branch/schedule.js b/lib/workers/branch/schedule.js
index c81283a4ec..b41d8d7df7 100644
--- a/lib/workers/branch/schedule.js
+++ b/lib/workers/branch/schedule.js
@@ -44,56 +44,54 @@ function hasValidSchedule(schedule) {
 }
 
 function isScheduledNow(config) {
+  const { logger } = config;
   let configSchedule = config.schedule;
-  config.logger.debug({ schedule: configSchedule }, `Checking schedule`);
+  logger.debug({ schedule: configSchedule }, `Checking schedule`);
   if (
     !configSchedule ||
     configSchedule.length === 0 ||
     configSchedule[0] === ''
   ) {
-    config.logger.debug('No schedule defined');
+    logger.debug('No schedule defined');
     return true;
   }
   if (!Array.isArray(configSchedule)) {
-    config.logger.warn(
+    logger.warn(
       `config schedule is not an array: ${JSON.stringify(configSchedule)}`
     );
     configSchedule = [configSchedule];
   }
   const [validSchedule, errorMessage] = hasValidSchedule(configSchedule);
   if (!validSchedule) {
-    config.logger.warn(errorMessage);
+    logger.warn(errorMessage);
     return true;
   }
   let now = moment();
-  config.logger.debug(`now=${now.format()}`);
+  logger.debug(`now=${now.format()}`);
   // Adjust the time if repo is in a different timezone to renovate
   if (config.timezone) {
     // TODO: check for validity manually
     now = now.tz(config.timezone);
-    config.logger.debug(`now=${now.format()}`);
+    logger.debug(`now=${now.format()}`);
   }
   // Get today in text form, e.g. "Monday";
   const currentDay = now.format('dddd');
-  config.logger.debug(`currentDay=${currentDay}`);
+  logger.debug(`currentDay=${currentDay}`);
   // Get the number of seconds since midnight
   const currentSeconds =
     now.hours() * 3600 + now.minutes() * 60 + now.seconds();
-  config.logger.debug(`currentSeconds=${currentSeconds}`);
+  logger.debug(`currentSeconds=${currentSeconds}`);
   // Support a single string but massage to array for processing
-  config.logger.debug(`Checking ${configSchedule.length} schedule(s)`);
+  logger.debug(`Checking ${configSchedule.length} schedule(s)`);
   // We run if any schedule matches
   const isWithinSchedule = configSchedule.some(scheduleText => {
     const parsedSchedule = later.parse.text(fixShortHours(scheduleText));
-    config.logger.debug(
-      { parsedSchedule },
-      `Checking schedule "${scheduleText}"`
-    );
+    logger.debug({ parsedSchedule }, `Checking schedule "${scheduleText}"`);
     // Later library returns array of schedules
     return parsedSchedule.schedules.some(schedule => {
       // Check if days are defined
       if (schedule.d) {
-        config.logger.debug({ schedule_d: schedule.d }, `schedule.d`);
+        logger.debug({ schedule_d: schedule.d }, `schedule.d`);
         // We need to compare text instead of numbers because
         // 'moment' adjusts day of week for locale while 'later' does not
         // later days run from 1..7
@@ -108,9 +106,9 @@ function isScheduledNow(config) {
           'Saturday',
         ];
         const scheduledDays = schedule.d.map(day => dowMap[day]);
-        config.logger.debug({ scheduledDays }, `scheduledDays`);
+        logger.debug({ scheduledDays }, `scheduledDays`);
         if (scheduledDays.indexOf(currentDay) === -1) {
-          config.logger.debug(
+          logger.debug(
             `Does not match schedule because ${currentDay} is not in ${scheduledDays}`
           );
           return false;
@@ -120,7 +118,7 @@ function isScheduledNow(config) {
       if (schedule.t_a) {
         const startSeconds = schedule.t_a[0];
         if (currentSeconds < startSeconds) {
-          config.logger.debug(
+          logger.debug(
             `Does not match schedule because ${currentSeconds} is earlier than ${startSeconds}`
           );
           return false;
@@ -130,18 +128,18 @@ function isScheduledNow(config) {
       if (schedule.t_b) {
         const endSeconds = schedule.t_b[0];
         if (currentSeconds > endSeconds) {
-          config.logger.debug(
+          logger.debug(
             `Does not match schedule because ${currentSeconds} is later than ${endSeconds}`
           );
           return false;
         }
       }
-      config.logger.debug(`Matches schedule ${scheduleText}`);
+      logger.debug(`Matches schedule ${scheduleText}`);
       return true;
     });
   });
   if (!isWithinSchedule) {
-    config.logger.debug('Package not scheduled');
+    logger.debug('Package not scheduled');
     return false;
   }
   return true;
diff --git a/lib/workers/branch/status-checks.js b/lib/workers/branch/status-checks.js
index f031dabda7..a7a427cf06 100644
--- a/lib/workers/branch/status-checks.js
+++ b/lib/workers/branch/status-checks.js
@@ -3,6 +3,7 @@ module.exports = {
 };
 
 async function setUnpublishable(config) {
+  const { logger } = config;
   let unpublishable;
   for (const upgrade of config.upgrades) {
     if (typeof upgrade.unpublishable !== 'undefined') {
@@ -35,9 +36,9 @@ async function setUnpublishable(config) {
       : 'Packages < 24 hours old can be unpublished';
     // Check if state needs setting
     if (existingState === state) {
-      config.logger.debug('Status check is already up-to-date');
+      logger.debug('Status check is already up-to-date');
     } else {
-      config.logger.debug(`Updating status check state to ${state}`);
+      logger.debug(`Updating status check state to ${state}`);
       await config.api.setBranchStatus(
         config.branchName,
         context,
diff --git a/lib/workers/global/versions.js b/lib/workers/global/versions.js
index e19f85d042..e7cf793b61 100644
--- a/lib/workers/global/versions.js
+++ b/lib/workers/global/versions.js
@@ -7,7 +7,8 @@ module.exports = {
 };
 
 function detectVersions(config) {
-  config.logger.debug('Detecting versions');
+  const { logger } = config;
+  logger.debug('Detecting versions');
   const versions = {};
   try {
     versions.renovate = root('package.json').version;
@@ -18,8 +19,8 @@ function detectVersions(config) {
     });
     versions.npm = result.stdout.toString().split('\n')[0];
   } catch (err) {
-    config.logger.error({ err }, 'Error detecting versions');
+    logger.error({ err }, 'Error detecting versions');
   }
-  config.logger.debug({ versions }, 'Detected versions');
+  logger.debug({ versions }, 'Detected versions');
   return versions;
 }
diff --git a/lib/workers/repository/apis.js b/lib/workers/repository/apis.js
index 90756b09ff..662df87e40 100644
--- a/lib/workers/repository/apis.js
+++ b/lib/workers/repository/apis.js
@@ -23,14 +23,15 @@ module.exports = {
 };
 
 async function detectSemanticCommits(config) {
+  const { logger } = config;
   const commitMessages = await config.api.getCommitMessages();
-  config.logger.trace(`commitMessages=${JSON.stringify(commitMessages)}`);
+  logger.trace(`commitMessages=${JSON.stringify(commitMessages)}`);
   const type = conventionalCommitsDetector(commitMessages);
   if (type === 'unknown') {
-    config.logger.debug('No semantic commit type found');
+    logger.debug('No semantic commit type found');
     return false;
   }
-  config.logger.debug(
+  logger.debug(
     `Found semantic commit type ${type} - enabling semantic commits`
   );
   return true;
@@ -63,7 +64,7 @@ async function checkMonorepos(input) {
   if (!lernaJson) {
     return config;
   }
-  config.logger.debug({ lernaJson }, 'Found lerna config');
+  logger.debug({ lernaJson }, 'Found lerna config');
   if (!lernaJson.packages) {
     return config;
   }
@@ -82,14 +83,15 @@ async function checkMonorepos(input) {
 
 // Check for .npmrc in repository and pass it to npm api if found
 async function getNpmrc(config) {
+  const { logger } = config;
   let npmrc;
   try {
     npmrc = await config.api.getFileContent('.npmrc');
     if (npmrc) {
-      config.logger.debug('Found .npmrc file in repository');
+      logger.debug('Found .npmrc file in repository');
     }
   } catch (err) {
-    config.logger.error('Failed to set .npmrc');
+    logger.error('Failed to set .npmrc');
   }
   return { ...config, npmrc };
 }
@@ -120,12 +122,13 @@ async function initApis(inputConfig, token) {
 }
 
 function migrateAndValidate(config, input) {
+  const { logger } = config;
   const { isMigrated, migratedConfig } = configMigration.migrateConfig(
     input,
     config
   );
   if (isMigrated) {
-    config.logger.info(
+    logger.info(
       { oldConfig: input, newConfig: migratedConfig },
       'Config migration necessary'
     );
@@ -134,10 +137,10 @@ function migrateAndValidate(config, input) {
   const { warnings, errors } = configValidation.validateConfig(massagedConfig);
   // istanbul ignore if
   if (warnings.length) {
-    config.logger.debug({ warnings }, 'Found renovate config warnings');
+    logger.debug({ warnings }, 'Found renovate config warnings');
   }
   if (errors.length) {
-    config.logger.warn({ errors }, 'Found renovate config errors');
+    logger.warn({ errors }, 'Found renovate config errors');
     /* TODO #556
     renovateJsonErrors.forEach(error => {
       config.errors.push(
@@ -154,16 +157,17 @@ function migrateAndValidate(config, input) {
 
 // Check for config in `renovate.json`
 async function mergeRenovateJson(config, branchName) {
+  const { logger } = config;
   let returnConfig = { ...config };
   const renovateJsonContent = await config.api.getFileContent(
     'renovate.json',
     branchName
   );
   if (!renovateJsonContent) {
-    config.logger.debug('No renovate.json found');
+    logger.debug('No renovate.json found');
     return returnConfig;
   }
-  config.logger.debug('Found renovate.json file');
+  logger.debug('Found renovate.json file');
   let renovateJson;
   try {
     let allowDuplicateKeys = true;
@@ -176,7 +180,7 @@ async function mergeRenovateJson(config, branchName) {
         depName: 'renovate.json',
         message: jsonValidationError,
       };
-      config.logger.warn(error.message);
+      logger.warn(error.message);
       returnConfig.errors.push(error);
       // Return unless error can be ignored
       return returnConfig;
@@ -191,25 +195,19 @@ async function mergeRenovateJson(config, branchName) {
         depName: 'renovate.json',
         message: jsonValidationError,
       };
-      config.logger.warn(error.message);
+      logger.warn(error.message);
       returnConfig.errors.push(error);
       // Return unless error can be ignored
     }
     renovateJson = JSON.parse(renovateJsonContent);
-    config.logger.debug({ config: renovateJson }, 'renovate.json config');
+    logger.debug({ config: renovateJson }, 'renovate.json config');
     const migratedConfig = migrateAndValidate(config, renovateJson);
-    config.logger.debug(
-      { config: migratedConfig },
-      'renovate.json migrated config'
-    );
+    logger.debug({ config: migratedConfig }, 'renovate.json migrated config');
     const resolvedConfig = await presets.resolveConfigPresets(
       migratedConfig,
       config.logger
     );
-    config.logger.debug(
-      { config: resolvedConfig },
-      'renovate.json resolved config'
-    );
+    logger.debug({ config: resolvedConfig }, 'renovate.json resolved config');
     returnConfig = configParser.mergeChildConfig(returnConfig, resolvedConfig);
     returnConfig.renovateJsonPresent = true;
   } catch (err) {
@@ -218,7 +216,7 @@ async function mergeRenovateJson(config, branchName) {
       depName: 'renovate.json',
       message: `Could not parse repository's renovate.json file`,
     };
-    config.logger.warn(error.message);
+    logger.warn(error.message);
     returnConfig.errors.push(error);
   }
   return returnConfig;
@@ -267,7 +265,7 @@ async function detectPackageFiles(input) {
     }
   }
   if (config.packageFiles.length === 0) {
-    config.logger.debug('Checking manually if repository has a package.json');
+    logger.debug('Checking manually if repository has a package.json');
     if (await config.api.getFileJson('package.json')) {
       config.packageFiles = ['package.json'];
     }
@@ -285,15 +283,14 @@ async function detectPackageFiles(input) {
 
 async function resolvePackageFiles(inputConfig) {
   const config = { ...inputConfig };
-  config.logger.trace({ config }, 'resolvePackageFiles()');
+  const { logger } = config;
+  logger.trace({ config }, 'resolvePackageFiles()');
   const packageFiles = [];
   for (let packageFile of config.packageFiles) {
     packageFile =
       typeof packageFile === 'string' ? { packageFile } : packageFile;
     if (packageFile.packageFile.endsWith('package.json')) {
-      config.logger.debug(
-        `Resolving packageFile ${JSON.stringify(packageFile)}`
-      );
+      logger.debug(`Resolving packageFile ${JSON.stringify(packageFile)}`);
       packageFile.content = await config.api.getFileJson(
         packageFile.packageFile,
         config.baseBranch
@@ -318,13 +315,13 @@ async function resolvePackageFiles(inputConfig) {
           packageFile.packageFile === 'package.json' &&
           packageFile.content.workspaces
         ) {
-          config.logger.info('Found yarn workspaces configuration');
+          logger.info('Found yarn workspaces configuration');
           config.hasYarnWorkspaces = true;
         }
         // hoist renovate config if exists
         if (packageFile.content.renovate) {
           config.hasPackageJsonRenovateConfig = true;
-          config.logger.debug(
+          logger.debug(
             {
               packageFile: packageFile.packageFile,
               config: packageFile.content.renovate,
@@ -335,7 +332,7 @@ async function resolvePackageFiles(inputConfig) {
             config,
             packageFile.content.renovate
           );
-          config.logger.debug(
+          logger.debug(
             { config: migratedConfig },
             'package.json migrated config'
           );
@@ -343,14 +340,14 @@ async function resolvePackageFiles(inputConfig) {
             migratedConfig,
             config.logger
           );
-          config.logger.debug(
+          logger.debug(
             { config: resolvedConfig },
             'package.json resolved config'
           );
           Object.assign(packageFile, resolvedConfig);
           delete packageFile.content.renovate;
         } else {
-          config.logger.debug(
+          logger.debug(
             { packageFile: packageFile.packageFile },
             `No renovate config`
           );
@@ -363,7 +360,7 @@ async function resolvePackageFiles(inputConfig) {
         if (
           await config.api.getFileContent(yarnLockFileName, config.baseBranch)
         ) {
-          config.logger.debug(
+          logger.debug(
             { packageFile: packageFile.packageFile },
             'Found yarn.lock'
           );
@@ -381,7 +378,7 @@ async function resolvePackageFiles(inputConfig) {
             config.baseBranch
           )
         ) {
-          config.logger.debug(
+          logger.debug(
             { packageFile: packageFile.packageFile },
             'Found package-lock.json'
           );
@@ -390,7 +387,7 @@ async function resolvePackageFiles(inputConfig) {
           packageFile.hasPackageLock = false;
         }
       } else {
-        config.logger.warn(
+        logger.warn(
           { packageFile: packageFile.packageFile },
           'package file not found'
         );
diff --git a/lib/workers/repository/onboarding.js b/lib/workers/repository/onboarding.js
index 79aeb00692..4aac5e7542 100644
--- a/lib/workers/repository/onboarding.js
+++ b/lib/workers/repository/onboarding.js
@@ -19,14 +19,15 @@ async function isRepoPrivate(config) {
 }
 
 async function createBranch(config) {
+  const { logger } = config;
   const onboardBranchName = `${config.branchPrefix}configure`;
   const repoIsPrivate = await module.exports.isRepoPrivate(config);
   let onboardingConfigString;
   if (repoIsPrivate) {
-    config.logger.debug('Repo is private - setting to app type');
+    logger.debug('Repo is private - setting to app type');
     onboardingConfigString = `{\n  "extends": [":app"]\n}\n`;
   } else {
-    config.logger.debug('Repo is not private - setting to library');
+    logger.debug('Repo is not private - setting to library');
     onboardingConfigString = `{\n  "extends": [":library"]\n}\n`;
   }
   const existingContent = await config.api.getFileContent(
@@ -34,16 +35,16 @@ async function createBranch(config) {
     onboardBranchName
   );
   if (existingContent === onboardingConfigString) {
-    config.logger.debug('Onboarding branch is already up-to-date');
+    logger.debug('Onboarding branch is already up-to-date');
     return;
   }
   if (existingContent) {
-    config.logger.debug(
+    logger.debug(
       { existingContent, onboardingConfigString },
       'Updating onboarding branch'
     );
   } else {
-    config.logger.debug('Creating onboarding branch');
+    logger.debug('Creating onboarding branch');
   }
 
   await config.api.commitFilesToBranch(
@@ -59,8 +60,7 @@ async function createBranch(config) {
 }
 
 async function ensurePr(config, branchUpgrades) {
-  const warnings = config.warnings;
-  const errors = config.errors;
+  const { logger, errors, warnings } = config;
   const description = config.description || [];
   if (config.assignees && config.assignees.length) {
     const assignees = config.assignees.map(
@@ -196,12 +196,12 @@ With your current configuration, renovate will initially create the following Pu
   if (existingPr) {
     // Check if existing PR needs updating
     if (existingPr.title === onboardPrTitle && existingPr.body === prBody) {
-      config.logger.info(`${existingPr.displayNumber} does not need updating`);
+      logger.info(`${existingPr.displayNumber} does not need updating`);
       return;
     }
     // PR must need updating
     await config.api.updatePr(existingPr.number, onboardPrTitle, prBody);
-    config.logger.info(`Updated ${existingPr.displayNumber}`);
+    logger.info(`Updated ${existingPr.displayNumber}`);
     return;
   }
   const pr = await config.api.createPr(
@@ -210,18 +210,19 @@ With your current configuration, renovate will initially create the following Pu
     prBody,
     true
   );
-  config.logger.debug(`Created ${pr.displayNumber} for configuration`);
+  logger.debug(`Created ${pr.displayNumber} for configuration`);
 }
 
 async function getOnboardingStatus(config) {
-  config.logger.debug('Checking if repo is configured');
+  const { logger } = config;
+  logger.debug('Checking if repo is configured');
   // Check if repository is configured
   if (config.onboarding === false) {
-    config.logger.debug('Repo onboarding is disabled');
+    logger.debug('Repo onboarding is disabled');
     return true;
   }
   if (config.renovateJsonPresent || config.hasPackageJsonRenovateConfig) {
-    config.logger.debug('Repo has been configured');
+    logger.debug('Repo has been configured');
     return true;
   }
   const pr = await config.api.findPr(
@@ -229,13 +230,13 @@ async function getOnboardingStatus(config) {
     'Configure Renovate'
   );
   if (pr) {
-    config.logger.debug(`Found existing onboarding PR#${pr.number}`);
+    logger.debug(`Found existing onboarding PR#${pr.number}`);
     if (pr.isClosed) {
-      config.logger.debug('Found closed Configure Renovate PR');
+      logger.debug('Found closed Configure Renovate PR');
       return true;
     }
     // PR exists but hasn't been closed yet
-    config.logger.debug(
+    logger.debug(
       `PR #${pr.displayNumber} needs to be closed to enable renovate to continue`
     );
     const prDetails = await config.api.getPr(pr.number);
diff --git a/lib/workers/repository/upgrades.js b/lib/workers/repository/upgrades.js
index aa7c58dc90..428402c14a 100644
--- a/lib/workers/repository/upgrades.js
+++ b/lib/workers/repository/upgrades.js
@@ -13,9 +13,9 @@ module.exports = {
 };
 
 async function determineRepoUpgrades(config) {
-  config.logger.trace({ config }, 'determineRepoUpgrades');
+  logger.trace({ config }, 'determineRepoUpgrades');
   if (config.packageFiles.length === 0) {
-    config.logger.warn('No package files found');
+    logger.warn('No package files found');
   }
   let upgrades = [];
   // Iterate through repositories sequentially
-- 
GitLab