Skip to content

Commit 1ab9400

Browse files
committed
Add json format output option
1 parent 516844f commit 1ab9400

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ Optionally, change the `fail-build` field to `false` to avoid failing the build
116116

117117
### Action Inputs
118118

119-
The inputs `image`, `path`, and `sbom` are mutually exclusive to specify the source to scan; all the other keys are optional. These are all the available keys to configure this action, along with the defaults:
119+
The inputs `image`, `path`, and `sbom` are mutually exclusive to specify the source to scan;inputs `output-format` and`acs-report-enable` are mutually exclusive to specify the report format;all the other keys are optional. These are all the available keys to configure this action, along with the defaults:
120120

121121
| Input Name | Description | Default Value |
122122
| ------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------- |
@@ -126,14 +126,16 @@ The inputs `image`, `path`, and `sbom` are mutually exclusive to specify the sou
126126
| `registry-username` | The registry username to use when authenticating to an external registry | |
127127
| `registry-password` | The registry password to use when authenticating to an external registry | |
128128
| `fail-build` | Fail the build if a vulnerability is found with a higher severity. That severity defaults to `"medium"` and can be set with `severity-cutoff`. | `true` |
129-
| `acs-report-enable` | Generate a SARIF report and set the `sarif` output parameter after successful action execution. This report is compatible with GitHub Automated Code Scanning (ACS), as the artifact to upload for display as a Code Scanning Alert report. | `true` |
129+
| `output-format` | Set the output parameter after successful action execution. Valid choices are "json" and "sarif" | `sarif` |
130+
| `acs-report-enable` | Generate a SARIF report and set the `sarif` output parameter (Override the output-format) after successful action execution. This report is compatible with GitHub Automated Code Scanning (ACS), as the artifact to upload for display as a Code Scanning Alert report. | `true` |
130131
| `severity-cutoff` | With ACS reporting enabled, optionally specify the minimum vulnerability severity to trigger an "error" level ACS result. Valid choices are "negligible", "low", "medium", "high" and "critical". Any vulnerability with a severity less than this value will lead to a "warning" result. Default is "medium". | `"medium"` |
131132

132133
### Action Outputs
133134

134135
| Output Name | Description | Type |
135136
| ----------- | ----------------------------- | ------ |
136137
| `sarif` | Path to the SARIF report file | string |
138+
| `report` | Path to the report file | string |
137139

138140
### Example Workflows
139141

dist/index.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,14 @@ async function run() {
103103
const source = sourceInput();
104104
const failBuild = core.getInput("fail-build") || "true";
105105
const acsReportEnable = core.getInput("acs-report-enable") || "true";
106+
const outputFormat = core.getInput("output-format") || "sarif";
106107
const severityCutoff = core.getInput("severity-cutoff") || "medium";
107108
const out = await runScan({
108109
source,
109110
failBuild,
110111
acsReportEnable,
111112
severityCutoff,
113+
outputFormat
112114
});
113115
Object.keys(out).map((key) => {
114116
core.setOutput(key, out[key]);
@@ -118,7 +120,7 @@ async function run() {
118120
}
119121
}
120122

121-
async function runScan({ source, failBuild, acsReportEnable, severityCutoff }) {
123+
async function runScan({ source, failBuild, acsReportEnable, severityCutoff ,outputFormat}) {
122124
const out = {};
123125

124126
const env = {
@@ -139,6 +141,8 @@ async function runScan({ source, failBuild, acsReportEnable, severityCutoff }) {
139141
}
140142

141143
const SEVERITY_LIST = ["negligible", "low", "medium", "high", "critical"];
144+
const FORMAT_LIST = ["sarif", "json"];
145+
142146
let cmdArgs = [];
143147

144148
if (core.isDebug()) {
@@ -152,7 +156,7 @@ async function runScan({ source, failBuild, acsReportEnable, severityCutoff }) {
152156
if (acsReportEnable) {
153157
cmdArgs.push("-o", "sarif");
154158
} else {
155-
cmdArgs.push("-o", "json");
159+
cmdArgs.push("-o", outputFormat);
156160
}
157161

158162
if (
@@ -166,6 +170,17 @@ async function runScan({ source, failBuild, acsReportEnable, severityCutoff }) {
166170
`Invalid severity-cutoff value is set to ${severityCutoff} - please ensure you are choosing either negligible, low, medium, high, or critical`
167171
);
168172
}
173+
if (
174+
!FORMAT_LIST.some(
175+
(item) =>
176+
typeof outputFormat.toLowerCase() === "string" &&
177+
item === outputFormat.toLowerCase()
178+
)
179+
) {
180+
throw new Error(
181+
`Invalid output-format value is set to ${outputFormat} - please ensure you are choosing either json or sarif`
182+
);
183+
}
169184

170185
core.debug(`Installing grype version ${grypeVersion}`);
171186
await installGrype(grypeVersion);
@@ -225,6 +240,10 @@ async function runScan({ source, failBuild, acsReportEnable, severityCutoff }) {
225240
const SARIF_FILE = "./results.sarif";
226241
fs.writeFileSync(SARIF_FILE, cmdOutput);
227242
out.sarif = SARIF_FILE;
243+
}else {
244+
const REPORT_FILE = "./results.report";
245+
fs.writeFileSync(REPORT_FILE, cmdOutput);
246+
out.report = REPORT_FILE;
228247
}
229248

230249
if (failBuild === true && exitCode > 0) {
@@ -6916,4 +6935,4 @@ module.exports = require("util");
69166935
/******/ // Load entry module and return exports
69176936
/******/ return __webpack_require__(932);
69186937
/******/ })()
6919-
;
6938+
;

0 commit comments

Comments
 (0)