From 53959f677ca325a2e8e857fa87ef79f331d9dfbf Mon Sep 17 00:00:00 2001 From: tdurieux Date: Tue, 5 May 2026 00:59:38 +0300 Subject: [PATCH] fix(repo): reuse existing record on rename to avoid externalId duplicate key When a GitHub repo is renamed and looked up by its new name, the lookup by name misses but a record with the same externalId still exists, causing E11000 on save. Fall back to a lookup by externalId before creating a new document. Fixes #500 Co-Authored-By: Claude Opus 4.7 --- src/core/source/GitHubRepository.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/core/source/GitHubRepository.ts b/src/core/source/GitHubRepository.ts index f1b8825..49b28d3 100644 --- a/src/core/source/GitHubRepository.ts +++ b/src/core/source/GitHubRepository.ts @@ -298,6 +298,12 @@ export async function getRepositoryFromGitHub(opt: { repo: opt.repo, }, }); + // If the lookup-by-name missed but we already have a record for this + // GitHub repo id (e.g. the repo was renamed on GitHub), reuse it instead + // of creating a duplicate that would violate the externalId unique index. + if (!dbModel && isConnected) { + dbModel = await RepositoryModel.findOne({ externalId: "gh_" + r.id }); + } const model = dbModel || new RepositoryModel({ externalId: "gh_" + r.id }); model.name = r.full_name; model.url = r.html_url;