From 844193e4a2b9b26110c2ba593a48f4feeb576edc Mon Sep 17 00:00:00 2001 From: Pavel Raiskup Date: Fri, 6 Sep 2024 12:52:43 +0200 Subject: [PATCH] Calculate the previous commit to diff against The github.event.pull_request.base.sha provides a wrong commit SHA pointing at the *current* upstream HEAD. This particular one may not be even fetched (a concurrent PR merged in the meantime). The GitHub Context provides no info about the previous commit SHA we need, so let's calculate it manually from the ${{ github.event.commits }} number --- action.yml | 8 ++++---- container/cmd | 5 ++++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/action.yml b/action.yml index 9655516..cc1ef9c 100644 --- a/action.yml +++ b/action.yml @@ -55,10 +55,10 @@ inputs: default: ${{ github.event.pull_request.head.sha }} required: false - pull_request_base: - description: "The commit right before the pull-request commits (intenral)" - default: ${{ github.event.pull_request.base.sha }} - required: true + pull_request_commits: + description: "Number of commits in this PR" + required: false + default: ${{ github.event.commits }} outputs: sarif: diff --git a/container/cmd b/container/cmd index 72bde58..4b2b4f5 100755 --- a/container/cmd +++ b/container/cmd @@ -42,7 +42,6 @@ set_linter_options() for tag in $INPUT_LINTER_TAGS; do linter_options+=( --linter-tag "$tag" ) done - linter_options+=( --compare-against "$INPUT_PULL_REQUEST_BASE" ) } populate_sarif_data() @@ -89,6 +88,10 @@ if test "$GITHUB_EVENT_NAME" = 'pull_request'; then # We don't want to rebase on top of the current upstream's head; the line # numbers would be shifted and the provided output would be misleading. + # Get the last commit before this PR. + last_committed=$(git rev-parse "HEAD~$INPUT_PULL_REQUEST_COMMITS") + linter_options+=( --compare-against "$last_committed" ) + test -z "$INPUT_SUBDIRECTORY" || INPUT_SUBDIRECTORIES=$INPUT_SUBDIRECTORY test -z "$INPUT_SUBDIRECTORIES" && INPUT_SUBDIRECTORIES=.