Skip to content

Commit b5bd94c

Browse files
authored
Add path prefix (#5)
* Adding prefix * Fix SARIF typo and add working dir * Update action.yml * Update action.yml * Update action.yml * Fixed jq expression * Fixed jq expression * Update README.md
1 parent d8b45b0 commit b5bd94c

2 files changed

Lines changed: 29 additions & 8 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Then, set up this Action as a step in your Actions workflow, e.g. for a typical
2727
* `java_distribution`: The Java distribution to use. Default: `microsoft`
2828
* `java_version`: The Java version to use. Default: `11`
2929
* `no_cache`: Do not use cached versions of the Spotbugs and FindSecBugs tools. Default: `false`
30+
* `path_prefix`: Add this path prefix to the start of file locations. Required: `false`
3031

3132
## Full sample workflow
3233

@@ -40,6 +41,9 @@ A: Several SpotBugs plugins are usable in CI/CD and Actions, but don't output SA
4041
Q: Why doesn't the Action support setting argument X of SpotBugs?
4142
A: It's a work-in-progress. Please raise an issue or a PR if you need a feature.
4243

44+
Q: Why do the files not resolve in the Code Scanning results?
45+
A: The paths in the Jar or Class file metadata might not match up with the root of the repository. Try using the input `path_prefix`. If two build targets don't share the same prefix, then try running this Action twice, once per target with a different prefix for each.
46+
4347
## Requirements
4448

4549
* GitHub Actions runner

action.yml

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ inputs:
4040
required: false
4141
type: boolean
4242
default: 'false'
43+
path_prefix:
44+
description: 'Add this path prefix to the start of file locations'
45+
required: false
4346

4447
runs:
4548
using: "composite"
@@ -67,10 +70,10 @@ runs:
6770
if: inputs.no_cache == 'true' || steps.cache-spotbugs.outputs.cache-hit != 'true'
6871
env:
6972
INPUT_SPOTBUGS_VERSION: ${{ inputs.spotbugs_version }}
73+
SPOTBUGS_HOME: /home/runner/work/spotbugs+/
7074
run: |
71-
cd /home/runner/work/
72-
mkdir -p 'spotbugs+'
73-
cd 'spotbugs+'
75+
mkdir -p "${SPOTBUGS_HOME}"
76+
cd "${SPOTBUGS_HOME}"
7477
wget -q -O spotbugs-"${INPUT_SPOTBUGS_VERSION}".tgz "https://github.com/spotbugs/spotbugs/releases/download/${INPUT_SPOTBUGS_VERSION}/spotbugs-${INPUT_SPOTBUGS_VERSION}.tgz"
7578
tar -xzf spotbugs-"${INPUT_SPOTBUGS_VERSION}".tgz
7679
chmod +x spotbugs-"${INPUT_SPOTBUGS_VERSION}"/bin/spotbugs
@@ -81,10 +84,10 @@ runs:
8184
if: inputs.no_cache == 'true' || steps.cache-findsecbugs.outputs.cache-hit != 'true'
8285
env:
8386
INPUT_FINDSECBUGS_VERSION: ${{ inputs.findsecbugs_version }}
87+
FINDSECBUGS_HOME: /home/runner/work/findsecbugs+/
8488
run: |
85-
cd /home/runner/work/
86-
mkdir -p 'findsecbugs+'
87-
cd 'findsecbugs+'
89+
mkdir -p "${FINDSECBUGS_HOME}"
90+
cd "${FINDSECBUGS_HOME}"
8891
wget -q -O "findsecbugs-plugin-${INPUT_FINDSECBUGS_VERSION}.jar" "https://search.maven.org/remotecontent?filepath=com/h3xstream/findsecbugs/findsecbugs-plugin/${INPUT_FINDSECBUGS_VERSION}/findsecbugs-plugin-${INPUT_FINDSECBUGS_VERSION}.jar"
8992
ls "findsecbugs-plugin-${INPUT_FINDSECBUGS_VERSION}.jar"
9093
echo "Got findsecbugs"
@@ -96,12 +99,26 @@ runs:
9699
INPUT_SPOTBUGS_GLOB: ${{ inputs.spotbugs_filename_glob }}
97100
SPOTBUGS_HOME: /home/runner/work/spotbugs+/spotbugs-${{ inputs.spotbugs_version }}
98101
FINDSECBUGS_HOME: /home/runner/work/findsecbugs+/
102+
SPOTBUGS_WORKING: /home/runner/work/spotbugs_working+/
99103
run: |
100-
SPOTBUGS_FILES=$(find "${INPUT_SPOTBUGS_TARGET}" -type f -name "${INPUT_SPOTBUGS_GLOB}" -exec echo -n {} \+)
104+
mkdir -p "${SPOTBUGS_WORKING}"
105+
cd "${SPOTBUGS_WORKING}"
106+
SPOTBUGS_FILES=$(find "${GITHUB_WORKSPACE}/${INPUT_SPOTBUGS_TARGET}" -type f -name "${INPUT_SPOTBUGS_GLOB}" -exec echo -n {} \+)
101107
"${SPOTBUGS_HOME}/bin/spotbugs" -textui -quiet -effort:max -low -bugCategories SECURITY -pluginList "${FINDSECBUGS_HOME}/findsecbugs-plugin-${INPUT_FINDSECBUGS_VERSION}.jar" -sarif=spotbugs.sarif ${SPOTBUGS_FILES}
102108
shell: bash
109+
- name: Adjust file paths
110+
if: inputs.path_prefix != null
111+
env:
112+
INPUT_PATH_PREFIX: ${{ inputs.path_prefix }}
113+
SPOTBUGS_WORKING: /home/runner/work/spotbugs_working+/
114+
run: |
115+
cd "${SPOTBUGS_WORKING}"
116+
jq --arg prefix "${INPUT_PATH_PREFIX}" '.runs[].results[].locations[].physicalLocation.artifactLocation.uri |= $prefix + .' < spotbugs.sarif > spotbugs_edited.sarif
117+
mv spotbugs.sarif spotbugs_orig.sarif
118+
mv spotbugs_edited.sarif spotbugs.sarif
119+
shell: bash
103120
- name: Upload SARIF file
104121
if: inputs.upload_sarif == 'true'
105122
uses: github/codeql-action/upload-sarif@v2
106123
with:
107-
sarif_file: spotbugs.sarif
124+
sarif_file: /home/runner/work/spotbugs_working+/spotbugs.sarif

0 commit comments

Comments
 (0)