|
1 | 1 | import { fetchGithubRepositoryByName, fetchReadme } from '$lib/server/github/api'; |
| 2 | + |
| 3 | +export class RepoTransferredError extends Error { |
| 4 | + constructor(public configId: number) { |
| 5 | + super('Repo has been transferred to a different dotfyle user'); |
| 6 | + } |
| 7 | +} |
2 | 8 | import type { GithubRepository, GithubTree } from '$lib/server/github/schema'; |
3 | 9 | import { prismaClient } from '$lib/server/prisma/client'; |
4 | 10 | import type { CreateNeovimConfigDTO } from '$lib/server/prisma/neovimconfigs/schema'; |
@@ -55,12 +61,50 @@ export async function syncReadme(token: string, config: NeovimConfig) { |
55 | 61 |
|
56 | 62 | export async function syncExistingRepoInfo(token: string, config: NeovimConfig) { |
57 | 63 | const repo = await fetchGithubRepositoryByName(token, config.owner, config.repo); |
58 | | - if (repo.name !== config.repo || repo.owner.login !== config.owner) { |
59 | | - console.log( |
60 | | - `[SYNC_CONFIGS] [WARNING] Redirected ${config.owner}/${config.repo} → ${repo.owner.login}/${repo.name}` |
61 | | - ); |
| 64 | + const ownerChanged = repo.owner.login !== config.owner; |
| 65 | + const nameChanged = repo.name !== config.repo; |
| 66 | + if (ownerChanged || nameChanged) { |
| 67 | + if (ownerChanged) { |
| 68 | + const newOwnerUser = await prismaClient.user.findUnique({ |
| 69 | + where: { username: repo.owner.login } |
| 70 | + }); |
| 71 | + const isRename = !newOwnerUser || newOwnerUser.id === config.userId; |
| 72 | + if (isRename) { |
| 73 | + console.log( |
| 74 | + `[SYNC_CONFIGS] [WARNING] Redirected ${config.owner}/${config.repo} -> ${repo.owner.login}/${repo.name}. Updating data.` |
| 75 | + ); |
| 76 | + await prismaClient.user.update({ |
| 77 | + where: { id: config.userId }, |
| 78 | + data: { |
| 79 | + username: repo.owner.login, |
| 80 | + neovimConfigs: { |
| 81 | + updateMany: { |
| 82 | + where: { owner: config.owner }, |
| 83 | + data: { owner: repo.owner.login } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + }); |
| 88 | + } else { |
| 89 | + console.log( |
| 90 | + `[SYNC_CONFIGS] [WARNING] ${config.owner}/${config.repo} owner changed to ${repo.owner.login} who is a different dotfyle user — skipping sync` |
| 91 | + ); |
| 92 | + throw new RepoTransferredError(config.id); |
| 93 | + } |
| 94 | + } |
| 95 | + if (nameChanged) { |
| 96 | + await prismaClient.neovimConfig.update({ |
| 97 | + where: { id: config.id }, |
| 98 | + data: { repo: repo.name } |
| 99 | + }); |
| 100 | + } |
62 | 101 | } |
63 | | - const upsertDTO = upsertNeovimConfigDTOFactory(config.owner, config.root, config.initFile, repo); |
| 102 | + const upsertDTO = upsertNeovimConfigDTOFactory( |
| 103 | + repo.owner.login, |
| 104 | + config.root, |
| 105 | + config.initFile, |
| 106 | + repo |
| 107 | + ); |
64 | 108 | return upsertNeovimConfig(config.userId, upsertDTO); |
65 | 109 | } |
66 | 110 |
|
|
0 commit comments