Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add minimum priority option #370

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Dependency directory
node_modules

# MacOS files
.DS_Store

# See https://github.com/github/gitignore/blob/main/Node.gitignore
# Logs
logs
Expand Down Expand Up @@ -131,4 +134,10 @@ dist
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
.pnp.*

# PMD cache
.pmdCache

# Test artifacts
pmd.filelist
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ See also [Uploading a SARIF file to GitHub](https://docs.github.com/en/code-secu
|`analyzeModifiedFilesOnly`|no|"true"|Instead of analyze all files under "sourcePath", only the files that have been touched in a pull request or push will be analyzed. This makes the analysis faster and helps especially bigger projects which gradually want to introduce PMD. This helps in enforcing that no new code violation is introduced.<br>Depending on the analyzed language, the results might be less accurate results. At the moment, this is not a problem, as PMD mostly analyzes each file individually, but that might change in the future.<br>If the change is very big, not all files might be analyzed. Currently the maximum number of modified files is 300.<br>Note: When using PMD as a code scanner in order to create "Code scanning alerts" on GitHub, all files should be analyzed in order to produce a complete picture of the project. Otherwise alerts might get closed too soon.|
|`createGitHubAnnotations`|no|"true"|By default, all detected violations are added as annotations to the pull request. You can disable this by setting FALSE. This can be useful if you are using another tool for this purpose.|
|`uploadSarifReport`|no|"true"|By default, the generated SARIF report will be uploaded as an artifact named "PMD Report". This can be disabled, e.g. if there are multiple executions on multiple os of this action.|
|`minimumPriority`|no|"5"|Rule priority threshold; rules with lower priority than configured here won't be shown. Valid values are integers between 1 (High) and 5 (Low). Numbers 1-5 correspond to priorities `High`, `Medium_High`, `Medium`, `Medium_Low`, `Low`.

## Outputs

Expand Down
74 changes: 59 additions & 15 deletions __tests__/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,16 @@ describe('pmd-github-action-util', function () {
'.',
'ruleset.xml',
'sarif',
'pmd-report.sarif'
'pmd-report.sarif',
'5'
)
const reportFile = path.join('.', 'pmd-report.sarif')
await expect(fs.access(reportFile)).resolves.toBe(undefined)
const report = JSON.parse(await fs.readFile(reportFile, 'utf8'))
expect(report.runs[0].tool.driver.version).toBe('6.40.0')
expect(execOutput.exitCode).toBe(0)
expect(execOutput.stdout.trim()).toBe(
'Running PMD 6.40.0 with: pmd -no-cache -d . -f sarif -R ruleset.xml -r pmd-report.sarif'
'Running PMD 6.40.0 with: pmd -no-cache -d . -f sarif -R ruleset.xml -r pmd-report.sarif --minimum-priority 5'
)
await io.rmRF(reportFile)
})
Expand All @@ -207,15 +208,16 @@ describe('pmd-github-action-util', function () {
'.',
'ruleset.xml',
'sarif',
'pmd-report.sarif'
'pmd-report.sarif',
'5'
)
const reportFile = path.join('.', 'pmd-report.sarif')
await expect(fs.access(reportFile)).resolves.toBe(undefined)
const report = JSON.parse(await fs.readFile(reportFile, 'utf8'))
expect(report.runs[0].tool.driver.version).toBe('6.41.0')
expect(execOutput.exitCode).toBe(0)
expect(execOutput.stdout.trim()).toBe(
'Running PMD 6.41.0 with: pmd --no-cache -d . -f sarif -R ruleset.xml -r pmd-report.sarif'
'Running PMD 6.41.0 with: pmd --no-cache -d . -f sarif -R ruleset.xml -r pmd-report.sarif --minimum-priority 5'
)
await io.rmRF(reportFile)
})
Expand All @@ -235,7 +237,8 @@ describe('pmd-github-action-util', function () {
'.',
'ruleset.xml',
'sarif',
'pmd-report.sarif'
'pmd-report.sarif',
'5'
)
).rejects.toThrow()
})
Expand All @@ -262,7 +265,8 @@ describe('pmd-github-action-util', function () {
'.',
'ruleset.xml',
'sarif',
'pmd-report.sarif'
'pmd-report.sarif',
'5'
)

expect(execMock).toHaveBeenCalledWith(
Expand All @@ -276,7 +280,9 @@ describe('pmd-github-action-util', function () {
'-R',
'ruleset.xml',
'-r',
'pmd-report.sarif'
'pmd-report.sarif',
'--minimum-priority',
'5'
],
{
ignoreReturnCode: true
Expand Down Expand Up @@ -414,15 +420,16 @@ describe('pmd-github-action-util', function () {
['src/file1.txt', 'src/file2.txt'],
'ruleset.xml',
'sarif',
'pmd-report.sarif'
'pmd-report.sarif',
'5'
)
const pmdFilelist = path.join('.', 'pmd.filelist')
await expect(fs.access(pmdFilelist)).resolves.toBe(undefined)
const pmdFilelistContent = await fs.readFile(pmdFilelist, 'utf8')
expect(pmdFilelistContent).toBe('src/file1.txt,src/file2.txt')
expect(execOutput.exitCode).toBe(0)
expect(execOutput.stdout.trim()).toBe(
'Running PMD 6.40.0 with: pmd -no-cache -filelist pmd.filelist -f sarif -R ruleset.xml -r pmd-report.sarif'
'Running PMD 6.40.0 with: pmd -no-cache -filelist pmd.filelist -f sarif -R ruleset.xml -r pmd-report.sarif --minimum-priority 5'
)
await io.rmRF(pmdFilelist)
await io.rmRF(path.join('.', 'pmd-report.sarif'))
Expand All @@ -446,15 +453,16 @@ describe('pmd-github-action-util', function () {
['src/file1.txt', 'src/file2.txt'],
'ruleset.xml',
'sarif',
'pmd-report.sarif'
'pmd-report.sarif',
'5'
)
const pmdFilelist = path.join('.', 'pmd.filelist')
await expect(fs.access(pmdFilelist)).resolves.toBe(undefined)
const pmdFilelistContent = await fs.readFile(pmdFilelist, 'utf8')
expect(pmdFilelistContent).toBe('src/file1.txt,src/file2.txt')
expect(execOutput.exitCode).toBe(0)
expect(execOutput.stdout.trim()).toBe(
'Running PMD 6.41.0 with: pmd --no-cache --file-list pmd.filelist -f sarif -R ruleset.xml -r pmd-report.sarif'
'Running PMD 6.41.0 with: pmd --no-cache --file-list pmd.filelist -f sarif -R ruleset.xml -r pmd-report.sarif --minimum-priority 5'
)
await io.rmRF(pmdFilelist)
await io.rmRF(path.join('.', 'pmd-report.sarif'))
Expand Down Expand Up @@ -647,20 +655,55 @@ describe('pmd-github-action-util', function () {
['src/file1.txt', 'src/file2.txt'],
'ruleset.xml',
'sarif',
'pmd-report.sarif'
'pmd-report.sarif',
'5'
)
const pmdFilelist = path.join('.', 'pmd.filelist')
await expect(fs.access(pmdFilelist)).resolves.toBe(undefined)
const pmdFilelistContent = await fs.readFile(pmdFilelist, 'utf8')
expect(pmdFilelistContent).toBe('src/file1.txt,src/file2.txt')
expect(execOutput.exitCode).toBe(0)
expect(execOutput.stdout.trim()).toBe(
'Running PMD 7.0.0-rc1 with: check --no-progress --no-cache --file-list pmd.filelist -f sarif -R ruleset.xml -r pmd-report.sarif'
'Running PMD 7.0.0-rc1 with: check --no-progress --no-cache --file-list pmd.filelist -f sarif -R ruleset.xml -r pmd-report.sarif --minimum-priority 5'
)
await io.rmRF(pmdFilelist)
await io.rmRF(path.join('.', 'pmd-report.sarif'))
})

test('can execute PMD 7 with custom priority', async () => {
fetchMock.get(
'https://api.github.com/repos/pmd/pmd/releases/tags/pmd_releases%2F7.0.0-rc1',
async () =>
JSON.parse(
await fs.readFile(`${__dirname}/data/releases-7.0.0-rc1.json`, 'utf8')
)
)
nock('https://github.com')
.get(
'/pmd/pmd/releases/download/pmd_releases/7.0.0-rc1/pmd-bin-7.0.0-rc1.zip'
)
.replyWithFile(200, `${__dirname}/data/pmd-bin-7.0.0-rc1.zip`)

const pmdInfo = await util.downloadPmd(
'7.0.0-rc1',
'my_test_token',
undefined
)
const execOutput = await util.executePmd(
pmdInfo,
['src/file1.txt', 'src/file2.txt'],
'ruleset.xml',
'sarif',
'pmd-report.sarif',
'4'
)

expect(execOutput.stdout.trim()).toBe(
'Running PMD 7.0.0-rc1 with: check --no-progress --no-cache --file-list pmd.filelist -f sarif -R ruleset.xml -r pmd-report.sarif --minimum-priority 4'
)
await io.rmRF(path.join('.', 'pmd-report.sarif'))
})

test('PMD 7 release candidates and final release version ordering', () => {
// see method util#isPmd7Cli
expect(semver.major('6.55.0') >= 7).toBe(false)
Expand Down Expand Up @@ -691,15 +734,16 @@ describe('pmd-github-action-util', function () {
'.',
'ruleset.xml',
'sarif',
'pmd-report.sarif'
'pmd-report.sarif',
'5'
)
const reportFile = path.join('.', 'pmd-report.sarif')
await expect(fs.access(reportFile)).resolves.toBe(undefined)
const report = JSON.parse(await fs.readFile(reportFile, 'utf8'))
expect(report.runs[0].tool.driver.version).toBe('7.0.0-SNAPSHOT')
expect(execOutput.exitCode).toBe(0)
expect(execOutput.stdout.trim()).toBe(
'Running PMD 7.0.0-SNAPSHOT with: check --no-progress --no-cache -d . -f sarif -R ruleset.xml -r pmd-report.sarif'
'Running PMD 7.0.0-SNAPSHOT with: check --no-progress --no-cache -d . -f sarif -R ruleset.xml -r pmd-report.sarif --minimum-priority 5'
)
await io.rmRF(reportFile)
})
Expand Down
13 changes: 11 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ inputs:
default: 'true'
createGitHubAnnotations:
description: >-
By default, all detected violations are added as annotations to the pull
request. You can disable this by setting FALSE. This can be useful if you
By default, all detected violations are added as annotations to the pull
request. You can disable this by setting FALSE. This can be useful if you
are using another tool for this purpose.
required: false
default: 'true'
Expand All @@ -90,6 +90,15 @@ inputs:
executions on multiple os of this action.
required: false
default: 'true'
minimumPriority:
description: >-
The lowest priority violation that will be reported. Must be a number 1-5
(no decimal). 1 will only show Priority 1 violations. 2 will show Priority
1 and 2, and so on. ("High" = 1, "Medium High" = 2, "Medium" = 3, "Medium
Low" = 4, "Low" = 5) The default is 5, which will show all priorities
(1-5).
required: false
default: '5'
outputs:
violations:
description: Number of violations found
Expand Down
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ async function main(): Promise<void> {
let modifiedFiles
let execOutput
let violations
let minimumPriority
const token = core.getInput('token', { required: false })
const sourcePath = validator.validateSourcePath(
core.getInput('sourcePath', { required: false })
Expand Down Expand Up @@ -40,12 +41,17 @@ async function main(): Promise<void> {
}
}

minimumPriority = validator.validateMinimumPriority(
core.getInput('minimumPriority', { required: false })
)

execOutput = await util.executePmd(
pmdInfo,
modifiedFiles || sourcePath,
validator.validateRulesets(core.getInput('rulesets', { required: true })),
reportFormat,
reportFile
reportFile,
minimumPriority
)

core.info(`PMD exited with ${execOutput.exitCode}`)
Expand Down
7 changes: 5 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ async function executePmd(
fileListOrSourcePath: string | string[],
ruleset: string,
reportFormat: string,
reportFile: string
reportFile: string,
minimumPriority: string
): Promise<ExecOutput> {
let pmdExecutable = '/bin/run.sh pmd'
if (isPmd7Cli(pmdInfo.version)) {
Expand Down Expand Up @@ -128,7 +129,9 @@ async function executePmd(
'-R',
ruleset,
'-r',
reportFile
reportFile,
'--minimum-priority',
minimumPriority
],
{
ignoreReturnCode: true
Expand Down
14 changes: 12 additions & 2 deletions src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function validateSourcePath(sourcePath: string): string {
typeof normalized !== 'string' ||
normalized.match(/[ ;:"'$]/)
) {
throw Error('Invalid sourcePath')
throw new Error('Invalid sourcePath')
}
return normalized
}
Expand All @@ -41,9 +41,19 @@ function validateDownloadUrl(url: string): string {
throw new Error('Invalid downloadUrl')
}

function validateMinimumPriority(priority: string): string {
const priorities = ['1', '2', '3', '4', '5']
if (typeof priority === 'string' && priorities.includes(priority)) {
// valid
return priority
}
throw new Error('Invalid minimum priority')
}

export {
validateVersion,
validateSourcePath,
validateRulesets,
validateDownloadUrl
validateDownloadUrl,
validateMinimumPriority
}