From 69173ea009394fa262fbc97099efb0dbb058f168 Mon Sep 17 00:00:00 2001 From: "Michael B. Gale" Date: Tue, 20 Jan 2026 12:41:22 +0000 Subject: [PATCH] Refactor artifact suffix computation into `getArtifactSuffix` --- lib/analyze-action-post.js | 31 +++++++++++++----------- lib/init-action-post.js | 31 +++++++++++++----------- lib/upload-sarif-action-post.js | 31 +++++++++++++----------- src/debug-artifacts.ts | 43 +++++++++++++++++++++------------ 4 files changed, 79 insertions(+), 57 deletions(-) diff --git a/lib/analyze-action-post.js b/lib/analyze-action-post.js index feb45a8f0..7c6ae02a5 100644 --- a/lib/analyze-action-post.js +++ b/lib/analyze-action-post.js @@ -125707,6 +125707,22 @@ async function uploadCombinedSarifArtifacts(logger, gitHubVariant, codeQlVersion }); } } +function getArtifactSuffix(matrix) { + let suffix = ""; + if (matrix) { + try { + for (const [, matrixVal] of Object.entries( + JSON.parse(matrix) + ).sort()) + suffix += `-${matrixVal}`; + } catch { + core12.info( + "Could not parse user-specified `matrix` input into JSON. The debug artifact will not be named with the user's `matrix` input." + ); + } + } + return suffix; +} async function uploadDebugArtifacts(logger, toUpload, rootDir, artifactName, ghVariant, codeQlVersion) { if (toUpload.length === 0) { return "no-artifacts-to-upload"; @@ -125722,20 +125738,7 @@ async function uploadDebugArtifacts(logger, toUpload, rootDir, artifactName, ghV await scanArtifactsForTokens(toUpload, logger); core12.exportVariable("CODEQL_ACTION_ARTIFACT_SCAN_FINISHED", "true"); } - let suffix = ""; - const matrix = getOptionalInput("matrix"); - if (matrix) { - try { - for (const [, matrixVal] of Object.entries( - JSON.parse(matrix) - ).sort()) - suffix += `-${matrixVal}`; - } catch { - core12.info( - "Could not parse user-specified `matrix` input into JSON. The debug artifact will not be named with the user's `matrix` input." - ); - } - } + const suffix = getArtifactSuffix(getOptionalInput("matrix")); const artifactUploader = await getArtifactUploaderClient(logger, ghVariant); try { await artifactUploader.uploadArtifact( diff --git a/lib/init-action-post.js b/lib/init-action-post.js index 936790df3..e8803b8d2 100644 --- a/lib/init-action-post.js +++ b/lib/init-action-post.js @@ -130430,6 +130430,22 @@ async function tryUploadAllAvailableDebugArtifacts(codeql, config, logger, codeQ ); } } +function getArtifactSuffix(matrix) { + let suffix = ""; + if (matrix) { + try { + for (const [, matrixVal] of Object.entries( + JSON.parse(matrix) + ).sort()) + suffix += `-${matrixVal}`; + } catch { + core12.info( + "Could not parse user-specified `matrix` input into JSON. The debug artifact will not be named with the user's `matrix` input." + ); + } + } + return suffix; +} async function uploadDebugArtifacts(logger, toUpload, rootDir, artifactName, ghVariant, codeQlVersion) { if (toUpload.length === 0) { return "no-artifacts-to-upload"; @@ -130445,20 +130461,7 @@ async function uploadDebugArtifacts(logger, toUpload, rootDir, artifactName, ghV await scanArtifactsForTokens(toUpload, logger); core12.exportVariable("CODEQL_ACTION_ARTIFACT_SCAN_FINISHED", "true"); } - let suffix = ""; - const matrix = getOptionalInput("matrix"); - if (matrix) { - try { - for (const [, matrixVal] of Object.entries( - JSON.parse(matrix) - ).sort()) - suffix += `-${matrixVal}`; - } catch { - core12.info( - "Could not parse user-specified `matrix` input into JSON. The debug artifact will not be named with the user's `matrix` input." - ); - } - } + const suffix = getArtifactSuffix(getOptionalInput("matrix")); const artifactUploader = await getArtifactUploaderClient(logger, ghVariant); try { await artifactUploader.uploadArtifact( diff --git a/lib/upload-sarif-action-post.js b/lib/upload-sarif-action-post.js index 863caaadd..813c9ad85 100644 --- a/lib/upload-sarif-action-post.js +++ b/lib/upload-sarif-action-post.js @@ -124642,6 +124642,22 @@ async function uploadCombinedSarifArtifacts(logger, gitHubVariant, codeQlVersion }); } } +function getArtifactSuffix(matrix) { + let suffix = ""; + if (matrix) { + try { + for (const [, matrixVal] of Object.entries( + JSON.parse(matrix) + ).sort()) + suffix += `-${matrixVal}`; + } catch { + core12.info( + "Could not parse user-specified `matrix` input into JSON. The debug artifact will not be named with the user's `matrix` input." + ); + } + } + return suffix; +} async function uploadDebugArtifacts(logger, toUpload, rootDir, artifactName, ghVariant, codeQlVersion) { if (toUpload.length === 0) { return "no-artifacts-to-upload"; @@ -124657,20 +124673,7 @@ async function uploadDebugArtifacts(logger, toUpload, rootDir, artifactName, ghV await scanArtifactsForTokens(toUpload, logger); core12.exportVariable("CODEQL_ACTION_ARTIFACT_SCAN_FINISHED", "true"); } - let suffix = ""; - const matrix = getOptionalInput("matrix"); - if (matrix) { - try { - for (const [, matrixVal] of Object.entries( - JSON.parse(matrix) - ).sort()) - suffix += `-${matrixVal}`; - } catch { - core12.info( - "Could not parse user-specified `matrix` input into JSON. The debug artifact will not be named with the user's `matrix` input." - ); - } - } + const suffix = getArtifactSuffix(getOptionalInput("matrix")); const artifactUploader = await getArtifactUploaderClient(logger, ghVariant); try { await artifactUploader.uploadArtifact( diff --git a/src/debug-artifacts.ts b/src/debug-artifacts.ts index 3534cdc02..1a1917d70 100644 --- a/src/debug-artifacts.ts +++ b/src/debug-artifacts.ts @@ -246,6 +246,33 @@ export async function tryUploadAllAvailableDebugArtifacts( } } +/** + * When a build matrix is used, multiple different jobs arising from the matrix may attempt to upload + * workflow artifacts with the same base name. In that case, only one of the uploads will succeed and + * the others will fail. This function inspects the matrix object to compute a suffix for the artifact + * name that uniquely identifies the matrix values of the current job to avoid name clashes. + * + * @param matrix A stringified JSON value, usually the value of the `matrix` input. + * @returns A suffix that uniquely identifies the `matrix` value for the current job, or `""` if there + * is no matrix value. + */ +export function getArtifactSuffix(matrix: string | undefined): string { + let suffix = ""; + if (matrix) { + try { + for (const [, matrixVal] of Object.entries( + JSON.parse(matrix) as any[][], + ).sort()) + suffix += `-${matrixVal}`; + } catch { + core.info( + "Could not parse user-specified `matrix` input into JSON. The debug artifact will not be named with the user's `matrix` input.", + ); + } + } + return suffix; +} + export async function uploadDebugArtifacts( logger: Logger, toUpload: string[], @@ -279,21 +306,7 @@ export async function uploadDebugArtifacts( core.exportVariable("CODEQL_ACTION_ARTIFACT_SCAN_FINISHED", "true"); } - let suffix = ""; - const matrix = getOptionalInput("matrix"); - if (matrix) { - try { - for (const [, matrixVal] of Object.entries( - JSON.parse(matrix) as any[][], - ).sort()) - suffix += `-${matrixVal}`; - } catch { - core.info( - "Could not parse user-specified `matrix` input into JSON. The debug artifact will not be named with the user's `matrix` input.", - ); - } - } - + const suffix = getArtifactSuffix(getOptionalInput("matrix")); const artifactUploader = await getArtifactUploaderClient(logger, ghVariant); try {