diff --git a/lib/init-action-post.js b/lib/init-action-post.js index c7cb2af50..3874bd7e1 100644 --- a/lib/init-action-post.js +++ b/lib/init-action-post.js @@ -163855,6 +163855,34 @@ async function asyncSome(array, predicate) { const results = await Promise.all(array.map(predicate)); return results.some((result) => result); } +var Success = class { + constructor(value) { + this.value = value; + } + isSuccess() { + return true; + } + isFailure() { + return false; + } + orElse(_defaultValue) { + return this.value; + } +}; +var Failure = class { + constructor(value) { + this.value = value; + } + isSuccess() { + return false; + } + isFailure() { + return true; + } + orElse(defaultValue) { + return defaultValue; + } +}; // src/actions-util.ts var pkg = require_package(); @@ -169755,9 +169783,11 @@ function createFailedUploadFailedSarifResult(error3) { upload_failed_run_stack_trace: wrappedError.stack }; } -async function maybeUploadFailedSarif(config, repositoryNwo, features, logger) { +async function prepareFailedSarif(logger, features, config) { if (!config.codeQLCmd) { - return { upload_failed_run_skipped_because: "CodeQL command not found" }; + return new Failure({ + upload_failed_run_skipped_because: "CodeQL command not found" + }); } const workflow = await getWorkflow(logger); const jobName = getRequiredEnvParam("GITHUB_JOB"); @@ -169766,7 +169796,9 @@ async function maybeUploadFailedSarif(config, repositoryNwo, features, logger) { if (!["always", "failure-only"].includes( getUploadValue(shouldUpload) ) || shouldSkipSarifUpload()) { - return { upload_failed_run_skipped_because: "SARIF upload is disabled" }; + return new Failure({ + upload_failed_run_skipped_because: "SARIF upload is disabled" + }); } const category = getCategoryInputOrThrow(workflow, jobName, matrix); const checkoutPath = getCheckoutPathInputOrThrow(workflow, jobName, matrix); @@ -169778,11 +169810,19 @@ async function maybeUploadFailedSarif(config, repositoryNwo, features, logger) { } else { await codeql.databaseExportDiagnostics(databasePath, sarifFile, category); } - logger.info(`Uploading failed SARIF file ${sarifFile}`); + return new Success({ sarifFile, category, checkoutPath }); +} +async function maybeUploadFailedSarif(config, repositoryNwo, features, logger) { + const failedSarifResult = await prepareFailedSarif(logger, features, config); + if (failedSarifResult.isFailure()) { + return failedSarifResult.value; + } + const failedSarif = failedSarifResult.value; + logger.info(`Uploading failed SARIF file ${failedSarif.sarifFile}`); const uploadResult = await uploadFiles( - sarifFile, - checkoutPath, - category, + failedSarif.sarifFile, + failedSarif.checkoutPath, + failedSarif.category, features, logger, CodeScanning diff --git a/src/init-action-post-helper.ts b/src/init-action-post-helper.ts index 76a14be11..c03a0c1f0 100644 --- a/src/init-action-post-helper.ts +++ b/src/init-action-post-helper.ts @@ -19,10 +19,13 @@ import * as uploadLib from "./upload-lib"; import { checkDiskUsage, delay, + Failure, getErrorMessage, getRequiredEnvParam, parseMatrixInput, + Result, shouldSkipSarifUpload, + Success, wrapError, } from "./util"; import { @@ -62,18 +65,27 @@ function createFailedUploadFailedSarifResult( }; } +/** Records details about a SARIF file that can contains information about a failed analysis. */ +interface FailedSarifInfo { + sarifFile: string; + category: string | undefined; + checkoutPath: string; +} + /** - * Upload a failed SARIF file if we can verify that SARIF upload is enabled and determine the SARIF - * category for the workflow. + * Tries to prepare a SARIF file that can contains information about a failed analysis. + * + * @returns Either information about the SARIF file that was produced, or a reason why it couldn't be produced. */ -async function maybeUploadFailedSarif( - config: Config, - repositoryNwo: RepositoryNwo, - features: FeatureEnablement, +async function prepareFailedSarif( logger: Logger, -): Promise { + features: FeatureEnablement, + config: Config, +): Promise> { if (!config.codeQLCmd) { - return { upload_failed_run_skipped_because: "CodeQL command not found" }; + return new Failure({ + upload_failed_run_skipped_because: "CodeQL command not found", + }); } const workflow = await getWorkflow(logger); const jobName = getRequiredEnvParam("GITHUB_JOB"); @@ -85,7 +97,9 @@ async function maybeUploadFailedSarif( ) || shouldSkipSarifUpload() ) { - return { upload_failed_run_skipped_because: "SARIF upload is disabled" }; + return new Failure({ + upload_failed_run_skipped_because: "SARIF upload is disabled", + }); } const category = getCategoryInputOrThrow(workflow, jobName, matrix); const checkoutPath = getCheckoutPathInputOrThrow(workflow, jobName, matrix); @@ -105,11 +119,32 @@ async function maybeUploadFailedSarif( await codeql.databaseExportDiagnostics(databasePath, sarifFile, category); } - logger.info(`Uploading failed SARIF file ${sarifFile}`); + return new Success({ sarifFile, category, checkoutPath }); +} + +/** + * Upload a failed SARIF file if we can verify that SARIF upload is enabled and determine the SARIF + * category for the workflow. + */ +async function maybeUploadFailedSarif( + config: Config, + repositoryNwo: RepositoryNwo, + features: FeatureEnablement, + logger: Logger, +): Promise { + const failedSarifResult = await prepareFailedSarif(logger, features, config); + + if (failedSarifResult.isFailure()) { + return failedSarifResult.value; + } + + const failedSarif = failedSarifResult.value; + + logger.info(`Uploading failed SARIF file ${failedSarif.sarifFile}`); const uploadResult = await uploadLib.uploadFiles( - sarifFile, - checkoutPath, - category, + failedSarif.sarifFile, + failedSarif.checkoutPath, + failedSarif.category, features, logger, CodeScanning,