generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #91 from lazy-actions/refactor/inputs
refactor: Separate the process of input parameters
- Loading branch information
Showing
9 changed files
with
232 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Inputs } from '../src/inputs'; | ||
import { template } from './helper'; | ||
|
||
describe('Inputs class Test', () => { | ||
const initEnv = process.env; | ||
|
||
beforeEach(() => { | ||
process.env = { | ||
INPUT_TOKEN: 'xxxxx', | ||
INPUT_IMAGE: 'yyyyy', | ||
...initEnv | ||
}; | ||
}); | ||
|
||
test('Specify required parameters only', () => { | ||
expect(() => new Inputs()).not.toThrow(); | ||
}); | ||
|
||
test('Specify all parameter', () => { | ||
process.env = { | ||
INPUT_TOKEN: 'xxx', | ||
INPUT_IMAGE: 'yyy', | ||
INPUT_TRIVY_VERSION: '0.18.3', | ||
INPUT_SEVERITY: 'HIGH', | ||
INPUT_VULN_TYPE: 'os', | ||
INPUT_IGNORE_UNFIXED: 'true', | ||
INPUT_TEMPLATE: template, | ||
INPUT_ISSUE_TITLE: 'hello', | ||
INPUT_ISSUE_LABEL: 'world', | ||
INPUT_ISSUE_ASSIGNEE: 'aaaa', | ||
...initEnv | ||
}; | ||
const inputs = new Inputs(); | ||
expect(() => inputs.validate()).not.toThrow(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { TrivyCmdOptionValidator } from '../src/validator'; | ||
import { template } from './helper'; | ||
|
||
describe('TrivyCmdOptionValidator Test', () => { | ||
test('Correct option', () => { | ||
const validator = new TrivyCmdOptionValidator({ | ||
severity: 'HIGH', | ||
vulnType: 'os', | ||
ignoreUnfixed: false, | ||
template | ||
}); | ||
expect(() => validator.validate()).not.toThrow(); | ||
}); | ||
|
||
test('Invalid severity', () => { | ||
const validator = new TrivyCmdOptionValidator({ | ||
severity: '?', | ||
vulnType: 'os', | ||
ignoreUnfixed: false, | ||
template | ||
}); | ||
expect(() => validator.validate()).toThrow( | ||
'Trivy option error: ? is unknown severity' | ||
); | ||
}); | ||
|
||
test('Invalid vuln_type', () => { | ||
const validator = new TrivyCmdOptionValidator({ | ||
severity: 'HIGH', | ||
vulnType: '?', | ||
ignoreUnfixed: false, | ||
template | ||
}); | ||
expect(() => validator.validate()).toThrow( | ||
'Trivy option error: ? is unknown vuln-type' | ||
); | ||
}); | ||
|
||
test('Invalid template', () => { | ||
const validator = new TrivyCmdOptionValidator({ | ||
severity: 'HIGH', | ||
vulnType: 'os', | ||
ignoreUnfixed: false, | ||
template: '?' | ||
}); | ||
expect(() => validator.validate()).toThrow('Could not find ?'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import * as core from '@actions/core'; | ||
import { IssueInputs, TrivyInputs } from './interface'; | ||
import { TrivyCmdOptionValidator } from './validator'; | ||
|
||
export class Inputs { | ||
token: string; | ||
image: string; | ||
trivy: TrivyInputs; | ||
issue: IssueInputs; | ||
fail_on_vulnerabilities: boolean; | ||
|
||
constructor() { | ||
this.token = core.getInput('token', { required: true }); | ||
|
||
const image = core.getInput('image') || process.env.IMAGE_NAME; | ||
if (!image) { | ||
throw new Error('Please specify target image'); | ||
} | ||
this.image = image; | ||
|
||
this.trivy = { | ||
version: core.getInput('trivy_version').replace(/^v/, ''), | ||
option: { | ||
severity: core.getInput('severity').replace(/\s+/g, ''), | ||
vulnType: core.getInput('vuln_type').replace(/\s+/g, ''), | ||
ignoreUnfixed: core.getInput('ignore_unfixed').toLowerCase() === 'true', | ||
template: | ||
core.getInput('template') || `${__dirname}/template/default.tpl` | ||
} | ||
}; | ||
|
||
this.issue = { | ||
title: core.getInput('issue_title'), | ||
labels: core | ||
.getInput('issue_label') | ||
.replace(/\s+/g, '') | ||
.split(','), | ||
assignees: core | ||
.getInput('issue_assignee') | ||
.replace(/\s+/g, '') | ||
.split(',') | ||
}; | ||
|
||
this.fail_on_vulnerabilities = | ||
core.getInput('fail_on_vulnerabilities') === 'true'; | ||
} | ||
|
||
validate(): void { | ||
const trivy = new TrivyCmdOptionValidator(this.trivy.option); | ||
trivy.validate(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.