From 732958c843df7056c2c7df221ce2b2b0ae259077 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@keylocation.sg>
Date: Sun, 22 Jan 2017 14:40:14 +0100
Subject: [PATCH] Refactor worker

---
 lib/config/index.js  |  10 ++--
 lib/index.js         | 106 +++++++++++++++++++++----------------------
 test/config/index.js |  11 ++---
 3 files changed, 64 insertions(+), 63 deletions(-)

diff --git a/lib/config/index.js b/lib/config/index.js
index d39656391f..85b5537864 100644
--- a/lib/config/index.js
+++ b/lib/config/index.js
@@ -11,7 +11,7 @@ let config = null;
 module.exports = {
   parseConfigs,
   getCascadedConfig,
-  getGlobalConfig,
+  getRepositories,
 };
 
 function parseConfigs(env, argv) {
@@ -49,6 +49,10 @@ function parseConfigs(env, argv) {
       config.repositories[index] = { repository: repo };
     }
   });
+  // Copy token to repo if doesn't have one
+  config.repositories.forEach((repo, index) => {
+    config.repositories[index].token = repo.token || config.token;
+  });
   // Set default packageFiles
   config.repositories.forEach((repo, index) => {
     if (!repo.packageFiles || !repo.packageFiles.length) {
@@ -77,8 +81,8 @@ function getCascadedConfig(repo, packageFile) {
   return cascadedConfig;
 }
 
-function getGlobalConfig() {
-  return config;
+function getRepositories() {
+  return config.repositories;
 }
 
 function redact(inputConfig) {
diff --git a/lib/index.js b/lib/index.js
index cc68b6c427..dbb5269ef1 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -1,6 +1,6 @@
 const stringify = require('json-stringify-pretty-compact');
 const logger = require('./logger');
-const config = require('./config');
+const configParser = require('./config');
 const github = require('./api/github');
 
 // Require main source
@@ -8,12 +8,13 @@ const worker = require('./worker');
 
 module.exports = {
   start,
+  processRepo,
 };
 
 function start() {
   // Parse config
   try {
-    config.parseConfigs(process.env, process.argv);
+    configParser.parseConfigs(process.env, process.argv);
   } catch (error) {
     logger.error(error.message);
     process.exit(-1);
@@ -22,62 +23,11 @@ function start() {
   // Initialize our promise chain
   let p = Promise.resolve();
 
-  // Get global config
-  const globalConfig = config.getGlobalConfig();
-
   // Queue repositories in sequence
-  globalConfig.repositories.forEach((repo) => {
+  configParser.getRepositories().forEach((repo) => {
     p = p.then(() => processRepo(repo));
   });
 
-  // Queue package files in sequence within a repo
-  function processRepo(repo) {
-    logger.debug(`Processing repository: ${JSON.stringify(repo)}`);
-    // Set GitHub token for this repo
-    process.env.GITHUB_TOKEN = repo.token || globalConfig.token;
-    // Initialize repo
-    return github.initRepo(repo.repository)
-    .then(() => checkForRenovateJson(repo))
-    .then(ensurePackageFiles)
-    .then(getWorkers);
-  }
-
-  // Check for config in `renovate.json`
-  function checkForRenovateJson(repo) {
-    return github.getFileJson('renovate.json')
-    .then((contents) => {
-      if (!contents) {
-        logger.debug('No renovate.json found');
-        return repo;
-      }
-      logger.debug(`renovate.json config: ${stringify(contents)}`);
-      return Object.assign(repo, contents);
-    });
-  }
-
-  // Ensure repo contains packageFiles
-  function ensurePackageFiles(repo) {
-    // Check for manually configured package files
-    if (repo.packageFiles.length) {
-      return Promise.resolve(repo);
-    }
-    // Otherwise, autodiscover filenames
-    return github.findFilePaths('package.json')
-    .then((fileNames) => {
-      const packageFiles =
-        fileNames.map(fileName => ({ fileName }));
-      return Object.assign(repo, { packageFiles });
-    });
-  }
-
-  // Return a promise queue of package file workers
-  function getWorkers(repo) {
-    return repo.packageFiles.reduce((promise, packageFile) => {
-      const cascadedConfig = config.getCascadedConfig(repo, packageFile);
-      return promise.then(() => worker(repo.repository, packageFile.fileName, cascadedConfig));
-    }, Promise.resolve());
-  }
-
   // Process all promises
   p
   .then(github.getRateLimit)
@@ -89,3 +39,51 @@ function start() {
     logger.error(`Unexpected error: ${error}`);
   });
 }
+
+// Queue package files in sequence within a repo
+function processRepo(config) {
+  logger.debug(`Processing repository: ${JSON.stringify(config)}`);
+  // Set GitHub token for this repo
+  process.env.GITHUB_TOKEN = config.token;
+  // Initialize repo
+  return github.initRepo(config.repository)
+  .then(() => checkForRenovateJson(config))
+  .then(ensurePackageFiles)
+  .then(getWorkers);
+}
+
+// Check for config in `renovate.json`
+function checkForRenovateJson(config) {
+  return github.getFileJson('renovate.json')
+  .then((contents) => {
+    if (!contents) {
+      logger.debug('No renovate.json found');
+      return config;
+    }
+    logger.debug(`renovate.json config: ${stringify(contents)}`);
+    return Object.assign(config, contents);
+  });
+}
+
+// Ensure config contains packageFiles
+function ensurePackageFiles(config) {
+  // Check for manually configured package files
+  if (config.packageFiles.length) {
+    return Promise.resolve(config);
+  }
+  // Otherwise, autodiscover filenames
+  return github.findFilePaths('package.json')
+  .then((fileNames) => {
+    const packageFiles =
+      fileNames.map(fileName => ({ fileName }));
+    return Object.assign(config, { packageFiles });
+  });
+}
+
+// Return a promise queue of package file workers
+function getWorkers(config) {
+  return config.packageFiles.reduce((promise, packageFile) => {
+    const cascadedConfig = configParser.getCascadedConfig(config, packageFile);
+    return promise.then(() => worker(config.repository, packageFile.fileName, cascadedConfig));
+  }, Promise.resolve());
+}
diff --git a/test/config/index.js b/test/config/index.js
index fb01569a94..de484fb803 100644
--- a/test/config/index.js
+++ b/test/config/index.js
@@ -21,16 +21,15 @@ describe('config/index', () => {
       const env = {};
       const argv = defaultArgv.concat(['--token=abc', 'foo']);
       configParser.parseConfigs(env, argv);
-      const config = configParser.getGlobalConfig();
-      should.exist(config.token);
-      should.exist(config.repositories);
-      should.exist(config.recreateClosed);
+      const repos = configParser.getRepositories();
+      should.exist(repos);
+      repos.should.have.length(1);
+      repos[0].repository.should.eql('foo');
     });
     it('gets cascaded config', () => {
       const env = { RENOVATE_CONFIG_FILE: 'test/_fixtures/config/file.js' };
       configParser.parseConfigs(env, defaultArgv);
-      const config = configParser.getGlobalConfig();
-      const repo = config.repositories.pop();
+      const repo = configParser.getRepositories().pop();
       should.exist(repo);
       const cascadedConfig = configParser.getCascadedConfig(repo, null);
       should.exist(cascadedConfig.token);
-- 
GitLab