From f54b9f355b5a9af14a272d021e2d9c94455b6b57 Mon Sep 17 00:00:00 2001 From: tdurieux Date: Mon, 26 Jun 2023 18:17:49 +0200 Subject: [PATCH] fix: make the repoId case insensitive --- .../anonymizedRepositories.schema.ts | 2 +- src/database/database.ts | 8 +++++++- src/routes/repository-private.ts | 13 +++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/database/anonymizedRepositories/anonymizedRepositories.schema.ts b/src/database/anonymizedRepositories/anonymizedRepositories.schema.ts index d77e922..fc519ca 100644 --- a/src/database/anonymizedRepositories/anonymizedRepositories.schema.ts +++ b/src/database/anonymizedRepositories/anonymizedRepositories.schema.ts @@ -3,7 +3,7 @@ import { Schema } from "mongoose"; const AnonymizedRepositorySchema = new Schema({ repoId: { type: String, - index: { unique: true }, + index: { unique: true, collation: { locale: "en", strength: 2 } }, }, status: { type: String, diff --git a/src/database/database.ts b/src/database/database.ts index e377931..75ebd9a 100644 --- a/src/database/database.ts +++ b/src/database/database.ts @@ -42,7 +42,13 @@ export async function getRepository( if (!opts.includeFiles) { project.originalFiles = 0; } - const data = await AnonymizedRepositoryModel.findOne({ repoId }, project); + const data = await AnonymizedRepositoryModel.findOne( + { repoId }, + project + ).collation({ + locale: "en", + strength: 2, + }); if (!data) throw new AnonymousError("repo_not_found", { object: repoId, diff --git a/src/routes/repository-private.ts b/src/routes/repository-private.ts index e8b142e..eef6e44 100644 --- a/src/routes/repository-private.ts +++ b/src/routes/repository-private.ts @@ -441,6 +441,19 @@ router.post("/", async (req: express.Request, res: express.Response) => { const repoUpdate = req.body; try { + try { + await db.getRepository(repoUpdate.repoId, { includeFiles: false }); + throw new AnonymousError("repoId_already_used", { + httpStatus: 400, + object: repoUpdate, + }); + } catch (error: any) { + if (error.message == "repo_not_found") { + // the repository does not exist yet + } else { + throw error; + } + } validateNewRepo(repoUpdate); const r = gh(repoUpdate.fullName);