Repository navigation
fix: sweep every page of stale projects, fail the release job on failed publishes #89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gregory-boch-prisma
wants to merge
5
commits into
main
Choose a base branch
from
fix/stale-sweep-pagination-release-failures
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
551d479
fix(create-db-worker): sweep every page of stale projects
gregory-boch-prisma ee6aaae
fix(ci): fail the release job when a publish fails
gregory-boch-prisma 425ae01
fix(create-db-worker): stop the sweep on a cursor that does not advance
gregory-boch-prisma 21daa41
fix(ci): require the three CLIs to share one version before publishing
gregory-boch-prisma ca15445
chore(changeset): version the three CLIs as a fixed group
gregory-boch-prisma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
apps/create-db-worker/test/delete-stale-workflow.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| import { describe, it, expect, vi, beforeEach } from 'vitest'; | ||
| import { DeleteStaleProjectsWorkflow } from '../src/delete-stale-workflow'; | ||
|
|
||
| const HOUR_MS = 60 * 60 * 1000; | ||
|
|
||
| function hoursAgo(hours: number) { | ||
| return new Date(Date.now() - hours * HOUR_MS).toISOString(); | ||
| } | ||
|
|
||
| function page(data: { id: string; createdAt: string }[], nextCursor: string | null) { | ||
| return new Response( | ||
| JSON.stringify({ | ||
| data: data.map((project) => ({ ...project, name: project.id })), | ||
| pagination: { hasMore: nextCursor !== null, nextCursor }, | ||
| }), | ||
| ); | ||
| } | ||
|
|
||
| async function runWorkflow() { | ||
| const stepNames: string[] = []; | ||
| const step = { | ||
| do: async (name: string, callback: () => Promise<unknown>) => { | ||
| stepNames.push(name); | ||
| return callback(); | ||
| }, | ||
| }; | ||
| // workerd only constructs a WorkflowEntrypoint itself, so run the method on a stand-in `this` | ||
| await DeleteStaleProjectsWorkflow.prototype.run.call( | ||
| { env: { INTEGRATION_TOKEN: 'test-token' } } as any, | ||
| {} as any, | ||
| step as any, | ||
| ); | ||
| return stepNames; | ||
| } | ||
|
|
||
| describe('DeleteStaleProjectsWorkflow', () => { | ||
| let fetchMock: ReturnType<typeof vi.fn>; | ||
|
|
||
| function requestsTo(method: string) { | ||
| return fetchMock.mock.calls.filter(([, init]) => init?.method === method).map(([url]) => new URL(url)); | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| fetchMock = vi.fn(); | ||
| vi.spyOn(globalThis, 'fetch').mockImplementation(fetchMock); | ||
| }); | ||
|
|
||
| it('walks every page and deletes stale projects from all of them', async () => { | ||
| fetchMock | ||
| .mockResolvedValueOnce( | ||
| page( | ||
| [ | ||
| { id: 'stale-1', createdAt: hoursAgo(30) }, | ||
| { id: 'fresh-1', createdAt: hoursAgo(2) }, | ||
| ], | ||
| 'fresh-1', | ||
| ), | ||
| ) | ||
| .mockResolvedValueOnce(page([{ id: 'stale-2', createdAt: hoursAgo(25) }], null)) | ||
| .mockImplementation(async () => new Response(null, { status: 200 })); | ||
|
|
||
| const stepNames = await runWorkflow(); | ||
|
|
||
| const lists = requestsTo('GET'); | ||
| expect(lists).toHaveLength(2); | ||
| expect(lists[0].searchParams.get('cursor')).toBeNull(); | ||
| expect(lists[1].searchParams.get('cursor')).toBe('fresh-1'); | ||
|
|
||
| expect(requestsTo('DELETE').map((url) => url.pathname)).toEqual([ | ||
| '/v1/projects/stale-1', | ||
| '/v1/projects/stale-2', | ||
| ]); | ||
|
|
||
| expect(new Set(stepNames).size).toBe(stepNames.length); | ||
| }); | ||
|
|
||
| it('stops after one page when there are no more projects', async () => { | ||
| fetchMock.mockResolvedValueOnce(page([{ id: 'fresh-1', createdAt: hoursAgo(1) }], null)); | ||
|
|
||
| await runWorkflow(); | ||
|
|
||
| expect(requestsTo('GET')).toHaveLength(1); | ||
| expect(requestsTo('DELETE')).toHaveLength(0); | ||
| }); | ||
|
|
||
| it('fails when the API reports more pages without advancing the cursor', async () => { | ||
| fetchMock | ||
| .mockResolvedValueOnce(page([{ id: 'stale-1', createdAt: hoursAgo(30) }], 'stale-1')) | ||
| .mockResolvedValueOnce(page([{ id: 'stale-1', createdAt: hoursAgo(30) }], 'stale-1')); | ||
|
|
||
| await expect(runWorkflow()).rejects.toThrow('without a new cursor'); | ||
| expect(requestsTo('DELETE')).toHaveLength(0); | ||
| }); | ||
|
|
||
| it('fails when the API reports more pages with no cursor', async () => { | ||
| fetchMock.mockResolvedValueOnce( | ||
| new Response( | ||
| JSON.stringify({ | ||
| data: [{ id: 'stale-1', name: 'stale-1', createdAt: hoursAgo(30) }], | ||
| pagination: { hasMore: true, nextCursor: null }, | ||
| }), | ||
| ), | ||
| ); | ||
|
|
||
| await expect(runWorkflow()).rejects.toThrow('without a new cursor'); | ||
| expect(requestsTo('DELETE')).toHaveLength(0); | ||
| }); | ||
|
|
||
| it('fails when a page cannot be fetched', async () => { | ||
| fetchMock.mockResolvedValueOnce(new Response(null, { status: 500, statusText: 'Server Error' })); | ||
|
|
||
| await expect(runWorkflow()).rejects.toThrow('Failed to fetch projects'); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.