Factor out common code for cleaning up bundle artifacts

This commit is contained in:
Henry Mercer 2023-08-01 19:47:57 +01:00
parent 92c848eb82
commit 61cdd2503b
3 changed files with 45 additions and 34 deletions

36
lib/setup-codeql.js generated
View file

@ -429,14 +429,7 @@ async function downloadCodeQL(codeqlURL, maybeBundleVersion, maybeCliVersion, ap
const extractedBundlePath = await toolcache.extractTar(archivedBundlePath);
const extractionMs = Math.round(perf_hooks_1.performance.now() - extractionStart);
logger.debug(`Finished extracting CodeQL bundle to ${extractedBundlePath} (${extractionMs} ms).`);
logger.debug("Cleaning up CodeQL bundle archive.");
try {
await (0, del_1.default)(archivedBundlePath, { force: true });
logger.debug("Deleted CodeQL bundle archive.");
}
catch (e) {
logger.warning("Failed to delete CodeQL bundle archive.");
}
await cleanUpGlob(archivedBundlePath, "CodeQL bundle archive", logger);
const bundleVersion = maybeBundleVersion ?? tryGetBundleVersionFromUrl(codeqlURL, logger);
if (bundleVersion === undefined) {
logger.debug("Could not cache CodeQL tools because we could not determine the bundle version from the " +
@ -469,14 +462,7 @@ async function downloadCodeQL(codeqlURL, maybeBundleVersion, maybeCliVersion, ap
const toolcachedBundlePath = await toolcache.cacheDir(extractedBundlePath, "CodeQL", toolcacheVersion);
// Defensive check: we expect `cacheDir` to copy the bundle to a new location.
if (toolcachedBundlePath !== extractedBundlePath) {
logger.debug("Cleaning up downloaded CodeQL bundle.");
try {
await (0, del_1.default)(archivedBundlePath, { force: true });
logger.debug("Deleted CodeQL bundle from temporary directory.");
}
catch (e) {
logger.warning("Failed to delete CodeQL bundle from temporary directory.");
}
await cleanUpGlob(extractedBundlePath, "CodeQL bundle from temporary directory", logger);
}
return {
toolsVersion: maybeCliVersion ?? toolcacheVersion,
@ -536,4 +522,22 @@ async function setupCodeQLBundle(toolsInput, apiDetails, tempDir, variant, defau
return { codeqlFolder, toolsDownloadDurationMs, toolsSource, toolsVersion };
}
exports.setupCodeQLBundle = setupCodeQLBundle;
async function cleanUpGlob(glob, name, logger) {
logger.debug(`Cleaning up ${name}.`);
try {
const deletedPaths = await (0, del_1.default)(glob, { force: true });
if (deletedPaths.length === 0) {
logger.warning(`Failed to clean up ${name}: no files found matching ${glob}.`);
}
else if (deletedPaths.length === 1) {
logger.debug(`Cleaned up ${name}.`);
}
else {
logger.debug(`Cleaned up ${name} (${deletedPaths.length} files).`);
}
}
catch (e) {
logger.warning(`Failed to clean up ${name}: ${e}.`);
}
}
//# sourceMappingURL=setup-codeql.js.map