Skip to content

Commit 7f5d88b

Browse files
committed
Modify creating issue is optional
1 parent 0f27c0d commit 7f5d88b

File tree

4 files changed

+33
-18
lines changed

4 files changed

+33
-18
lines changed

action.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ name: 'Trivy Action'
22
description: 'Scan docker image vulnerability using Trivy and create GitHub Issue'
33
author: 'homoluctus'
44
inputs:
5-
token:
6-
description: 'GitHub access token'
7-
required: true
85
trivy_version:
96
description: 'Trivy version'
107
default: 'latest'
@@ -24,6 +21,13 @@ inputs:
2421
description: 'Ignore unfixed vulnerabilities [true, false]'
2522
default: 'false'
2623
required: false
24+
issue:
25+
description: 'Decide whether to create a issue when vulnerabilities are found [true, false]'
26+
default: 'true'
27+
required: false
28+
token:
29+
description: 'GitHub access token used to create a issue'
30+
required: false
2731
issue_title:
2832
description: 'Issue title'
2933
default: 'Security Alert'

src/index.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,42 @@ import {
1010

1111
async function run() {
1212
try {
13-
const token: string = core.getInput('token', { required: true });
1413
const trivyVersion: string = core
1514
.getInput('trivy_version')
1615
.replace(/^v/, '');
1716
const image: string | undefined =
1817
core.getInput('image') || process.env.IMAGE_NAME;
18+
const issueFlag: boolean = core.getInput('issue').toLowerCase() == 'true';
1919

2020
if (image === undefined || image === '') {
2121
throw new Error('Please specify scan target image name');
2222
}
2323

24-
const trivyOptions: TrivyOption = {
24+
const trivyOption: TrivyOption = {
2525
severity: core.getInput('severity').replace(/\s+/g, ''),
2626
vulnType: core.getInput('vuln_type').replace(/\s+/g, ''),
2727
ignoreUnfixed: core.getInput('ignore_unfixed').toLowerCase() === 'true',
28+
format: issueFlag ? 'json' : 'table',
2829
};
2930

3031
const downloader = new Downloader();
3132
const trivyCmdPath: string = await downloader.download(trivyVersion);
32-
const result: Vulnerability[] = Trivy.scan(
33+
const result: Vulnerability[] | string = Trivy.scan(
3334
trivyCmdPath,
3435
image,
35-
trivyOptions
36+
trivyOption
3637
);
37-
const issueContent: string = Trivy.parse(result);
38+
39+
if (!issueFlag) {
40+
core.info(
41+
`Not create a issue because issue parameter is false.
42+
Vulnerabilities:
43+
${result}`
44+
);
45+
return;
46+
}
47+
48+
const issueContent: string = Trivy.parse(result as Vulnerability[]);
3849

3950
if (issueContent === '') {
4051
core.info(
@@ -43,7 +54,7 @@ async function run() {
4354
return;
4455
}
4556

46-
const issueOptions: IssueOption = {
57+
const issueOption: IssueOption = {
4758
title: core.getInput('issue_title'),
4859
body: issueContent,
4960
labels: core
@@ -55,7 +66,8 @@ async function run() {
5566
.replace(/\s+/g, '')
5667
.split(','),
5768
};
58-
const output: IssueResponse = await createIssue(token, issueOptions);
69+
const token: string = core.getInput('token', { required: true });
70+
const output: IssueResponse = await createIssue(token, issueOption);
5971
core.setOutput('html_url', output.htmlUrl);
6072
core.setOutput('issue_number', output.issueNumber.toString());
6173
} catch (error) {

src/interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export interface TrivyOption {
1414
severity: string;
1515
vulnType: string;
1616
ignoreUnfixed: boolean;
17+
format: string;
1718
}
1819

1920
export interface Vulnerability {

src/trivy.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export class Trivy {
123123
trivyPath: string,
124124
image: string,
125125
option: TrivyOption
126-
): Vulnerability[] {
126+
): Vulnerability[] | string {
127127
Trivy.validateOption(option);
128128

129129
const args: string[] = [
@@ -132,22 +132,21 @@ export class Trivy {
132132
'--vuln-type',
133133
option.vulnType,
134134
'--format',
135-
'json',
135+
option.format,
136136
'--quiet',
137137
'--no-progress',
138138
];
139139

140-
if (option.ignoreUnfixed) {
141-
args.push('--ignore-unfixed');
142-
}
143-
140+
if (option.ignoreUnfixed) args.push('--ignore-unfixed');
144141
args.push(image);
142+
145143
const result: SpawnSyncReturns<string> = spawnSync(trivyPath, args, {
146144
encoding: 'utf-8',
147145
});
148146

149147
if (result.stdout && result.stdout.length > 0) {
150-
const vulnerabilities: Vulnerability[] = JSON.parse(result.stdout);
148+
const vulnerabilities: Vulnerability[] | string =
149+
option.format === 'json' ? JSON.parse(result.stdout) : result.stdout;
151150
if (vulnerabilities.length > 0) {
152151
return vulnerabilities;
153152
}
@@ -185,7 +184,6 @@ export class Trivy {
185184
}
186185
issueContent += `${vulnTable}\n\n`;
187186
}
188-
console.debug(issueContent);
189187
return issueContent;
190188
}
191189

0 commit comments

Comments
 (0)