Skip to content

Commit 12f3cfe

Browse files
committed
Write processed SARIF files if post-process-output input is provided
1 parent c2bec36 commit 12f3cfe

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
@@ -359,6 +359,7 @@ async function run() {
359359
checkoutPath,
360360
outputDir,
361361
category,
362+
actionsUtil.getOptionalInput("post-process-output"),
362363
);
363364
} else {
364365
uploadResults = {};

src/upload-lib.ts

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

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

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

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)