Skip to content

Commit

Permalink
ACS-5993 Classpath support (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
tpage-alfresco committed Nov 14, 2023
1 parent ed1c78b commit 47fdb05
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ steps:
pmd-ruleset-repo: "Alfresco/pmd-ruleset" # The GitHub repository containing the PMD ruleset (by default https://github.com/Alfresco/pmd-ruleset/).
pmd-ruleset-ref: "master" # The git reference (e.g. branch name, tag name or commit id) for the ruleset project.
pmd-ruleset-path: "pmd-ruleset.xml" # The path to the PMD ruleset file from the root of the ruleset project. Optionally other paths to local rulesets can be appended to this separated by commas.
classpath-enable: "true" # Whether to set the classpath before the scan (used by certain rules - for example MissingOverride). This assumes the project uses maven.
classpath-build-command: "mvn -ntp test-compile" # Command to build the class files so that the classpath can be used.
classpath-directory-list: "**/target/classes" # A colon-separated list of directories containing class files. Using wildcards (*) or globstar (**) is also supported in order to select items at one or many levels deep.
```

All parameters have default values and can be skipped.
Expand Down
21 changes: 20 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ inputs:
can be appended to this separated by commas.
required: false
default: pmd-ruleset.xml
classpath-enable:
description: |
Whether to set the classpath before the scan (used by certain rules - for example MissingOverride). This assumes
the project uses maven.
required: false
default: "true"
classpath-build-command:
description: Command to build the class files so that the classpath can be used.
required: false
default: mvn -ntp test-compile
classpath-directory-list:
description: |
A colon-separated list of directories containing class files. Using wildcards (*) or globstar (**) is
also supported in order to select items at one or many levels deep.
required: false
default: "**/target/classes"

runs:
using: "composite"
Expand Down Expand Up @@ -66,7 +82,10 @@ runs:
"pmd-ruleset/${{ inputs.pmd-ruleset-path }}" \
"origin/${{ github.base_ref }}" \
"origin/${{ github.head_ref }}" \
"${{ inputs.create-github-annotations }}"
"${{ inputs.create-github-annotations }}" \
"${{ inputs.classpath-enable }}" \
"${{ inputs.classpath-build-command }}" \
"${{ inputs.classpath-directory-list }}"
if: ${{ github.event_name == 'pull_request' }}
shell: bash
env:
Expand Down
38 changes: 38 additions & 0 deletions delta-scan.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,49 @@ target_ref=$2
head_ref=$3
# Whether to generate a sarif report for GitHub annotations.
generate_sarif=$4
# Whether to set the classpath before the scan.
classpath_enable=$5
# Command to build the class files so that the classpath can be used.
classpath_build_command=$6
# A colon-separated list of directories containing class files - may contain wildcards or globstar syntax (**).
classpath_directory_list=$7

# Requires PMD to have already been downloaded to this location.
run_pmd="/opt/hostedtoolcache/pmd/${PMD_VERSION}/x64/pmd-bin-${PMD_VERSION}/bin/pmd"

# Create a temporary directory for storing files.
tmp_dir=$(mktemp -d)

function set_classpath {
classpath_enable=$1
classpath_build_command=$2
classpath_directory_list=$3

if [[ ${classpath_enable} == "true" ]]
then
# Build the class files.
${classpath_build_command}
# Set the classpath.
classpath=""
old_ifs=$IFS
IFS=":"
shopt -s globstar
for file_glob in ${classpath_directory_list}
do
IFS=${old_ifs}
for directory in ${file_glob}
do
classpath="${classpath}:${directory}"
done
done
# Reset the bash settings.
IFS=${old_ifs}
shopt -u globstar
export CLASSPATH="${classpath:1}"
echo "CLASSPATH set to ${CLASSPATH}"
fi
}

# Create a list of the files changed by this PR.
baseline_ref=$(git merge-base "${target_ref}" "${head_ref}")
git diff --name-only ${baseline_ref} ${head_ref} > ${tmp_dir}/file-list.txt
Expand All @@ -36,6 +72,7 @@ do
old_file_count=$((old_file_count+1))
fi
done
set_classpath "${classpath_enable}" "${classpath_build_command}" "${classpath_directory_list}"
${run_pmd} check --cache ${tmp_dir}/pmd.cache --file-list ${tmp_dir}/old-files.txt -R ${ruleset_location} -r ${tmp_dir}/old_report.txt --no-fail-on-violation
old_issue_count=$(cat ${tmp_dir}/old_report.txt | wc -l)
echo "${old_issue_count} issue(s) found in ${old_file_count} old file(s) on ${baseline_ref}"
Expand All @@ -52,6 +89,7 @@ do
new_file_count=$((new_file_count+1))
fi
done
set_classpath "${classpath_enable}" "${classpath_build_command}" "${classpath_directory_list}"
if [[ "${generate_sarif}" == "true" ]]
then
echo "Generating sarif.json for GitHub annotations."
Expand Down

0 comments on commit 47fdb05

Please sign in to comment.