Skip to content

Commit ff712bc

Browse files
authored
fix: correctly handle pull_request_target event (community-scripts#1327)
* Fix workflow on pull_request_target * fix fromJSON
1 parent 9330e9a commit ff712bc

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

.github/workflows/validate-filenames.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ jobs:
1616

1717
steps:
1818
- name: Get pull request information
19+
if: github.event_name == 'pull_request_target'
1920
uses: actions/github-script@v7
2021
id: pr
2122
with:
@@ -30,13 +31,13 @@ jobs:
3031
uses: actions/checkout@v4
3132
with:
3233
fetch-depth: 0 # Ensure the full history is fetched for accurate diffing
33-
ref: ${{ fromJSON(steps.pr.outputs.result).merge_commit_sha }}
34+
ref: ${{ github.event_name == 'pull_request_target' && fromJSON(steps.pr.outputs.result).merge_commit_sha || '' }}
3435

3536
- name: Get changed files
3637
id: changed-files
3738
run: |
38-
if ${{ github.event_name == 'pull_request' }}; then
39-
echo "files=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.sha }} | xargs)" >> $GITHUB_OUTPUT
39+
if ${{ github.event_name == 'pull_request_target' }}; then
40+
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
4041
else
4142
echo "files=$(git diff --name-only ${{ github.event.before }} ${{ github.event.after }} | xargs)" >> $GITHUB_OUTPUT
4243
fi
@@ -88,7 +89,7 @@ jobs:
8889
fi
8990
9091
- name: Post results and comment
91-
if: always() && steps.check-scripts.outputs.files != '' && steps.check-json.outputs.files != '' && github.event_name == 'pull_request'
92+
if: always() && steps.check-scripts.outputs.files != '' && steps.check-json.outputs.files != '' && github.event_name == 'pull_request_target'
9293
uses: actions/github-script@v7
9394
with:
9495
script: |

.github/workflows/validate-formatting.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ jobs:
1818

1919
steps:
2020
- name: Get pull request information
21+
if: github.event_name == 'pull_request_target'
2122
uses: actions/github-script@v7
2223
id: pr
2324
with:
@@ -32,13 +33,13 @@ jobs:
3233
uses: actions/checkout@v4
3334
with:
3435
fetch-depth: 0 # Ensure the full history is fetched for accurate diffing
35-
ref: ${{ fromJSON(steps.pr.outputs.result).merge_commit_sha }}
36+
ref: ${{ github.event_name == 'pull_request_target' && fromJSON(steps.pr.outputs.result).merge_commit_sha || '' }}
3637

3738
- name: Get changed files
3839
id: changed-files
3940
run: |
4041
if ${{ github.event_name == 'pull_request' }}; then
41-
echo "files=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.sha }} | grep -E '\.(sh|func)$' | xargs)" >> $GITHUB_OUTPUT
42+
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
4243
else
4344
echo "files=$(git diff --name-only ${{ github.event.before }} ${{ github.event.after }} | grep -E '\.(sh|func)$' | xargs)" >> $GITHUB_OUTPUT
4445
fi

.github/workflows/validate-scripts.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ jobs:
1717

1818
steps:
1919
- name: Get pull request information
20+
if: github.event_name == 'pull_request_target'
2021
uses: actions/github-script@v7
2122
id: pr
2223
with:
@@ -31,7 +32,7 @@ jobs:
3132
uses: actions/checkout@v4
3233
with:
3334
fetch-depth: 0 # Ensure the full history is fetched for accurate diffing
34-
ref: ${{fromJSON(steps.pr.outputs.result).merge_commit_sha}}
35+
ref: ${{ github.event_name == 'pull_request_target' && fromJSON(steps.pr.outputs.result).merge_commit_sha || '' }}
3536

3637
- name: Set execute permission for .sh files
3738
run: |
@@ -40,8 +41,8 @@ jobs:
4041
- name: Get changed files
4142
id: changed-files
4243
run: |
43-
if ${{ github.event_name == 'pull_request' }}; then
44-
echo "files=$(git diff --name-only -r HEAD^1 HEAD | grep -E '\.(sh|func)$' | xargs)" >> $GITHUB_OUTPUT
44+
if ${{ github.event_name == 'pull_request_target' }}; then
45+
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
4546
else
4647
echo "files=$(git diff --name-only ${{ github.event.before }} ${{ github.event.after }} | grep -E '\.(sh|func)$' | xargs)" >> $GITHUB_OUTPUT
4748
fi
@@ -167,7 +168,7 @@ jobs:
167168
fi
168169
169170
- name: Post results and comment
170-
if: always() && steps.changed-files.outputs.files != '' && github.event_name == 'pull_request'
171+
if: always() && steps.changed-files.outputs.files != '' && github.event_name == 'pull_request_target'
171172
uses: actions/github-script@v7
172173
with:
173174
script: |

0 commit comments

Comments
 (0)