Address review comments from @henrymercer

This commit is contained in:
Edoardo Pirovano 2022-08-09 18:37:22 +01:00
parent 8f867dcb21
commit 6df93613d7
No known key found for this signature in database
GPG key ID: 047556B5D93FFE28
15 changed files with 125 additions and 63 deletions

43
lib/trap-caching.js generated
View file

@ -32,11 +32,13 @@ const util_1 = require("./util");
// this for CLI/extractor changes, since the CLI version also
// goes into the cache key.
const CACHE_VERSION = 1;
// This constant sets the size of each TRAP cache in megabytes.
const CACHE_SIZE_MB = 1024;
async function getTrapCachingExtractorConfigArgs(config) {
const result = [];
for (const language of config.languages)
result.push(...(await getTrapCachingExtractorConfigArgsForLang(config, language)));
return result;
result.push(await getTrapCachingExtractorConfigArgsForLang(config, language));
return result.flat();
}
exports.getTrapCachingExtractorConfigArgs = getTrapCachingExtractorConfigArgs;
async function getTrapCachingExtractorConfigArgsForLang(config, language) {
@ -46,11 +48,19 @@ async function getTrapCachingExtractorConfigArgsForLang(config, language) {
const write = await actionsUtil.isAnalyzingDefaultBranch();
return [
`-O=${language}.trap.cache.dir=${cacheDir}`,
`-O=${language}.trap.cache.bound=1024`,
`-O=${language}.trap.cache.bound=${CACHE_SIZE_MB}`,
`-O=${language}.trap.cache.write=${write}`,
];
}
exports.getTrapCachingExtractorConfigArgsForLang = getTrapCachingExtractorConfigArgsForLang;
/**
* Download TRAP caches from the Actions cache.
* @param codeql The CodeQL instance to use.
* @param languages The languages being analyzed.
* @param logger A logger to record some informational messages to.
* @returns A partial map from languages to TRAP cache paths on disk, with
* languages for which we shouldn't use TRAP caching omitted.
*/
async function downloadTrapCaches(codeql, languages, logger) {
var _a, _b;
const result = {};
@ -70,22 +80,25 @@ async function downloadTrapCaches(codeql, languages, logger) {
}
let baseSha = "unknown";
const eventPath = process.env.GITHUB_EVENT_PATH;
if (eventPath !== undefined) {
if (process.env.GITHUB_EVENT_NAME === "pull_request" &&
eventPath !== undefined) {
const event = JSON.parse(fs.readFileSync(path.resolve(eventPath), "utf-8"));
baseSha = ((_b = (_a = event.pull_request) === null || _a === void 0 ? void 0 : _a.base) === null || _b === void 0 ? void 0 : _b.sha) || "unknown";
baseSha = ((_b = (_a = event.pull_request) === null || _a === void 0 ? void 0 : _a.base) === null || _b === void 0 ? void 0 : _b.sha) || baseSha;
}
for (const language of languages) {
const cacheDir = result[language];
if (cacheDir === undefined)
continue;
// The SHA from the base of the PR is the most similar commit we might have a cache for
const preferredKey = `codeql-trap-${CACHE_VERSION}-${await codeql.getVersion()}-${language}-${baseSha}`;
const preferredKey = await cacheKey(codeql, language, baseSha);
logger.info(`Looking in Actions cache for TRAP cache with key ${preferredKey}`);
const found = await cache.restoreCache([cacheDir], preferredKey, [
`codeql-trap-${CACHE_VERSION}-${await codeql.getVersion()}-${language}-`, // Fall back to any cache with the right key prefix
await cachePrefix(codeql, language), // Fall back to any cache with the right key prefix
]);
if (found === undefined) {
// Didn't find any cache, let's unset to avoid lookups in an empty directory
// We didn't find a TRAP cache in the Actions cache, so the directory on disk is
// still just an empty directory. There's no reason to tell the extractor to use it,
// so let's unset the entry in the map so we don't set any extractor options.
logger.info(`No TRAP cache found in Actions cache for ${language}`);
result[language] = undefined;
}
@ -96,14 +109,16 @@ exports.downloadTrapCaches = downloadTrapCaches;
async function uploadTrapCaches(codeql, config, logger) {
if (!(await actionsUtil.isAnalyzingDefaultBranch()))
return; // Only upload caches from the default branch
const toAwait = [];
for (const language of config.languages) {
const cacheDir = config.trapCaches[language];
if (cacheDir === undefined)
continue;
const key = `codeql-trap-${CACHE_VERSION}-${await codeql.getVersion()}-${language}-${process.env.GITHUB_SHA || "unknown"}`;
const key = await cacheKey(codeql, language, process.env.GITHUB_SHA || "unknown");
logger.info(`Uploading TRAP cache to Actions cache with key ${key}`);
await cache.saveCache([cacheDir], key);
toAwait.push(cache.saveCache([cacheDir], key));
}
await Promise.all(toAwait);
}
exports.uploadTrapCaches = uploadTrapCaches;
async function getLanguagesSupportingCaching(codeql, languages, logger) {
@ -113,6 +128,8 @@ async function getLanguagesSupportingCaching(codeql, languages, logger) {
return result;
const resolveResult = await codeql.betterResolveLanguages();
outer: for (const lang of languages) {
if (resolveResult.extractors[lang].length !== 1)
continue;
const extractor = resolveResult.extractors[lang][0];
const trapCacheOptions = (_d = (_c = (_b = (_a = extractor.extractor_options) === null || _a === void 0 ? void 0 : _a.trap) === null || _b === void 0 ? void 0 : _b.properties) === null || _c === void 0 ? void 0 : _c.cache) === null || _d === void 0 ? void 0 : _d.properties;
if (trapCacheOptions === undefined) {
@ -130,4 +147,10 @@ async function getLanguagesSupportingCaching(codeql, languages, logger) {
return result;
}
exports.getLanguagesSupportingCaching = getLanguagesSupportingCaching;
async function cacheKey(codeql, language, baseSha) {
return `${await cachePrefix(codeql, language)}-${baseSha}`;
}
async function cachePrefix(codeql, language) {
return `codeql-trap-${CACHE_VERSION}-${await codeql.getVersion()}-${language}-`;
}
//# sourceMappingURL=trap-caching.js.map