Skip to content

Commit eb50a88

Browse files
authored
Merge pull request #3097 from github/redsun82/only-dump-sarif
Dump soon to be uploaded SARIF on request
2 parents 31d3ae8 + 4c53461 commit eb50a88

File tree

6 files changed

+112
-0
lines changed

6 files changed

+112
-0
lines changed

lib/analyze-action.js

Lines changed: 19 additions & 0 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: 19 additions & 0 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 & 0 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: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/environment.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,10 @@ export enum EnvVar {
119119
* Whether to enable experimental extractors for CodeQL.
120120
*/
121121
EXPERIMENTAL_FEATURES = "CODEQL_ENABLE_EXPERIMENTAL_FEATURES",
122+
123+
/**
124+
* Whether and where to dump the processed SARIF file that would be uploaded, regardless of
125+
* whether the upload is disabled. This is intended for testing and debugging purposes.
126+
*/
127+
SARIF_DUMP_DIR = "CODEQL_ACTION_SARIF_DUMP_DIR",
122128
}

src/upload-lib.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,12 @@ export async function uploadSpecifiedFiles(
696696
validateUniqueCategory(sarif, uploadTarget.sentinelPrefix);
697697
logger.debug(`Serializing SARIF for upload`);
698698
const sarifPayload = JSON.stringify(sarif);
699+
700+
const dumpDir = process.env[EnvVar.SARIF_DUMP_DIR];
701+
if (dumpDir) {
702+
dumpSarifFile(sarifPayload, dumpDir, logger, uploadTarget);
703+
}
704+
699705
logger.debug(`Compressing serialized SARIF`);
700706
const zippedSarif = zlib.gzipSync(sarifPayload).toString("base64");
701707
const checkoutURI = url.pathToFileURL(checkoutPath).href;
@@ -742,6 +748,30 @@ export async function uploadSpecifiedFiles(
742748
};
743749
}
744750

751+
/**
752+
* Dumps the given processed SARIF file contents to `outputDir`.
753+
*/
754+
function dumpSarifFile(
755+
sarifPayload: string,
756+
outputDir: string,
757+
logger: Logger,
758+
uploadTarget: analyses.AnalysisConfig,
759+
) {
760+
if (!fs.existsSync(outputDir)) {
761+
fs.mkdirSync(outputDir, { recursive: true });
762+
} else if (!fs.lstatSync(outputDir).isDirectory()) {
763+
throw new ConfigurationError(
764+
`The path specified by the ${EnvVar.SARIF_DUMP_DIR} environment variable exists and is not a directory: ${outputDir}`,
765+
);
766+
}
767+
const outputFile = path.resolve(
768+
outputDir,
769+
`upload${uploadTarget.sarifExtension}`,
770+
);
771+
logger.info(`Dumping processed SARIF file to ${outputFile}`);
772+
fs.writeFileSync(outputFile, sarifPayload);
773+
}
774+
745775
const STATUS_CHECK_FREQUENCY_MILLISECONDS = 5 * 1000;
746776
const STATUS_CHECK_TIMEOUT_MILLISECONDS = 2 * 60 * 1000;
747777

0 commit comments

Comments
 (0)