Skip to content

Commit 36adfa7

Browse files
authored
Merge pull request #3166 from github/mbg/upload-sarif/add-tests
Add tests for `upload-sarif`
2 parents f0a08a4 + 9715962 commit 36adfa7

File tree

6 files changed

+452
-128
lines changed

6 files changed

+452
-128
lines changed

.github/workflows/__upload-quality-sarif.yml

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

pr-checks/checks/upload-quality-sarif.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ steps:
2222
ref: 'refs/heads/main'
2323
sha: '5e235361806c361d4d3f8859e3c897658025a9a2'
2424
- name: "Check output from `upload-sarif` step"
25-
if: fromJSON(steps.upload-sarif.outputs.sarif-ids)[0].analysis != 'code-quality'
25+
if: '!(fromJSON(steps.upload-sarif.outputs.sarif-ids).code-quality)'
2626
run: exit 1

src/upload-sarif-action.ts

Lines changed: 17 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import * as fs from "fs";
2-
31
import * as core from "@actions/core";
42

53
import * as actionsUtil from "./actions-util";
@@ -18,6 +16,7 @@ import {
1816
isThirdPartyAnalysis,
1917
} from "./status-report";
2018
import * as upload_lib from "./upload-lib";
19+
import { uploadSarif } from "./upload-sarif";
2120
import {
2221
ConfigurationError,
2322
checkActionVersion,
@@ -32,60 +31,6 @@ interface UploadSarifStatusReport
3231
extends StatusReportBase,
3332
upload_lib.UploadStatusReport {}
3433

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 SARIF file or directory containing SARIF files.
42-
* @param pathStats Information about `sarifPath`.
43-
* @param checkoutPath The checkout path.
44-
* @param analysis The configuration of the analysis we should upload SARIF files for.
45-
* @param category The SARIF category to use for the upload.
46-
* @returns The result of uploading the SARIF file(s) or `undefined` if there are none.
47-
*/
48-
async function findAndUpload(
49-
logger: Logger,
50-
features: Features,
51-
sarifPath: string,
52-
pathStats: fs.Stats,
53-
checkoutPath: string,
54-
analysis: analyses.AnalysisConfig,
55-
category?: string,
56-
): Promise<upload_lib.UploadResult | undefined> {
57-
let sarifFiles: string[] | undefined;
58-
59-
if (pathStats.isDirectory()) {
60-
sarifFiles = upload_lib.findSarifFilesInDir(
61-
sarifPath,
62-
analysis.sarifPredicate,
63-
);
64-
} else if (
65-
pathStats.isFile() &&
66-
(analysis.sarifPredicate(sarifPath) ||
67-
(analysis.kind === analyses.AnalysisKind.CodeScanning &&
68-
!analyses.CodeQuality.sarifPredicate(sarifPath)))
69-
) {
70-
sarifFiles = [sarifPath];
71-
} else {
72-
return undefined;
73-
}
74-
75-
if (sarifFiles.length !== 0) {
76-
return await upload_lib.uploadSpecifiedFiles(
77-
sarifFiles,
78-
checkoutPath,
79-
category,
80-
features,
81-
logger,
82-
analysis,
83-
);
84-
}
85-
86-
return undefined;
87-
}
88-
8934
async function sendSuccessStatusReport(
9035
startedAt: Date,
9136
uploadStats: upload_lib.UploadStatusReport,
@@ -144,56 +89,37 @@ async function run() {
14489
const sarifPath = actionsUtil.getRequiredInput("sarif_file");
14590
const checkoutPath = actionsUtil.getRequiredInput("checkout_path");
14691
const category = actionsUtil.getOptionalInput("category");
147-
const pathStats = fs.lstatSync(sarifPath, { throwIfNoEntry: false });
148-
149-
if (pathStats === undefined) {
150-
throw new ConfigurationError(`Path does not exist: ${sarifPath}.`);
151-
}
15292

153-
const sarifIds: Array<{ analysis: string; id: string }> = [];
154-
const uploadResult = await findAndUpload(
93+
const uploadResults = await uploadSarif(
15594
logger,
15695
features,
157-
sarifPath,
158-
pathStats,
15996
checkoutPath,
160-
analyses.CodeScanning,
97+
sarifPath,
16198
category,
16299
);
163-
if (uploadResult !== undefined) {
164-
core.setOutput("sarif-id", uploadResult.sarifID);
165-
sarifIds.push({
166-
analysis: analyses.AnalysisKind.CodeScanning,
167-
id: uploadResult.sarifID,
168-
});
100+
101+
// Fail if we didn't upload anything.
102+
if (Object.keys(uploadResults).length === 0) {
103+
throw new ConfigurationError(
104+
`No SARIF files found to upload in "${sarifPath}".`,
105+
);
169106
}
170107

171-
// If there are `.quality.sarif` files in `sarifPath`, then upload those to the code quality service.
172-
const qualityUploadResult = await findAndUpload(
173-
logger,
174-
features,
175-
sarifPath,
176-
pathStats,
177-
checkoutPath,
178-
analyses.CodeQuality,
179-
actionsUtil.fixCodeQualityCategory(logger, category),
180-
);
181-
if (qualityUploadResult !== undefined) {
182-
sarifIds.push({
183-
analysis: analyses.AnalysisKind.CodeQuality,
184-
id: qualityUploadResult.sarifID,
185-
});
108+
const codeScanningResult =
109+
uploadResults[analyses.AnalysisKind.CodeScanning];
110+
if (codeScanningResult !== undefined) {
111+
core.setOutput("sarif-id", codeScanningResult.sarifID);
186112
}
187-
core.setOutput("sarif-ids", JSON.stringify(sarifIds));
113+
core.setOutput("sarif-ids", JSON.stringify(uploadResults));
188114

189115
// We don't upload results in test mode, so don't wait for processing
190116
if (isInTestMode()) {
191117
core.debug("In test mode. Waiting for processing is disabled.");
192118
} else if (actionsUtil.getRequiredInput("wait-for-processing") === "true") {
193-
if (uploadResult !== undefined) {
119+
if (codeScanningResult !== undefined) {
194120
await upload_lib.waitForProcessing(
195121
getRepositoryNwo(),
196-
uploadResult.sarifID,
122+
codeScanningResult.sarifID,
197123
logger,
198124
);
199125
}
@@ -202,7 +128,7 @@ async function run() {
202128
}
203129
await sendSuccessStatusReport(
204130
startedAt,
205-
uploadResult?.statusReport || {},
131+
codeScanningResult?.statusReport || {},
206132
logger,
207133
);
208134
} catch (unwrappedError) {

0 commit comments

Comments
 (0)