-
-
Notifications
You must be signed in to change notification settings - Fork 18
306 lines (263 loc) Β· 11.4 KB
/
data-refresh.yaml
File metadata and controls
306 lines (263 loc) Β· 11.4 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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
name: Weekly Data Refresh
on:
schedule:
# Run at 02:00 UTC every Sunday
- cron: "0 2 * * Sun"
# Allow manual triggering
workflow_dispatch:
permissions:
contents: write
pull-requests: write
issues: write
jobs:
test-data:
runs-on: ubuntu-latest
outputs:
tests-passed: ${{ (steps.test.outcome == 'success' && 'true') || 'false' }}
branch-name: ${{ steps.branch.outputs.name }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: "22"
cache: "npm"
- name: Create or reset branch
id: branch
run: |
BRANCH_NAME="data-refresh"
git checkout -b $BRANCH_NAME
echo "name=$BRANCH_NAME" >> $GITHUB_OUTPUT
- name: Install dependencies
run: npm ci
- name: Redownload mock data
run: npm run redownload
- name: Run tests
id: test
continue-on-error: true
run: npm run test
- name: Upload mock data
if: steps.test.outcome == 'failure'
uses: actions/upload-artifact@v4
with:
name: mock-data
path: tests/mocks/data/
retention-days: 1
if-no-files-found: error
update-snapshots:
runs-on: ubuntu-latest
needs: test-data
# TODO: this is a workaround to allow the job to continue even if the tests fail so
# we can post the outcome of the job in case of failure but it will show the job as successful
# even if the tests fail. We should figure out how to do this without continuing on error.
continue-on-error: true
if: ${{ needs.test-data.outputs.tests-passed == 'false' }}
outputs:
tests-passed: ${{ (steps.retest.outcome == 'success' && 'true') || 'false' }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: "22"
cache: "npm"
- name: Checkout branch
run: git checkout -b ${{ needs.test-data.outputs.branch-name }}
# Each job runs on a new machine, so we need to reinstall dependencies
# even though we're on the same branch
- name: Install dependencies
run: npm ci
# Download the mock data from the previous job to ensure we're using
# the same data that caused the test failure
- name: Download mock data
uses: actions/download-artifact@v4
with:
name: mock-data
path: tests/mocks/data/
- name: Update snapshots
# TODO: check if we need the next step or we can just run this one instead
continue-on-error: true
run: npm run test:update
- name: Run tests again
id: retest
continue-on-error: true
run: npm run test
- name: Commit changes
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add .
git commit -m "π€π β Update snapshots after data refresh" || echo "No changes to commit"
- name: Push branch
run: git push --force -u origin ${{ needs.test-data.outputs.branch-name }}
create-success-pr:
runs-on: ubuntu-latest
needs: [test-data, update-snapshots]
if: ${{ needs.update-snapshots.outputs.tests-passed == 'true' }}
steps:
- name: Create or update Pull Request
uses: actions/github-script@v7
with:
script: |
const head = '${{ needs.test-data.outputs.branch-name }}';
const today = new Date().toISOString().split('T')[0];
const runUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`;
const [{ data: existingIssues }, { data: existingPRs }] = await Promise.all([
github.rest.issues.listForRepo({
owner: context.repo.owner,
repo: context.repo.repo,
labels: 'data-refresh',
state: 'open',
per_page: 100,
}),
github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
head: `${context.repo.owner}:${head}`,
base: 'main',
state: 'open',
}),
]);
const openIssues = existingIssues.filter(i => !i.pull_request);
const fixesLine = openIssues.length > 0
? `\n\nFixes ${openIssues.map(i => `#${i.number}`).join(', ')}`
: '';
const body = `The Automatic data refresh on ${today} detected changes in AO3 responses.
Snapshots have been updated to reflect current status. All tests are now passing! πππ
Changes look good? β‘οΈ Merge this PR to update the snapshots and tests.
Changes look _yikes_? β Check out this PR and go get 'em! πͺπ₯${fixesLine}`;
if (existingPRs.length > 0) {
const prNumber = existingPRs[0].number;
await github.rest.pulls.update({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
title: 'πβ
Data Refresh: Snapshot Update Complete!',
body,
});
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: `πβ¨ Fresh data just dropped (${today})! Tests are passing and this PR is ready for another look~ [Workflow run](${runUrl}) π`,
});
core.info(`Updated existing PR #${prNumber}`);
} else {
const pr = await github.rest.pulls.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: 'πβ
Data Refresh: Snapshot Update Complete!',
head,
base: 'main',
body,
});
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.data.number,
labels: ['data-refresh'],
});
core.info(`Created new PR #${pr.data.number}`);
}
create-failure-pr-and-issue:
runs-on: ubuntu-latest
needs: [test-data, update-snapshots]
if: ${{ needs.update-snapshots.outputs.tests-passed == 'false' }}
steps:
- name: Create or update issue and PR for failed tests
uses: actions/github-script@v7
with:
script: |
const head = '${{ needs.test-data.outputs.branch-name }}';
const today = new Date().toISOString().split('T')[0];
const runUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`;
const [{ data: existingIssues }, { data: existingPRs }] = await Promise.all([
github.rest.issues.listForRepo({
owner: context.repo.owner,
repo: context.repo.repo,
labels: 'data-refresh',
state: 'open',
per_page: 100,
}),
github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
head: `${context.repo.owner}:${head}`,
base: 'main',
state: 'open',
}),
]);
const openIssues = existingIssues.filter(i => !i.pull_request);
let issueNumber;
const issueBody = `The daily data refresh tests failed on ${today}. Yes, even with the snapshot updates!
Please check the [workflow run](${runUrl}) for details.
---
Want to learn more about this issue and help us fix it? Check out the [README](https://github.com/FujoWebDev/AO3.js/blob/main/README.md#about--data-refresh-tests-failed-issues) for more information!`;
if (openIssues.length > 0) {
issueNumber = openIssues[0].number;
await github.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumber,
body: issueBody,
});
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumber,
body: `π¨ Oop, tests are STILL failing as of ${today}. The bugs persist! ππͺ [Workflow run](${runUrl})`,
});
core.info(`Updated existing issue #${issueNumber}`);
} else {
const issue = await github.rest.issues.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: 'π¨ Data Refresh Tests Failed After Snapshot Update',
body: issueBody,
labels: ['data-refresh'],
});
issueNumber = issue.data.number;
core.info(`Created new issue #${issueNumber}`);
}
const prBody = `Automatic data refresh on ${today} detected changes in AO3 responses.
Despite our best efforts (in the form of a snapshot update), tests are still failing.
Manual investigation is required ππ΅οΈ To help you get started, we've created this PR with the already-updated data. May the bugs be ever in your favor! π
Check the [workflow run](${runUrl}) for details.
Closes #${issueNumber}...eventually π€
---
Want to learn more about this issue and help us fix it? Check out the [README](https://github.com/FujoWebDev/AO3.js/blob/main/README.md#about--data-refresh-tests-failed-issues) for more information!`;
if (existingPRs.length > 0) {
const prNumber = existingPRs[0].number;
await github.rest.pulls.update({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
title: 'π¨ HALP! Tests are failing after data refresh!',
body: prBody,
});
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: `π¨ Another week, another data refresh (${today}), and the tests are still throwing a tantrum π€ [Workflow run](${runUrl}). Someone come get their bugs! π`,
});
core.info(`Updated existing PR #${prNumber}`);
} else {
const pr = await github.rest.pulls.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: 'π¨ HALP! Tests are failing after data refresh!',
head,
base: 'main',
body: prBody,
});
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.data.number,
labels: ['data-refresh'],
});
core.info(`Created new PR #${pr.data.number}`);
}