mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-12-02 17:13:33 +01:00
feat: store the repository picker list and update it by webhook
The anonymize page's "Your repositories" picker now pages and searches a per-user list stored in MongoDB instead of calling GitHub on every page and search. GitHub is read only when the list is empty, when the user presses the new refresh button, or after an installation change marks it stale. - One sync per list (Mongo lock with a renewed lease); full pagination in the background after the first page is returned; rate limits pause syncs. - GitHub App installation, installation_repositories and repository webhooks update the stored lists idempotently. The App must subscribe to the Repository event (documented in docs/github-app-setup.md). - Legacy OAuth lists change only on refresh. - Picker header shows "Updated 3 min ago" and a refresh button. - dev-mock: stored list, simulated refresh and webhook endpoint. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0640d741f1
commit
c73e992b7c
22 files changed
+1026
-153
No files matched your search
+42
-3
@@ -996,12 +996,51 @@ app.get("/api/admin/users/:username", (req, res) => {
|
||||
});
|
||||
app.post("/api/admin/users/:username/ban", (req, res) => res.json({ ok: true, reposQueued: 2 }));
|
||||
app.post("/api/admin/users/:username/:action", ok);
|
||||
// Stored "Your repositories" list, as the server keeps it in MongoDB: paging
|
||||
// and search read it, `force=1` simulates a GitHub refresh (about 2.5 s, then
|
||||
// one new repository), and a simulated App webhook changes it in place:
|
||||
// curl -XPOST localhost:4002/__mock/repository-webhook -H 'Content-Type: application/json' \
|
||||
// -d '{"action":"added","fullName":"research/new-paper"}' # also: removed, renamed (+ newName)
|
||||
// `{"action":"reset"}` restores the fixture list, saved 3 minutes ago.
|
||||
// `?repos=empty` on the first picker call starts from an empty list (first fetch).
|
||||
const fixtureRepos = () => Array.from({ length: 63 }, (_, id) => ({ id, fullName: `research/project-${String(id + 1).padStart(2, "0")}`, private: id % 3 === 0 }));
|
||||
const storedRepos = { rows: fixtureRepos(), syncedAt: new Date(Date.now() - 3 * 60000), syncingUntil: 0, nextId: 1000 };
|
||||
const finishMockSync = () => {
|
||||
if (!storedRepos.syncingUntil || Date.now() < storedRepos.syncingUntil) return;
|
||||
storedRepos.syncingUntil = 0;
|
||||
storedRepos.syncedAt = new Date();
|
||||
if (!storedRepos.rows.some(r => r.fullName === "research/camera-ready")) storedRepos.rows.push({ id: storedRepos.nextId++, fullName: "research/camera-ready", private: true });
|
||||
};
|
||||
app.get("/api/user/repository_picker", (req, res) => {
|
||||
finishMockSync();
|
||||
if (req.query.repos === "empty") { storedRepos.rows = []; storedRepos.syncedAt = null; }
|
||||
if (req.query.force === "1" || (!storedRepos.rows.length && !storedRepos.syncingUntil)) {
|
||||
storedRepos.syncingUntil = Date.now() + 2500;
|
||||
if (!storedRepos.rows.length) storedRepos.rows = fixtureRepos();
|
||||
}
|
||||
const cursor = Number(req.query.cursor || 0);
|
||||
const query = String(req.query.q || "").toLowerCase();
|
||||
const repos = Array.from({ length: 63 }, (_, id) => ({ id, fullName: `research/project-${String(id + 1).padStart(2, "0")}` }));
|
||||
const matches = repos.filter(repo => repo.fullName.toLowerCase().includes(query));
|
||||
res.json({ items: matches.slice(cursor, cursor + 20), nextCursor: cursor + 20 < matches.length ? cursor + 20 : null });
|
||||
const matches = storedRepos.rows.filter(repo => repo.fullName.toLowerCase().includes(query))
|
||||
.sort((a, b) => a.fullName.toLowerCase().localeCompare(b.fullName.toLowerCase()));
|
||||
res.json({
|
||||
items: matches.slice(cursor, cursor + 20),
|
||||
nextCursor: cursor + 20 < matches.length ? cursor + 20 : null,
|
||||
syncedAt: storedRepos.syncedAt, syncing: !!storedRepos.syncingUntil,
|
||||
webhooks: req.query.connection !== "oauth",
|
||||
});
|
||||
});
|
||||
app.post("/__mock/repository-webhook", express.json(), (req, res) => {
|
||||
const { action, fullName, newName } = req.body || {};
|
||||
if (action === "reset") {
|
||||
Object.assign(storedRepos, { rows: fixtureRepos(), syncedAt: new Date(Date.now() - 3 * 60000), syncingUntil: 0 });
|
||||
return res.json({ count: storedRepos.rows.length });
|
||||
}
|
||||
if (!/^[^/\s]+\/[^/\s]+$/.test(fullName || "")) return res.status(400).json({ error: "fullName owner/name required" });
|
||||
const existing = storedRepos.rows.find(r => r.fullName === fullName);
|
||||
if (action === "added" && !existing) storedRepos.rows.push({ id: storedRepos.nextId++, fullName, private: true });
|
||||
else if (action === "removed") storedRepos.rows = storedRepos.rows.filter(r => r.fullName !== fullName);
|
||||
else if (action === "renamed" && existing && /^[^/\s]+\/[^/\s]+$/.test(newName || "")) existing.fullName = newName;
|
||||
res.json({ count: storedRepos.rows.length });
|
||||
});
|
||||
|
||||
app.get("/api/user/:username/all_repositories", (req, res) => {
|
||||
|
||||
Reference in new issue
Block a user