Skip to content

Commit 35454d3

Browse files
committed
Refactor CQ SARIF upload in upload-sarif into a function
1 parent b73659a commit 35454d3

File tree

2 files changed

+69
-25
lines changed

2 files changed

+69
-25
lines changed

lib/upload-sarif-action.js

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

src/upload-sarif-action.ts

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,45 @@ interface UploadSarifStatusReport
3232
extends StatusReportBase,
3333
upload_lib.UploadStatusReport {}
3434

35+
/**
36+
* Searches for SARIF files for the given `analysis` in the given `sarifPath`.
37+
* If any are found, then they are uploaded to the appropriate endpoint for the given `analysis`.
38+
*
39+
* @param logger The logger to use.
40+
* @param features Information about FFs.
41+
* @param sarifPath The path to a directory containing SARIF files.
42+
* @param checkoutPath The checkout path.
43+
* @param analysis The configuration of the analysis we should upload SARIF files for.
44+
* @param category The SARIF category to use for the upload.
45+
* @returns The result of uploading the SARIF file(s) or `undefined` if there are none.
46+
*/
47+
async function findAndUpload(
48+
logger: Logger,
49+
features: Features,
50+
sarifPath: string,
51+
checkoutPath: string,
52+
analysis: analyses.AnalysisConfig,
53+
category?: string,
54+
): Promise<upload_lib.UploadResult | undefined> {
55+
const sarifFiles = upload_lib.findSarifFilesInDir(
56+
sarifPath,
57+
analysis.sarifPredicate,
58+
);
59+
60+
if (sarifFiles.length !== 0) {
61+
return await upload_lib.uploadSpecifiedFiles(
62+
sarifFiles,
63+
checkoutPath,
64+
category,
65+
features,
66+
logger,
67+
analysis,
68+
);
69+
}
70+
71+
return undefined;
72+
}
73+
3574
async function sendSuccessStatusReport(
3675
startedAt: Date,
3776
uploadStats: upload_lib.UploadStatusReport,
@@ -86,6 +125,7 @@ async function run() {
86125
}
87126

88127
try {
128+
// `sarifPath` can either be a path to a single file, or a path to a directory.
89129
const sarifPath = actionsUtil.getRequiredInput("sarif_file");
90130
const checkoutPath = actionsUtil.getRequiredInput("checkout_path");
91131
const category = actionsUtil.getOptionalInput("category");
@@ -104,21 +144,14 @@ async function run() {
104144
// Code quality can currently only be enabled on top of security, so we'd currently always expect to
105145
// have a directory for the results here.
106146
if (fs.lstatSync(sarifPath).isDirectory()) {
107-
const qualitySarifFiles = upload_lib.findSarifFilesInDir(
147+
await findAndUpload(
148+
logger,
149+
features,
108150
sarifPath,
109-
analyses.CodeQuality.sarifPredicate,
151+
checkoutPath,
152+
analyses.CodeQuality,
153+
actionsUtil.fixCodeQualityCategory(logger, category),
110154
);
111-
112-
if (qualitySarifFiles.length !== 0) {
113-
await upload_lib.uploadSpecifiedFiles(
114-
qualitySarifFiles,
115-
checkoutPath,
116-
actionsUtil.fixCodeQualityCategory(logger, category),
117-
features,
118-
logger,
119-
analyses.CodeQuality,
120-
);
121-
}
122155
}
123156

124157
// We don't upload results in test mode, so don't wait for processing

0 commit comments

Comments
 (0)