List matching Actions caches

This commit is contained in:
Henry Mercer 2024-05-21 15:44:39 +01:00
parent b1bd8da5e7
commit cc96c825ba
9 changed files with 78 additions and 15 deletions

View file

@ -291,7 +291,7 @@ export function getRelativeScriptPath(): string {
}
/** Returns the contents of `GITHUB_EVENT_PATH` as a JSON object. */
function getWorkflowEvent(): any {
export function getWorkflowEvent(): any {
const eventJsonFile = getRequiredEnvParam("GITHUB_EVENT_PATH");
try {
return JSON.parse(fs.readFileSync(eventJsonFile, "utf-8"));

View file

@ -4,6 +4,7 @@ import * as retry from "@octokit/plugin-retry";
import consoleLogLevel from "console-log-level";
import { getActionVersion, getRequiredInput } from "./actions-util";
import { parseRepositoryNwo } from "./repository";
import {
ConfigurationError,
getRequiredEnvParam,
@ -195,6 +196,20 @@ export function computeAutomationID(
return automationID;
}
/** List all Actions cache entries matching the provided key and ref. */
export async function listActionsCaches(key: string, ref: string) {
const repositoryNwo = parseRepositoryNwo(
getRequiredEnvParam("GITHUB_REPOSITORY"),
);
const apiClient = getApiClient();
return await apiClient.paginate(
"GET /repos/{owner}/{repo}/actions/caches",
{ owner: repositoryNwo.owner, repo: repositoryNwo.repo, key, ref },
(response) => response.data.actions_caches,
);
}
export function wrapApiConfigurationError(e: unknown) {
if (isHTTPError(e)) {
if (

View file

@ -1,9 +1,10 @@
import * as fs from "fs";
import * as path from "path";
import * as cache from "@actions/cache";
import * as actionsCache from "@actions/cache";
import * as actionsUtil from "./actions-util";
import * as apiClient from "./api-client";
import { CodeQL } from "./codeql";
import type { Config } from "./config-utils";
import { Language } from "./languages";
@ -17,6 +18,8 @@ import { tryGetFolderBytes, withTimeout } from "./util";
// goes into the cache key.
const CACHE_VERSION = 1;
const CODEQL_TRAP_CACHE_PREFIX = "codeql-trap";
// This constant sets the minimum size in megabytes of a TRAP
// cache for us to consider it worth uploading.
const MINIMUM_CACHE_MB_TO_UPLOAD = 10;
@ -87,7 +90,7 @@ export async function downloadTrapCaches(
);
const found = await withTimeout(
MAX_CACHE_OPERATION_MS,
cache.restoreCache([cacheDir], preferredKey, [
actionsCache.restoreCache([cacheDir], preferredKey, [
// Fall back to any cache with the right key prefix
await cachePrefix(codeql, language),
]),
@ -147,7 +150,7 @@ export async function uploadTrapCaches(
logger.info(`Uploading TRAP cache to Actions cache with key ${key}`);
await withTimeout(
MAX_CACHE_OPERATION_MS,
cache.saveCache([cacheDir], key),
actionsCache.saveCache([cacheDir], key),
() => {
logger.info(
`Timed out waiting for TRAP cache for ${language} to upload, will continue without uploading`,
@ -158,6 +161,27 @@ export async function uploadTrapCaches(
return true;
}
export async function cleanupTrapCaches(logger: Logger) {
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;
}
const matchingCaches = await apiClient.listActionsCaches(
CODEQL_TRAP_CACHE_PREFIX,
defaultBranch,
);
for (const cache of matchingCaches) {
logger.info(`Matched Actions cache ${JSON.stringify(cache)}`);
}
}
export async function getLanguagesSupportingCaching(
codeql: CodeQL,
languages: Language[],
@ -225,7 +249,7 @@ async function cachePrefix(
codeql: CodeQL,
language: Language,
): Promise<string> {
return `codeql-trap-${CACHE_VERSION}-${
return `${CODEQL_TRAP_CACHE_PREFIX}-${CACHE_VERSION}-${
(await codeql.getVersion()).version
}-${language}-`;
}