From 4c68175f81b45dd50b5c6a380153b493f29b38af Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Thu, 5 Nov 2020 21:04:53 +0100
Subject: [PATCH] fix: update-lockfile in-range updates (#7660)

* Revert "fix: update-lockfile branch reuse (#7651)"

This reverts commit 802572bc3319bd8ae6b82339e736094a149eb67e.

* fix: update-lockfile in-range updates
---
 .../__snapshots__/get-updated.spec.ts.snap    |  9 ----
 lib/workers/branch/get-updated.spec.ts        | 22 --------
 lib/workers/branch/get-updated.ts             | 50 ++++++++++++++-----
 3 files changed, 37 insertions(+), 44 deletions(-)

diff --git a/lib/workers/branch/__snapshots__/get-updated.spec.ts.snap b/lib/workers/branch/__snapshots__/get-updated.spec.ts.snap
index fb04118997..1917689508 100644
--- a/lib/workers/branch/__snapshots__/get-updated.spec.ts.snap
+++ b/lib/workers/branch/__snapshots__/get-updated.spec.ts.snap
@@ -1,14 +1,5 @@
 // Jest Snapshot v1, https://goo.gl/fbAQLP
 
-exports[`workers/branch/get-updated getUpdatedPackageFiles()  does not update artifacts on update-lockfile if packageFile already updated 1`] = `
-Object {
-  "artifactErrors": Array [],
-  "reuseExistingBranch": undefined,
-  "updatedArtifacts": Array [],
-  "updatedPackageFiles": Array [],
-}
-`;
-
 exports[`workers/branch/get-updated getUpdatedPackageFiles() handles autoreplace base updated 1`] = `
 Object {
   "artifactErrors": Array [],
diff --git a/lib/workers/branch/get-updated.spec.ts b/lib/workers/branch/get-updated.spec.ts
index ac51df6d1a..2fcf25c41a 100644
--- a/lib/workers/branch/get-updated.spec.ts
+++ b/lib/workers/branch/get-updated.spec.ts
@@ -168,28 +168,6 @@ describe('workers/branch/get-updated', () => {
       ]);
       const res = await getUpdatedPackageFiles(config);
       expect(res).toMatchSnapshot();
-      expect(res.updatedPackageFiles).toHaveLength(1);
-    });
-    it(' does not update artifacts on update-lockfile if packageFile already updated', async () => {
-      config.upgrades.push({
-        manager: 'composer',
-        branchName: undefined,
-        rangeStrategy: 'update-lockfile',
-        currentValue: '^1.0.0',
-        newValue: '^2.0.0',
-      });
-      autoReplace.doAutoReplace.mockResolvedValueOnce('existing content');
-      composer.updateArtifacts.mockResolvedValueOnce([
-        {
-          file: {
-            name: 'composer.lock',
-            contents: 'some contents',
-          },
-        },
-      ]);
-      const res = await getUpdatedPackageFiles(config);
-      expect(res).toMatchSnapshot();
-      expect(res.updatedPackageFiles).toHaveLength(0);
     });
   });
 });
diff --git a/lib/workers/branch/get-updated.ts b/lib/workers/branch/get-updated.ts
index b05a248004..66a2c8b04a 100644
--- a/lib/workers/branch/get-updated.ts
+++ b/lib/workers/branch/get-updated.ts
@@ -25,6 +25,7 @@ export async function getUpdatedPackageFiles(
     'manager.getUpdatedPackageFiles()'
   );
   const updatedFileContents: Record<string, string> = {};
+  const nonUpdatedFileContents: Record<string, string> = {};
   const packageFileManagers: Record<string, string> = {};
   const packageFileUpdatedDeps: Record<string, string[]> = {};
   const lockFileMaintenanceFiles = [];
@@ -65,12 +66,9 @@ export async function getUpdatedPackageFiles(
         if (res) {
           if (res === existingContent) {
             logger.debug({ packageFile, depName }, 'No content changed');
-            if (
-              upgrade.currentValue === upgrade.newValue &&
-              upgrade.rangeStrategy === 'update-lockfile'
-            ) {
+            if (upgrade.rangeStrategy === 'update-lockfile') {
               logger.debug({ packageFile, depName }, 'update-lockfile add');
-              updatedFileContents[packageFile] = res;
+              nonUpdatedFileContents[packageFile] = res;
             }
           } else {
             logger.debug({ packageFile, depName }, 'Contents updated');
@@ -123,16 +121,11 @@ export async function getUpdatedPackageFiles(
         updatedFileContents[packageFile] = newContent;
       }
       if (newContent === existingContent) {
+        // istanbul ignore else
         if (upgrade.datasource === datasourceGitSubmodules.id) {
           updatedFileContents[packageFile] = newContent;
-        }
-        // istanbul ignore next
-        if (
-          upgrade.rangeStrategy === 'update-lockfile' &&
-          upgrade.currentValue === upgrade.newValue
-        ) {
-          // Treat the file as modified because we need to update artifacts
-          updatedFileContents[packageFile] = newContent;
+        } else if (upgrade.rangeStrategy === 'update-lockfile') {
+          nonUpdatedFileContents[packageFile] = newContent;
         }
       }
     }
@@ -166,6 +159,37 @@ export async function getUpdatedPackageFiles(
       }
     }
   }
+  const nonUpdatedPackageFiles = Object.keys(nonUpdatedFileContents).map(
+    (name) => ({
+      name,
+      contents: nonUpdatedFileContents[name],
+    })
+  );
+  for (const packageFile of nonUpdatedPackageFiles) {
+    const manager = packageFileManagers[packageFile.name];
+    const updatedDeps = packageFileUpdatedDeps[packageFile.name];
+    const updateArtifacts = get(manager, 'updateArtifacts');
+    if (updateArtifacts) {
+      const results = await updateArtifacts({
+        packageFileName: packageFile.name,
+        updatedDeps,
+        newPackageFileContent: packageFile.contents,
+        config,
+      });
+      if (is.nonEmptyArray(results)) {
+        updatedPackageFiles.push(packageFile);
+        for (const res of results) {
+          const { file, artifactError } = res;
+          // istanbul ignore else
+          if (file) {
+            updatedArtifacts.push(file);
+          } else if (artifactError) {
+            artifactErrors.push(artifactError);
+          }
+        }
+      }
+    }
+  }
   if (!config.reuseExistingBranch) {
     // Only perform lock file maintenance if it's a fresh commit
     for (const packageFile of lockFileMaintenanceFiles) {
-- 
GitLab