mirror of
https://code.forgejo.org/actions/cache.git
synced 2025-04-29 14:59:55 +02:00
Include keys in getCacheEntry request
This commit is contained in:
parent
bd756c16ae
commit
072d513f28
6 changed files with 19 additions and 37 deletions
|
@ -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"
|
||||
);
|
||||
});
|
||||
|
||||
|
|
|
@ -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(
|
||||
|
|
8
dist/restore/index.js
vendored
8
dist/restore/index.js
vendored
|
@ -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;
|
||||
|
|
6
dist/save/index.js
vendored
6
dist/save/index.js
vendored
|
@ -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;
|
||||
|
|
|
@ -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<ArtifactCacheEntry | null> {
|
||||
export async function getCacheEntry(
|
||||
keys: string[]
|
||||
): Promise<ArtifactCacheEntry | null> {
|
||||
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<ArtifactCacheEntry>(
|
||||
getCacheApiUrl(resource)
|
||||
|
|
|
@ -54,7 +54,7 @@ async function run(): Promise<void> {
|
|||
}
|
||||
|
||||
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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue