Restrict TRAP cache cleanup to analyzed language

This commit is contained in:
Henry Mercer 2024-05-21 16:33:20 +01:00
parent cc96c825ba
commit 087f0b04c6
5 changed files with 48 additions and 21 deletions

View file

@ -196,8 +196,15 @@ export function computeAutomationID(
return automationID;
}
export interface ActionsCacheItem {
key?: string | undefined;
}
/** List all Actions cache entries matching the provided key and ref. */
export async function listActionsCaches(key: string, ref: string) {
export async function listActionsCaches(
key: string,
ref: string,
): Promise<ActionsCacheItem[]> {
const repositoryNwo = parseRepositoryNwo(
getRequiredEnvParam("GITHUB_REPOSITORY"),
);

View file

@ -161,25 +161,35 @@ export async function uploadTrapCaches(
return true;
}
export async function cleanupTrapCaches(logger: Logger) {
export async function cleanupTrapCaches(language: Language, logger: Logger) {
try {
const matchingCaches = await getTrapCachesForLanguage(language);
for (const cache of matchingCaches) {
logger.info(`Matched Actions cache ${JSON.stringify(cache)}`);
}
} catch (e) {
logger.info(`Failed to cleanup trap caches, continuing. Details: ${e}`);
}
}
async function getTrapCachesForLanguage(
language: Language,
): Promise<apiClient.ActionsCacheItem[]> {
const event = actionsUtil.getWorkflowEvent();
const defaultBranch = event?.repository?.default_branch as string | undefined;
if (!defaultBranch) {
logger.info(
"Could not determine default branch, skipping TRAP cache cleanup",
);
return;
throw new Error("Could not determine default branch");
}
const matchingCaches = await apiClient.listActionsCaches(
const allCaches = await apiClient.listActionsCaches(
CODEQL_TRAP_CACHE_PREFIX,
defaultBranch,
);
for (const cache of matchingCaches) {
logger.info(`Matched Actions cache ${JSON.stringify(cache)}`);
}
return allCaches.filter((cache) => {
return cache.key?.includes(`-${language}-`);
});
}
export async function getLanguagesSupportingCaching(