-
-
Notifications
You must be signed in to change notification settings - Fork 275
152 lines (129 loc) · 5.05 KB
/
apply-test-ids.yml
File metadata and controls
152 lines (129 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
name: apply-test-ids
on:
pull_request_target:
types: [opened, synchronize]
paths:
- 'tests/**/*.json'
permissions:
contents: write
pull-requests: write
jobs:
apply:
runs-on: ubuntu-latest
steps:
# 1. Detect what changed in this PR
- name: Check changed paths
uses: dorny/paths-filter@v3
id: filter
with:
filters: |
tests:
- 'tests/**/*.json'
scripts:
- 'scripts/**'
- 'package.json'
- 'package-lock.json'
# 2. Fail if both tests AND scripts changed together
- name: Block mixed changes
if: steps.filter.outputs.tests == 'true' && steps.filter.outputs.scripts == 'true'
run: |
echo "This PR changes both test files and scripts at the same time."
echo "Please split them into separate PRs."
exit 1
# 3. Checkout repo
- name: Checkout
if: steps.filter.outputs.tests == 'true'
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: 0
# 4. Setup Node.js
- name: Setup Node.js
if: steps.filter.outputs.tests == 'true'
uses: actions/setup-node@v4
with:
node-version: 18
# 5. Install dependencies
- name: Install dependencies
if: steps.filter.outputs.tests == 'true'
run: npm ci
# 6. Fetch list of files changed in this PR
- name: Get changed test files
if: steps.filter.outputs.tests == 'true'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
curl -s \
-H "Authorization: Bearer $GITHUB_TOKEN" \
"${{ github.event.pull_request.url }}/files?per_page=100" \
| jq -r ".[].filename" \
> changed-files.txt
grep -E "^tests/(draft[^/]+)/.*\.json$" changed-files.txt \
| sed -E "s|^tests/([^/]+)/.*|\1|" \
| sort -u \
> affected-drafts.txt
# 7. Run add-test-ids.js for each changed file
- name: Apply missing test IDs
if: steps.filter.outputs.tests == 'true'
env:
CI: "false"
run: |
if [ ! -s affected-drafts.txt ]; then
echo "No test JSON files changed - nothing to do."
exit 0
fi
while IFS= read -r draft; do
echo "Processing dialect: $draft"
grep -E "^tests/${draft}/.*\.json$" changed-files.txt | while IFS= read -r file; do
if [ -f "$file" ]; then
echo " -> $file"
node scripts/add-test-ids.js "$draft" "$file"
fi
done
done < affected-drafts.txt
# 8. Commit and push back to the PR branch
- name: Commit and push changes
if: steps.filter.outputs.tests == 'true'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
git config user.name "test-id-bot"
git config user.email "test-id-bot@users.noreply.github.com"
git add "tests/**/*.json"
if git diff --cached --quiet; then
echo "No test ID changes needed - nothing to commit."
echo "CHANGES_MADE=false" >> $GITHUB_ENV
else
SUMMARY=""
TOTAL=0
while IFS= read -r file; do
COUNT=$(git diff --cached -- "$file" | grep "^+" | grep '"id":' | wc -l | tr -d " ")
if [ "$COUNT" -gt 0 ]; then
SUMMARY="${SUMMARY}\n- \`${file}\` - **${COUNT}** ID(s) added"
TOTAL=$((TOTAL + COUNT))
fi
done < <(git diff --cached --name-only)
printf "%b" "$SUMMARY" > /tmp/summary.txt
echo "TOTAL_IDS=${TOTAL}" >> $GITHUB_ENV
git commit -m "chore: auto-add missing test IDs"
COMMIT_SHA=$(git rev-parse HEAD)
echo "COMMIT_SHA=${COMMIT_SHA}" >> $GITHUB_ENV
git push \
https://x-access-token:${GITHUB_TOKEN}@github.com/${{ github.event.pull_request.head.repo.full_name }}.git \
HEAD:refs/heads/${{ github.event.pull_request.head.ref }}
echo "CHANGES_MADE=true" >> $GITHUB_ENV
fi
# 9. Post a summary comment on the PR
- name: Post PR comment
if: env.CHANGES_MADE == 'true'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
SUMMARY=$(cat /tmp/summary.txt)
COMMIT_URL="https://github.com/${{ github.event.pull_request.head.repo.full_name }}/commit/${COMMIT_SHA}"
BODY="### test-id-bot added ${TOTAL_IDS} missing test ID(s)\n\n${SUMMARY}\n\nView the full diff of added IDs: ${COMMIT_URL}\n\n> IDs are deterministic hashes based on the schema, test data, and expected result."
curl -s -X POST \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Content-Type: application/json" \
"${{ github.event.pull_request.comments_url }}" \
--data "$(jq -n --arg body "$(printf '%b' "$BODY")" '{body: $body}')"