Skip to content

Commit c35d1b3

Browse files
committed
Extract getCacheWorkflowKeyPrefix()
1 parent eb5531f commit c35d1b3

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

src/overlay-database-utils.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
downloadOverlayBaseDatabaseFromCache,
1414
getCacheRestoreKeyPrefix,
1515
getCacheSaveKey,
16+
getCacheWorkflowKeyPrefix,
1617
OverlayDatabaseMode,
1718
writeBaseDatabaseOidsFile,
1819
writeOverlayChangesFile,
@@ -295,8 +296,22 @@ test("overlay-base database cache keys remain stable", async (t) => {
295296
"This may indicate breaking changes in the cache key generation logic.",
296297
);
297298

299+
const workflowKeyPrefix = await getCacheWorkflowKeyPrefix();
300+
const expectedWorkflowKeyPrefix =
301+
"codeql-overlay-base-database-1-c5666c509a2d9895-";
302+
t.is(
303+
workflowKeyPrefix,
304+
expectedWorkflowKeyPrefix,
305+
"Cache workflow key prefix changed unexpectedly. " +
306+
"This may indicate breaking changes in the cache key generation logic.",
307+
);
308+
298309
t.true(
299310
saveKey.startsWith(restoreKeyPrefix),
300311
`Expected save key "${saveKey}" to start with restore key prefix "${restoreKeyPrefix}"`,
301312
);
313+
t.true(
314+
restoreKeyPrefix.startsWith(workflowKeyPrefix),
315+
`Expected restore key prefix "${restoreKeyPrefix}" to start with workflow key prefix "${workflowKeyPrefix}"`,
316+
);
302317
});

src/overlay-database-utils.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -480,26 +480,37 @@ export async function getCacheRestoreKeyPrefix(
480480
codeQlVersion: string,
481481
): Promise<string> {
482482
const languages = [...config.languages].sort().join("_");
483-
484-
const cacheKeyComponents = {
485-
automationID: await getAutomationID(),
486-
// Add more components here as needed in the future
487-
};
488-
const componentsHash = createCacheKeyHash(cacheKeyComponents);
483+
const workflowPrefix = await getCacheWorkflowKeyPrefix();
489484

490485
// For a cached overlay-base database to be considered compatible for overlay
491486
// analysis, all components in the cache restore key must match:
492487
//
493-
// CACHE_PREFIX: distinguishes overlay-base databases from other cache objects
494-
// CACHE_VERSION: cache format version
495-
// componentsHash: hash of additional components (see above for details)
488+
// workflowPrefix contains components that depend only on the workflow:
489+
// CACHE_PREFIX: distinguishes overlay-base databases from other cache objects
490+
// CACHE_VERSION: cache format version
491+
// componentsHash: hash of additional components (see above for details)
496492
// languages: the languages included in the overlay-base database
497493
// codeQlVersion: CodeQL bundle version
498494
//
499495
// Technically we can also include languages and codeQlVersion in the
500496
// componentsHash, but including them explicitly in the cache key makes it
501497
// easier to debug and understand the cache key structure.
502-
return `${CACHE_PREFIX}-${CACHE_VERSION}-${componentsHash}-${languages}-${codeQlVersion}-`;
498+
return `${workflowPrefix}${languages}-${codeQlVersion}-`;
499+
}
500+
501+
/**
502+
* Computes the cache key prefix that depends only on the workflow.
503+
*
504+
* @returns A promise that resolves to the common cache key prefix in the format
505+
* `${CACHE_PREFIX}-${CACHE_VERSION}-${componentsHash}-`
506+
*/
507+
export async function getCacheWorkflowKeyPrefix(): Promise<string> {
508+
const cacheKeyComponents = {
509+
automationID: await getAutomationID(),
510+
// Add more components here as needed in the future
511+
};
512+
const componentsHash = createCacheKeyHash(cacheKeyComponents);
513+
return `${CACHE_PREFIX}-${CACHE_VERSION}-${componentsHash}-`;
503514
}
504515

505516
/**

0 commit comments

Comments
 (0)