Skip to content

Commit 0cbd930

Browse files
committed
Move createCacheKeyHash to caching-utils
1 parent 0324490 commit 0cbd930

File tree

6 files changed

+67
-65
lines changed

6 files changed

+67
-65
lines changed

lib/analyze-action.js

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

lib/init-action.js

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

lib/setup-codeql-action.js

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

lib/upload-sarif-action.js

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

src/caching-utils.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as crypto from "crypto";
2+
13
import * as core from "@actions/core";
24

35
import { getOptionalInput, isDefaultSetup } from "./actions-util";
@@ -71,6 +73,30 @@ export function getCachingKind(input: string | undefined): CachingKind {
7173
}
7274
}
7375

76+
/**
77+
* Creates a SHA-256 hash of the cache key components to ensure uniqueness
78+
* while keeping the cache key length manageable.
79+
*
80+
* @param components Object containing all components that should influence cache key uniqueness
81+
* @returns A short SHA-256 hash (first 16 characters) of the components
82+
*/
83+
export function createCacheKeyHash(components: Record<string, any>): string {
84+
// From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
85+
//
86+
// "Properties are visited using the same algorithm as Object.keys(), which
87+
// has a well-defined order and is stable across implementations. For example,
88+
// JSON.stringify on the same object will always produce the same string, and
89+
// JSON.parse(JSON.stringify(obj)) would produce an object with the same key
90+
// ordering as the original (assuming the object is completely
91+
// JSON-serializable)."
92+
const componentsJson = JSON.stringify(components);
93+
return crypto
94+
.createHash("sha256")
95+
.update(componentsJson)
96+
.digest("hex")
97+
.substring(0, 16);
98+
}
99+
74100
/** Determines whether dependency caching is enabled. */
75101
export function getDependencyCachingEnabled(): CachingKind {
76102
// If the workflow specified something always respect that

src/overlay-database-utils.ts

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as crypto from "crypto";
21
import * as fs from "fs";
32
import * as path from "path";
43

@@ -11,6 +10,7 @@ import {
1110
getWorkflowRunID,
1211
} from "./actions-util";
1312
import { getAutomationID } from "./api-client";
13+
import { createCacheKeyHash } from "./caching-utils";
1414
import { type CodeQL } from "./codeql";
1515
import { type Config } from "./config-utils";
1616
import { getCommitOid, getFileOidsUnderPath } from "./git-utils";
@@ -514,27 +514,3 @@ export async function getCacheRestoreKeyPrefix(
514514
// easier to debug and understand the cache key structure.
515515
return `${CACHE_PREFIX}-${CACHE_VERSION}-${componentsHash}-${languages}-${codeQlVersion}-`;
516516
}
517-
518-
/**
519-
* Creates a SHA-256 hash of the cache key components to ensure uniqueness
520-
* while keeping the cache key length manageable.
521-
*
522-
* @param components Object containing all components that should influence cache key uniqueness
523-
* @returns A short SHA-256 hash (first 16 characters) of the components
524-
*/
525-
function createCacheKeyHash(components: Record<string, any>): string {
526-
// From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
527-
//
528-
// "Properties are visited using the same algorithm as Object.keys(), which
529-
// has a well-defined order and is stable across implementations. For example,
530-
// JSON.stringify on the same object will always produce the same string, and
531-
// JSON.parse(JSON.stringify(obj)) would produce an object with the same key
532-
// ordering as the original (assuming the object is completely
533-
// JSON-serializable)."
534-
const componentsJson = JSON.stringify(components);
535-
return crypto
536-
.createHash("sha256")
537-
.update(componentsJson)
538-
.digest("hex")
539-
.substring(0, 16);
540-
}

0 commit comments

Comments
 (0)