Skip to content

Commit 9f452fa

Browse files
committed
Move core upload-sarif logic to upload-sarif module
Note that this also fixes the format of the `sarif-ids` outputs to match what is documented
1 parent 5fc9e66 commit 9f452fa

File tree

5 files changed

+119
-79
lines changed

5 files changed

+119
-79
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: 42 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: 11 additions & 40 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,7 +16,7 @@ import {
1816
isThirdPartyAnalysis,
1917
} from "./status-report";
2018
import * as upload_lib from "./upload-lib";
21-
import { findAndUpload } from "./upload-sarif";
19+
import { uploadSarif } from "./upload-sarif";
2220
import {
2321
ConfigurationError,
2422
checkActionVersion,
@@ -91,56 +89,29 @@ async function run() {
9189
const sarifPath = actionsUtil.getRequiredInput("sarif_file");
9290
const checkoutPath = actionsUtil.getRequiredInput("checkout_path");
9391
const category = actionsUtil.getOptionalInput("category");
94-
const pathStats = fs.lstatSync(sarifPath, { throwIfNoEntry: false });
95-
96-
if (pathStats === undefined) {
97-
throw new ConfigurationError(`Path does not exist: ${sarifPath}.`);
98-
}
9992

100-
const sarifIds: Array<{ analysis: string; id: string }> = [];
101-
const uploadResult = await findAndUpload(
93+
const uploadResults = await uploadSarif(
10294
logger,
10395
features,
104-
sarifPath,
105-
pathStats,
10696
checkoutPath,
107-
analyses.CodeScanning,
108-
category,
109-
);
110-
if (uploadResult !== undefined) {
111-
core.setOutput("sarif-id", uploadResult.sarifID);
112-
sarifIds.push({
113-
analysis: analyses.AnalysisKind.CodeScanning,
114-
id: uploadResult.sarifID,
115-
});
116-
}
117-
118-
// If there are `.quality.sarif` files in `sarifPath`, then upload those to the code quality service.
119-
const qualityUploadResult = await findAndUpload(
120-
logger,
121-
features,
12297
sarifPath,
123-
pathStats,
124-
checkoutPath,
125-
analyses.CodeQuality,
126-
actionsUtil.fixCodeQualityCategory(logger, category),
98+
category,
12799
);
128-
if (qualityUploadResult !== undefined) {
129-
sarifIds.push({
130-
analysis: analyses.AnalysisKind.CodeQuality,
131-
id: qualityUploadResult.sarifID,
132-
});
100+
const codeScanningResult =
101+
uploadResults[analyses.AnalysisKind.CodeScanning];
102+
if (codeScanningResult !== undefined) {
103+
core.setOutput("sarif-id", codeScanningResult.sarifID);
133104
}
134-
core.setOutput("sarif-ids", JSON.stringify(sarifIds));
105+
core.setOutput("sarif-ids", JSON.stringify(uploadResults));
135106

136107
// We don't upload results in test mode, so don't wait for processing
137108
if (isInTestMode()) {
138109
core.debug("In test mode. Waiting for processing is disabled.");
139110
} else if (actionsUtil.getRequiredInput("wait-for-processing") === "true") {
140-
if (uploadResult !== undefined) {
111+
if (codeScanningResult !== undefined) {
141112
await upload_lib.waitForProcessing(
142113
getRepositoryNwo(),
143-
uploadResult.sarifID,
114+
codeScanningResult.sarifID,
144115
logger,
145116
);
146117
}
@@ -149,7 +120,7 @@ async function run() {
149120
}
150121
await sendSuccessStatusReport(
151122
startedAt,
152-
uploadResult?.statusReport || {},
123+
codeScanningResult?.statusReport || {},
153124
logger,
154125
);
155126
} catch (unwrappedError) {

src/upload-sarif.ts

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import * as fs from "fs";
22

3+
import * as actionsUtil from "./actions-util";
34
import * as analyses from "./analyses";
4-
import { Features } from "./feature-flags";
5+
import { FeatureEnablement } from "./feature-flags";
56
import { Logger } from "./logging";
67
import * as upload_lib from "./upload-lib";
8+
import { ConfigurationError } from "./util";
79

810
/**
911
* Searches for SARIF files for the given `analysis` in the given `sarifPath`.
@@ -20,7 +22,7 @@ import * as upload_lib from "./upload-lib";
2022
*/
2123
export async function findAndUpload(
2224
logger: Logger,
23-
features: Features,
25+
features: FeatureEnablement,
2426
sarifPath: string,
2527
pathStats: fs.Stats,
2628
checkoutPath: string,
@@ -58,3 +60,63 @@ export async function findAndUpload(
5860

5961
return undefined;
6062
}
63+
64+
// Maps analysis kinds to SARIF IDs.
65+
type UploadSarifResults = Partial<
66+
Record<analyses.AnalysisKind, upload_lib.UploadResult>
67+
>;
68+
69+
/**
70+
* Finds SARIF files in `sarifPath` and uploads them to the appropriate services.
71+
*
72+
* @param logger The logger to use.
73+
* @param features Information about enabled features.
74+
* @param checkoutPath The path where the repository was checked out at.
75+
* @param sarifPath The path to the file or directory to upload.
76+
* @param category The analysis category.
77+
*
78+
* @returns A partial mapping from analysis kinds to the upload results.
79+
*/
80+
export async function uploadSarif(
81+
logger: Logger,
82+
features: FeatureEnablement,
83+
checkoutPath: string,
84+
sarifPath: string,
85+
category?: string,
86+
): Promise<UploadSarifResults> {
87+
const pathStats = fs.lstatSync(sarifPath, { throwIfNoEntry: false });
88+
89+
if (pathStats === undefined) {
90+
throw new ConfigurationError(`Path does not exist: ${sarifPath}.`);
91+
}
92+
93+
const uploadResults: UploadSarifResults = {};
94+
const uploadResult = await findAndUpload(
95+
logger,
96+
features,
97+
sarifPath,
98+
pathStats,
99+
checkoutPath,
100+
analyses.CodeScanning,
101+
category,
102+
);
103+
if (uploadResult !== undefined) {
104+
uploadResults[analyses.AnalysisKind.CodeScanning] = uploadResult;
105+
}
106+
107+
// If there are `.quality.sarif` files in `sarifPath`, then upload those to the code quality service.
108+
const qualityUploadResult = await findAndUpload(
109+
logger,
110+
features,
111+
sarifPath,
112+
pathStats,
113+
checkoutPath,
114+
analyses.CodeQuality,
115+
actionsUtil.fixCodeQualityCategory(logger, category),
116+
);
117+
if (qualityUploadResult !== undefined) {
118+
uploadResults[analyses.AnalysisKind.CodeQuality] = qualityUploadResult;
119+
}
120+
121+
return uploadResults;
122+
}

0 commit comments

Comments
 (0)