Skip to content

Commit 0570c2f

Browse files
committed
Upload failed SARIF as workflow artifact in test mode
1 parent a033fca commit 0570c2f

3 files changed

Lines changed: 52 additions & 11 deletions

File tree

‎lib/entry-points.js‎

Lines changed: 20 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: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
} from "./debug-artifacts";
2626
import * as dependencyCaching from "./dependency-caching";
2727
import { getTempDiagnosticPath } from "./diagnostics";
28-
import { EnvVar, ReadOnlyEnv } from "./environment";
28+
import { EnvVar, isInTestMode, ReadOnlyEnv } from "./environment";
2929
import { Feature, FeatureEnablement } from "./feature-flags";
3030
import { Logger } from "./logging";
3131
import { OverlayDatabaseMode } from "./overlay/overlay-database-mode";
@@ -253,6 +253,19 @@ async function maybeUploadFailedSarif(
253253
logger,
254254
{ isUnsuccessfulExecution: true },
255255
);
256+
257+
// If we are in test mode, `uploadFiles` will not have uploaded the failed SARIF file
258+
// and instead will have written it to disk. Upload it as a workflow artifact so that
259+
// we can inspect it in validation jobs.
260+
if (uploadResult.sarifID && isInTestMode()) {
261+
await uploadSarifArtifact(logger, "test-mode-sarif", {
262+
// We defaulted to CodeScanning in the `uploadFiles` call above.
263+
sarifFile: uploadLib.getSavedPayloadPath(CodeScanning.kind),
264+
category: failedSarif.category,
265+
checkoutPath: failedSarif.checkoutPath,
266+
});
267+
}
268+
256269
return uploadResult
257270
? { ...uploadResult.statusReport, sarifID: uploadResult.sarifID }
258271
: {};
@@ -271,7 +284,15 @@ async function maybeUploadFailedSarifArtifact(
271284
}
272285

273286
const failedSarif = failedSarifResult.value;
287+
return uploadSarifArtifact(logger, "sarif-artifact", failedSarif);
288+
}
274289

290+
/** Uploads a failed SARIF file as workflow artifact. */
291+
async function uploadSarifArtifact(
292+
logger: Logger,
293+
prefix: string,
294+
failedSarif: FailedSarifInfo,
295+
): Promise<UploadFailedSarifResult> {
275296
logger.info(
276297
`Uploading failed SARIF file ${failedSarif.sarifFile} as artifact`,
277298
);
@@ -280,7 +301,7 @@ async function maybeUploadFailedSarifArtifact(
280301
const client = await getArtifactUploaderClient(logger, gitHubVersion.type);
281302

282303
const suffix = getArtifactSuffix(actionsUtil.getOptionalInput("matrix"));
283-
const name = sanitizeArtifactName(`sarif-artifact-${suffix}`);
304+
const name = sanitizeArtifactName(`${prefix}-${suffix}`);
284305
await client.uploadArtifact(
285306
name,
286307
[path.normalize(failedSarif.sarifFile)],

‎src/upload-lib.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,14 @@ function getAutomationID(
227227
return api.computeAutomationID(analysis_key, environment);
228228
}
229229

230+
/** Gets the path to a saved payload file for {@link analysisKind}. */
231+
export function getSavedPayloadPath(analysisKind: analyses.AnalysisKind) {
232+
return path.join(
233+
actionsUtil.getTemporaryDirectory(),
234+
`payload-${analysisKind}.json`,
235+
);
236+
}
237+
230238
/**
231239
* Upload the given payload.
232240
* If the request fails then this will retry a small number of times.
@@ -241,10 +249,7 @@ export async function uploadPayload(
241249
logger.info("Uploading results");
242250

243251
if (util.shouldSkipSarifUpload()) {
244-
const payloadSaveFile = path.join(
245-
actionsUtil.getTemporaryDirectory(),
246-
`payload-${analysis.kind}.json`,
247-
);
252+
const payloadSaveFile = getSavedPayloadPath(analysis.kind);
248253
logger.info(
249254
`SARIF upload disabled by an environment variable. Saving to ${payloadSaveFile}`,
250255
);

0 commit comments

Comments
 (0)