-
Notifications
You must be signed in to change notification settings - Fork 0
221 lines (189 loc) · 7.69 KB
/
test.yml
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
name: Test Package
on:
pull_request:
branches:
- "**"
types:
- opened
- synchronize
- reopened
paths:
- "packages/**"
- "package.json"
- ".github/workflows/test.yml"
issue_comment:
types: [created]
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
paths-filter:
name: Filter Changed Packages
runs-on: ubuntu-24.04
if: github.event_name == 'pull_request' || (github.event_name == 'issue_comment' && github.event.issue.pull_request && !contains(github.event.comment.user.name, 'bot') && contains(github.event.comment.body, '/run-snapshot'))
permissions:
contents: read
pull-requests: read
timeout-minutes: 5
outputs:
eslint: ${{ steps.filter.outputs.eslint-config }}
prettier: ${{ steps.filter.outputs.prettier-config }}
stylelint: ${{ steps.filter.outputs.stylelint-config }}
head_ref: ${{ steps.head-ref.outputs.head_ref }}
pr_number: ${{ steps.pr-number.outputs.pr_number }}
steps:
- name: Get PR information (issue_comment)
if: github.event_name == 'issue_comment'
id: pr-request
uses: octokit/request-action@dad4362715b7fb2ddedf9772c8670824af564f0d # v2.4.0
with:
route: ${{ github.event.issue.pull_request.url }}
env:
GITHUB_TOKEN: ${{ github.token }}
- name: Check PR (issue_comment)
if: github.event_name == 'issue_comment'
run: |
if [ "${{ fromJson(steps.pr-request.outputs.data).base.ref }}" != 'main' ]; then
echo "::error::PR base branch is not main."
exit 1
fi
if [ "${{ fromJson(steps.pr-request.outputs.data).state }}" != 'open' ]; then
echo "::error::PR is not open."
exit 1
fi
if [ "${{ fromJson(steps.pr-request.outputs.data).draft }}" == 'true' ]; then
echo "::error::Cannot update snapshot for draft PR."
exit 1
fi
echo "head_ref=${{ fromJson(steps.pr-request.outputs.data).head.ref }}" >> $GITHUB_ENV
- name: Define head ref
id: head-ref
run: |
if [ "${{ github.event_name }}" == 'pull_request' ]; then
echo "head_ref=${{ github.head_ref }}" >> $GITHUB_OUTPUT
elif [ "${{ github.event_name }}" == 'issue_comment' ]; then
echo "head_ref=${{ env.head_ref }}" >> $GITHUB_OUTPUT
else
echo "::error::Unexpected event name: ${{ github.event_name }}"
exit 1
fi
- name: Define PR Number
id: pr-number
run: |
if [ "${{ github.event_name }}" == 'pull_request' ]; then
echo "pr_number=${{ github.event.pull_request.number }}" >> $GITHUB_OUTPUT
elif [ "${{ github.event_name }}" == 'issue_comment' ]; then
echo "pr_number=${{ github.event.issue.number }}" >> $GITHUB_OUTPUT
else
echo "::error::Unexpected event name: ${{ github.event_name }}"
exit 1
fi
- name: Checkout Repo
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
with:
ref: ${{ steps.head-ref.outputs.head_ref }}
- uses: dorny/paths-filter@de90cc6fb38fc0963ad72b210f1f284cd68cea36 # v3.0.2
id: filter
with:
ref: ${{ steps.head-ref.outputs.head_ref }}
# package.json を含めているのはl、ビルド環境が変わった場合にすべてのパッケージをビルドするようにするため
filters: |
eslint-config:
- 'packages/eslint-config/**'
- 'package.json'
prettier-config:
- 'packages/prettier-config/**'
- 'package.json'
stylelint-config:
- 'packages/stylelint-config/**'
- 'package.json'
test:
needs: paths-filter
name: Vitest
if: ${{ needs.paths-filter.outputs.eslint == 'true' || needs.paths-filter.outputs.prettier == 'true' || needs.paths-filter.outputs.stylelint == 'true' }}
runs-on: ubuntu-24.04
timeout-minutes: 10
permissions:
contents: read
pull-requests: write
steps:
- name: Checkout Repo
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
with:
ref: ${{ needs.paths-filter.outputs.head_ref }}
- run: corepack enable pnpm
- name: Setup Node.js 20.x
uses: actions/setup-node@0a44ba7841725637a19e28fa30b79a866c81b0a6 # v4.0.4
with:
node-version-file: "package.json"
cache: "pnpm"
- name: Install Dependencies
run: pnpm install --frozen-lockfile
- name: Test ESLint Config
id: eslint
if: ${{ needs.paths-filter.outputs.eslint == 'true' }}
continue-on-error: true
run: pnpm run build:eslint && pnpm run test --project eslint-config
- name: Test Prettier Config
id: prettier
if: ${{ needs.paths-filter.outputs.prettier == 'true' }}
continue-on-error: true
run: pnpm run build:prettier && pnpm run test --project prettier-config
- name: Test Stylelint Config
id: stylelint
if: ${{ needs.paths-filter.outputs.stylelint == 'true' }}
continue-on-error: true
run: pnpm run build:stylelint && test --project stylelint-config
- name: Comment if any Error
if: steps.eslint.outcome == 'failure' || steps.prettier.outcome == 'failure' || steps.stylelint.outcome == 'failure'
run: |
set -eu
# Create a comment body
cat << EOF > COMMENT.md
## 🚨 Snapshot test failed
See the details in the [workflow run details](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})
### Errors
EOF
# Append the error message to the comment body
if [ ${{ steps.eslint.outcome }} == 'failure' ]; then
echo "- ESLint" >> COMMENT.md
fi
if [ ${{ steps.prettier.outcome }} == 'failure' ]; then
echo "- Prettier" >> COMMENT.md
fi
if [ ${{ steps.stylelint.outcome }} == 'failure' ]; then
echo "- Stylelint" >> COMMENT.md
fi
# Append the next steps to the comment body
cat << EOF >> COMMENT.md
---
### ⏭️ Next Steps
If snapshot changes are...
**expected**: update the snapshots by \`/update-snapshot\`
**unexpected**: retry the workflow by \`/run-snapshot\`
EOF
- name: Comment if success
if: steps.eslint.outcome != 'failure' && steps.prettier.outcome != 'failure' && steps.stylelint.outcome != 'failure'
run: |
set -eu
# Create a comment body
cat << EOF > COMMENT.md
## ✅ Snapshot test success
See the details in the [workflow run details](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})
EOF
- name: Comment to PR
run: |
# Edit the last comment if it exists
if [ ! -f COMMENT.md ]; then
echo "::warning::comment body not found."
exit 0
fi
gh pr comment ${{ needs.paths-filter.outputs.pr_number }} --body-file COMMENT.md --edit-last \
|| gh pr comment ${{ needs.paths-filter.outputs.pr_number }} --body-file COMMENT.md
env:
GH_TOKEN: ${{ github.token }}
- name: Mark as failure if any Error
if: steps.eslint.outcome == 'failure' || steps.prettier.outcome == 'failure' || steps.stylelint.outcome == 'failure'
run: |
echo "::error::Snapshot test failed."
exit 1