Skip to content

Commit 7dfbfdc

Browse files
committed
Report overall cache usage for CodeQL dependency caches
1 parent 3d7d7c9 commit 7dfbfdc

File tree

5 files changed

+96
-7
lines changed

5 files changed

+96
-7
lines changed

lib/init-action-post.js

Lines changed: 37 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/api-client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ export interface ActionsCacheItem {
214214
/** List all Actions cache entries matching the provided key and ref. */
215215
export async function listActionsCaches(
216216
key: string,
217-
ref: string,
217+
ref?: string,
218218
): Promise<ActionsCacheItem[]> {
219219
const repositoryNwo = getRepositoryNwo();
220220

src/dependency-caching.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import * as actionsCache from "@actions/cache";
55
import * as glob from "@actions/glob";
66

77
import { getTemporaryDirectory } from "./actions-util";
8+
import { listActionsCaches } from "./api-client";
89
import { getTotalCacheSize } from "./caching-utils";
910
import { Config } from "./config-utils";
1011
import { EnvVar } from "./environment";
1112
import { KnownLanguage, Language } from "./languages";
1213
import { Logger } from "./logging";
13-
import { getRequiredEnvParam } from "./util";
14+
import { getErrorMessage, getRequiredEnvParam } from "./util";
1415

1516
/**
1617
* Caching configuration for a particular language.
@@ -344,3 +345,34 @@ async function cachePrefix(
344345

345346
return `${prefix}-${CODEQL_DEPENDENCY_CACHE_VERSION}-${runnerOs}-${language}-`;
346347
}
348+
349+
/** Represents information about our overall cache usage for CodeQL dependency caches. */
350+
export interface DependencyCachingUsageReport {
351+
count: number;
352+
size_bytes: number;
353+
}
354+
355+
/**
356+
* Tries to determine the overall cache usage for CodeQL dependencies caches.
357+
*
358+
* @param logger The logger to log errors to.
359+
* @returns Returns the overall cache usage for CodeQL dependencies caches, or `undefined` if we couldn't determine it.
360+
*/
361+
export async function getDependencyCacheUsage(
362+
logger: Logger,
363+
): Promise<DependencyCachingUsageReport | undefined> {
364+
try {
365+
const caches = await listActionsCaches(CODEQL_DEPENDENCY_CACHE_PREFIX);
366+
const totalSize = caches.reduce(
367+
(acc, cache) => acc + (cache.size_in_bytes ?? 0),
368+
0,
369+
);
370+
return { count: caches.length, size_bytes: totalSize };
371+
} catch (err) {
372+
logger.warning(
373+
`Unable to retrieve information about dependency cache usage: ${getErrorMessage(err)}`,
374+
);
375+
}
376+
377+
return undefined;
378+
}

src/init-action-post-helper.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ export interface JobStatusReport {
4545
job_status: JobStatus;
4646
}
4747

48+
export interface DependencyCachingUsageReport {
49+
dependency_caching_usage?: string;
50+
}
51+
4852
function createFailedUploadFailedSarifResult(
4953
error: unknown,
5054
): UploadFailedSarifResult {

src/init-action-post.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,16 @@ import {
1212
printDebugLogs,
1313
} from "./actions-util";
1414
import { getGitHubVersion } from "./api-client";
15+
import { CachingKind } from "./caching-utils";
1516
import { getCodeQL } from "./codeql";
1617
import { Config, getConfig } from "./config-utils";
1718
import * as debugArtifacts from "./debug-artifacts";
19+
import {
20+
DependencyCachingUsageReport,
21+
getDependencyCacheUsage,
22+
} from "./dependency-caching";
1823
import { Features } from "./feature-flags";
24+
import * as gitUtils from "./git-utils";
1925
import * as initActionPostHelper from "./init-action-post-helper";
2026
import { getActionsLogger } from "./logging";
2127
import { getRepositoryNwo } from "./repository";
@@ -32,7 +38,8 @@ import { checkDiskUsage, checkGitHubVersionInRange, wrapError } from "./util";
3238
interface InitPostStatusReport
3339
extends StatusReportBase,
3440
initActionPostHelper.UploadFailedSarifResult,
35-
initActionPostHelper.JobStatusReport {}
41+
initActionPostHelper.JobStatusReport,
42+
initActionPostHelper.DependencyCachingUsageReport {}
3643

3744
async function runWrapper() {
3845
const logger = getActionsLogger();
@@ -41,6 +48,7 @@ async function runWrapper() {
4148
let uploadFailedSarifResult:
4249
| initActionPostHelper.UploadFailedSarifResult
4350
| undefined;
51+
let dependencyCachingUsage: DependencyCachingUsageReport | undefined;
4452
try {
4553
// Restore inputs from `init` Action.
4654
restoreInputs();
@@ -73,6 +81,17 @@ async function runWrapper() {
7381
features,
7482
logger,
7583
);
84+
85+
// If we are analysing the default branch and some kind of caching is enabled,
86+
// then try to determine our overall cache usage for dependency caches. We only
87+
// do this under these circumstances to avoid slowing down analyses for PRs
88+
// and where caching may not be enabled.
89+
if (
90+
(await gitUtils.isAnalyzingDefaultBranch()) &&
91+
config.dependencyCachingEnabled !== CachingKind.None
92+
) {
93+
dependencyCachingUsage = await getDependencyCacheUsage(logger);
94+
}
7695
}
7796
} catch (unwrappedError) {
7897
const error = wrapError(unwrappedError);
@@ -109,6 +128,7 @@ async function runWrapper() {
109128
...statusReportBase,
110129
...uploadFailedSarifResult,
111130
job_status: initActionPostHelper.getFinalJobStatus(),
131+
dependency_caching_usage: JSON.stringify(dependencyCachingUsage ?? {}),
112132
};
113133
logger.info("Sending status report for init-post step.");
114134
await sendStatusReport(statusReport);

0 commit comments

Comments
 (0)