From d56654cd8893651cd6d504d526b982784d94cd5e Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@keylocation.sg>
Date: Mon, 4 Dec 2017 15:07:35 +0100
Subject: [PATCH] =?UTF-8?q?=20fix:=20don=E2=80=99t=20exception=20if=20no?=
 =?UTF-8?q?=20rollback=20versions=20found=20(#1263)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 lib/manager/npm/registry.js     |  2 +-
 lib/workers/package/versions.js | 37 +++++++++++++++++++++------------
 2 files changed, 25 insertions(+), 14 deletions(-)

diff --git a/lib/manager/npm/registry.js b/lib/manager/npm/registry.js
index 3448c9744b..ec5531c134 100644
--- a/lib/manager/npm/registry.js
+++ b/lib/manager/npm/registry.js
@@ -81,7 +81,7 @@ async function getDependency(name) {
         time: res.time ? res.time[version] : '2017-01-01T12:00:00.000Z',
       };
     });
-    logger.trace({ dependency: dep }, 'dependency');
+    logger.trace({ dep }, 'dep');
     return dep;
   } catch (err) {
     logger.debug({ err }, `Dependency not found: ${name}`);
diff --git a/lib/workers/package/versions.js b/lib/workers/package/versions.js
index 0835f5de99..4f340f5544 100644
--- a/lib/workers/package/versions.js
+++ b/lib/workers/package/versions.js
@@ -12,6 +12,8 @@ module.exports = {
 };
 
 function determineUpgrades(npmDep, config) {
+  logger.debug(`determineUpgrades()`);
+  logger.trace({ npmDep, config });
   const result = {
     type: 'warning',
   };
@@ -28,6 +30,7 @@ function determineUpgrades(npmDep, config) {
   // Check for a current range and pin it
   if (isRange(currentVersion)) {
     // Pin ranges to their maximum satisfying version
+    logger.debug('currentVersion is range');
     const maxSatisfying = semver.maxSatisfying(versionList, currentVersion);
     allUpgrades.pin = {
       type: 'pin',
@@ -37,19 +40,27 @@ function determineUpgrades(npmDep, config) {
     };
     changeLogFromVersion = maxSatisfying;
   } else if (versionList.indexOf(currentVersion) === -1) {
-    const rollbackVersion = semver.maxSatisfying(
-      versionList,
-      `<${currentVersion}`
-    );
-    allUpgrades.rollback = {
-      type: 'rollback',
-      isRollback: true,
-      newVersion: rollbackVersion,
-      newVersionMajor: semver.major(rollbackVersion),
-      semanticCommitType: 'fix',
-      branchName:
-        '{{branchPrefix}}rollback-{{depNameSanitized}}-{{newVersionMajor}}.x',
-    };
+    logger.debug('Cannot find currentVersion');
+    try {
+      const rollbackVersion = semver.maxSatisfying(
+        versionList,
+        `<${currentVersion}`
+      );
+      allUpgrades.rollback = {
+        type: 'rollback',
+        isRollback: true,
+        newVersion: rollbackVersion,
+        newVersionMajor: semver.major(rollbackVersion),
+        semanticCommitType: 'fix',
+        branchName:
+          '{{branchPrefix}}rollback-{{depNameSanitized}}-{{newVersionMajor}}.x',
+      };
+    } catch (err) /* istanbul ignore next */ {
+      logger.info(
+        { dependency: npmDep.name, currentVersion },
+        'Warning: current version is missing from npm registry and cannot roll back'
+      );
+    }
   }
   _(versionList)
     // Filter out older versions as we can't upgrade to those
-- 
GitLab