From 072d513f28fdb5e78f1520f953da0db7600154f5 Mon Sep 17 00:00:00 2001 From: Ethan Dennis Date: Mon, 16 Mar 2020 12:49:13 -0700 Subject: [PATCH] Include keys in getCacheEntry request --- __tests__/cacheHttpsClient.test.ts | 24 ++++-------------------- __tests__/restore.test.ts | 6 +++--- dist/restore/index.js | 8 +++----- dist/save/index.js | 6 ++---- src/cacheHttpClient.ts | 10 ++++++---- src/restore.ts | 2 +- 6 files changed, 19 insertions(+), 37 deletions(-) diff --git a/__tests__/cacheHttpsClient.test.ts b/__tests__/cacheHttpsClient.test.ts index 0fca5dd..99de775 100644 --- a/__tests__/cacheHttpsClient.test.ts +++ b/__tests__/cacheHttpsClient.test.ts @@ -1,34 +1,18 @@ import * as testUtils from "../src/utils/testUtils"; import { getCacheVersion } from "../src/cacheHttpClient"; +import { Inputs } from "../src/constants"; afterEach(() => { testUtils.clearInputs(); }); -test("getCacheVersion with no restore keys returns version", async () => { - testUtils.setInputs({ - path: "node-test", - key: "node_modules" - }); +test("getCacheVersion with path input returns version", async () => { + testUtils.setInput(Inputs.Path, "node_modules"); const result = getCacheVersion(); expect(result).toEqual( - "ee9d5dc2e8e2df8e32f62c367796abefc134790584015d8e1207523c9085e87e" - ); -}); - -test("getCacheVersion with restore keys returns version", async () => { - testUtils.setInputs({ - path: "node-test", - key: "node_modules", - restoreKeys: ["node-", "node"] - }); - - const result = getCacheVersion(); - - expect(result).toEqual( - "b8596b1e42c34a25be7b43c7b91892ed3ba81cba1e075365f408b35dbfabb61b" + "b3e0c6cb5ecf32614eeb2997d905b9c297046d7cbf69062698f25b14b4cb0985" ); }); diff --git a/__tests__/restore.test.ts b/__tests__/restore.test.ts index ac91835..0f003ac 100644 --- a/__tests__/restore.test.ts +++ b/__tests__/restore.test.ts @@ -241,7 +241,7 @@ test("restore with cache found", async () => { await run(); expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key); - expect(getCacheMock).toHaveBeenCalledWith(); + expect(getCacheMock).toHaveBeenCalledWith([key]); expect(setCacheStateMock).toHaveBeenCalledWith(cacheEntry); expect(createTempDirectoryMock).toHaveBeenCalledTimes(1); expect(downloadCacheMock).toHaveBeenCalledWith( @@ -307,7 +307,7 @@ test("restore with a pull request event and cache found", async () => { await run(); expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key); - expect(getCacheMock).toHaveBeenCalledWith(); + expect(getCacheMock).toHaveBeenCalledWith([key]); expect(setCacheStateMock).toHaveBeenCalledWith(cacheEntry); expect(createTempDirectoryMock).toHaveBeenCalledTimes(1); expect(downloadCacheMock).toHaveBeenCalledWith( @@ -374,7 +374,7 @@ test("restore with cache found for restore key", async () => { await run(); expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key); - expect(getCacheMock).toHaveBeenCalledWith(); + expect(getCacheMock).toHaveBeenCalledWith([key, restoreKey]); expect(setCacheStateMock).toHaveBeenCalledWith(cacheEntry); expect(createTempDirectoryMock).toHaveBeenCalledTimes(1); expect(downloadCacheMock).toHaveBeenCalledWith( diff --git a/dist/restore/index.js b/dist/restore/index.js index 64232e4..2a2648d 100644 --- a/dist/restore/index.js +++ b/dist/restore/index.js @@ -2237,8 +2237,6 @@ function createHttpClient() { function getCacheVersion() { // Add salt to cache version to support breaking changes in cache entry const components = [ - core.getInput(constants_1.Inputs.Key, { required: true }), - core.getInput(constants_1.Inputs.RestoreKeys, { required: false }), core.getInput(constants_1.Inputs.Path, { required: true }), versionSalt ]; @@ -2248,12 +2246,12 @@ function getCacheVersion() { .digest("hex"); } exports.getCacheVersion = getCacheVersion; -function getCacheEntry() { +function getCacheEntry(keys) { var _a; return __awaiter(this, void 0, void 0, function* () { const httpClient = createHttpClient(); const version = getCacheVersion(); - const resource = `cache?version=${version}`; + const resource = `cache?keys=${encodeURIComponent(keys.join(","))}&version=${version}`; const response = yield httpClient.getJson(getCacheApiUrl(resource)); if (response.statusCode === 204) { return null; @@ -4588,7 +4586,7 @@ function run() { } } try { - const cacheEntry = yield cacheHttpClient.getCacheEntry(); + const cacheEntry = yield cacheHttpClient.getCacheEntry(keys); if (!((_a = cacheEntry) === null || _a === void 0 ? void 0 : _a.archiveLocation)) { core.info(`Cache not found for input keys: ${keys.join(", ")}`); return; diff --git a/dist/save/index.js b/dist/save/index.js index 6f45755..4e6a9e0 100644 --- a/dist/save/index.js +++ b/dist/save/index.js @@ -2237,8 +2237,6 @@ function createHttpClient() { function getCacheVersion() { // Add salt to cache version to support breaking changes in cache entry const components = [ - core.getInput(constants_1.Inputs.Key, { required: true }), - core.getInput(constants_1.Inputs.RestoreKeys, { required: false }), core.getInput(constants_1.Inputs.Path, { required: true }), versionSalt ]; @@ -2248,12 +2246,12 @@ function getCacheVersion() { .digest("hex"); } exports.getCacheVersion = getCacheVersion; -function getCacheEntry() { +function getCacheEntry(keys) { var _a; return __awaiter(this, void 0, void 0, function* () { const httpClient = createHttpClient(); const version = getCacheVersion(); - const resource = `cache?version=${version}`; + const resource = `cache?keys=${encodeURIComponent(keys.join(","))}&version=${version}`; const response = yield httpClient.getJson(getCacheApiUrl(resource)); if (response.statusCode === 204) { return null; diff --git a/src/cacheHttpClient.ts b/src/cacheHttpClient.ts index d00355a..23f797c 100644 --- a/src/cacheHttpClient.ts +++ b/src/cacheHttpClient.ts @@ -84,8 +84,6 @@ function createHttpClient(): HttpClient { export function getCacheVersion(): string { // Add salt to cache version to support breaking changes in cache entry const components = [ - core.getInput(Inputs.Key, { required: true }), - core.getInput(Inputs.RestoreKeys, { required: false }), core.getInput(Inputs.Path, { required: true }), versionSalt ]; @@ -96,10 +94,14 @@ export function getCacheVersion(): string { .digest("hex"); } -export async function getCacheEntry(): Promise { +export async function getCacheEntry( + keys: string[] +): Promise { const httpClient = createHttpClient(); const version = getCacheVersion(); - const resource = `cache?version=${version}`; + const resource = `cache?keys=${encodeURIComponent( + keys.join(",") + )}&version=${version}`; const response = await httpClient.getJson( getCacheApiUrl(resource) diff --git a/src/restore.ts b/src/restore.ts index 7d7e864..f70c17b 100644 --- a/src/restore.ts +++ b/src/restore.ts @@ -54,7 +54,7 @@ async function run(): Promise { } try { - const cacheEntry = await cacheHttpClient.getCacheEntry(); + const cacheEntry = await cacheHttpClient.getCacheEntry(keys); if (!cacheEntry?.archiveLocation) { core.info(`Cache not found for input keys: ${keys.join(", ")}`); return;