Skip to content

Commit 148f9e7

Browse files
Refactor data refresh workflow to reuse existing issues/PRs for snapshot updates
1 parent b96f0ba commit 148f9e7

1 file changed

Lines changed: 158 additions & 58 deletions

File tree

.github/workflows/data-refresh.yaml

Lines changed: 158 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ jobs:
2929
node-version: "22"
3030
cache: "npm"
3131

32-
- name: Create new branch
32+
- name: Create or reset branch
3333
id: branch
3434
run: |
35-
BRANCH_NAME="data-refresh-$(date +%Y-%m-%d)"
35+
BRANCH_NAME="data-refresh"
3636
git checkout -b $BRANCH_NAME
3737
echo "name=$BRANCH_NAME" >> $GITHUB_OUTPUT
3838
@@ -111,96 +111,196 @@ jobs:
111111
git commit -m "🤖💖 — Update snapshots after data refresh" || echo "No changes to commit"
112112
113113
- name: Push branch
114-
run: git push -u origin ${{ needs.test-data.outputs.branch-name }}
114+
run: git push --force -u origin ${{ needs.test-data.outputs.branch-name }}
115115

116116
create-success-pr:
117117
runs-on: ubuntu-latest
118118
needs: [test-data, update-snapshots]
119119
if: ${{ needs.update-snapshots.outputs.tests-passed == 'true' }}
120120

121121
steps:
122-
- name: Create Pull Request
122+
- name: Create or update Pull Request
123123
uses: actions/github-script@v7
124124
with:
125125
script: |
126-
const pr = await github.rest.pulls.create({
127-
owner: context.repo.owner,
128-
repo: context.repo.repo,
129-
title: '🔄✅ Data Refresh: Snapshot Update Complete!',
130-
head: '${{ needs.test-data.outputs.branch-name }}',
131-
base: 'main',
132-
body: `The Automatic data refresh on ${new Date().toISOString().split('T')[0]} detected changes in AO3 responses.
126+
const head = '${{ needs.test-data.outputs.branch-name }}';
127+
const today = new Date().toISOString().split('T')[0];
128+
const runUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`;
129+
130+
const [{ data: existingIssues }, { data: existingPRs }] = await Promise.all([
131+
github.rest.issues.listForRepo({
132+
owner: context.repo.owner,
133+
repo: context.repo.repo,
134+
labels: 'data-refresh',
135+
state: 'open',
136+
per_page: 100,
137+
}),
138+
github.rest.pulls.list({
139+
owner: context.repo.owner,
140+
repo: context.repo.repo,
141+
head: `${context.repo.owner}:${head}`,
142+
base: 'main',
143+
state: 'open',
144+
}),
145+
]);
146+
const openIssues = existingIssues.filter(i => !i.pull_request);
147+
const fixesLine = openIssues.length > 0
148+
? `\n\nFixes ${openIssues.map(i => `#${i.number}`).join(', ')}`
149+
: '';
150+
151+
const body = `The Automatic data refresh on ${today} detected changes in AO3 responses.
133152
134153
Snapshots have been updated to reflect current status. All tests are now passing! 🎉🎊🌟
135154
136155
Changes look good? ➡️ Merge this PR to update the snapshots and tests.
137-
Changes look _yikes_? ❌ Check out this PR and go get 'em! 💪🔥`
138-
});
139-
140-
await github.rest.issues.addLabels({
141-
owner: context.repo.owner,
142-
repo: context.repo.repo,
143-
issue_number: pr.data.number,
144-
labels: ['data-refresh']
145-
});
156+
Changes look _yikes_? ❌ Check out this PR and go get 'em! 💪🔥${fixesLine}`;
157+
158+
if (existingPRs.length > 0) {
159+
const prNumber = existingPRs[0].number;
160+
await github.rest.pulls.update({
161+
owner: context.repo.owner,
162+
repo: context.repo.repo,
163+
pull_number: prNumber,
164+
title: '🔄✅ Data Refresh: Snapshot Update Complete!',
165+
body,
166+
});
167+
await github.rest.issues.createComment({
168+
owner: context.repo.owner,
169+
repo: context.repo.repo,
170+
issue_number: prNumber,
171+
body: `🔄✨ Fresh data just dropped (${today})! Tests are passing and this PR is ready for another look~ [Workflow run](${runUrl}) 👀`,
172+
});
173+
core.info(`Updated existing PR #${prNumber}`);
174+
} else {
175+
const pr = await github.rest.pulls.create({
176+
owner: context.repo.owner,
177+
repo: context.repo.repo,
178+
title: '🔄✅ Data Refresh: Snapshot Update Complete!',
179+
head,
180+
base: 'main',
181+
body,
182+
});
183+
await github.rest.issues.addLabels({
184+
owner: context.repo.owner,
185+
repo: context.repo.repo,
186+
issue_number: pr.data.number,
187+
labels: ['data-refresh'],
188+
});
189+
core.info(`Created new PR #${pr.data.number}`);
190+
}
146191
147192
create-failure-pr-and-issue:
148193
runs-on: ubuntu-latest
149194
needs: [test-data, update-snapshots]
150195
if: ${{ needs.update-snapshots.outputs.tests-passed == 'false' }}
151196

152197
steps:
153-
- name: Create issue for failed tests
154-
id: create-issue
198+
- name: Create or update issue and PR for failed tests
155199
uses: actions/github-script@v7
156200
with:
157201
script: |
158-
const issue = await github.rest.issues.create({
159-
owner: context.repo.owner,
160-
repo: context.repo.repo,
161-
title: '🚨 Data Refresh Tests Failed After Snapshot Update',
162-
body: `The daily data refresh tests failed on ${new Date().toISOString().split('T')[0]}. Yes, even with the snapshot updates!
163-
164-
Please check the [workflow run](${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}) for details.
202+
const head = '${{ needs.test-data.outputs.branch-name }}';
203+
const today = new Date().toISOString().split('T')[0];
204+
const runUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`;
205+
206+
const [{ data: existingIssues }, { data: existingPRs }] = await Promise.all([
207+
github.rest.issues.listForRepo({
208+
owner: context.repo.owner,
209+
repo: context.repo.repo,
210+
labels: 'data-refresh',
211+
state: 'open',
212+
per_page: 100,
213+
}),
214+
github.rest.pulls.list({
215+
owner: context.repo.owner,
216+
repo: context.repo.repo,
217+
head: `${context.repo.owner}:${head}`,
218+
base: 'main',
219+
state: 'open',
220+
}),
221+
]);
222+
const openIssues = existingIssues.filter(i => !i.pull_request);
223+
224+
let issueNumber;
225+
const issueBody = `The daily data refresh tests failed on ${today}. Yes, even with the snapshot updates!
226+
227+
Please check the [workflow run](${runUrl}) for details.
165228
166229
---
167230
168-
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!`
169-
});
170-
171-
return issue.data.number;
172-
173-
- name: Create Pull Request
174-
uses: actions/github-script@v7
175-
with:
176-
script: |
177-
const issueNumber = ${{ steps.create-issue.outputs.result }};
178-
179-
const pr = await github.rest.pulls.create({
180-
owner: context.repo.owner,
181-
repo: context.repo.repo,
182-
title: '🚨 HALP! Tests are failing after data refresh!',
183-
head: '${{ needs.test-data.outputs.branch-name }}',
184-
base: 'main',
185-
body: `Automatic data refresh on ${new Date().toISOString().split('T')[0]} detected changes in AO3 responses.
231+
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!`;
232+
233+
if (openIssues.length > 0) {
234+
issueNumber = openIssues[0].number;
235+
await github.rest.issues.update({
236+
owner: context.repo.owner,
237+
repo: context.repo.repo,
238+
issue_number: issueNumber,
239+
body: issueBody,
240+
});
241+
await github.rest.issues.createComment({
242+
owner: context.repo.owner,
243+
repo: context.repo.repo,
244+
issue_number: issueNumber,
245+
body: `🚨 Oop, tests are STILL failing as of ${today}. The bugs persist! 🐛💪 [Workflow run](${runUrl})`,
246+
});
247+
core.info(`Updated existing issue #${issueNumber}`);
248+
} else {
249+
const issue = await github.rest.issues.create({
250+
owner: context.repo.owner,
251+
repo: context.repo.repo,
252+
title: '🚨 Data Refresh Tests Failed After Snapshot Update',
253+
body: issueBody,
254+
labels: ['data-refresh'],
255+
});
256+
issueNumber = issue.data.number;
257+
core.info(`Created new issue #${issueNumber}`);
258+
}
259+
260+
const prBody = `Automatic data refresh on ${today} detected changes in AO3 responses.
186261
187262
Despite our best efforts (in the form of a snapshot update), tests are still failing.
188263
189264
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! 🍀
190265
191-
Check the [workflow run](${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}) for details.
266+
Check the [workflow run](${runUrl}) for details.
192267
193268
Closes #${issueNumber}...eventually 🤞
194269
195270
---
196271
197-
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!`
198-
});
199-
200-
// Add label to the PR for easy identification
201-
await github.rest.issues.addLabels({
202-
owner: context.repo.owner,
203-
repo: context.repo.repo,
204-
issue_number: pr.data.number,
205-
labels: ['data-refresh']
206-
});
272+
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!`;
273+
274+
if (existingPRs.length > 0) {
275+
const prNumber = existingPRs[0].number;
276+
await github.rest.pulls.update({
277+
owner: context.repo.owner,
278+
repo: context.repo.repo,
279+
pull_number: prNumber,
280+
title: '🚨 HALP! Tests are failing after data refresh!',
281+
body: prBody,
282+
});
283+
await github.rest.issues.createComment({
284+
owner: context.repo.owner,
285+
repo: context.repo.repo,
286+
issue_number: prNumber,
287+
body: `🚨 Another week, another data refresh (${today}), and the tests are still throwing a tantrum 😤 [Workflow run](${runUrl}). Someone come get their bugs! 🍀`,
288+
});
289+
core.info(`Updated existing PR #${prNumber}`);
290+
} else {
291+
const pr = await github.rest.pulls.create({
292+
owner: context.repo.owner,
293+
repo: context.repo.repo,
294+
title: '🚨 HALP! Tests are failing after data refresh!',
295+
head,
296+
base: 'main',
297+
body: prBody,
298+
});
299+
await github.rest.issues.addLabels({
300+
owner: context.repo.owner,
301+
repo: context.repo.repo,
302+
issue_number: pr.data.number,
303+
labels: ['data-refresh'],
304+
});
305+
core.info(`Created new PR #${pr.data.number}`);
306+
}

0 commit comments

Comments
 (0)