Move createCacheKeyHash to caching-utils

This commit is contained in:
Michael B. Gale
2025-11-05 16:14:01 +00:00
parent 0324490286
commit 0cbd930deb
6 changed files with 67 additions and 65 deletions

10
lib/analyze-action.js generated
View File

@@ -88172,6 +88172,7 @@ var fs6 = __toESM(require("fs"));
var path6 = __toESM(require("path"));
// src/caching-utils.ts
var crypto = __toESM(require("crypto"));
var core6 = __toESM(require_core());
async function getTotalCacheSize(paths, logger, quiet = false) {
const sizes = await Promise.all(
@@ -88182,6 +88183,10 @@ async function getTotalCacheSize(paths, logger, quiet = false) {
function shouldStoreCache(kind) {
return kind === "full" /* Full */ || kind === "store" /* Store */;
}
function createCacheKeyHash(components) {
const componentsJson = JSON.stringify(components);
return crypto.createHash("sha256").update(componentsJson).digest("hex").substring(0, 16);
}
// src/config/db-config.ts
var jsonschema = __toESM(require_lib3());
@@ -88207,7 +88212,6 @@ var bundleVersion = "codeql-bundle-v2.23.3";
var cliVersion = "2.23.3";
// src/overlay-database-utils.ts
var crypto = __toESM(require("crypto"));
var fs3 = __toESM(require("fs"));
var path3 = __toESM(require("path"));
var actionsCache = __toESM(require_cache3());
@@ -88619,10 +88623,6 @@ async function getCacheRestoreKeyPrefix(config, codeQlVersion) {
const componentsHash = createCacheKeyHash(cacheKeyComponents);
return `${CACHE_PREFIX}-${CACHE_VERSION}-${componentsHash}-${languages}-${codeQlVersion}-`;
}
function createCacheKeyHash(components) {
const componentsJson = JSON.stringify(components);
return crypto.createHash("sha256").update(componentsJson).digest("hex").substring(0, 16);
}
// src/tools-features.ts
var semver3 = __toESM(require_semver2());

10
lib/init-action.js generated
View File

@@ -85233,6 +85233,7 @@ function wrapApiConfigurationError(e) {
}
// src/caching-utils.ts
var crypto = __toESM(require("crypto"));
var core6 = __toESM(require_core());
async function getTotalCacheSize(paths, logger, quiet = false) {
const sizes = await Promise.all(
@@ -85265,6 +85266,10 @@ function getCachingKind(input) {
return "none" /* None */;
}
}
function createCacheKeyHash(components) {
const componentsJson = JSON.stringify(components);
return crypto.createHash("sha256").update(componentsJson).digest("hex").substring(0, 16);
}
function getDependencyCachingEnabled() {
const dependencyCaching = getOptionalInput("dependency-caching") || process.env["CODEQL_ACTION_DEPENDENCY_CACHING" /* DEPENDENCY_CACHING */];
if (dependencyCaching !== void 0) return getCachingKind(dependencyCaching);
@@ -85631,7 +85636,6 @@ var bundleVersion = "codeql-bundle-v2.23.3";
var cliVersion = "2.23.3";
// src/overlay-database-utils.ts
var crypto = __toESM(require("crypto"));
var fs3 = __toESM(require("fs"));
var path4 = __toESM(require("path"));
var actionsCache = __toESM(require_cache3());
@@ -86033,10 +86037,6 @@ async function getCacheRestoreKeyPrefix(config, codeQlVersion) {
const componentsHash = createCacheKeyHash(cacheKeyComponents);
return `${CACHE_PREFIX}-${CACHE_VERSION}-${componentsHash}-${languages}-${codeQlVersion}-`;
}
function createCacheKeyHash(components) {
const componentsJson = JSON.stringify(components);
return crypto.createHash("sha256").update(componentsJson).digest("hex").substring(0, 16);
}
// src/tools-features.ts
var semver3 = __toESM(require_semver2());

View File

@@ -83595,14 +83595,17 @@ var fs3 = __toESM(require("fs"));
var path3 = __toESM(require("path"));
var actionsCache = __toESM(require_cache3());
// src/git-utils.ts
// src/caching-utils.ts
var core6 = __toESM(require_core());
// src/git-utils.ts
var core7 = __toESM(require_core());
var toolrunner2 = __toESM(require_toolrunner());
var io3 = __toESM(require_io2());
var runGitCommand = async function(workingDirectory, args, customErrorMessage) {
let stdout = "";
let stderr = "";
core6.debug(`Running git command: git ${args.join(" ")}`);
core7.debug(`Running git command: git ${args.join(" ")}`);
try {
await new toolrunner2.ToolRunner(await io3.which("git", true), args, {
silent: true,
@@ -83622,7 +83625,7 @@ var runGitCommand = async function(workingDirectory, args, customErrorMessage) {
if (stderr.includes("not a git repository")) {
reason = "The checkout path provided to the action does not appear to be a git repository.";
}
core6.info(`git call failed. ${customErrorMessage} Error: ${reason}`);
core7.info(`git call failed. ${customErrorMessage} Error: ${reason}`);
throw error4;
}
};
@@ -83733,7 +83736,7 @@ async function getRef() {
) !== head;
if (hasChangedRef) {
const newRef = ref.replace(pull_ref_regex, "refs/pull/$1/head");
core6.debug(
core7.debug(
`No longer on merge commit, rewriting ref from ${ref} to ${newRef}.`
);
return newRef;
@@ -83759,16 +83762,16 @@ async function isAnalyzingDefaultBranch() {
}
// src/logging.ts
var core7 = __toESM(require_core());
var core8 = __toESM(require_core());
function getActionsLogger() {
return {
debug: core7.debug,
info: core7.info,
warning: core7.warning,
error: core7.error,
isDebug: core7.isDebug,
startGroup: core7.startGroup,
endGroup: core7.endGroup
debug: core8.debug,
info: core8.info,
warning: core8.warning,
error: core8.error,
isDebug: core8.isDebug,
startGroup: core8.startGroup,
endGroup: core8.endGroup
};
}
function formatDuration(durationMs) {
@@ -84566,9 +84569,6 @@ var AnalysisKind = /* @__PURE__ */ ((AnalysisKind2) => {
})(AnalysisKind || {});
var supportedAnalysisKinds = new Set(Object.values(AnalysisKind));
// src/caching-utils.ts
var core8 = __toESM(require_core());
// src/config/db-config.ts
var jsonschema = __toESM(require_lib4());
var semver4 = __toESM(require_semver2());

View File

@@ -86511,14 +86511,17 @@ var fs3 = __toESM(require("fs"));
var path3 = __toESM(require("path"));
var actionsCache = __toESM(require_cache3());
// src/git-utils.ts
// src/caching-utils.ts
var core6 = __toESM(require_core());
// src/git-utils.ts
var core7 = __toESM(require_core());
var toolrunner2 = __toESM(require_toolrunner());
var io3 = __toESM(require_io2());
var runGitCommand = async function(workingDirectory, args, customErrorMessage) {
let stdout = "";
let stderr = "";
core6.debug(`Running git command: git ${args.join(" ")}`);
core7.debug(`Running git command: git ${args.join(" ")}`);
try {
await new toolrunner2.ToolRunner(await io3.which("git", true), args, {
silent: true,
@@ -86538,7 +86541,7 @@ var runGitCommand = async function(workingDirectory, args, customErrorMessage) {
if (stderr.includes("not a git repository")) {
reason = "The checkout path provided to the action does not appear to be a git repository.";
}
core6.info(`git call failed. ${customErrorMessage} Error: ${reason}`);
core7.info(`git call failed. ${customErrorMessage} Error: ${reason}`);
throw error4;
}
};
@@ -86683,7 +86686,7 @@ async function getRef() {
) !== head;
if (hasChangedRef) {
const newRef = ref.replace(pull_ref_regex, "refs/pull/$1/head");
core6.debug(
core7.debug(
`No longer on merge commit, rewriting ref from ${ref} to ${newRef}.`
);
return newRef;
@@ -86709,16 +86712,16 @@ async function isAnalyzingDefaultBranch() {
}
// src/logging.ts
var core7 = __toESM(require_core());
var core8 = __toESM(require_core());
function getActionsLogger() {
return {
debug: core7.debug,
info: core7.info,
warning: core7.warning,
error: core7.error,
isDebug: core7.isDebug,
startGroup: core7.startGroup,
endGroup: core7.endGroup
debug: core8.debug,
info: core8.info,
warning: core8.warning,
error: core8.error,
isDebug: core8.isDebug,
startGroup: core8.startGroup,
endGroup: core8.endGroup
};
}
function formatDuration(durationMs) {
@@ -87266,9 +87269,6 @@ var core9 = __toESM(require_core());
var fs6 = __toESM(require("fs"));
var path6 = __toESM(require("path"));
// src/caching-utils.ts
var core8 = __toESM(require_core());
// src/config/db-config.ts
var jsonschema = __toESM(require_lib4());
var semver4 = __toESM(require_semver2());

View File

@@ -1,3 +1,5 @@
import * as crypto from "crypto";
import * as core from "@actions/core";
import { getOptionalInput, isDefaultSetup } from "./actions-util";
@@ -71,6 +73,30 @@ export function getCachingKind(input: string | undefined): CachingKind {
}
}
/**
* Creates a SHA-256 hash of the cache key components to ensure uniqueness
* while keeping the cache key length manageable.
*
* @param components Object containing all components that should influence cache key uniqueness
* @returns A short SHA-256 hash (first 16 characters) of the components
*/
export function createCacheKeyHash(components: Record<string, any>): string {
// From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
//
// "Properties are visited using the same algorithm as Object.keys(), which
// has a well-defined order and is stable across implementations. For example,
// JSON.stringify on the same object will always produce the same string, and
// JSON.parse(JSON.stringify(obj)) would produce an object with the same key
// ordering as the original (assuming the object is completely
// JSON-serializable)."
const componentsJson = JSON.stringify(components);
return crypto
.createHash("sha256")
.update(componentsJson)
.digest("hex")
.substring(0, 16);
}
/** Determines whether dependency caching is enabled. */
export function getDependencyCachingEnabled(): CachingKind {
// If the workflow specified something always respect that

View File

@@ -1,4 +1,3 @@
import * as crypto from "crypto";
import * as fs from "fs";
import * as path from "path";
@@ -11,6 +10,7 @@ import {
getWorkflowRunID,
} from "./actions-util";
import { getAutomationID } from "./api-client";
import { createCacheKeyHash } from "./caching-utils";
import { type CodeQL } from "./codeql";
import { type Config } from "./config-utils";
import { getCommitOid, getFileOidsUnderPath } from "./git-utils";
@@ -514,27 +514,3 @@ export async function getCacheRestoreKeyPrefix(
// easier to debug and understand the cache key structure.
return `${CACHE_PREFIX}-${CACHE_VERSION}-${componentsHash}-${languages}-${codeQlVersion}-`;
}
/**
* Creates a SHA-256 hash of the cache key components to ensure uniqueness
* while keeping the cache key length manageable.
*
* @param components Object containing all components that should influence cache key uniqueness
* @returns A short SHA-256 hash (first 16 characters) of the components
*/
function createCacheKeyHash(components: Record<string, any>): string {
// From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
//
// "Properties are visited using the same algorithm as Object.keys(), which
// has a well-defined order and is stable across implementations. For example,
// JSON.stringify on the same object will always produce the same string, and
// JSON.parse(JSON.stringify(obj)) would produce an object with the same key
// ordering as the original (assuming the object is completely
// JSON-serializable)."
const componentsJson = JSON.stringify(components);
return crypto
.createHash("sha256")
.update(componentsJson)
.digest("hex")
.substring(0, 16);
}