Skip to content

Commit ab25581

Browse files
committed
changed the script restore method to the path filtring
1 parent 90d4ebc commit ab25581

File tree

1 file changed

+35
-32
lines changed

1 file changed

+35
-32
lines changed

.github/workflows/apply-test-ids.yml

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ name: apply-test-ids
33
on:
44
pull_request_target:
55
types: [opened, synchronize]
6+
paths:
7+
- 'tests/**/*.json'
68

79
permissions:
810
contents: write
@@ -13,51 +15,50 @@ jobs:
1315
runs-on: ubuntu-latest
1416

1517
steps:
16-
# 1. Checkout base branch (trusted code )
17-
- name: Checkout base branch
18-
uses: actions/checkout@v4
18+
# 1. Detect what changed in this PR
19+
- name: Check changed paths
20+
uses: dorny/paths-filter@v3
21+
id: filter
1922
with:
20-
ref: ${{ github.event.pull_request.base.ref }}
21-
fetch-depth: 0
22-
23-
# 2. Save trusted scripts before checking out PR code
24-
- name: Save base automation scripts
23+
filters: |
24+
tests:
25+
- 'tests/**/*.json'
26+
scripts:
27+
- 'scripts/**'
28+
- 'package.json'
29+
- 'package-lock.json'
30+
31+
# 2. Fail if both tests AND scripts changed together
32+
- name: Block mixed changes
33+
if: steps.filter.outputs.tests == 'true' && steps.filter.outputs.scripts == 'true'
2534
run: |
26-
mkdir -p /tmp/base-scripts/scripts/utils
27-
cp scripts/add-test-ids.js /tmp/base-scripts/scripts/add-test-ids.js
28-
cp scripts/normalize.js /tmp/base-scripts/scripts/normalize.js
29-
cp scripts/load-remotes.js /tmp/base-scripts/scripts/load-remotes.js
30-
cp scripts/utils/generateTestIds.js /tmp/base-scripts/scripts/utils/generateTestIds.js
31-
cp scripts/utils/jsonfiles.js /tmp/base-scripts/scripts/utils/jsonfiles.js
32-
33-
# 3. Checkout PR branch (contributor test files)
34-
- name: Checkout PR branch
35+
echo "This PR changes both test files and scripts at the same time."
36+
echo "Please split them into separate PRs."
37+
exit 1
38+
39+
# 3. Checkout repo
40+
- name: Checkout
41+
if: steps.filter.outputs.tests == 'true'
3542
uses: actions/checkout@v4
3643
with:
3744
ref: ${{ github.event.pull_request.head.sha }}
3845
fetch-depth: 0
3946

40-
# 4. Restore trusted scripts over PR code (prevents script injection)
41-
- name: Restore trusted scripts
42-
run: |
43-
cp /tmp/base-scripts/scripts/add-test-ids.js scripts/add-test-ids.js
44-
cp /tmp/base-scripts/scripts/normalize.js scripts/normalize.js
45-
cp /tmp/base-scripts/scripts/load-remotes.js scripts/load-remotes.js
46-
cp /tmp/base-scripts/scripts/utils/generateTestIds.js scripts/utils/generateTestIds.js
47-
cp /tmp/base-scripts/scripts/utils/jsonfiles.js scripts/utils/jsonfiles.js
48-
49-
# 5. Setup Node.js
47+
# 4. Setup Node.js
5048
- name: Setup Node.js
49+
if: steps.filter.outputs.tests == 'true'
5150
uses: actions/setup-node@v4
5251
with:
5352
node-version: 18
5453

55-
# 6. Install dependencies
54+
# 5. Install dependencies
5655
- name: Install dependencies
56+
if: steps.filter.outputs.tests == 'true'
5757
run: npm ci
5858

59-
# 7. Fetch list of files changed in this PR
59+
# 6. Fetch list of files changed in this PR
6060
- name: Get changed test files
61+
if: steps.filter.outputs.tests == 'true'
6162
env:
6263
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
6364
run: |
@@ -72,8 +73,9 @@ jobs:
7273
| sort -u \
7374
> affected-drafts.txt
7475
75-
# 8. Run add-test-ids.js for each changed file
76+
# 7. Run add-test-ids.js for each changed file
7677
- name: Apply missing test IDs
78+
if: steps.filter.outputs.tests == 'true'
7779
env:
7880
CI: "false"
7981
run: |
@@ -92,8 +94,9 @@ jobs:
9294
done
9395
done < affected-drafts.txt
9496
95-
# 9. Commit and push back to the PR branch
97+
# 8. Commit and push back to the PR branch
9698
- name: Commit and push changes
99+
if: steps.filter.outputs.tests == 'true'
97100
env:
98101
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
99102
run: |
@@ -131,7 +134,7 @@ jobs:
131134
echo "CHANGES_MADE=true" >> $GITHUB_ENV
132135
fi
133136
134-
# 10. Post a summary comment on the PR
137+
# 9. Post a summary comment on the PR
135138
- name: Post PR comment
136139
if: env.CHANGES_MADE == 'true'
137140
env:

0 commit comments

Comments
 (0)