Skip to content

Commit 82913c5

Browse files
authored
Merge pull request #176 from adangel/support-pmd7
Support upcoming PMD 7.0.0-rc1 release
2 parents a552efd + 909a119 commit 82913c5

File tree

16 files changed

+223
-10
lines changed

16 files changed

+223
-10
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ See also [Uploading a SARIF file to GitHub](https://docs.github.com/en/code-secu
7878
|------------|---|--------|---------------|
7979
|`token` |no |"github.token"|Personal access token (PAT) used to query the latest PMD release via api.github.com and to determine the modified files of a push/pull request (see option "analyzeModifiedFilesOnly").<br>By default the automatic token for GitHub Actions is used.<br>If this action is used in GHES environment (e.g. the baseUrl is not "api.github.com"), then the token is only used for querying the modified files of a push/pull request. The token won't be used to query the latest PMD release.<br>[Learn more about automatic token authentication](https://docs.github.com/en/actions/security-guides/automatic-token-authentication)<br>[Learn more about creating and using encrypted secrets](https://docs.github.com/en/actions/security-guides/encrypted-secrets)|
8080
|`version` |no |"latest"|PMD version to use. Using "latest" automatically downloads the latest version.<br>Available versions: <https://github.com/pmd/pmd/releases><br>Note: Only PMD 6.31.0 and later is supported due to required support for [Sarif report format](https://pmd.github.io/latest/pmd_userdocs_report_formats.html#sarif).|
81+
|`downloadUrl`|no|"" |Manually specify the download URL from where the PMD binary distribution will be downloaded. By default, this parameter is empty and the download URL is automatically determined by querying the PMD releases at <https://github.com/pmd/pmd/releases>.<br>This can be used to test PMD versions that are not official releases.<br>If a downloadUrl is specified, then the version must not be "latest". You need to specify a concrete version. The downloaded PMD won't be cached and will always be downloaded again.|
8182
|`sourcePath`|no |"." |Root directory for sources. Uses by default the current directory|
8283
|`rulesets` |yes| |Comma separated list of ruleset names to use.|
8384
|`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.|

action.yml

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,18 @@ description: Execute PMD static code analysis.
33
inputs:
44
token:
55
description: >-
6-
Personal access token (PAT) used to query the latest PMD release and the
7-
download URL for PMD.
6+
Personal access token (PAT) used to query the latest PMD release via
7+
api.github.com and to determine the modified files of a push/pull request
8+
(see option "analyzeModifiedFilesOnly").
89
9-
By default the automatic token for GitHub Actions is used. [Learn more
10-
about automatic token
10+
By default the automatic token for GitHub Actions is used.
11+
12+
If this action is used in GHES environment (e.g. the baseUrl is not
13+
"api.github.com"), then the token is only used for querying the modified
14+
files of a push/pull request. The token won't be used to query the latest
15+
PMD release.
16+
17+
[Learn more about automatic token
1118
authentication](https://docs.github.com/en/actions/security-guides/automatic-token-authentication)
1219
1320
[Learn more about creating and using encrypted
@@ -26,6 +33,20 @@ inputs:
2633
format](https://pmd.github.io/latest/pmd_userdocs_report_formats.html#sarif).
2734
required: false
2835
default: latest
36+
downloadUrl:
37+
description: >-
38+
Manually specify the download URL from where the PMD binary distribution
39+
will be downloaded. By default, this parameter is empty and the download
40+
URL is automatically determined by querying the PMD releases at
41+
<https://github.com/pmd/pmd/releases>.
42+
43+
This can be used to test PMD versions that are not official releases.
44+
45+
If a downloadUrl is specified, then the version must not be "latest". You
46+
need to specify a concrete version. The downloaded PMD won't be cached and
47+
will always be downloaded again.
48+
required: false
49+
default: ''
2950
sourcePath:
3051
description: Root directory for sources
3152
required: false

dist/index.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ async function main() {
1515
try {
1616
pmdInfo = await util.downloadPmd(
1717
validator.validateVersion(core.getInput('version'), { required: true }),
18-
token
18+
token,
19+
validator.validateDownloadUrl(core.getInput('downloadUrl'), { required: true })
1920
);
2021

2122
if (core.getInput('analyzeModifiedFilesOnly', { required: true }) === 'true') {

lib/util.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const { Octokit } = require('@octokit/rest');
1414
// repos/compareCommits API calls.
1515
const MAX_PAGE = 10;
1616

17-
const downloadPmd = async function(version, token) {
17+
async function downloadPmdRelease(version, token) {
1818
let pmdVersion = version;
1919
let cachedPmdPath = tc.find('pmd', version);
2020
core.debug(`cached path result: ${cachedPmdPath}`);
@@ -33,12 +33,45 @@ const downloadPmd = async function(version, token) {
3333
}
3434
}
3535

36+
async function downloadPmdUrl(version, downloadUrl) {
37+
let pmdVersion = version;
38+
const pathToZipDistribution = await tc.downloadTool(downloadUrl);
39+
const pmdExtractedFolder = await tc.extractZip(pathToZipDistribution);
40+
core.info(`Downloaded PMD ${pmdVersion} from ${downloadUrl} to ${pmdExtractedFolder}`);
41+
const files = await fs.readdir(pmdExtractedFolder);
42+
core.debug(`ZIP archive content: ${files}`);
43+
let subpath = files[0];
44+
core.debug(`Using the first entry as basepath for PMD: ${subpath}`)
45+
return {
46+
version: pmdVersion,
47+
path: path.join(pmdExtractedFolder, subpath)
48+
}
49+
}
50+
51+
const downloadPmd = async function(version, token, downloadUrl) {
52+
if (version === 'latest' && downloadUrl !== undefined && downloadUrl !== '')
53+
throw `Can't combine version=${version} with custom downloadUrl=${downloadUrl}`
54+
55+
if (downloadUrl === undefined || downloadUrl === '') {
56+
return downloadPmdRelease(version, token);
57+
} else {
58+
return downloadPmdUrl(version, downloadUrl);
59+
}
60+
}
61+
3662
const executePmd = async function(pmdInfo, fileListOrSourcePath, ruleset, reportFormat, reportFile) {
3763
let pmdExecutable = '/bin/run.sh pmd';
64+
if (isPmd7Cli(pmdInfo.version)) {
65+
pmdExecutable = '/bin/pmd';
66+
}
3867
if (os.platform() === 'win32') {
3968
pmdExecutable = '\\bin\\pmd.bat';
4069
}
4170

71+
if (isPmd7Cli(pmdInfo.version)) {
72+
pmdExecutable += ' check --no-progress';
73+
}
74+
4275
let sourceParameter = ['-d', fileListOrSourcePath];
4376
if (Array.isArray(fileListOrSourcePath)) {
4477
await writeFileList(fileListOrSourcePath);
@@ -69,6 +102,10 @@ function useNewArgsFormat(pmdVersion) {
69102
return semver.gte(pmdVersion, '6.41.0');
70103
}
71104

105+
function isPmd7Cli(pmdVersion) {
106+
return semver.major(pmdVersion) >= 7;
107+
}
108+
72109
async function determinePmdRelease(pmdVersion, token) {
73110
core.debug(`determine release info for ${pmdVersion}`);
74111

lib/validator.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,15 @@ const validateRulesets = function(rulesets) {
2828
return normalized;
2929
}
3030

31+
const validateDownloadUrl = function(url) {
32+
if (typeof(url) === 'string' && (url === '' || url.match(/^https?:\/\//)))
33+
// valid
34+
return url;
35+
36+
throw 'Invalid downloadUrl';
37+
}
38+
3139
module.exports.validateVersion = validateVersion;
3240
module.exports.validateSourcePath = validateSourcePath;
3341
module.exports.validateRulesets = validateRulesets;
34-
42+
module.exports.validateDownloadUrl = validateDownloadUrl;

tests/data/create-zips.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
zip -r pmd-bin-6.39.0.zip pmd-bin-6.39.0
44
zip -r pmd-bin-6.40.0.zip pmd-bin-6.40.0
55
zip -r pmd-bin-6.41.0.zip pmd-bin-6.41.0
6+
zip -r pmd-bin-7.0.0-rc1.zip pmd-bin-7.0.0-rc1
7+
zip -r pmd-bin-7.0.0-SNAPSHOT.zip pmd-bin-7.0.0-SNAPSHOT
1.06 KB
Binary file not shown.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env bash
2+
3+
echo "Running PMD 7.0.0-SNAPSHOT with: $@"
4+
5+
echo '{
6+
"runs": [
7+
{
8+
"tool": {
9+
"driver": {
10+
"name": "PMD",
11+
"version": "7.0.0-SNAPSHOT"
12+
}
13+
}
14+
}
15+
]
16+
}' > pmd-report.sarif
17+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
@echo off
2+
echo Running PMD 7.0.0-SNAPSHOT with: %*
3+
4+
(
5+
echo {
6+
echo "runs": [
7+
echo {
8+
echo "tool": {
9+
echo "driver": {
10+
echo "name": "PMD",
11+
echo "version": "7.0.0-SNAPSHOT"
12+
echo }
13+
echo }
14+
echo }
15+
echo ]
16+
echo }
17+
)>"pmd-report.sarif"

0 commit comments

Comments
 (0)