Skip to content

Commit 6507cf9

Browse files
committed
Handle renamed repositories
1 parent c399b56 commit 6507cf9

3 files changed

Lines changed: 72 additions & 7 deletions

File tree

src/lib/server/nvim-sync/config/syncRepoInfo.ts

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
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+
}
28
import type { GithubRepository, GithubTree } from '$lib/server/github/schema';
39
import { prismaClient } from '$lib/server/prisma/client';
410
import type { CreateNeovimConfigDTO } from '$lib/server/prisma/neovimconfigs/schema';
@@ -55,12 +61,50 @@ export async function syncReadme(token: string, config: NeovimConfig) {
5561

5662
export async function syncExistingRepoInfo(token: string, config: NeovimConfig) {
5763
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+
}
62101
}
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+
);
64108
return upsertNeovimConfig(config.userId, upsertDTO);
65109
}
66110

src/lib/trpc/procedures/syncExistingNeovimConfig.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import { t } from '../t';
44
import { TRPCError } from '@trpc/server';
55
import { getConfigBySlug } from '$lib/server/prisma/neovimconfigs/service';
66
import { getGithubToken } from '$lib/server/prisma/users/service';
7-
import { syncExistingRepoInfo, syncReadme } from '$lib/server/nvim-sync/config/syncRepoInfo';
7+
import {
8+
syncExistingRepoInfo,
9+
syncReadme,
10+
RepoTransferredError
11+
} from '$lib/server/nvim-sync/config/syncRepoInfo';
812
import { getNeovimConfigSyncer } from '$lib/server/nvim-sync/config/NeovimConfigSyncer';
913
import { hasBeenOneDay } from '$lib/utils';
1014

@@ -20,7 +24,15 @@ export const syncExistingNeovimConfig = t.procedure
2024
throw new TRPCError({ code: 'FORBIDDEN' });
2125
}
2226
const token = await getGithubToken(user.id);
23-
const config = await syncExistingRepoInfo(token, configBeforeSync);
27+
const config = await syncExistingRepoInfo(token, configBeforeSync).catch((e) => {
28+
if (e instanceof RepoTransferredError) {
29+
throw new TRPCError({
30+
code: 'CONFLICT',
31+
message: 'Repo has been transferred to another user'
32+
});
33+
}
34+
throw e;
35+
});
2436
await syncReadme(token, config);
2537
const syncer = await getNeovimConfigSyncer(user, config);
2638
return await syncer.treeSync();

src/routes/api/sync/configs/+server.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { syncExistingRepoInfo, syncReadme } from '$lib/server/nvim-sync/config/s
44
import { getConfigsWithToken } from '$lib/server/prisma/neovimconfigs/service';
55
import { getAllNeovimPluginNames } from '$lib/server/prisma/neovimplugins/service';
66
import { deleteGithubToken } from '$lib/server/prisma/users/service';
7+
import { RepoTransferredError } from '$lib/server/nvim-sync/config/syncRepoInfo';
8+
import { prismaClient } from '$lib/server/prisma/client';
79
import type { RequestHandler } from '@sveltejs/kit';
810

911
const PAGE_SIZE = 50;
@@ -31,6 +33,13 @@ async function* getConfigSyncTasks() {
3133
await syncer.treeSync();
3234
})
3335
]).catch(async (e: { status?: number; message?: string }) => {
36+
if (e instanceof RepoTransferredError) {
37+
await prismaClient.neovimConfig.update({
38+
where: { id: e.configId },
39+
data: { lastSyncedAt: new Date() }
40+
});
41+
return;
42+
}
3443
if (e.status === 401) {
3544
await deleteGithubToken(config.userId);
3645
console.log(

0 commit comments

Comments
 (0)