Skip to content

Commit

Permalink
Record the stack trace if applicable
Browse files Browse the repository at this point in the history
  • Loading branch information
Henry Mercer committed Dec 9, 2022
1 parent dc9c1c1 commit 118e294
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 37 deletions.
33 changes: 20 additions & 13 deletions lib/init-action-post-helper.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/init-action-post-helper.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 52 additions & 23 deletions src/init-action-post-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,36 @@ import {
} from "./workflow";

export interface UploadFailedSarifResult {
/** Size in bytes of the unzipped SARIF payload uploaded for the failed run. */
/**
* If a SARIF file was uploaded for a failed run, this is the size in bytes of the unzipped
* SARIF payload.
*/
upload_failed_run_raw_upload_size_bytes?: number;
/** Size in bytes of actual SARIF payload uploaded for the failed run. */
/**
* If a SARIF file was uploaded for a failed run, this is the size in bytes of the actual
* zipped payload.
*/
upload_failed_run_zipped_upload_size_bytes?: number;

/** Error encountered during uploading the failed run. */
/** If there was an error while uploading a failed run, this is its message. */
upload_failed_run_error?: string;
/** If there was an error while uploading a failed run, this is its stack trace. */
upload_failed_run_stack_trace?: string;
/** Reason why we did not upload a SARIF payload with `executionSuccessful: false`. */
upload_failed_run_skipped_because?: string;
}

function createFailedUploadFailedSarifResult(
error: unknown
): UploadFailedSarifResult {
return {
upload_failed_run_error:
error instanceof Error ? error.message : String(error),
upload_failed_run_stack_trace:
error instanceof Error ? error.stack : undefined,
};
}

export async function uploadFailedSarif(
config: Config,
repositoryNwo: RepositoryNwo,
Expand Down Expand Up @@ -89,31 +108,19 @@ export async function uploadFailedSarif(
};
}

export async function run(
uploadDatabaseBundleDebugArtifact: Function,
uploadLogsDebugArtifact: Function,
printDebugLogs: Function,
export async function uploadSarifIfRunFailed(
config: Config,
repositoryNwo: RepositoryNwo,
featureEnablement: FeatureEnablement,
logger: Logger
) {
const config = await getConfig(actionsUtil.getTemporaryDirectory(), logger);
if (config === undefined) {
logger.warning(
"Debugging artifacts are unavailable since the 'init' Action failed before it could produce any."
);
return;
}

): Promise<UploadFailedSarifResult> {
// Environment variable used to integration test uploading a SARIF file for failed runs
const expectFailedSarifUpload =
process.env["CODEQL_ACTION_EXPECT_UPLOAD_FAILED_SARIF"] === "true";

let uploadFailedSarifResult: UploadFailedSarifResult;

if (process.env[CODEQL_ACTION_ANALYZE_DID_UPLOAD_SARIF] !== "true") {
try {
uploadFailedSarifResult = await uploadFailedSarif(
return await uploadFailedSarif(
config,
repositoryNwo,
featureEnablement,
Expand All @@ -129,19 +136,41 @@ export async function run(
logger.info(
`Failed to upload a SARIF file for the failed run. Error: ${e}`
);
uploadFailedSarifResult = {
upload_failed_run_error: e instanceof Error ? e.message : String(e),
};
return createFailedUploadFailedSarifResult(e);
}
} else if (expectFailedSarifUpload) {
throw new Error(
"Expected to upload a SARIF file for the failed run, but didn't."
);
} else {
uploadFailedSarifResult = {
return {
upload_failed_run_skipped_because: "SARIF file already uploaded",
};
}
}

export async function run(
uploadDatabaseBundleDebugArtifact: Function,
uploadLogsDebugArtifact: Function,
printDebugLogs: Function,
repositoryNwo: RepositoryNwo,
featureEnablement: FeatureEnablement,
logger: Logger
) {
const config = await getConfig(actionsUtil.getTemporaryDirectory(), logger);
if (config === undefined) {
logger.warning(
"Debugging artifacts are unavailable since the 'init' Action failed before it could produce any."
);
return;
}

const uploadFailedSarifResult = await uploadSarifIfRunFailed(
config,
repositoryNwo,
featureEnablement,
logger
);

// Upload appropriate Actions artifacts for debugging
if (config.debugMode) {
Expand Down

0 comments on commit 118e294

Please sign in to comment.