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

Avoid using deprecated CLI options for PMD >= 6.41.0 #17

Merged
merged 1 commit into from
Dec 10, 2021
Merged
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
6 changes: 3 additions & 3 deletions dist/index.js

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions dist/licenses.txt
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,25 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.


lru-cache
ISC
The ISC License

Copyright (c) Isaac Z. Schlueter and Contributors

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.


minimatch
ISC
The ISC License
Expand Down Expand Up @@ -1011,3 +1030,22 @@ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.


yallist
ISC
The ISC License

Copyright (c) Isaac Z. Schlueter and Contributors

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
9 changes: 4 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@ const validator = require('../lib/validator');
const reportFormat = 'sarif';
const reportFile = 'pmd-report.sarif'

let pmdPath, exitCode, violations;

async function run() {
let pmdInfo, execOutput, violations;
try {
pmdPath = await util.downloadPmd(validator.validateVersion(core.getInput('version'), { required: true }));
exitCode = await util.executePmd(pmdPath,
pmdInfo = await util.downloadPmd(validator.validateVersion(core.getInput('version'), { required: true }));
execOutput = await util.executePmd(pmdInfo,
validator.validateSourcePath(core.getInput('sourcePath', { required: true })),
validator.validateRulesets(core.getInput('rulesets', { required: true })),
reportFormat, reportFile)

core.info(`PMD exited with ${exitCode}`);
core.info(`PMD exited with ${execOutput.exitCode}`);

violations = sarif.countViolations(reportFile);
core.setOutput('violations', violations);
Expand Down
24 changes: 16 additions & 8 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const core = require('@actions/core');
const { Octokit } = require("@octokit/rest");
const tc = require('@actions/tool-cache');
const exec = require('@actions/exec');
const semver = require('semver');

const downloadPmd = async function(version) {
try {
Expand All @@ -17,18 +18,21 @@ const downloadPmd = async function(version) {
}

core.info(`Using PMD ${pmdVersion} from cached path ${cachedPmdPath}`);
return `${cachedPmdPath}/pmd-bin-${pmdVersion}`;
return {
version: pmdVersion,
path: `${cachedPmdPath}/pmd-bin-${pmdVersion}`
};
} catch (error) {
core.setFailed(error.message || error);
}
}

const executePmd = async function(pmdPath, sourcePath, ruleset, reportFormat, reportFile) {
const executePmd = async function(pmdInfo, sourcePath, ruleset, reportFormat, reportFile) {
try {
const { exitCode, stdout, stderr } = await exec.getExecOutput(`${pmdPath}/bin/run.sh`,
const execOutput = await exec.getExecOutput(`${pmdInfo.path}/bin/run.sh`,
[
'pmd',
'-no-cache',
useNewArgsFormat(pmdInfo.version) ? '--no-cache' : '-no-cache',
'-d', sourcePath,
'-f', reportFormat,
'-R', ruleset,
Expand All @@ -37,15 +41,19 @@ const executePmd = async function(pmdPath, sourcePath, ruleset, reportFormat, re
{
ignoreReturnCode: true
});
core.debug(`stdout: ${stdout}`);
core.debug(`stderr: ${stderr}`);
core.debug(`exitCode: ${exitCode}`);
return exitCode;
core.debug(`stdout: ${execOutput.stdout}`);
core.debug(`stderr: ${execOutput.stderr}`);
core.debug(`exitCode: ${execOutput.exitCode}`);
return execOutput;
} catch (error) {
core.setFailed(error.message || error);
}
}

function useNewArgsFormat(pmdVersion) {
return semver.gte(pmdVersion, '6.41.0');
}

async function determinePmdRelease(pmdVersion) {
core.debug(`determine release info for ${pmdVersion}`);
const octokit = new Octokit({baseUrl: getGithubBaseUrl()});
Expand Down
3 changes: 2 additions & 1 deletion lib/validator.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const path = require('path');
const semver = require('semver');

const validateVersion = function(version) {
if (typeof(version) === 'string'
&& (version === 'latest' || version.match(/^\d+\.\d+\.\d+$/))) {
&& (version === 'latest' || semver.valid(version) === version)) {
// valid
return version;
}
Expand Down
13 changes: 4 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"@actions/core": "^1.6.0",
"@actions/exec": "^1.1.0",
"@actions/tool-cache": "^1.7.1",
"@octokit/rest": "^18.12.0"
"@octokit/rest": "^18.12.0",
"semver": "^7.3.5"
},
"devDependencies": {
"@actions/io": "^1.1.1",
Expand Down
1 change: 1 addition & 0 deletions tests/data/create-zips.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

zip -r pmd-bin-6.39.0.zip pmd-bin-6.39.0
zip -r pmd-bin-6.40.0.zip pmd-bin-6.40.0
zip -r pmd-bin-6.41.0.zip pmd-bin-6.41.0
Binary file added tests/data/pmd-bin-6.41.0.zip
Binary file not shown.
17 changes: 17 additions & 0 deletions tests/data/pmd-bin-6.41.0/bin/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash

echo "Running PMD 6.41.0 with: $@"

echo '{
"runs": [
{
"tool": {
"driver": {
"name": "PMD",
"version": "6.41.0"
}
}
}
]
}' > pmd-report.sarif

11 changes: 11 additions & 0 deletions tests/data/releases-6.41.0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"id": "1",
"name": "pmd release for test (6.41.0)",
"tag_name": "pmd_releases/6.41.0",
"assets": [
{
"browser_download_url": "https://github.com/pmd/pmd/releases/download/pmd_releases/6.41.0/pmd-bin-6.41.0.zip",
"name": "pmd-bin-6.41.0.zip"
}
]
}
63 changes: 43 additions & 20 deletions tests/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ const tempPath = path.join(__dirname, 'TEMP')
process.env['RUNNER_TEMP'] = tempPath
process.env['RUNNER_TOOL_CACHE'] = cachePath

describe('pmd-github-action-util', function() {
beforeAll(function() {
describe('pmd-github-action-util', function () {
beforeAll(function () {
setGlobal('TEST_DOWNLOAD_TOOL_RETRY_MIN_SECONDS', 0)
setGlobal('TEST_DOWNLOAD_TOOL_RETRY_MAX_SECONDS', 0)
})

beforeEach(async function() {
beforeEach(async function () {
await io.rmRF(cachePath)
await io.rmRF(tempPath)
await io.mkdirP(cachePath)
await io.mkdirP(tempPath)
})

afterEach(function() {
afterEach(function () {
})

afterAll(async function() {
afterAll(async function () {
await io.rmRF(tempPath)
await io.rmRF(cachePath)
setGlobal('TEST_DOWNLOAD_TOOL_RETRY_MIN_SECONDS', undefined)
Expand All @@ -38,52 +38,75 @@ describe('pmd-github-action-util', function() {
it('use latest PMD', async () => {
nock('https://api.github.com')
.get('/repos/pmd/pmd/releases/latest')
.replyWithFile(200, __dirname + '/data/releases-latest.json', {
'Content-Type': 'application/json',
})
.replyWithFile(200, __dirname + '/data/releases-latest.json', {
'Content-Type': 'application/json',
})
nock('https://github.com')
.get('/pmd/pmd/releases/download/pmd_releases/6.40.0/pmd-bin-6.40.0.zip')
.replyWithFile(200, __dirname + '/data/pmd-bin-6.40.0.zip')

const cachedPmdPath = await util.downloadPmd('latest');
const pmdInfo = await util.downloadPmd('latest');

const toolCache = path.join(cachePath, 'pmd', '6.40.0', os.arch(), 'pmd-bin-6.40.0');
expect(cachedPmdPath).toBe(toolCache);
expect(pmdInfo).toStrictEqual({ path: toolCache, version: '6.40.0' });
expect(fs.existsSync(toolCache)).toBeTruthy();
})

it('use specific PMD version', async () => {
nock('https://api.github.com')
.get('/repos/pmd/pmd/releases/tags/pmd_releases%2F6.39.0')
.replyWithFile(200, __dirname + '/data/releases-6.39.0.json', {
'Content-Type': 'application/json',
})
.replyWithFile(200, __dirname + '/data/releases-6.39.0.json', {
'Content-Type': 'application/json',
})
nock('https://github.com')
.get('/pmd/pmd/releases/download/pmd_releases/6.39.0/pmd-bin-6.39.0.zip')
.replyWithFile(200, __dirname + '/data/pmd-bin-6.39.0.zip');
const cachedPmdPath = await util.downloadPmd('6.39.0');
const pmdInfo = await util.downloadPmd('6.39.0');

const toolCache = path.join(cachePath, 'pmd', '6.39.0', os.arch(), 'pmd-bin-6.39.0');
expect(cachedPmdPath).toBe(toolCache);
expect(pmdInfo).toStrictEqual({ path: toolCache, version: '6.39.0' });
expect(fs.existsSync(toolCache)).toBeTruthy();
})

it('can execute PMD', async () => {
nock('https://api.github.com')
.get('/repos/pmd/pmd/releases/latest')
.replyWithFile(200, __dirname + '/data/releases-latest.json', {
'Content-Type': 'application/json',
})
.replyWithFile(200, __dirname + '/data/releases-latest.json', {
'Content-Type': 'application/json',
})
nock('https://github.com')
.get('/pmd/pmd/releases/download/pmd_releases/6.40.0/pmd-bin-6.40.0.zip')
.replyWithFile(200, __dirname + '/data/pmd-bin-6.40.0.zip')

const cachedPmdPath = await util.downloadPmd('latest');
await util.executePmd(cachedPmdPath, '.', 'ruleset.xml', 'sarif', 'pmd-report.sarif');
const pmdInfo = await util.downloadPmd('latest');
const execOutput = await util.executePmd(pmdInfo, '.', 'ruleset.xml', 'sarif', 'pmd-report.sarif');
const reportFile = path.join('.', 'pmd-report.sarif');
expect(fs.existsSync(reportFile)).toBeTruthy();
const report = JSON.parse(fs.readFileSync(reportFile));
expect(report.runs[0].tool.driver.version).toBe('6.40.0');
expect(execOutput.exitCode).toBe(0);
expect(execOutput.stdout).toBe('Running PMD 6.40.0 with: pmd -no-cache -d . -f sarif -R ruleset.xml -r pmd-report.sarif\n');
await io.rmRF(reportFile)
})

it('can execute PMD >= 6.41.0', async () => {
nock('https://api.github.com')
.get('/repos/pmd/pmd/releases/tags/pmd_releases%2F6.41.0')
.replyWithFile(200, __dirname + '/data/releases-6.41.0.json', {
'Content-Type': 'application/json',
})
nock('https://github.com')
.get('/pmd/pmd/releases/download/pmd_releases/6.41.0/pmd-bin-6.41.0.zip')
.replyWithFile(200, __dirname + '/data/pmd-bin-6.41.0.zip')

const pmdInfo = await util.downloadPmd('6.41.0');
const execOutput = await util.executePmd(pmdInfo, '.', 'ruleset.xml', 'sarif', 'pmd-report.sarif');
const reportFile = path.join('.', 'pmd-report.sarif');
expect(fs.existsSync(reportFile)).toBeTruthy();
const report = JSON.parse(fs.readFileSync(reportFile));
expect(report.runs[0].tool.driver.version).toBe('6.41.0');
expect(execOutput.exitCode).toBe(0);
expect(execOutput.stdout).toBe('Running PMD 6.41.0 with: pmd --no-cache -d . -f sarif -R ruleset.xml -r pmd-report.sarif\n');
await io.rmRF(reportFile)
})
});
Expand Down