Skip to content

Commit

Permalink
Diff behaviour changed on push events
Browse files Browse the repository at this point in the history
It now checks the commits between the before and after of a push, not the branches.
  • Loading branch information
Clint-Chester authored Aug 14, 2021
1 parent d586a82 commit 8b9a97b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
17 changes: 9 additions & 8 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ outputs:
runs:
using: "composite"
steps:
- id: branches
- id: code
run: |
if [ ${{ github.event_name }} == 'pull_request' ]; then
echo "::set-output name=target::${{ github.base_ref }}"
echo "::set-output name=source::${{ github.head_ref }}"
echo "::set-output name=current_code::${{ github.base_ref }}"
echo "::set-output name=changed_code::${{ github.head_ref }}"
else
echo "::set-output name=target::${{ github.event.repository.default_branch }}"
echo "::set-output name=source::${{ github.ref }}"
echo "::set-output name=current_code::${{ github.event.before }}"
echo "::set-output name=changed_code::${{ github.event.after }}"
fi
shell: bash
- id: pmd-analysis
Expand All @@ -56,12 +56,13 @@ runs:
FILE_PATH: ${{ inputs.file-path }}
RULES_PATH: ${{ inputs.rules-path }}
ANALYSE_ALL_CODE: ${{ inputs.analyse-all-code }}
TARGET_BRANCH: ${{ steps.branches.outputs.target }}
SOURCE_BRANCH: ${{ steps.branches.outputs.source }}
CURRENT_CODE: ${{ steps.code.outputs.current_code }}
CHANGED_CODE: ${{ steps.code.outputs.changed_code }}
ERROR_RULES: ${{ inputs.error-rules }}
NOTE_RULES: ${{ inputs.note-rules }}
REPO_NAME: ${{ github.event.repository.full_name }}
PR_NUMBER: ${{ github.event.number }}
AUTH_TOKEN: ${{ inputs.auth-token }}
FILE_DIFF_TYPE: ${{ inputs.file-diff-type }}
WORKSPACE: ${{ github.workspace }}/
WORKSPACE: ${{ github.workspace }}/
ACTION_EVENT_NAME: ${{ github.event_name }}
15 changes: 10 additions & 5 deletions pmd-analyser.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ unzip pmd-bin-"${PMD_VERSION}".zip
if [ "$ANALYSE_ALL_CODE" == 'true' ]; then
pmd-bin-"${PMD_VERSION}"/bin/run.sh pmd -d "$FILE_PATH" -R "$RULES_PATH" -failOnViolation false -f sarif > pmd-raw-output.sarif
else
# Now to determine whether to get the files changed from a git diff or using the files changed in a GitHub Pull Request
# Both options will generate a CSV file first with the files changed
if [ "$FILE_DIFF_TYPE" == 'git' ]; then
git diff --name-only --diff-filter=d origin/"$TARGET_BRANCH"..origin/"${SOURCE_BRANCH#"refs/heads/"}" | paste -s -d "," >> diff-file.csv
if [ "$ACTION_EVENT_NAME" == 'pull_request' ]; then
# Now to determine whether to get the files changed from a git diff or using the files changed in a GitHub Pull Request
# Both options will generate a CSV file first with the files changed
if [ "$FILE_DIFF_TYPE" == 'git' ]; then
git diff --name-only --diff-filter=d origin/"$CURRENT_CODE"..origin/"${CHANGED_CODE#"refs/heads/"}" | paste -s -d "," >> diff-file.csv
else
curl -H "Accept: application/vnd.github.v3+json" -H "Authorization: token ${AUTH_TOKEN}" https://api.github.com/repos/"$REPO_NAME"/pulls/"$PR_NUMBER"/files | jq --raw-output '.[] .filename' | paste -s -d "," >> diff-file.csv
fi
else
curl -H "Accept: application/vnd.github.v3+json" -H "Authorization: token ${AUTH_TOKEN}" https://api.github.com/repos/"$REPO_NAME"/pulls/"$PR_NUMBER"/files | jq --raw-output '.[] .filename' | paste -s -d "," >> diff-file.csv
# Irrespective of the file type diff selected on a push event, we will always do a git diff (as we can't get that from the GitHub API)
git diff --name-only --diff-filter=d "$CURRENT_CODE".."$CHANGED_CODE" | paste -s -d "," >> diff-file.csv
fi
# Run the analysis
pmd-bin-"${PMD_VERSION}"/bin/run.sh pmd -filelist diff-file.csv -R "$RULES_PATH" -failOnViolation false -f sarif > pmd-raw-output.sarif
Expand Down

0 comments on commit 8b9a97b

Please sign in to comment.