From cd145abe1d2354dbf4904775a42bd6b019653012 Mon Sep 17 00:00:00 2001
From: Bariscankibarcekici <bariscankibarcekici@gmail.com>
Date: Thu, 28 Jul 2022 23:59:22 +0300
Subject: [PATCH] Revert "Consuming 3.0 actions/cache (#834)"

This reverts commit 354a2ae15e3e38c9c450c9bacb8f2fe7c684cacb.
---
 .licenses/npm/@actions/cache.dep.yml |  2 +-
 __tests__/restore.test.ts            | 34 +++++++++++
 __tests__/save.test.ts               |  5 +-
 dist/restore/index.js                | 84 ++++++++++++----------------
 dist/save/index.js                   | 68 ++++++++++------------
 package-lock.json                    | 14 ++---
 package.json                         |  2 +-
 src/restore.ts                       | 49 +++++++++-------
 src/save.ts                          | 18 ++++--
 9 files changed, 152 insertions(+), 124 deletions(-)

diff --git a/.licenses/npm/@actions/cache.dep.yml b/.licenses/npm/@actions/cache.dep.yml
index cb0614a..e2af04c 100644
--- a/.licenses/npm/@actions/cache.dep.yml
+++ b/.licenses/npm/@actions/cache.dep.yml
@@ -1,6 +1,6 @@
 ---
 name: "@actions/cache"
-version: 3.0.0
+version: 2.0.6
 type: npm
 summary: 
 homepage: 
diff --git a/__tests__/restore.test.ts b/__tests__/restore.test.ts
index e9a505b..b4dbba9 100644
--- a/__tests__/restore.test.ts
+++ b/__tests__/restore.test.ts
@@ -227,6 +227,40 @@ test("restore with no cache found", async () => {
     );
 });
 
+test("restore with server error should fail", async () => {
+    const path = "node_modules";
+    const key = "node-test";
+    testUtils.setInputs({
+        path: path,
+        key
+    });
+
+    const logWarningMock = jest.spyOn(actionUtils, "logWarning");
+    const failedMock = jest.spyOn(core, "setFailed");
+    const stateMock = jest.spyOn(core, "saveState");
+    const restoreCacheMock = jest
+        .spyOn(cache, "restoreCache")
+        .mockImplementationOnce(() => {
+            throw new Error("HTTP Error Occurred");
+        });
+    const setCacheHitOutputMock = jest.spyOn(actionUtils, "setCacheHitOutput");
+
+    await run();
+
+    expect(restoreCacheMock).toHaveBeenCalledTimes(1);
+    expect(restoreCacheMock).toHaveBeenCalledWith([path], key, []);
+
+    expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);
+
+    expect(logWarningMock).toHaveBeenCalledTimes(1);
+    expect(logWarningMock).toHaveBeenCalledWith("HTTP Error Occurred");
+
+    expect(setCacheHitOutputMock).toHaveBeenCalledTimes(1);
+    expect(setCacheHitOutputMock).toHaveBeenCalledWith(false);
+
+    expect(failedMock).toHaveBeenCalledTimes(0);
+});
+
 test("restore with restore keys and no cache found", async () => {
     const path = "node_modules";
     const key = "node-test";
diff --git a/__tests__/save.test.ts b/__tests__/save.test.ts
index 4a3ae39..7598e0c 100644
--- a/__tests__/save.test.ts
+++ b/__tests__/save.test.ts
@@ -267,6 +267,7 @@ test("save with large cache outputs warning", async () => {
 });
 
 test("save with reserve cache failure outputs warning", async () => {
+    const infoMock = jest.spyOn(core, "info");
     const logWarningMock = jest.spyOn(actionUtils, "logWarning");
     const failedMock = jest.spyOn(core, "setFailed");
 
@@ -305,10 +306,10 @@ test("save with reserve cache failure outputs warning", async () => {
         expect.anything()
     );
 
-    expect(logWarningMock).toHaveBeenCalledWith(
+    expect(infoMock).toHaveBeenCalledWith(
         `Unable to reserve cache with key ${primaryKey}, another job may be creating this cache.`
     );
-    expect(logWarningMock).toHaveBeenCalledTimes(1);
+    expect(logWarningMock).toHaveBeenCalledTimes(0);
     expect(failedMock).toHaveBeenCalledTimes(0);
 });
 
diff --git a/dist/restore/index.js b/dist/restore/index.js
index cc1c684..9351a66 100644
--- a/dist/restore/index.js
+++ b/dist/restore/index.js
@@ -46850,18 +46850,17 @@ function restoreCache(paths, primaryKey, restoreKeys, options) {
             checkKey(key);
         }
         const compressionMethod = yield utils.getCompressionMethod();
-        let archivePath = '';
+        // path are needed to compute version
+        const cacheEntry = yield cacheHttpClient.getCacheEntry(keys, paths, {
+            compressionMethod
+        });
+        if (!(cacheEntry === null || cacheEntry === void 0 ? void 0 : cacheEntry.archiveLocation)) {
+            // Cache not found
+            return undefined;
+        }
+        const archivePath = path.join(yield utils.createTempDirectory(), utils.getCacheFileName(compressionMethod));
+        core.debug(`Archive Path: ${archivePath}`);
         try {
-            // path are needed to compute version
-            const cacheEntry = yield cacheHttpClient.getCacheEntry(keys, paths, {
-                compressionMethod
-            });
-            if (!(cacheEntry === null || cacheEntry === void 0 ? void 0 : cacheEntry.archiveLocation)) {
-                // Cache not found
-                return undefined;
-            }
-            archivePath = path.join(yield utils.createTempDirectory(), utils.getCacheFileName(compressionMethod));
-            core.debug(`Archive Path: ${archivePath}`);
             // Download the cache from the cache entry
             yield cacheHttpClient.downloadCache(cacheEntry.archiveLocation, archivePath, options);
             if (core.isDebug()) {
@@ -46871,17 +46870,6 @@ function restoreCache(paths, primaryKey, restoreKeys, options) {
             core.info(`Cache Size: ~${Math.round(archiveFileSize / (1024 * 1024))} MB (${archiveFileSize} B)`);
             yield tar_1.extractTar(archivePath, compressionMethod);
             core.info('Cache restored successfully');
-            return cacheEntry.cacheKey;
-        }
-        catch (error) {
-            const typedError = error;
-            if (typedError.name === ValidationError.name) {
-                throw error;
-            }
-            else {
-                // Supress all non-validation cache related errors because caching should be optional
-                core.warning(`Failed to restore: ${error.message}`);
-            }
         }
         finally {
             // Try to delete the archive to save space
@@ -46892,7 +46880,7 @@ function restoreCache(paths, primaryKey, restoreKeys, options) {
                 core.debug(`Failed to delete archive: ${error}`);
             }
         }
-        return undefined;
+        return cacheEntry.cacheKey;
     });
 }
 exports.restoreCache = restoreCache;
@@ -46910,7 +46898,7 @@ function saveCache(paths, key, options) {
         checkPaths(paths);
         checkKey(key);
         const compressionMethod = yield utils.getCompressionMethod();
-        let cacheId = -1;
+        let cacheId = null;
         const cachePaths = yield utils.resolvePaths(paths);
         core.debug('Cache Paths:');
         core.debug(`${JSON.stringify(cachePaths)}`);
@@ -46949,18 +46937,6 @@ function saveCache(paths, key, options) {
             core.debug(`Saving Cache (ID: ${cacheId})`);
             yield cacheHttpClient.saveCache(cacheId, archivePath, options);
         }
-        catch (error) {
-            const typedError = error;
-            if (typedError.name === ValidationError.name) {
-                throw error;
-            }
-            else if (typedError.name === ReserveCacheError.name) {
-                core.info(`Failed to save: ${typedError.message}`);
-            }
-            else {
-                core.warning(`Failed to save: ${typedError.message}`);
-            }
-        }
         finally {
             // Try to delete the archive to save space
             try {
@@ -49020,19 +48996,31 @@ function run() {
             const cachePaths = utils.getInputAsArray(constants_1.Inputs.Path, {
                 required: true
             });
-            const cacheKey = yield cache.restoreCache(cachePaths, primaryKey, restoreKeys);
-            if (!cacheKey) {
-                core.info(`Cache not found for input keys: ${[
-                    primaryKey,
-                    ...restoreKeys
-                ].join(", ")}`);
-                return;
+            try {
+                const cacheKey = yield cache.restoreCache(cachePaths, primaryKey, restoreKeys);
+                if (!cacheKey) {
+                    core.info(`Cache not found for input keys: ${[
+                        primaryKey,
+                        ...restoreKeys
+                    ].join(", ")}`);
+                    return;
+                }
+                // Store the matched cache key
+                utils.setCacheState(cacheKey);
+                const isExactKeyMatch = utils.isExactKeyMatch(primaryKey, cacheKey);
+                utils.setCacheHitOutput(isExactKeyMatch);
+                core.info(`Cache restored from key: ${cacheKey}`);
+            }
+            catch (error) {
+                const typedError = error;
+                if (typedError.name === cache.ValidationError.name) {
+                    throw error;
+                }
+                else {
+                    utils.logWarning(typedError.message);
+                    utils.setCacheHitOutput(false);
+                }
             }
-            // Store the matched cache key
-            utils.setCacheState(cacheKey);
-            const isExactKeyMatch = utils.isExactKeyMatch(primaryKey, cacheKey);
-            utils.setCacheHitOutput(isExactKeyMatch);
-            core.info(`Cache restored from key: ${cacheKey}`);
         }
         catch (error) {
             core.setFailed(error.message);
diff --git a/dist/save/index.js b/dist/save/index.js
index b0f3e41..983cb5d 100644
--- a/dist/save/index.js
+++ b/dist/save/index.js
@@ -46792,12 +46792,24 @@ function run() {
             const cachePaths = utils.getInputAsArray(constants_1.Inputs.Path, {
                 required: true
             });
-            const cacheId = yield cache.saveCache(cachePaths, primaryKey, {
-                uploadChunkSize: utils.getInputAsInt(constants_1.Inputs.UploadChunkSize)
-            });
-            if (cacheId != -1) {
+            try {
+                yield cache.saveCache(cachePaths, primaryKey, {
+                    uploadChunkSize: utils.getInputAsInt(constants_1.Inputs.UploadChunkSize)
+                });
                 core.info(`Cache saved with key: ${primaryKey}`);
             }
+            catch (error) {
+                const typedError = error;
+                if (typedError.name === cache.ValidationError.name) {
+                    throw error;
+                }
+                else if (typedError.name === cache.ReserveCacheError.name) {
+                    core.info(typedError.message);
+                }
+                else {
+                    utils.logWarning(typedError.message);
+                }
+            }
         }
         catch (error) {
             utils.logWarning(error.message);
@@ -46936,18 +46948,17 @@ function restoreCache(paths, primaryKey, restoreKeys, options) {
             checkKey(key);
         }
         const compressionMethod = yield utils.getCompressionMethod();
-        let archivePath = '';
+        // path are needed to compute version
+        const cacheEntry = yield cacheHttpClient.getCacheEntry(keys, paths, {
+            compressionMethod
+        });
+        if (!(cacheEntry === null || cacheEntry === void 0 ? void 0 : cacheEntry.archiveLocation)) {
+            // Cache not found
+            return undefined;
+        }
+        const archivePath = path.join(yield utils.createTempDirectory(), utils.getCacheFileName(compressionMethod));
+        core.debug(`Archive Path: ${archivePath}`);
         try {
-            // path are needed to compute version
-            const cacheEntry = yield cacheHttpClient.getCacheEntry(keys, paths, {
-                compressionMethod
-            });
-            if (!(cacheEntry === null || cacheEntry === void 0 ? void 0 : cacheEntry.archiveLocation)) {
-                // Cache not found
-                return undefined;
-            }
-            archivePath = path.join(yield utils.createTempDirectory(), utils.getCacheFileName(compressionMethod));
-            core.debug(`Archive Path: ${archivePath}`);
             // Download the cache from the cache entry
             yield cacheHttpClient.downloadCache(cacheEntry.archiveLocation, archivePath, options);
             if (core.isDebug()) {
@@ -46957,17 +46968,6 @@ function restoreCache(paths, primaryKey, restoreKeys, options) {
             core.info(`Cache Size: ~${Math.round(archiveFileSize / (1024 * 1024))} MB (${archiveFileSize} B)`);
             yield tar_1.extractTar(archivePath, compressionMethod);
             core.info('Cache restored successfully');
-            return cacheEntry.cacheKey;
-        }
-        catch (error) {
-            const typedError = error;
-            if (typedError.name === ValidationError.name) {
-                throw error;
-            }
-            else {
-                // Supress all non-validation cache related errors because caching should be optional
-                core.warning(`Failed to restore: ${error.message}`);
-            }
         }
         finally {
             // Try to delete the archive to save space
@@ -46978,7 +46978,7 @@ function restoreCache(paths, primaryKey, restoreKeys, options) {
                 core.debug(`Failed to delete archive: ${error}`);
             }
         }
-        return undefined;
+        return cacheEntry.cacheKey;
     });
 }
 exports.restoreCache = restoreCache;
@@ -46996,7 +46996,7 @@ function saveCache(paths, key, options) {
         checkPaths(paths);
         checkKey(key);
         const compressionMethod = yield utils.getCompressionMethod();
-        let cacheId = -1;
+        let cacheId = null;
         const cachePaths = yield utils.resolvePaths(paths);
         core.debug('Cache Paths:');
         core.debug(`${JSON.stringify(cachePaths)}`);
@@ -47035,18 +47035,6 @@ function saveCache(paths, key, options) {
             core.debug(`Saving Cache (ID: ${cacheId})`);
             yield cacheHttpClient.saveCache(cacheId, archivePath, options);
         }
-        catch (error) {
-            const typedError = error;
-            if (typedError.name === ValidationError.name) {
-                throw error;
-            }
-            else if (typedError.name === ReserveCacheError.name) {
-                core.info(`Failed to save: ${typedError.message}`);
-            }
-            else {
-                core.warning(`Failed to save: ${typedError.message}`);
-            }
-        }
         finally {
             // Try to delete the archive to save space
             try {
diff --git a/package-lock.json b/package-lock.json
index 5f16fd2..fec209b 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -9,7 +9,7 @@
       "version": "3.0.4",
       "license": "MIT",
       "dependencies": {
-        "@actions/cache": "^3.0.0",
+        "@actions/cache": "^2.0.6",
         "@actions/core": "^1.7.0",
         "@actions/exec": "^1.1.1",
         "@actions/io": "^1.1.2"
@@ -36,9 +36,9 @@
       }
     },
     "node_modules/@actions/cache": {
-      "version": "3.0.0",
-      "resolved": "https://registry.npmjs.org/@actions/cache/-/cache-3.0.0.tgz",
-      "integrity": "sha512-GL9CT1Fnu+pqs8TTB621q8Xa8Cilw2n9MwvbgMedetH7L1q2n6jY61gzbwGbKgtVbp3gVJ12aNMi4osSGXx3KQ==",
+      "version": "2.0.6",
+      "resolved": "https://registry.npmjs.org/@actions/cache/-/cache-2.0.6.tgz",
+      "integrity": "sha512-Z39ZrWaTRRPaV/AOQdY7hve+Iy/HloH5prpz+k+0lZgGQs/3SeO0UYSIakVuXOk2pdMZnl0Nv0PoK1rmh9YfGQ==",
       "dependencies": {
         "@actions/core": "^1.2.6",
         "@actions/exec": "^1.0.1",
@@ -9533,9 +9533,9 @@
   },
   "dependencies": {
     "@actions/cache": {
-      "version": "3.0.0",
-      "resolved": "https://registry.npmjs.org/@actions/cache/-/cache-3.0.0.tgz",
-      "integrity": "sha512-GL9CT1Fnu+pqs8TTB621q8Xa8Cilw2n9MwvbgMedetH7L1q2n6jY61gzbwGbKgtVbp3gVJ12aNMi4osSGXx3KQ==",
+      "version": "2.0.6",
+      "resolved": "https://registry.npmjs.org/@actions/cache/-/cache-2.0.6.tgz",
+      "integrity": "sha512-Z39ZrWaTRRPaV/AOQdY7hve+Iy/HloH5prpz+k+0lZgGQs/3SeO0UYSIakVuXOk2pdMZnl0Nv0PoK1rmh9YfGQ==",
       "requires": {
         "@actions/core": "^1.2.6",
         "@actions/exec": "^1.0.1",
diff --git a/package.json b/package.json
index 7c8bfaa..5d418b2 100644
--- a/package.json
+++ b/package.json
@@ -23,7 +23,7 @@
   "author": "GitHub",
   "license": "MIT",
   "dependencies": {
-    "@actions/cache": "^3.0.0",
+    "@actions/cache": "^2.0.6",
     "@actions/core": "^1.7.0",
     "@actions/exec": "^1.1.1",
     "@actions/io": "^1.1.2"
diff --git a/src/restore.ts b/src/restore.ts
index 5bc17fa..648e4cb 100644
--- a/src/restore.ts
+++ b/src/restore.ts
@@ -29,29 +29,38 @@ async function run(): Promise<void> {
             required: true
         });
 
-        const cacheKey = await cache.restoreCache(
-            cachePaths,
-            primaryKey,
-            restoreKeys
-        );
-
-        if (!cacheKey) {
-            core.info(
-                `Cache not found for input keys: ${[
-                    primaryKey,
-                    ...restoreKeys
-                ].join(", ")}`
+        try {
+            const cacheKey = await cache.restoreCache(
+                cachePaths,
+                primaryKey,
+                restoreKeys
             );
+            if (!cacheKey) {
+                core.info(
+                    `Cache not found for input keys: ${[
+                        primaryKey,
+                        ...restoreKeys
+                    ].join(", ")}`
+                );
+                return;
+            }
 
-            return;
+            // Store the matched cache key
+            utils.setCacheState(cacheKey);
+
+            const isExactKeyMatch = utils.isExactKeyMatch(primaryKey, cacheKey);
+            utils.setCacheHitOutput(isExactKeyMatch);
+
+            core.info(`Cache restored from key: ${cacheKey}`);
+        } catch (error: unknown) {
+            const typedError = error as Error;
+            if (typedError.name === cache.ValidationError.name) {
+                throw error;
+            } else {
+                utils.logWarning(typedError.message);
+                utils.setCacheHitOutput(false);
+            }
         }
-
-        // Store the matched cache key
-        utils.setCacheState(cacheKey);
-
-        const isExactKeyMatch = utils.isExactKeyMatch(primaryKey, cacheKey);
-        utils.setCacheHitOutput(isExactKeyMatch);
-        core.info(`Cache restored from key: ${cacheKey}`);
     } catch (error: unknown) {
         core.setFailed((error as Error).message);
     }
diff --git a/src/save.ts b/src/save.ts
index a0a21bf..7b333fb 100644
--- a/src/save.ts
+++ b/src/save.ts
@@ -44,12 +44,20 @@ async function run(): Promise<void> {
             required: true
         });
 
-        const cacheId = await cache.saveCache(cachePaths, primaryKey, {
-            uploadChunkSize: utils.getInputAsInt(Inputs.UploadChunkSize)
-        });
-
-        if (cacheId != -1) {
+        try {
+            await cache.saveCache(cachePaths, primaryKey, {
+                uploadChunkSize: utils.getInputAsInt(Inputs.UploadChunkSize)
+            });
             core.info(`Cache saved with key: ${primaryKey}`);
+        } catch (error: unknown) {
+            const typedError = error as Error;
+            if (typedError.name === cache.ValidationError.name) {
+                throw error;
+            } else if (typedError.name === cache.ReserveCacheError.name) {
+                core.info(typedError.message);
+            } else {
+                utils.logWarning(typedError.message);
+            }
         }
     } catch (error: unknown) {
         utils.logWarning((error as Error).message);