From f2833c8275b6e378726f76ea666c25b470bb405c Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@keylocation.sg>
Date: Mon, 3 Jul 2017 10:31:36 +0200
Subject: [PATCH] refactor: Add 'package' stage to config definitions (#421)

* Add package phase

* Rename phase to stage
---
 lib/config/definitions.js                     | 42 +++++++++++--------
 lib/config/index.js                           | 17 ++++----
 lib/workers/package/index.js                  | 16 ++++---
 .../package/__snapshots__/index.spec.js.snap  | 26 ++++++++++++
 test/workers/package/index.spec.js            |  8 ++--
 5 files changed, 73 insertions(+), 36 deletions(-)
 create mode 100644 test/workers/package/__snapshots__/index.spec.js.snap

diff --git a/lib/config/definitions.js b/lib/config/definitions.js
index 114594bd69..5ab73e0654 100644
--- a/lib/config/definitions.js
+++ b/lib/config/definitions.js
@@ -20,18 +20,19 @@ const options = [
   {
     name: 'enabled',
     description: 'Enable or disable renovate',
+    stage: 'package',
     type: 'boolean',
   },
   {
     name: 'logFile',
     description: 'Log file path',
-    phase: 'global',
+    stage: 'global',
     type: 'string',
   },
   {
     name: 'logFileLevel',
     description: 'Log file log level',
-    phase: 'global',
+    stage: 'global',
     type: 'string',
     default: 'debug',
   },
@@ -39,24 +40,26 @@ const options = [
     name: 'timezone',
     description:
       '[IANA Time Zone](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)',
+    stage: 'package',
     type: 'string',
   },
   {
     name: 'schedule',
     description: 'Times of day/week to renovate',
     type: 'list',
+    stage: 'package',
   },
   {
     name: 'onboarding',
     description: 'Require a Configuration PR first',
-    phase: 'repository',
+    stage: 'repository',
     type: 'boolean',
     onboarding: false,
   },
   {
     name: 'platform',
     description: 'Platform type of repository',
-    phase: 'repository',
+    stage: 'repository',
     type: 'string',
     default: 'github',
     onboarding: false,
@@ -64,21 +67,21 @@ const options = [
   {
     name: 'endpoint',
     description: 'Custom endpoint to use',
-    phase: 'repository',
+    stage: 'repository',
     type: 'string',
     onboarding: false,
   },
   {
     name: 'token',
     description: 'Repository Auth Token',
-    phase: 'repository',
+    stage: 'repository',
     type: 'string',
     onboarding: false,
   },
   {
     name: 'autodiscover',
     description: 'Autodiscover all repositories',
-    phase: 'repository',
+    stage: 'repository',
     type: 'boolean',
     default: false,
     onboarding: false,
@@ -86,19 +89,19 @@ const options = [
   {
     name: 'githubAppId',
     description: 'GitHub App ID (enables GitHub App functionality if set)',
-    phase: 'global',
+    stage: 'global',
     type: 'integer',
   },
   {
     name: 'githubAppKey',
     description: 'GitHub App Private Key (.pem file contents)',
-    phase: 'global',
+    stage: 'global',
     type: 'string',
   },
   {
     name: 'repositories',
     description: 'List of Repositories',
-    phase: 'global',
+    stage: 'global',
     type: 'list',
     cli: false,
   },
@@ -106,12 +109,12 @@ const options = [
     name: 'packageFiles',
     description: 'Package file paths',
     type: 'list',
-    phase: 'repository',
+    stage: 'repository',
   },
   {
     name: 'depTypes',
     description: 'Dependency types',
-    phase: 'packageFile',
+    stage: 'packageFile',
     type: 'list',
     default: [
       { depType: 'dependencies', semanticPrefix: 'fix: ' },
@@ -124,13 +127,13 @@ const options = [
     name: 'ignoreDeps',
     description: 'Dependencies to ignore',
     type: 'list',
-    phase: 'depType',
+    stage: 'depType',
   },
   {
     name: 'packages',
     description: 'Package Rules',
     type: 'list',
-    phase: 'depType',
+    stage: 'depType',
     cli: false,
     env: false,
     onboarding: false,
@@ -139,29 +142,34 @@ const options = [
   {
     name: 'pinVersions',
     description: 'Convert ranged versions in package.json to pinned versions',
+    stage: 'package',
     type: 'boolean',
   },
   {
     name: 'separateMajorReleases',
     description:
       'If set to false, it will upgrade dependencies to latest release only, and not separate major/minor branches',
+    stage: 'package',
     type: 'boolean',
   },
   {
     name: 'ignoreFuture',
     description: 'Ignore versions tagged as "future"',
+    stage: 'package',
     type: 'boolean',
     onboarding: false,
   },
   {
     name: 'ignoreUnstable',
     description: 'Ignore versions with unstable semver',
+    stage: 'package',
     type: 'boolean',
     onboarding: false,
   },
   {
     name: 'respectLatest',
     description: 'Ignore versions newer than npm "latest" version',
+    stage: 'package',
     type: 'boolean',
     onboarding: false,
   },
@@ -251,14 +259,14 @@ const options = [
     name: 'yarnCacheFolder',
     description:
       'Location of yarn cache folder to use. Set to empty string to disable',
-    phase: 'global',
+    stage: 'global',
     type: 'string',
     default: '/tmp/yarn-cache',
   },
   {
     name: 'lockFileMaintenance',
     description: 'Configuration for lock file maintenance',
-    phase: 'packageFile',
+    stage: 'packageFile',
     type: 'json',
     default: {
       enabled: true,
@@ -333,7 +341,7 @@ const options = [
   {
     name: 'logLevel',
     description: 'Logging level',
-    phase: 'global',
+    stage: 'global',
     type: 'string',
     default: 'info',
     env: 'LOG_LEVEL',
diff --git a/lib/config/index.js b/lib/config/index.js
index 17035c0cf7..46b7e7bf92 100644
--- a/lib/config/index.js
+++ b/lib/config/index.js
@@ -118,7 +118,7 @@ async function parseConfigs(env, argv) {
   return config;
 }
 
-function mergeChildConfig(parentConfig, childConfig, additional) {
+function mergeChildConfig(parentConfig, childConfig) {
   const config = Object.assign({}, parentConfig, childConfig);
   for (const option of definitions.getOptions()) {
     if (option.mergeable && childConfig[option.name]) {
@@ -134,16 +134,13 @@ function mergeChildConfig(parentConfig, childConfig, additional) {
       );
     }
   }
-  if (additional) {
-    Object.assign(config, additional);
-  }
   return config;
 }
 
-function filterConfig(inputConfig, targetPhase) {
-  logger.trace({ config: inputConfig }, `filterConfig('${targetPhase}')`);
+function filterConfig(inputConfig, targetStage) {
+  logger.trace({ config: inputConfig }, `filterConfig('${targetStage}')`);
   const outputConfig = Object.assign({}, inputConfig);
-  const phases = [
+  const stages = [
     'global',
     'repository',
     'packageFile',
@@ -152,9 +149,9 @@ function filterConfig(inputConfig, targetPhase) {
     'branch',
     'pr',
   ];
-  const targetIndex = phases.indexOf(targetPhase);
+  const targetIndex = stages.indexOf(targetStage);
   for (const option of definitions.getOptions()) {
-    const optionIndex = phases.indexOf(option.phase);
+    const optionIndex = stages.indexOf(option.stage);
     if (optionIndex !== -1 && optionIndex < targetIndex) {
       delete outputConfig[option.name];
     }
@@ -165,7 +162,7 @@ function filterConfig(inputConfig, targetPhase) {
 function getOnboardingConfig(repoConfig) {
   const config = {};
   for (const option of definitions.getOptions()) {
-    if (option.phase !== 'global' && option.onboarding !== false) {
+    if (option.stage !== 'global' && option.onboarding !== false) {
       config[option.name] = repoConfig[option.name];
     }
   }
diff --git a/lib/workers/package/index.js b/lib/workers/package/index.js
index a5cf6810a4..9b40ca2bf1 100644
--- a/lib/workers/package/index.js
+++ b/lib/workers/package/index.js
@@ -17,7 +17,11 @@ async function findUpgrades(config) {
     return [];
   }
   // Check schedule
-  if (config.schedule && !schedule.isScheduledNow(config)) {
+  if (
+    config.schedule &&
+    config.schedule.length &&
+    schedule.isScheduledNow(config) === false
+  ) {
     logger.debug('Skipping package as it is not scheduled');
     return [];
   }
@@ -39,9 +43,9 @@ async function findUpgrades(config) {
     logger.debug(`${config.depName}: No upgrades required`);
   }
   // Flatten the upgrade on top of config, add repositoryUrl
-  return upgrades.map(upgrade =>
-    configParser.mergeChildConfig(config, upgrade, {
-      repositoryUrl: npmDep.repositoryUrl,
-    })
-  );
+  return upgrades.map(upgrade => {
+    const upg = configParser.mergeChildConfig(config, upgrade);
+    upg.repositoryUrl = npmDep.repositoryUrl;
+    return configParser.filterConfig(upg, 'branch');
+  });
 }
diff --git a/test/workers/package/__snapshots__/index.spec.js.snap b/test/workers/package/__snapshots__/index.spec.js.snap
new file mode 100644
index 0000000000..7c7e3242ee
--- /dev/null
+++ b/test/workers/package/__snapshots__/index.spec.js.snap
@@ -0,0 +1,26 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`lib/workers/package/index findUpgrades(config) returns array if upgrades found 1`] = `
+Array [
+  "semanticCommits",
+  "semanticPrefix",
+  "recreateClosed",
+  "rebaseStalePrs",
+  "prCreation",
+  "automerge",
+  "automergeType",
+  "branchName",
+  "commitMessage",
+  "prTitle",
+  "prBody",
+  "lazyGrouping",
+  "groupName",
+  "groupSlug",
+  "group",
+  "labels",
+  "assignees",
+  "reviewers",
+  "depName",
+  "repositoryUrl",
+]
+`;
diff --git a/test/workers/package/index.spec.js b/test/workers/package/index.spec.js
index 67a933412c..59ba394c04 100644
--- a/test/workers/package/index.spec.js
+++ b/test/workers/package/index.spec.js
@@ -2,6 +2,8 @@ const npmApi = require('../../../lib/api/npm');
 const schedule = require('../../../lib/workers/package/schedule');
 const versions = require('../../../lib/workers/package/versions');
 const pkgWorker = require('../../../lib/workers/package/index');
+const defaultConfig = require('../../../lib/config/defaults').getConfig();
+const configParser = require('../../../lib/config');
 
 jest.mock('../../../lib/workers/package/schedule');
 jest.mock('../../../lib/workers/package/versions');
@@ -11,9 +13,8 @@ describe('lib/workers/package/index', () => {
   describe('findUpgrades(config)', () => {
     let config;
     beforeEach(() => {
-      config = {
-        depName: 'foo',
-      };
+      config = configParser.filterConfig(defaultConfig, 'package');
+      config.depName = 'foo';
     });
     it('returns empty if package is disabled', async () => {
       config.enabled = false;
@@ -45,6 +46,7 @@ describe('lib/workers/package/index', () => {
       versions.determineUpgrades.mockReturnValueOnce([{}]);
       const res = await pkgWorker.findUpgrades(config);
       expect(res).toHaveLength(1);
+      expect(Object.keys(res[0])).toMatchSnapshot();
     });
   });
 });
-- 
GitLab