Skip to content

Commit 4fbc427

Browse files
committed
Add test fot issue option
1 parent 7f5d88b commit 4fbc427

File tree

3 files changed

+78
-12
lines changed

3 files changed

+78
-12
lines changed

.github/workflows/test.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,33 @@ jobs:
9494
job_name: ':ts: *test gitrivy*'
9595
channel: '#develop'
9696
url: ${{ secrets.SLACK_WEBHOOK }}
97+
98+
test3:
99+
name: Test not to create issue
100+
runs-on: ubuntu-18.04
101+
steps:
102+
- uses: actions/checkout@v1
103+
104+
- name: Install dependencies
105+
run: npm install
106+
107+
# - name: Test
108+
# run: npm run test
109+
110+
- name: Build
111+
run: npm run build
112+
113+
- name: Pull docker image
114+
run: docker pull alpine:3.10.3
115+
116+
- uses: ./
117+
with:
118+
issue: 'false'
119+
120+
- uses: homoluctus/[email protected]
121+
if: always()
122+
with:
123+
type: ${{ job.status }}
124+
job_name: ':ts: *test gitrivy*'
125+
channel: '#develop'
126+
url: ${{ secrets.SLACK_WEBHOOK }}

__tests__/trivy.test.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,26 +128,55 @@ describe('Scan', () => {
128128
severity: 'HIGH,CRITICAL',
129129
vulnType: 'os,library',
130130
ignoreUnfixed: true,
131+
format: 'json',
131132
};
132-
const result: Vulnerability[] = Trivy.scan(trivyPath, image, options);
133+
const result: Vulnerability[] | string = Trivy.scan(
134+
trivyPath,
135+
image,
136+
options
137+
);
133138
expect(result.length).toBeGreaterThanOrEqual(1);
139+
expect(result).toBeInstanceOf(Object);
134140
});
135141

136142
test('without ignoreUnfixed', () => {
137143
const options: TrivyOption = {
138144
severity: 'HIGH,CRITICAL',
139145
vulnType: 'os,library',
140146
ignoreUnfixed: false,
147+
format: 'json',
141148
};
142-
const result: Vulnerability[] = Trivy.scan(trivyPath, image, options);
149+
const result: Vulnerability[] | string = Trivy.scan(
150+
trivyPath,
151+
image,
152+
options
153+
);
154+
expect(result.length).toBeGreaterThanOrEqual(1);
155+
expect(result).toBeInstanceOf(Object);
156+
});
157+
158+
test('with table format', () => {
159+
const options: TrivyOption = {
160+
severity: 'HIGH,CRITICAL',
161+
vulnType: 'os,library',
162+
ignoreUnfixed: false,
163+
format: 'table',
164+
};
165+
const result: Vulnerability[] | string = Trivy.scan(
166+
trivyPath,
167+
image,
168+
options
169+
);
143170
expect(result.length).toBeGreaterThanOrEqual(1);
171+
expect(result).toMatch(/alpine:3\.10/);
144172
});
145173

146174
test('with invalid severity', () => {
147175
const invalidOption: TrivyOption = {
148176
severity: 'INVALID',
149177
vulnType: 'os,library',
150178
ignoreUnfixed: true,
179+
format: 'json',
151180
};
152181
expect(() => {
153182
Trivy.scan(trivyPath, image, invalidOption);
@@ -159,6 +188,7 @@ describe('Scan', () => {
159188
severity: 'HIGH',
160189
vulnType: 'INVALID',
161190
ignoreUnfixed: true,
191+
format: 'json',
162192
};
163193
expect(() => {
164194
Trivy.scan(trivyPath, image, invalidOption);

dist/index.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6572,28 +6572,35 @@ const issue_1 = __webpack_require__(163);
65726572
function run() {
65736573
return __awaiter(this, void 0, void 0, function* () {
65746574
try {
6575-
const token = core.getInput('token', { required: true });
65766575
const trivyVersion = core
65776576
.getInput('trivy_version')
65786577
.replace(/^v/, '');
65796578
const image = core.getInput('image') || process.env.IMAGE_NAME;
6579+
const issueFlag = core.getInput('issue').toLowerCase() == 'true';
65806580
if (image === undefined || image === '') {
65816581
throw new Error('Please specify scan target image name');
65826582
}
6583-
const trivyOptions = {
6583+
const trivyOption = {
65846584
severity: core.getInput('severity').replace(/\s+/g, ''),
65856585
vulnType: core.getInput('vuln_type').replace(/\s+/g, ''),
65866586
ignoreUnfixed: core.getInput('ignore_unfixed').toLowerCase() === 'true',
6587+
format: issueFlag ? 'json' : 'table',
65876588
};
65886589
const downloader = new trivy_1.Downloader();
65896590
const trivyCmdPath = yield downloader.download(trivyVersion);
6590-
const result = trivy_1.Trivy.scan(trivyCmdPath, image, trivyOptions);
6591+
const result = trivy_1.Trivy.scan(trivyCmdPath, image, trivyOption);
6592+
if (!issueFlag) {
6593+
core.info(`Not create a issue because issue parameter is false.
6594+
Vulnerabilities:
6595+
${result}`);
6596+
return;
6597+
}
65916598
const issueContent = trivy_1.Trivy.parse(result);
65926599
if (issueContent === '') {
65936600
core.info('Vulnerabilities were not found.\nYour maintenance looks good 👍');
65946601
return;
65956602
}
6596-
const issueOptions = {
6603+
const issueOption = {
65976604
title: core.getInput('issue_title'),
65986605
body: issueContent,
65996606
labels: core
@@ -6605,7 +6612,8 @@ function run() {
66056612
.replace(/\s+/g, '')
66066613
.split(','),
66076614
};
6608-
const output = yield issue_1.createIssue(token, issueOptions);
6615+
const token = core.getInput('token', { required: true });
6616+
const output = yield issue_1.createIssue(token, issueOption);
66096617
core.setOutput('html_url', output.htmlUrl);
66106618
core.setOutput('issue_number', output.issueNumber.toString());
66116619
}
@@ -13315,19 +13323,18 @@ class Trivy {
1331513323
'--vuln-type',
1331613324
option.vulnType,
1331713325
'--format',
13318-
'json',
13326+
option.format,
1331913327
'--quiet',
1332013328
'--no-progress',
1332113329
];
13322-
if (option.ignoreUnfixed) {
13330+
if (option.ignoreUnfixed)
1332313331
args.push('--ignore-unfixed');
13324-
}
1332513332
args.push(image);
1332613333
const result = child_process_1.spawnSync(trivyPath, args, {
1332713334
encoding: 'utf-8',
1332813335
});
1332913336
if (result.stdout && result.stdout.length > 0) {
13330-
const vulnerabilities = JSON.parse(result.stdout);
13337+
const vulnerabilities = option.format === 'json' ? JSON.parse(result.stdout) : result.stdout;
1333113338
if (vulnerabilities.length > 0) {
1333213339
return vulnerabilities;
1333313340
}
@@ -13359,7 +13366,6 @@ class Trivy {
1335913366
}
1336013367
issueContent += `${vulnTable}\n\n`;
1336113368
}
13362-
console.debug(issueContent);
1336313369
return issueContent;
1336413370
}
1336513371
static validateOption(option) {

0 commit comments

Comments
 (0)