Skip to content

Commit ab1c842

Browse files
committed
Change hash to be a function that can use Features
1 parent 2a7680f commit ab1c842

File tree

3 files changed

+29
-25
lines changed

3 files changed

+29
-25
lines changed

lib/analyze-action.js

Lines changed: 8 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/init-action.js

Lines changed: 8 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/dependency-caching.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ interface CacheConfig {
2222
/** Gets the paths of directories on the runner that should be included in the cache. */
2323
getDependencyPaths: () => string[];
2424
/**
25-
* Patterns for the paths of files whose contents affect which dependencies are used
25+
* Gets patterns for the paths of files whose contents affect which dependencies are used
2626
* by a project. We find all files which match these patterns, calculate a hash for
2727
* their contents, and use that hash as part of the cache key.
2828
*/
29-
hash: string[];
29+
getHashPatterns: (codeql: CodeQL, features: Features) => Promise<string[]>;
3030
}
3131

3232
const CODEQL_DEPENDENCY_CACHE_PREFIX = "codeql-dependencies";
@@ -66,7 +66,7 @@ export function getJavaDependencyDirs(): string[] {
6666
const defaultCacheConfigs: { [language: string]: CacheConfig } = {
6767
java: {
6868
getDependencyPaths: getJavaDependencyDirs,
69-
hash: [
69+
getHashPatterns: async () => [
7070
// Maven
7171
"**/pom.xml",
7272
// Gradle
@@ -80,7 +80,7 @@ const defaultCacheConfigs: { [language: string]: CacheConfig } = {
8080
},
8181
csharp: {
8282
getDependencyPaths: () => [join(os.homedir(), ".nuget", "packages")],
83-
hash: [
83+
getHashPatterns: async () => [
8484
// NuGet
8585
"**/packages.lock.json",
8686
// Paket
@@ -89,7 +89,7 @@ const defaultCacheConfigs: { [language: string]: CacheConfig } = {
8989
},
9090
go: {
9191
getDependencyPaths: () => [join(os.homedir(), "go", "pkg", "mod")],
92-
hash: ["**/go.sum"],
92+
getHashPatterns: async () => ["**/go.sum"],
9393
},
9494
};
9595

@@ -149,7 +149,8 @@ export async function downloadDependencyCaches(
149149

150150
// Check that we can find files to calculate the hash for the cache key from, so we don't end up
151151
// with an empty string.
152-
const globber = await makeGlobber(cacheConfig.hash);
152+
const patterns = await cacheConfig.getHashPatterns(codeql, features);
153+
const globber = await makeGlobber(patterns);
153154

154155
if ((await globber.glob()).length === 0) {
155156
status.push({ language, hit_kind: CacheHitKind.NoHash });
@@ -159,7 +160,7 @@ export async function downloadDependencyCaches(
159160
continue;
160161
}
161162

162-
const primaryKey = await cacheKey(codeql, features, language, cacheConfig);
163+
const primaryKey = await cacheKey(codeql, features, language, patterns);
163164
const restoreKeys: string[] = [
164165
await cachePrefix(codeql, features, language),
165166
];
@@ -244,7 +245,8 @@ export async function uploadDependencyCaches(
244245

245246
// Check that we can find files to calculate the hash for the cache key from, so we don't end up
246247
// with an empty string.
247-
const globber = await makeGlobber(cacheConfig.hash);
248+
const patterns = await cacheConfig.getHashPatterns(codeql, features);
249+
const globber = await makeGlobber(patterns);
248250

249251
if ((await globber.glob()).length === 0) {
250252
status.push({ language, result: CacheStoreResult.NoHash });
@@ -279,7 +281,7 @@ export async function uploadDependencyCaches(
279281
continue;
280282
}
281283

282-
const key = await cacheKey(codeql, features, language, cacheConfig);
284+
const key = await cacheKey(codeql, features, language, patterns);
283285

284286
logger.info(
285287
`Uploading cache of size ${size} for ${language} with key ${key}...`,
@@ -330,9 +332,9 @@ async function cacheKey(
330332
codeql: CodeQL,
331333
features: Features,
332334
language: Language,
333-
cacheConfig: CacheConfig,
335+
patterns: string[],
334336
): Promise<string> {
335-
const hash = await glob.hashFiles(cacheConfig.hash.join("\n"));
337+
const hash = await glob.hashFiles(patterns.join("\n"));
336338
return `${await cachePrefix(codeql, features, language)}${hash}`;
337339
}
338340

0 commit comments

Comments
 (0)