mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-11-17 17:44:09 +01:00
perf: paginate repository discovery and search
This commit is contained in:
1 parent
3fbf2cbad7
commit
0b967aa8af
11 files changed
+291
-76
No files matched your search
@@ -0,0 +1,22 @@
|
||||
import { octokit } from "./GitHubUtils";
|
||||
|
||||
/** One upstream page per request. Search cursors resume within that page. */
|
||||
export async function repositoryPickerPage(token: string, cursor = 0, query = "") {
|
||||
const size = query ? 100 : 20;
|
||||
const page = Math.floor(cursor / size) + 1;
|
||||
const offset = cursor % size;
|
||||
const response = await octokit(token).request("GET /user/repos", {
|
||||
visibility: "all", sort: "full_name", direction: "asc", per_page: size, page,
|
||||
});
|
||||
const items: { id: number; fullName: string }[] = [];
|
||||
let index = offset;
|
||||
for (; index < response.data.length; index++) {
|
||||
const repo = response.data[index];
|
||||
if (!query || repo.full_name.toLowerCase().includes(query.toLowerCase())) {
|
||||
items.push({ id: repo.id, fullName: repo.full_name });
|
||||
if (items.length === 20) { index++; break; }
|
||||
}
|
||||
}
|
||||
const hasNext = index < response.data.length || /rel="next"/.test(response.headers.link || "");
|
||||
return { items, nextCursor: hasNext ? (page - 1) * size + index : null };
|
||||
}
|
||||
@@ -1,3 +1,6 @@
|
||||
import { repositoryPickerPage } from "../../core/github-repository-picker";
|
||||
import { appUserToken } from "../../core/github-app";
|
||||
import { getCredentialToken } from "../../core/credentials";
|
||||
import AnonymousError from "../../core/AnonymousError";
|
||||
import { BillingQuota, BillingDraft } from "../../core/billing/models";
|
||||
import { cancelAccountBilling } from "../../core/billing/stripe";
|
||||
@@ -369,6 +372,25 @@ export async function getAllRepositories(user: User, force: boolean) {
|
||||
};
|
||||
});
|
||||
}
|
||||
router.get("/repository_picker", async (req, res) => {
|
||||
try {
|
||||
const user = await getUser(req);
|
||||
const cursor = req.query.cursor === undefined ? 0 : Number(req.query.cursor);
|
||||
const query = typeof req.query.q === "string" ? req.query.q.trim() : "";
|
||||
const connection = req.query.connection;
|
||||
if (!Number.isSafeInteger(cursor) || cursor < 0 || cursor > 1000000 || query.length > 200
|
||||
|| (connection !== undefined && connection !== "oauth" && connection !== "github-app")) {
|
||||
res.status(400).json({ error: "Invalid repository search" });
|
||||
return;
|
||||
}
|
||||
const token = connection === "github-app" ? await appUserToken(user.id)
|
||||
: connection === "oauth" ? await getCredentialToken(user.id) : await user.getAccessToken();
|
||||
if (!token) { res.status(401).json({ error: "GitHub connection required" }); return; }
|
||||
res.set("Cache-Control", "no-store");
|
||||
res.json(await repositoryPickerPage(token, cursor, query));
|
||||
} catch (error) { handleError(error, res, req); }
|
||||
});
|
||||
|
||||
router.get(
|
||||
"/all_repositories",
|
||||
async (req, res) => {
|
||||
|
||||
Reference in new issue
Block a user