-
Notifications
You must be signed in to change notification settings - Fork 0
205 lines (178 loc) · 7.53 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
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
pull_request:
types: [labeled]
branches:
- "main"
paths:
- "packages/**"
- "package.json"
- ".github/workflows/update-snapshot.yml"
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
defaults:
run:
shell: bash
jobs:
check-target-packages:
name: Check Target Packages (Update Snapshot)
runs-on: ubuntu-24.04
if: github.event_name == 'workflow_dispatch' || (github.event_name == 'pull_request' && !contains(github.event.pull_request.user.name, 'bot') && github.event.label.name == 'update-snapshot')
outputs:
eslint: ${{ steps.target-packages.outputs.eslint }}
prettier: ${{ steps.target-packages.outputs.prettier }}
stylelint: ${{ steps.target-packages.outputs.stylelint }}
steps:
- name: Fail if triggered from invalid PR (Pull Request)
if: github.event_name == 'pull_request'
run: |
if [ "${{ github.event.pull_request.state }}" != 'open' ]; then
echo "::error::PR is not open."
exit 1
fi
if [ "${{ github.event.pull_request.draft }}" == 'true' ]; then
echo "::error::Cannot update snapshot for draft PR."
exit 1
fi
- name: Fail if triggered from invalid Tag (Workflow Dispatch)
if: github.event_name == 'workflow_dispatch'
run: |
if [ ${{ contains(github.ref, 'refs/tags') }} == 'true' ]; then
echo "::error::Cannot update snapshot for tag."
exit 1
fi
- name: Check target packages (PR Comment)
if: github.event_name == 'pull_request'
uses: dorny/paths-filter@de90cc6fb38fc0963ad72b210f1f284cd68cea36 # v3.0.2
id: pr-target
with:
filters: |
eslint:
- 'packages/eslint-config/**'
prettier:
- 'packages/prettier-config/**'
stylelint:
- 'packages/stylelint-config/**'
- name: Export target packages for output
id: target-packages
run: |
if [ ${{ github.event_name }} == 'pull_request' ]; then
echo "eslint=${{ steps.pr-target.outputs.eslint }}" >> $GITHUB_OUTPUT
echo "prettier=${{ steps.pr-target.outputs.prettier }}" >> $GITHUB_OUTPUT
echo "stylelint=${{ steps.pr-target.outputs.stylelint }}" >> $GITHUB_OUTPUT
elif [ ${{ 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
else
echo "::error::Workflow called by unexpected event."
exit 1
fi
update-snapshot:
name: Update Snapshot (Update Snapshot)
needs: check-target-packages
if: (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:
- name: Get GitHub Access Token from GitHub App
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: 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'
- name: Checkout Repo (Pull Request)
if: github.event_name == 'pull_request'
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
ref: ${{ github.event.pull_request.head.ref }}
token: ${{ steps.app-token.outputs.token }}
- name: Checkout Repo (Workflow Dispatch)
if: github.event_name == 'workflow_dispatch'
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
ref: ${{ github.ref }}
token: ${{ steps.app-token.outputs.token }}
- name: Create Branch (Workflow Dispatch)
if: github.event_name == 'workflow_dispatch'
run: |
git switch -c "bot/update-snapshot-$(TZ=UTC-9 date +'%Y%m%d')-${{ github.run_id }}-${{ github.run_attempt }}"
- 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: |
git add .
git commit -m "chore: update snapshot (github-actions)"
git push origin HEAD
- name: Create change summary markdown
run: |
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 (Pull Request)
if: github.event_name == 'pull_request'
run: |
if [ ! -f CHANGES.md ]; then
echo "::warning::comment body not found."
exit 0
fi
gh pr comment ${{ github.event.pull_request.number }} --body-file CHANGES.md --edit-last \
|| gh pr comment ${{ github.event.pull_request.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: |
if [ ! -f CHANGES.md ]; then
echo "::warning::comment body not found."
exit 0
fi
gh pr create --base ${{ github.ref }} --body-file CHANGES.md --title "update snapshot (github-actions)"
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}