Skip to content

Commit ced7858

Browse files
authored
Optimize comment-on-asciidoc-changes.yml (#367)
* Optimize comment-on-asciidoc-changes.yml * Checkout correct commit when used with pull_request_target * Update .github/workflows/comment-on-asciidoc-changes.yml * Add error exit code if there are changes for the asciidoc comment workflow (#357) * Merge main
1 parent 0da3f8d commit ced7858

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

.github/workflows/comment-on-asciidoc-changes.yml

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ name: Comment on PR for .asciidoc changes
33
on:
44
workflow_call: ~
55

6+
permissions:
7+
contents: read
8+
pull-requests: write
9+
610
jobs:
711
comment-on-asciidoc-change:
812
runs-on: ubuntu-latest
@@ -11,19 +15,20 @@ jobs:
1115
- name: Checkout the repository
1216
uses: actions/checkout@v4
1317
with:
14-
fetch-depth: 0 # This is important to fetch all history
18+
# This is considered a security risk when used in conjunction with pull_request_target
19+
# However, we are not running any code from the PR, so it's safe
20+
# https://securitylab.github.com/resources/github-actions-preventing-pwn-requests/
21+
ref: ${{ github.event.pull_request.head.sha }}
1522

16-
- name: Check for changes in .asciidoc files
23+
- name: Get changed files
1724
id: check-files
18-
run: |
19-
git fetch origin ${{ github.base_ref }}
20-
if git diff --name-only origin/${{ github.base_ref }}..HEAD | grep -E '\.asciidoc$'; then
21-
echo "asciidoc_changed=true" >> $GITHUB_OUTPUT
22-
else
23-
echo "asciidoc_changed=false" >> $GITHUB_OUTPUT
24-
fi
25+
uses: tj-actions/changed-files@d6e91a2266cdb9d62096cebf1e8546899c6aa18f # v45.0.6
26+
with:
27+
files: |
28+
**/*.asciidoc
29+
2530
- name: Add a comment if .asciidoc files changed
26-
if: steps.check-files.outputs.asciidoc_changed == 'true'
31+
if: steps.check-files.outputs.any_changed == 'true'
2732
uses: actions/github-script@v6
2833
with:
2934
script: |
@@ -33,6 +38,7 @@ jobs:
3338
issue_number: context.payload.pull_request.number,
3439
body: 'It looks like this PR modifies one or more `.asciidoc` files. These files are being migrated to Markdown, and any changes merged now will be lost. See the [migration guide](https://elastic.github.io/docs-builder/migration/freeze/index.html) for details.'
3540
})
41+
3642
- name: Error if .asciidoc files changed
37-
if: steps.check-files.outputs.asciidoc_changed == 'true'
43+
if: steps.check-files.outputs.any_changed == 'true'
3844
run: exit 1

0 commit comments

Comments
 (0)