Skip to content

Commit f8ca2f0

Browse files
committed
Write processed SARIF files if post-process-output input is provided
1 parent e205370 commit f8ca2f0

File tree

7 files changed

+109
-49
lines changed

7 files changed

+109
-49
lines changed

lib/analyze-action.js

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

lib/init-action-post.js

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

lib/upload-lib.js

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

lib/upload-sarif-action.js

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

src/analyze-action.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ async function run() {
353353
checkoutPath,
354354
outputDir,
355355
category,
356+
actionsUtil.getOptionalInput("post-process-output"),
356357
);
357358

358359
core.setOutput(

src/upload-lib.ts

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,36 @@ export async function postProcessSarifFiles(
758758
return { sarif, analysisKey, environment };
759759
}
760760

761+
/**
762+
* Writes the processed SARIF file to disk, if needed based on `pathInput` or the `SARIF_DUMP_DIR`.
763+
*
764+
* @param logger The logger to use.
765+
* @param pathInput The input provided for `post-process-output`.
766+
* @param uploadTarget The upload target.
767+
* @param processingResults The results of post-processing SARIF files.
768+
*/
769+
export async function writeProcessedFiles(
770+
logger: Logger,
771+
pathInput: string | undefined,
772+
uploadTarget: analyses.AnalysisConfig,
773+
processingResults: PostProcessingResults,
774+
) {
775+
// If there's an explicit input, use that. Otherwise, use the value from the environment variable.
776+
const outputPath = pathInput || process.env[EnvVar.SARIF_DUMP_DIR];
777+
778+
// If we have an output path, write the SARIF file to it.
779+
if (outputPath !== undefined) {
780+
dumpSarifFile(
781+
JSON.stringify(processingResults.sarif),
782+
outputPath,
783+
logger,
784+
uploadTarget,
785+
);
786+
} else {
787+
logger.debug(`Not writing processed SARIF files.`);
788+
}
789+
}
790+
761791
/**
762792
* Uploads a single SARIF file or a directory of SARIF files depending on what `inputSarifPath` refers
763793
* to.
@@ -840,11 +870,6 @@ export async function uploadProcessedFiles(
840870
logger.debug(`Serializing SARIF for upload`);
841871
const sarifPayload = JSON.stringify(sarif);
842872

843-
const dumpDir = process.env[EnvVar.SARIF_DUMP_DIR];
844-
if (dumpDir) {
845-
dumpSarifFile(sarifPayload, dumpDir, logger, uploadTarget);
846-
}
847-
848873
logger.debug(`Compressing serialized SARIF`);
849874
const zippedSarif = zlib.gzipSync(sarifPayload).toString("base64");
850875
const checkoutURI = url.pathToFileURL(checkoutPath).href;
@@ -904,14 +929,14 @@ function dumpSarifFile(
904929
fs.mkdirSync(outputDir, { recursive: true });
905930
} else if (!fs.lstatSync(outputDir).isDirectory()) {
906931
throw new ConfigurationError(
907-
`The path specified by the ${EnvVar.SARIF_DUMP_DIR} environment variable exists and is not a directory: ${outputDir}`,
932+
`The path that processed SARIF files should be written to exists, but is not a directory: ${outputDir}`,
908933
);
909934
}
910935
const outputFile = path.resolve(
911936
outputDir,
912937
`upload${uploadTarget.sarifExtension}`,
913938
);
914-
logger.info(`Dumping processed SARIF file to ${outputFile}`);
939+
logger.info(`Writing processed SARIF file to ${outputFile}`);
915940
fs.writeFileSync(outputFile, sarifPayload);
916941
}
917942

src/upload-sarif.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export type UploadSarifResults = Partial<
1919
* @param checkoutPath The path where the repository was checked out at.
2020
* @param sarifPath The path to the file or directory to upload.
2121
* @param category The analysis category.
22+
* @param processedOutputPath The path to a directory to which the post-processed SARIF files should be written to.
2223
*
2324
* @returns A partial mapping from analysis kinds to the upload results.
2425
*/
@@ -29,6 +30,7 @@ export async function uploadSarif(
2930
checkoutPath: string,
3031
sarifPath: string,
3132
category?: string,
33+
processedOutputPath?: string,
3234
): Promise<UploadSarifResults> {
3335
const sarifGroups = await upload_lib.getGroupedSarifFilePaths(
3436
logger,
@@ -49,6 +51,15 @@ export async function uploadSarif(
4951
analysisConfig,
5052
);
5153

54+
// Write the processed SARIF files to disk. This will only write them if needed based on user inputs
55+
// or environment variables.
56+
await upload_lib.writeProcessedFiles(
57+
logger,
58+
processedOutputPath,
59+
analysisConfig,
60+
processingResults,
61+
);
62+
5263
// Only perform the actual upload of the processed files, if `uploadKind` is `always`.
5364
if (uploadKind === "always") {
5465
uploadResults[analysisKind] = await upload_lib.uploadProcessedFiles(

0 commit comments

Comments
 (0)