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 <noreply@anthropic.com>
This commit is contained in:
tdurieux
2026-05-05 00:59:38 +03:00
parent 1dde5dc308
commit 53959f677c
+6
View File
@@ -298,6 +298,12 @@ export async function getRepositoryFromGitHub(opt: {
repo: opt.repo, 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 }); const model = dbModel || new RepositoryModel({ externalId: "gh_" + r.id });
model.name = r.full_name; model.name = r.full_name;
model.url = r.html_url; model.url = r.html_url;