-
Notifications
You must be signed in to change notification settings - Fork 0
220 lines (192 loc) · 8.11 KB
/
update-snapshot.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
name: Update Snapshot
on:
workflow_dispatch:
inputs:
eslint:
description: "Update ESLint Config Snapshot"
required: true
default: false
type: boolean
prettier:
description: "Update Prettier Config Snapshot"
required: true
default: false
type: boolean
stylelint:
description: "Update Stylelint Config Snapshot"
required: true
default: false
type: boolean
issue_comment:
types: [created]
jobs:
filter-packages:
name: Check Changed Packages (Update Snapshot)
runs-on: ubuntu-24.04
if: github.event_name == 'issue_comment' && github.event.issue.pull_request && !contains(github.event.comment.user.name, 'bot') && contains(github.event.comment.body, '/update-snapshot')
outputs:
head_ref: ${{ fromJson(steps.pr-request.outputs.data).head.ref }}
eslint: ${{ false || steps.filter.outputs.eslint }}
prettier: ${{ false || steps.filter.outputs.prettier }}
stylelint: ${{ false || steps.filter.outputs.stylelint }}
permissions:
contents: read
steps:
- name: Get PR information
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: Exit if invalid PR
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
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
ref: ${{ fromJson(steps.pr-request.outputs.data).head.ref }}
- name: Check changed packages (PR Comment)
uses: dorny/paths-filter@de90cc6fb38fc0963ad72b210f1f284cd68cea36 # v3.0.2
id: filter
with:
ref: ${{ fromJson(steps.pr-request.outputs.data).head.ref }}
filters: |
eslint:
- 'packages/eslint-config/**'
prettier:
- 'packages/prettier-config/**'
stylelint:
- 'packages/stylelint-config/**'
check-target-packages:
name: Check target packages (Update Snapshot)
if: always()
needs: filter-packages
runs-on: ubuntu-24.04
outputs:
eslint: ${{ steps.check.outputs.eslint }}
prettier: ${{ steps.check.outputs.prettier }}
stylelint: ${{ steps.check.outputs.stylelint }}
head_ref: ${{ steps.set-head-ref.outputs.head_ref }}
steps:
- name: Check target packages
id: check
run: |
if [ ${{ github.event_name }} == 'workflow_dispatch' ]; then
echo "eslint=${{ github.event.inputs.eslint }}" >> $GITHUB_OUTPUT
echo "prettier=${{ github.event.inputs.prettier }}" >> $GITHUB_OUTPUT
echo "stylelint=${{ github.event.inputs.stylelint }}" >> $GITHUB_OUTPUT
elif [ ${{ github.event_name }} == 'issue_comment' ]; then
echo "eslint=${{ needs.filter-packages.outputs.eslint }}" >> $GITHUB_OUTPUT
echo "prettier=${{ needs.filter-packages.outputs.prettier }}" >> $GITHUB_OUTPUT
echo "stylelint=${{ needs.filter-packages.outputs.stylelint }}" >> $GITHUB_OUTPUT
else
echo "::error::Workflow called by unexpected event."
exit 1
fi
- name: Set head ref
id: set-head-ref
run: |
if [ ${{ github.event_name }} == 'workflow_dispatch' ]; then
echo "head_ref=${{ github.ref }}" >> $GITHUB_OUTPUT
elif [ ${{ github.event_name }} == 'issue_comment' ]; then
echo "head_ref=${{ needs.filter-packages.outputs.head_ref }}" >> $GITHUB_OUTPUT
else
echo "::error::Workflow called by unexpected event."
exit 1
fi
update-snapshot:
name: Update Snapshot (Update Snapshot)
needs: check-target-packages
if: always() && (needs.check-target-packages.outputs.eslint == 'true' || needs.check-target-packages.outputs.prettier == 'true' || needs.check-target-packages.outputs.stylelint == 'true')
runs-on: ubuntu-24.04
permissions:
contents: write
pull-requests: write
timeout-minutes: 10
steps:
- uses: actions/create-github-app-token@5d869da34e18e7287c1daad50e0b8ea0f506ce69 # v1.11.0
id: app-token
with:
app-id: ${{ vars.ACTIONS_MIKU_APP_ID }}
private-key: ${{ secrets.ACTIONS_MIKU_PRIVATE_KEY }}
- name: Get GitHub App User ID
id: get-user-id
run: echo "user-id=$(gh api "/users/${{ steps.app-token.outputs.app-slug }}[bot]" --jq .id)" >> "$GITHUB_OUTPUT"
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
- name: Checkout Repo
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
ref: ${{ needs.check-target-packages.outputs.head_ref }}
token: ${{ steps.app-token.outputs.token }}
- name: Configure Git Identity
run: |
git config --global user.name '${{ steps.app-token.outputs.app-slug }}[bot]'
git config --global user.email '${{ steps.get-user-id.outputs.user-id }}+${{ steps.app-token.outputs.app-slug }}[bot]@users.noreply.github.com'
if [ ${{ github.event_name }} == 'workflow_dispatch' ]; then
git switch -c "bot/update-snapshot-$(TZ=UTC-9 date +'%Y%m')-${SHA:0:7}"
fi
- name: Setup Node.js and pnpm
uses: ./.github/common/setup-node-pnpm
- name: Update ESLint Config Snapshot
if: needs.check-target-packages.outputs.eslint == 'true'
run: pnpm run build:eslint && pnpm run test --project eslint-config -u
- name: Update Prettier Config Snapshot
if: needs.check-target-packages.outputs.prettier == 'true'
run: pnpm run build:prettier && pnpm run test --project prettier-config -u
- name: Update Stylelint Config Snapshot
if: needs.check-target-packages.outputs.stylelint == 'true'
run: pnpm run build:stylelint && pnpm run test --project stylelint-config -u
- name: Commit and Push Changes
run: |
set -eu
git add .
git commit -m "chore: update snapshot (github-actions)"
git push origin HEAD
- name: Create change summary markdown
run: |
set -eu
echo "## 🚚 Updated snapshots" >> CHANGES.md
if [ ${{ needs.check-target-packages.outputs.eslint }} == 'true' ]; then
echo "- ESLint" >> CHANGES.md
fi
if [ ${{ needs.check-target-packages.outputs.prettier }} == 'true' ]; then
echo "- Prettier" >> CHANGES.md
fi
if [ ${{ needs.check-target-packages.outputs.stylelint }} == 'true' ]; then
echo "- Stylelint" >> CHANGES.md
fi
- name: Comment to PR (Issue Comment)
if: github.event_name == 'issue_comment'
run: |
set -eu
if [ ! -f CHANGES.md ]; then
echo "::warning::comment body not found."
exit 0
fi
gh pr comment ${{ github.event.issue.number }} --body-file CHANGES.md --edit-last \
|| gh pr comment ${{ github.event.issue.number}} --body-file CHANGES.md
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
- name: Create Pull Request (Workflow Dispatch)
if: github.event_name == 'workflow_dispatch'
run: |
set -eu
if [ ! -f CHANGES.md ]; then
echo "::warning::comment body not found."
exit 0
fi
gh pr create --base "main" --body-file CHANGES.md --title "update snapshot (github-actions)"
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}