mirror of
https://github.com/RayLabsHQ/gitea-mirror.git
synced 2026-11-09 02:32:23 +01:00
* fix: honor GH_API_URL across all Octokit call sites Six Octokit call sites constructed `new Octokit(...)` directly instead of going through `createGitHubClient()`, so `GH_API_URL` (and the `GITHUB_API_URL` fallback) only applied to the handful of flows that used the helper. For GHES / GHEC-with-data-residency users this surfaced most visibly as the "Test Connection" button hitting `api.github.com/user` and failing with 401 even when `GH_API_URL` was set correctly (#269). Route everything through `createGitHubClient()`: - src/pages/api/github/test-connection.ts (the reported failure) - src/pages/api/sync/repository.ts (public-repo sync) - src/lib/gitea-enhanced.ts (force-push detection + metadata octokit) - src/lib/scheduler-service.ts (auto-discovery, auto-mirror, auto-start) - src/tests/test-metadata-mirroring.ts (dev harness, for consistency) Side benefit: scheduler + sync paths now also get throttling, rate-limit tracking, and the standard User-Agent, which they were missing. `createGitHubClient`'s `token` parameter is made optional so the public-repo sync path (`new Octokit()` with no auth) can keep working. Fixes #269 * fix: address review findings - scheduler: pass config.githubConfig?.owner (the real DB field) instead of ?.username, which doesn't exist on the DB row and was silently resolving to undefined — matches every other DB-reading call site. - sync/repository.ts: revert to bare Octokit for the unauthenticated public-repo lookup to preserve fast-fail on the 60 req/hr limit. Still reads GH_API_URL / GITHUB_API_URL inline so GHES / GHEC data-residency users benefit. The throttling plugin's retry-with- backoff is wrong UX for a one-shot button click. - github.ts: revert createGitHubClient token back to required (no remaining callers pass undefined after the above). - gitea-enhanced.ts: make the leftover Octokit import type-only. - test-connection.test.ts: replace mid-test mock.module re-call with a mutable stub reference — safer against ESM live-binding semantics.
90 lines
2.3 KiB
TypeScript
90 lines
2.3 KiB
TypeScript
import type { APIRoute } from "astro";
|
|
import { createGitHubClient } from "@/lib/github";
|
|
import { createSecureErrorResponse } from "@/lib/utils";
|
|
|
|
export const POST: APIRoute = async ({ request }) => {
|
|
try {
|
|
const body = await request.json();
|
|
const { token, username } = body;
|
|
|
|
if (!token) {
|
|
return new Response(
|
|
JSON.stringify({
|
|
success: false,
|
|
message: "GitHub token is required",
|
|
}),
|
|
{
|
|
status: 400,
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
}
|
|
);
|
|
}
|
|
|
|
// Create an Octokit instance with the provided token.
|
|
// Uses createGitHubClient so GH_API_URL / GITHUB_API_URL routes the call
|
|
// to the correct endpoint for GHES / GHEC with data residency.
|
|
const octokit = createGitHubClient(token);
|
|
|
|
// Test the connection by fetching the authenticated user
|
|
const { data } = await octokit.users.getAuthenticated();
|
|
|
|
// Verify that the authenticated user matches the provided username (if provided)
|
|
if (username && data.login !== username) {
|
|
return new Response(
|
|
JSON.stringify({
|
|
success: false,
|
|
message: `Token belongs to ${data.login}, not ${username}`,
|
|
}),
|
|
{
|
|
status: 400,
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
}
|
|
);
|
|
}
|
|
|
|
// Return success response with user data
|
|
return new Response(
|
|
JSON.stringify({
|
|
success: true,
|
|
message: `Successfully connected to GitHub as ${data.login}`,
|
|
user: {
|
|
login: data.login,
|
|
name: data.name,
|
|
avatar_url: data.avatar_url,
|
|
},
|
|
}),
|
|
{
|
|
status: 200,
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
}
|
|
);
|
|
} catch (error) {
|
|
console.error("GitHub connection test failed:", error);
|
|
|
|
// Handle specific error types
|
|
if (error instanceof Error && (error as any).status === 401) {
|
|
return new Response(
|
|
JSON.stringify({
|
|
success: false,
|
|
message: "Invalid GitHub token",
|
|
}),
|
|
{
|
|
status: 401,
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
}
|
|
);
|
|
}
|
|
|
|
// Generic error response
|
|
return createSecureErrorResponse(error, "GitHub connection test", 500);
|
|
}
|
|
};
|