Skip to content

Commit 850cc87

Browse files
committed
Upload failed SARIF as workflow artifact in test mode
1 parent 68eb797 commit 850cc87

3 files changed

Lines changed: 42 additions & 10 deletions

File tree

‎lib/entry-points.js‎

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

‎src/init-action-post-helper.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import {
4545
parseMatrixInput,
4646
Result,
4747
shouldSkipGenerateFailedSarif,
48+
shouldSkipSarifUpload,
4849
Success,
4950
wrapError,
5051
} from "./util";
@@ -240,6 +241,14 @@ async function maybeUploadFailedSarif(
240241
logger,
241242
{ isUnsuccessfulExecution: true },
242243
);
244+
245+
// If SARIF upload is disabled for testing, `uploadFiles` will not have uploaded the failed
246+
// SARIF file and instead will have written it to disk. Upload it as a workflow artifact so that
247+
// we can inspect it in validation jobs.
248+
if (uploadResult.sarifID && shouldSkipSarifUpload()) {
249+
await uploadSarifArtifact(logger, "test-mode-sarif", failedSarif);
250+
}
251+
243252
return uploadResult
244253
? { ...uploadResult.statusReport, sarifID: uploadResult.sarifID }
245254
: {};
@@ -258,7 +267,15 @@ async function maybeUploadFailedSarifArtifact(
258267
}
259268

260269
const failedSarif = failedSarifResult.value;
270+
return uploadSarifArtifact(logger, "sarif-artifact", failedSarif);
271+
}
261272

273+
/** Uploads a failed SARIF file as workflow artifact. */
274+
async function uploadSarifArtifact(
275+
logger: Logger,
276+
prefix: string,
277+
failedSarif: FailedSarifInfo,
278+
): Promise<UploadFailedSarifResult> {
262279
logger.info(
263280
`Uploading failed SARIF file ${failedSarif.sarifFile} as artifact`,
264281
);
@@ -267,7 +284,7 @@ async function maybeUploadFailedSarifArtifact(
267284
const client = await getArtifactUploaderClient(logger, gitHubVersion.type);
268285

269286
const suffix = getArtifactSuffix(actionsUtil.getOptionalInput("matrix"));
270-
const name = sanitizeArtifactName(`sarif-artifact-${suffix}`);
287+
const name = sanitizeArtifactName(`${prefix}-${suffix}`);
271288
await client.uploadArtifact(
272289
name,
273290
[path.normalize(failedSarif.sarifFile)],

‎src/upload-lib.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,14 @@ function getAutomationID(
231231
return api.computeAutomationID(analysis_key, environment);
232232
}
233233

234+
/** Gets the path to a saved payload file for {@link analysisKind}. */
235+
export function getSavedPayloadPath(analysisKind: analyses.AnalysisKind) {
236+
return path.join(
237+
actionsUtil.getTemporaryDirectory(),
238+
`payload-${analysisKind}.json`,
239+
);
240+
}
241+
234242
/**
235243
* Upload the given payload.
236244
* If the request fails then this will retry a small number of times.
@@ -245,10 +253,7 @@ export async function uploadPayload(
245253
logger.info("Uploading results");
246254

247255
if (util.shouldSkipSarifUpload()) {
248-
const payloadSaveFile = path.join(
249-
actionsUtil.getTemporaryDirectory(),
250-
`payload-${analysis.kind}.json`,
251-
);
256+
const payloadSaveFile = getSavedPayloadPath(analysis.kind);
252257
logger.info(
253258
`SARIF upload disabled by an environment variable. Saving to ${payloadSaveFile}`,
254259
);

0 commit comments

Comments
 (0)