Repository navigation
Clean up Artifacts repos on package delete and account deletion #511
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
Merged
Merged
Changes from all commits
Commits
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
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
173 changes: 173 additions & 0 deletions
173
packages/worker/src/repo/artifact-repo-cleanup.node.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,173 @@ | ||
| import { afterEach, expect, test, vi } from 'vitest' | ||
|
|
||
| const mockModule = vi.hoisted(() => ({ | ||
| deleteArtifactRepo: vi.fn(), | ||
| getEntitySourceById: vi.fn(), | ||
| listEntitySourcesByUser: vi.fn(), | ||
| listRepoSessionsBySource: vi.fn(), | ||
| listRepoSessionsByUser: vi.fn(), | ||
| hasArtifactsAccess: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock('./artifacts.ts', () => ({ | ||
| getArtifactsBinding: () => ({ | ||
| delete: (...args: Array<unknown>) => mockModule.deleteArtifactRepo(...args), | ||
| }), | ||
| hasArtifactsAccess: (...args: Array<unknown>) => | ||
| mockModule.hasArtifactsAccess(...args), | ||
| })) | ||
|
|
||
| vi.mock('./entity-sources.ts', () => ({ | ||
| getEntitySourceById: (...args: Array<unknown>) => | ||
| mockModule.getEntitySourceById(...args), | ||
| listEntitySourcesByUser: (...args: Array<unknown>) => | ||
| mockModule.listEntitySourcesByUser(...args), | ||
| })) | ||
|
|
||
| vi.mock('./repo-sessions.ts', () => ({ | ||
| listRepoSessionsBySource: (...args: Array<unknown>) => | ||
| mockModule.listRepoSessionsBySource(...args), | ||
| listRepoSessionsByUser: (...args: Array<unknown>) => | ||
| mockModule.listRepoSessionsByUser(...args), | ||
| })) | ||
|
|
||
| const { | ||
| cleanupAllUserArtifactRepos, | ||
| cleanupArtifactReposForPackage, | ||
| deleteUserScopedArtifactRepo, | ||
| } = await import('./artifact-repo-cleanup.ts') | ||
|
|
||
| const env = { APP_DB: {} } as Env | ||
|
|
||
| afterEach(() => { | ||
| vi.clearAllMocks() | ||
| }) | ||
|
|
||
| test('deleteUserScopedArtifactRepo deletes a repo and treats 404 as success', async () => { | ||
| mockModule.hasArtifactsAccess.mockReturnValue(true) | ||
| mockModule.deleteArtifactRepo.mockResolvedValueOnce({ | ||
| id: 'repo_1', | ||
| alreadyDeleted: false, | ||
| }) | ||
| mockModule.deleteArtifactRepo.mockResolvedValueOnce({ | ||
| id: null, | ||
| alreadyDeleted: true, | ||
| }) | ||
|
|
||
| expect( | ||
| await deleteUserScopedArtifactRepo({ | ||
| env, | ||
| userId: 'user-1', | ||
| repoName: 'package-src-1', | ||
| }), | ||
| ).toBe(true) | ||
| expect( | ||
| await deleteUserScopedArtifactRepo({ | ||
| env, | ||
| userId: 'user-1', | ||
| repoName: 'package-src-1-session-abc', | ||
| }), | ||
| ).toBe(true) | ||
| expect(mockModule.deleteArtifactRepo).toHaveBeenCalledTimes(2) | ||
| }) | ||
|
|
||
| test('cleanupArtifactReposForPackage deletes session fork repos before entity repo', async () => { | ||
| mockModule.hasArtifactsAccess.mockReturnValue(true) | ||
| mockModule.getEntitySourceById.mockResolvedValue({ | ||
| id: 'source-1', | ||
| user_id: 'user-1', | ||
| repo_id: 'package-pkg-1', | ||
| }) | ||
| mockModule.listRepoSessionsBySource.mockResolvedValue([ | ||
| { | ||
| id: 'session-1', | ||
| user_id: 'user-1', | ||
| source_id: 'source-1', | ||
| session_repo_name: 'package-pkg-1-abc123', | ||
| }, | ||
| ]) | ||
| mockModule.deleteArtifactRepo.mockResolvedValue({ | ||
| id: 'repo_deleted', | ||
| alreadyDeleted: false, | ||
| }) | ||
|
|
||
| const deleted = await cleanupArtifactReposForPackage({ | ||
| env, | ||
| userId: 'user-1', | ||
| sourceId: 'source-1', | ||
| }) | ||
|
|
||
| expect(deleted).toBe(2) | ||
| expect(mockModule.deleteArtifactRepo).toHaveBeenCalledWith( | ||
| 'package-pkg-1-abc123', | ||
| ) | ||
| expect(mockModule.deleteArtifactRepo).toHaveBeenCalledWith('package-pkg-1') | ||
| expect(mockModule.getEntitySourceById).toHaveBeenCalledWith({}, 'source-1') | ||
| }) | ||
|
|
||
| test('cleanupArtifactReposForPackage skips repos owned by another user', async () => { | ||
| mockModule.getEntitySourceById.mockResolvedValue({ | ||
| id: 'source-1', | ||
| user_id: 'user-other', | ||
| repo_id: 'package-pkg-1', | ||
| }) | ||
| const warnings: Array<string> = [] | ||
|
|
||
| const deleted = await cleanupArtifactReposForPackage({ | ||
| env, | ||
| userId: 'user-1', | ||
| sourceId: 'source-1', | ||
| warnings, | ||
| }) | ||
|
|
||
| expect(deleted).toBe(0) | ||
| expect(mockModule.deleteArtifactRepo).not.toHaveBeenCalled() | ||
| expect(warnings[0]).toContain('user scope mismatch') | ||
| }) | ||
|
|
||
| test('cleanupAllUserArtifactRepos deduplicates repos across sources and sessions', async () => { | ||
| mockModule.hasArtifactsAccess.mockReturnValue(true) | ||
| mockModule.listEntitySourcesByUser.mockResolvedValue([ | ||
| { id: 'source-1', user_id: 'user-1', repo_id: 'package-pkg-1' }, | ||
| { id: 'source-2', user_id: 'user-1', repo_id: 'job-job-1' }, | ||
| ]) | ||
| mockModule.listRepoSessionsByUser.mockResolvedValue([ | ||
| { | ||
| id: 'session-1', | ||
| user_id: 'user-1', | ||
| session_repo_name: 'package-pkg-1-abc123', | ||
| }, | ||
| ]) | ||
| mockModule.deleteArtifactRepo.mockResolvedValue({ | ||
| id: 'repo_deleted', | ||
| alreadyDeleted: false, | ||
| }) | ||
|
|
||
| const warnings: Array<string> = [] | ||
| const deleted = await cleanupAllUserArtifactRepos({ | ||
| env, | ||
| userId: 'user-1', | ||
| warnings, | ||
| }) | ||
|
|
||
| expect(deleted).toBe(3) | ||
| expect(mockModule.deleteArtifactRepo).toHaveBeenCalledTimes(3) | ||
| }) | ||
|
|
||
| test('cleanupAllUserArtifactRepos records a warning when Artifacts access is unavailable', async () => { | ||
| mockModule.hasArtifactsAccess.mockReturnValue(false) | ||
| mockModule.listEntitySourcesByUser.mockResolvedValue([ | ||
| { id: 'source-1', user_id: 'user-1', repo_id: 'package-pkg-1' }, | ||
| ]) | ||
| mockModule.listRepoSessionsByUser.mockResolvedValue([]) | ||
|
|
||
| const warnings: Array<string> = [] | ||
| const deleted = await cleanupAllUserArtifactRepos({ | ||
| env, | ||
| userId: 'user-1', | ||
| warnings, | ||
| }) | ||
|
|
||
| expect(deleted).toBe(0) | ||
| expect(warnings[0]).toContain('Cloudflare Artifacts access was unavailable') | ||
| }) |
Oops, something went wrong.
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.