Clean up old TRAP caches
This commit is contained in:
parent
898dead2d6
commit
610e72285f
7 changed files with 121 additions and 26 deletions
36
lib/trap-caching.js
generated
36
lib/trap-caching.js
generated
|
|
@ -29,6 +29,7 @@ const path = __importStar(require("path"));
|
|||
const actionsCache = __importStar(require("@actions/cache"));
|
||||
const actionsUtil = __importStar(require("./actions-util"));
|
||||
const apiClient = __importStar(require("./api-client"));
|
||||
const diagnostics_1 = require("./diagnostics");
|
||||
const util_1 = require("./util");
|
||||
// This constant should be bumped if we make a breaking change
|
||||
// to how the CodeQL Action stores or retrieves the TRAP cache,
|
||||
|
|
@ -138,10 +139,30 @@ async function cleanupTrapCaches(config, logger) {
|
|||
try {
|
||||
for (const language of config.languages) {
|
||||
if (config.trapCaches[language]) {
|
||||
const matchingCaches = await getTrapCachesForLanguage(language, logger);
|
||||
for (const cache of matchingCaches) {
|
||||
logger.info(`Matched Actions cache ${JSON.stringify(cache)}`);
|
||||
const cachesToRemove = await getTrapCachesForLanguage(language, logger);
|
||||
// Dates returned by the API are in ISO 8601 format, so we can sort them lexicographically
|
||||
cachesToRemove.sort((a, b) => b.created_at.localeCompare(a.created_at));
|
||||
// Keep the most recent cache
|
||||
logger.debug(`Keeping newest TRAP cache (${JSON.stringify(cachesToRemove[0])})`);
|
||||
cachesToRemove.pop();
|
||||
for (const cache of cachesToRemove) {
|
||||
logger.debug(`Deleting old TRAP cache (${JSON.stringify(cache)})`);
|
||||
await apiClient.deleteActionsCache(cache.id);
|
||||
}
|
||||
const totalBytesCleanedUp = cachesToRemove.reduce((acc, item) => acc + item.size_in_bytes, 0);
|
||||
const totalMegabytesCleanedUp = (totalBytesCleanedUp /
|
||||
(1024 * 1024)).toFixed(2);
|
||||
const message = `Cleaned up ${totalMegabytesCleanedUp} MiB of old TRAP caches for ${language}.`;
|
||||
logger.info(message);
|
||||
(0, diagnostics_1.addDiagnostic)(config, language, (0, diagnostics_1.makeDiagnostic)("codeql-action/trap-caching/cleanup", "TRAP caching cleanup statistics", {
|
||||
attributes: {
|
||||
totalBytesCleanedUp,
|
||||
},
|
||||
plaintextMessage: message,
|
||||
visibility: {
|
||||
telemetry: true,
|
||||
},
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -151,7 +172,14 @@ async function cleanupTrapCaches(config, logger) {
|
|||
}
|
||||
exports.cleanupTrapCaches = cleanupTrapCaches;
|
||||
async function getTrapCachesForLanguage(language, logger) {
|
||||
const allCaches = await apiClient.listActionsCaches(CODEQL_TRAP_CACHE_PREFIX, await actionsUtil.getRef(), logger);
|
||||
logger.debug(`Listing TRAP caches for ${language}`);
|
||||
const allCaches = await apiClient.listActionsCaches(CODEQL_TRAP_CACHE_PREFIX, await actionsUtil.getRef());
|
||||
for (const cache of allCaches) {
|
||||
if (!cache.created_at || !cache.id || !cache.key || !cache.size_in_bytes) {
|
||||
throw new Error("An unexpected cache item was returned from the API that was missing one or " +
|
||||
`more required fields: ${JSON.stringify(cache)}`);
|
||||
}
|
||||
}
|
||||
return allCaches.filter((cache) => {
|
||||
return cache.key?.includes(`-${language}-`);
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue