From c00f3025caeee0e9c78c18c43de11ab15fd3b486 Mon Sep 17 00:00:00 2001
From: Daniel LeCheminant <danlec@gmail.com>
Date: Fri, 8 Nov 2024 11:36:34 -0800
Subject: [PATCH] Adds an option to do lookup-only of the cache (#217)

---
 README.md             |  8 +++++++-
 action.yml            |  4 ++++
 dist/restore/index.js |  7 ++++---
 src/restore.ts        | 17 ++++++++++++++---
 4 files changed, 29 insertions(+), 7 deletions(-)

diff --git a/README.md b/README.md
index 0f33fca..495eeb8 100644
--- a/README.md
+++ b/README.md
@@ -60,7 +60,7 @@ sensible defaults.
     # default: "false"
     cache-all-crates: ""
 
-    # Determiners whether the cache should be saved.
+    # Determines whether the cache should be saved.
     # If `false`, the cache is only restored.
     # Useful for jobs where the matrix is additive e.g. additional Cargo features,
     # or when only runs from `master` should be saved to the cache.
@@ -69,6 +69,12 @@ sensible defaults.
     # To only cache runs from `master`:
     save-if: ${{ github.ref == 'refs/heads/master' }}
 
+    # Determines whether the cache should be restored.
+    # If `true` the cache key will be checked and the `cache-hit` output will be set
+    # but the cache itself won't be restored
+    # default: "false"
+    lookup-only: ""
+
     # Specifies what to use as the backend providing cache
     # Can be set to either "github" or "buildjet"
     # default: "github"
diff --git a/action.yml b/action.yml
index e000f77..cfe35dc 100644
--- a/action.yml
+++ b/action.yml
@@ -40,6 +40,10 @@ inputs:
     description: "Determines which provider to use for caching. Options are github or buildjet, defaults to github."
     required: false
     default: "github"
+  lookup-only:
+    description: "Check if a cache entry exists without downloading the cache"
+    required: false
+    default: "false"
 outputs:
   cache-hit:
     description: "A boolean value that indicates an exact match was found."
diff --git a/dist/restore/index.js b/dist/restore/index.js
index 676073e..07e4152 100644
--- a/dist/restore/index.js
+++ b/dist/restore/index.js
@@ -77876,20 +77876,21 @@ async function run() {
         if (cacheOnFailure !== "true") {
             cacheOnFailure = "false";
         }
+        var lookupOnly = lib_core.getInput("lookup-only").toLowerCase() === "true";
         lib_core.exportVariable("CACHE_ON_FAILURE", cacheOnFailure);
         lib_core.exportVariable("CARGO_INCREMENTAL", 0);
         const config = await CacheConfig.new();
         config.printInfo(cacheProvider);
         lib_core.info("");
-        lib_core.info(`... Restoring cache ...`);
+        lib_core.info(`... ${lookupOnly ? "Checking" : "Restoring"} cache ...`);
         const key = config.cacheKey;
         // Pass a copy of cachePaths to avoid mutating the original array as reported by:
         // https://github.com/actions/toolkit/pull/1378
         // TODO: remove this once the underlying bug is fixed.
-        const restoreKey = await cacheProvider.cache.restoreCache(config.cachePaths.slice(), key, [config.restoreKey]);
+        const restoreKey = await cacheProvider.cache.restoreCache(config.cachePaths.slice(), key, [config.restoreKey], { lookupOnly });
         if (restoreKey) {
             const match = restoreKey === key;
-            lib_core.info(`Restored from cache key "${restoreKey}" full match: ${match}.`);
+            lib_core.info(`${lookupOnly ? "Found" : "Restored from"} cache key "${restoreKey}" full match: ${match}.`);
             if (!match) {
                 // pre-clean the target directory on cache mismatch
                 for (const workspace of config.workspaces) {
diff --git a/src/restore.ts b/src/restore.ts
index 2bfffc2..289c00d 100644
--- a/src/restore.ts
+++ b/src/restore.ts
@@ -24,6 +24,8 @@ async function run() {
     if (cacheOnFailure !== "true") {
       cacheOnFailure = "false";
     }
+    var lookupOnly = core.getInput("lookup-only").toLowerCase() === "true";
+
     core.exportVariable("CACHE_ON_FAILURE", cacheOnFailure);
     core.exportVariable("CARGO_INCREMENTAL", 0);
 
@@ -31,15 +33,24 @@ async function run() {
     config.printInfo(cacheProvider);
     core.info("");
 
-    core.info(`... Restoring cache ...`);
+    core.info(`... ${lookupOnly ? "Checking" : "Restoring"} cache ...`);
     const key = config.cacheKey;
     // Pass a copy of cachePaths to avoid mutating the original array as reported by:
     // https://github.com/actions/toolkit/pull/1378
     // TODO: remove this once the underlying bug is fixed.
-    const restoreKey = await cacheProvider.cache.restoreCache(config.cachePaths.slice(), key, [config.restoreKey]);
+    const restoreKey = await cacheProvider.cache.restoreCache(
+      config.cachePaths.slice(),
+      key,
+      [config.restoreKey],
+      { lookupOnly }
+    );
     if (restoreKey) {
       const match = restoreKey === key;
-      core.info(`Restored from cache key "${restoreKey}" full match: ${match}.`);
+      core.info(
+        `${
+          lookupOnly ? "Found" : "Restored from"
+        } cache key "${restoreKey}" full match: ${match}.`
+      );
       if (!match) {
         // pre-clean the target directory on cache mismatch
         for (const workspace of config.workspaces) {