From ff712bc36e5b1ff0080876b3d379a96e635f8dbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastiaan?= Date: Wed, 8 Jan 2025 16:33:09 +0100 Subject: [PATCH] fix: correctly handle pull_request_target event (#1327) * Fix workflow on pull_request_target * fix fromJSON --- .github/workflows/validate-filenames.yml | 9 +++++---- .github/workflows/validate-formatting.yaml | 5 +++-- .github/workflows/validate-scripts.yml | 9 +++++---- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/.github/workflows/validate-filenames.yml b/.github/workflows/validate-filenames.yml index d4dffd8e..909c669d 100644 --- a/.github/workflows/validate-filenames.yml +++ b/.github/workflows/validate-filenames.yml @@ -16,6 +16,7 @@ jobs: steps: - name: Get pull request information + if: github.event_name == 'pull_request_target' uses: actions/github-script@v7 id: pr with: @@ -30,13 +31,13 @@ jobs: uses: actions/checkout@v4 with: fetch-depth: 0 # Ensure the full history is fetched for accurate diffing - ref: ${{ fromJSON(steps.pr.outputs.result).merge_commit_sha }} + ref: ${{ github.event_name == 'pull_request_target' && fromJSON(steps.pr.outputs.result).merge_commit_sha || '' }} - name: Get changed files id: changed-files run: | - if ${{ github.event_name == 'pull_request' }}; then - echo "files=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.sha }} | xargs)" >> $GITHUB_OUTPUT + if ${{ github.event_name == 'pull_request_target' }}; then + echo "files=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ steps.pr.outputs.result && fromJSON(steps.pr.outputs.result).merge_commit_sha }} | xargs)" >> $GITHUB_OUTPUT else echo "files=$(git diff --name-only ${{ github.event.before }} ${{ github.event.after }} | xargs)" >> $GITHUB_OUTPUT fi @@ -88,7 +89,7 @@ jobs: fi - name: Post results and comment - if: always() && steps.check-scripts.outputs.files != '' && steps.check-json.outputs.files != '' && github.event_name == 'pull_request' + if: always() && steps.check-scripts.outputs.files != '' && steps.check-json.outputs.files != '' && github.event_name == 'pull_request_target' uses: actions/github-script@v7 with: script: | diff --git a/.github/workflows/validate-formatting.yaml b/.github/workflows/validate-formatting.yaml index cde44d6a..c0b6ef3b 100644 --- a/.github/workflows/validate-formatting.yaml +++ b/.github/workflows/validate-formatting.yaml @@ -18,6 +18,7 @@ jobs: steps: - name: Get pull request information + if: github.event_name == 'pull_request_target' uses: actions/github-script@v7 id: pr with: @@ -32,13 +33,13 @@ jobs: uses: actions/checkout@v4 with: fetch-depth: 0 # Ensure the full history is fetched for accurate diffing - ref: ${{ fromJSON(steps.pr.outputs.result).merge_commit_sha }} + ref: ${{ github.event_name == 'pull_request_target' && fromJSON(steps.pr.outputs.result).merge_commit_sha || '' }} - name: Get changed files id: changed-files run: | if ${{ github.event_name == 'pull_request' }}; then - echo "files=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.sha }} | grep -E '\.(sh|func)$' | xargs)" >> $GITHUB_OUTPUT + echo "files=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ steps.pr.outputs.result && fromJSON(steps.pr.outputs.result).merge_commit_sha }} | grep -E '\.(sh|func)$' | xargs)" >> $GITHUB_OUTPUT else echo "files=$(git diff --name-only ${{ github.event.before }} ${{ github.event.after }} | grep -E '\.(sh|func)$' | xargs)" >> $GITHUB_OUTPUT fi diff --git a/.github/workflows/validate-scripts.yml b/.github/workflows/validate-scripts.yml index e7ee667d..0df07a74 100644 --- a/.github/workflows/validate-scripts.yml +++ b/.github/workflows/validate-scripts.yml @@ -17,6 +17,7 @@ jobs: steps: - name: Get pull request information + if: github.event_name == 'pull_request_target' uses: actions/github-script@v7 id: pr with: @@ -31,7 +32,7 @@ jobs: uses: actions/checkout@v4 with: fetch-depth: 0 # Ensure the full history is fetched for accurate diffing - ref: ${{fromJSON(steps.pr.outputs.result).merge_commit_sha}} + ref: ${{ github.event_name == 'pull_request_target' && fromJSON(steps.pr.outputs.result).merge_commit_sha || '' }} - name: Set execute permission for .sh files run: | @@ -40,8 +41,8 @@ jobs: - name: Get changed files id: changed-files run: | - if ${{ github.event_name == 'pull_request' }}; then - echo "files=$(git diff --name-only -r HEAD^1 HEAD | grep -E '\.(sh|func)$' | xargs)" >> $GITHUB_OUTPUT + if ${{ github.event_name == 'pull_request_target' }}; then + echo "files=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ steps.pr.outputs.result && fromJSON(steps.pr.outputs.result).merge_commit_sha }} | xargs)" >> $GITHUB_OUTPUT else echo "files=$(git diff --name-only ${{ github.event.before }} ${{ github.event.after }} | grep -E '\.(sh|func)$' | xargs)" >> $GITHUB_OUTPUT fi @@ -167,7 +168,7 @@ jobs: fi - name: Post results and comment - if: always() && steps.changed-files.outputs.files != '' && github.event_name == 'pull_request' + if: always() && steps.changed-files.outputs.files != '' && github.event_name == 'pull_request_target' uses: actions/github-script@v7 with: script: |