Compare commits

...

8 Commits

Author SHA1 Message Date
Arunavo Ray
99336e2607 chore: bump version to 2.9.2 2025-05-28 10:15:43 +05:30
Arunavo Ray
cba421d606 feat: enhance error logging for better debugging of JSON parsing issues
- Add comprehensive error logging in mirror-repo API endpoint
- Enhance HTTP client error handling with detailed response information
- Improve concurrency utility error reporting with context
- Add specific detection and guidance for JSON parsing errors
- Include troubleshooting information in error responses
- Update tests to accommodate enhanced logging

This will help users diagnose issues like 'JSON Parse error: Unexpected EOF'
by providing detailed information about what responses are being received
from the Gitea API and what might be causing the failures.
2025-05-28 10:13:41 +05:30
Arunavo Ray
c4b9a82806 chore: bump version to 2.9.1 2025-05-28 10:00:14 +05:30
Arunavo Ray
38e0fb33b9 fix: resolve JSON parsing error and standardize HTTP client usage
- Fix JSON parsing error in getOrCreateGiteaOrg function (#19)
  - Add content-type validation before JSON parsing
  - Add response cloning for better error debugging
  - Enhance error messages with actual response content
  - Add comprehensive logging for troubleshooting

- Standardize HTTP client usage across codebase
  - Create new http-client.ts utility with consistent error handling
  - Replace all superagent calls with fetch-based functions
  - Replace all axios calls with fetch-based functions
  - Remove superagent, axios, and @types/superagent dependencies
  - Update tests to mock new HTTP client
  - Maintain backward compatibility

- Benefits:
  - Smaller bundle size (removed 3 HTTP client libraries)
  - Better performance (leveraging Bun's optimized fetch)
  - Consistent error handling across all HTTP operations
  - Improved debugging with detailed error messages
  - Easier maintenance with single HTTP client pattern
2025-05-28 09:56:59 +05:30
Arunavo Ray
22a4b71653 docs: add SQLite permission troubleshooting for direct installation
- Add new section 'Database Permissions for Direct Installation' to README
- Explain common SQLite permission errors when running without Docker
- Provide secure permission fixes (chmod 755/644 instead of 777)
- Clarify why Docker deployment avoids these issues
- Recommend Docker/Docker Compose as preferred deployment method

Addresses permission issues reported by users running the application directly on their systems.
2025-05-28 09:21:01 +05:30
ARUNAVO RAY
52568eda36 Merge pull request #21 from arunavo4/18-meta-docs-links-are-broken
fix: correct broken documentation links in README
2025-05-28 09:11:59 +05:30
Arunavo Ray
a84191f0a5 fix: correct broken documentation links in README
- Fix Quick Start Guide link to point to src/content/docs/quickstart.md
- Fix Configuration Guide link to point to src/content/docs/configuration.md
- Links were previously pointing to non-existent docs/ directory
2025-05-28 09:10:38 +05:30
Arunavo Ray
33829eda20 fix: update image sizes in README for better display on dashboard 2025-05-25 11:08:36 +05:30
19 changed files with 699 additions and 675 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 140 KiB

After

Width:  |  Height:  |  Size: 816 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 164 KiB

After

Width:  |  Height:  |  Size: 945 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 194 KiB

After

Width:  |  Height:  |  Size: 943 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 88 KiB

After

Width:  |  Height:  |  Size: 784 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 170 KiB

After

Width:  |  Height:  |  Size: 970 KiB

View File

@@ -30,7 +30,7 @@ sudo LOCAL_REPO_DIR=~/Development/gitea-mirror ./scripts/gitea-mirror-lxc-local.
See the [LXC Container Deployment Guide](scripts/README-lxc.md).
<p align="center">
<img src=".github/assets/dashboard.png" alt="Dashboard" width="80%"/>
<img src=".github/assets/dashboard.png" alt="Dashboard" width="full"/>
</p>
## ✨ Features
@@ -50,12 +50,12 @@ See the [LXC Container Deployment Guide](scripts/README-lxc.md).
## 📸 Screenshots
<p align="center">
<img src=".github/assets/repositories.png" width="45%"/>
<img src=".github/assets/organisations.png" width="45%"/>
<img src=".github/assets/repositories.png" width="49%"/>
<img src=".github/assets/organisations.png" width="49%"/>
</p>
<p align="center">
<img src=".github/assets/configuration.png" width="45%"/>
<img src=".github/assets/activity.png" width="45%"/>
<img src=".github/assets/configuration.png" width="49%"/>
<img src=".github/assets/activity.png" width="49%"/>
</p>
### Dashboard
@@ -69,7 +69,7 @@ Easily configure your GitHub and Gitea connections, set up automatic mirroring s
## Getting Started
See the [Quick Start Guide](docs/quickstart.md) for detailed instructions on getting up and running quickly.
See the [Quick Start Guide](src/content/docs/quickstart.md) for detailed instructions on getting up and running quickly.
### Prerequisites
@@ -282,9 +282,39 @@ bun run reset-users
bun run check-db
```
##### Database Permissions for Direct Installation
> [!IMPORTANT]
> **If you're running the application directly** (not using Docker), you may encounter SQLite permission errors. This is because SQLite requires both read/write access to the database file and write access to the directory containing the database.
**Common Error:**
```
Error: [ERROR] SQLiteError: attempt to write a readonly database
```
**Solution:**
```bash
# Ensure the data directory exists and has proper permissions
mkdir -p data
chmod 755 data
# If the database file already exists, ensure it's writable
chmod 644 data/gitea-mirror.db
# Make sure the user running the application owns the data directory
chown -R $(whoami) data/
```
**Why Docker doesn't have this issue:**
- Docker containers run with a dedicated user (`gitea-mirror`) that owns the `/app/data` directory
- The container setup ensures proper permissions are set during image build
- Volume mounts are handled by Docker with appropriate permissions
**Recommended approach:** Use Docker or Docker Compose for deployment to avoid permission issues entirely.
### Configuration
Gitea Mirror can be configured through environment variables or through the web UI. See the [Configuration Guide](docs/configuration.md) for more details.
Gitea Mirror can be configured through environment variables or through the web UI. See the [Configuration Guide](src/content/docs/configuration.md) for more details.
Key configuration options include:

607
bun.lock

File diff suppressed because it is too large Load Diff

View File

@@ -1,19 +1,18 @@
{
"name": "gitea-mirror",
"type": "module",
"version": "2.9.0",
"version": "2.9.2",
"engines": {
"bun": ">=1.2.9"
},
"scripts": {
"setup": "bun install && bun run manage-db init && bun run update-db",
"setup": "bun install && bun run manage-db init",
"dev": "bunx --bun astro dev",
"dev:clean": "bun run cleanup-db && bun run manage-db init && bun run update-db && bunx --bun astro dev",
"dev:clean": "bun run cleanup-db && bun run manage-db init && bunx --bun astro dev",
"build": "bunx --bun astro build",
"cleanup-db": "rm -f gitea-mirror.db data/gitea-mirror.db",
"manage-db": "bun scripts/manage-db.ts",
"init-db": "bun scripts/manage-db.ts init",
"update-db": "bun scripts/update-mirror-jobs-table.ts",
"check-db": "bun scripts/manage-db.ts check",
"fix-db": "bun scripts/manage-db.ts fix",
"reset-users": "bun scripts/manage-db.ts reset-users",
@@ -26,7 +25,7 @@
"test-shutdown-cleanup": "bun scripts/test-graceful-shutdown.ts --cleanup",
"preview": "bunx --bun astro preview",
"start": "bun dist/server/entry.mjs",
"start:fresh": "bun run cleanup-db && bun run manage-db init && bun run update-db && bun dist/server/entry.mjs",
"start:fresh": "bun run cleanup-db && bun run manage-db init && bun dist/server/entry.mjs",
"test": "bun test",
"test:watch": "bun test --watch",
"test:coverage": "bun test --coverage",
@@ -55,7 +54,6 @@
"@types/react": "^19.1.4",
"@types/react-dom": "^19.1.5",
"astro": "^5.7.13",
"axios": "^1.9.0",
"bcryptjs": "^3.0.2",
"canvas-confetti": "^1.9.3",
"class-variance-authority": "^0.7.1",
@@ -70,7 +68,6 @@
"react-dom": "^19.1.0",
"react-icons": "^5.5.0",
"sonner": "^2.0.3",
"superagent": "^10.2.1",
"tailwind-merge": "^3.3.0",
"tailwindcss": "^4.1.7",
"tw-animate-css": "^1.3.0",
@@ -82,7 +79,6 @@
"@testing-library/react": "^16.3.0",
"@types/bcryptjs": "^3.0.0",
"@types/jsonwebtoken": "^9.0.9",
"@types/superagent": "^8.1.9",
"@types/uuid": "^10.0.0",
"@vitejs/plugin-react": "^4.4.1",
"jsdom": "^26.1.0",

View File

@@ -313,7 +313,6 @@ export function ActivityLog() {
setIsInitialLoading(true);
setShowCleanupDialog(false);
// Use fetch directly to avoid potential axios issues
const response = await fetch('/api/activities/cleanup', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },

View File

@@ -1,6 +1,7 @@
import { describe, test, expect, mock, beforeEach, afterEach } from "bun:test";
import { Octokit } from "@octokit/rest";
import { repoStatusEnum } from "@/types/Repository";
import { getOrCreateGiteaOrg } from "./gitea";
// Mock the isRepoPresentInGitea function
const mockIsRepoPresentInGitea = mock(() => Promise.resolve(false));
@@ -27,23 +28,17 @@ mock.module("@/lib/helpers", () => {
};
});
// Mock superagent
mock.module("superagent", () => {
const mockPost = mock(() => ({
set: () => ({
set: () => ({
send: () => Promise.resolve({ body: { id: 123 } })
})
})
}));
const mockGet = mock(() => ({
set: () => Promise.resolve({ body: [] })
}));
// Mock http-client
mock.module("@/lib/http-client", () => {
return {
post: mockPost,
get: mockGet
httpPost: mock(() => Promise.resolve({ data: { id: 123 }, status: 200, statusText: 'OK', headers: new Headers() })),
httpGet: mock(() => Promise.resolve({ data: [], status: 200, statusText: 'OK', headers: new Headers() })),
HttpError: class MockHttpError extends Error {
constructor(message: string, public status: number, public statusText: string, public response?: string) {
super(message);
this.name = 'HttpError';
}
}
};
});
@@ -117,4 +112,96 @@ describe("Gitea Repository Mirroring", () => {
// Check that the function was called
expect(mirrorGithubRepoToGitea).toHaveBeenCalled();
});
test("getOrCreateGiteaOrg handles JSON parsing errors gracefully", async () => {
// Mock fetch to return invalid JSON
const originalFetch = global.fetch;
global.fetch = mock(async (url: string) => {
if (url.includes("/api/v1/orgs/")) {
// Mock response that looks successful but has invalid JSON
return {
ok: true,
status: 200,
headers: {
get: (name: string) => name === "content-type" ? "application/json" : null
},
json: () => Promise.reject(new Error("Unexpected token in JSON")),
text: () => Promise.resolve("Invalid JSON response"),
clone: function() {
return {
text: () => Promise.resolve("Invalid JSON response")
};
}
} as any;
}
return originalFetch(url);
});
const config = {
userId: "user-id",
giteaConfig: {
url: "https://gitea.example.com",
token: "gitea-token"
}
};
try {
await getOrCreateGiteaOrg({
orgName: "test-org",
config
});
// Should not reach here
expect(true).toBe(false);
} catch (error) {
// Should catch the JSON parsing error with a descriptive message
expect(error).toBeInstanceOf(Error);
expect((error as Error).message).toContain("Failed to parse JSON response from Gitea API");
} finally {
// Restore original fetch
global.fetch = originalFetch;
}
});
test("getOrCreateGiteaOrg handles non-JSON content-type gracefully", async () => {
// Mock fetch to return HTML instead of JSON
const originalFetch = global.fetch;
global.fetch = mock(async (url: string) => {
if (url.includes("/api/v1/orgs/")) {
return {
ok: true,
status: 200,
headers: {
get: (name: string) => name === "content-type" ? "text/html" : null
},
text: () => Promise.resolve("<html><body>Error page</body></html>")
} as any;
}
return originalFetch(url);
});
const config = {
userId: "user-id",
giteaConfig: {
url: "https://gitea.example.com",
token: "gitea-token"
}
};
try {
await getOrCreateGiteaOrg({
orgName: "test-org",
config
});
// Should not reach here
expect(true).toBe(false);
} catch (error) {
// Should catch the content-type error
expect(error).toBeInstanceOf(Error);
expect((error as Error).message).toContain("Invalid response format from Gitea API");
expect((error as Error).message).toContain("text/html");
} finally {
// Restore original fetch
global.fetch = originalFetch;
}
});
});

View File

@@ -6,7 +6,7 @@ import {
import { Octokit } from "@octokit/rest";
import type { Config } from "@/types/config";
import type { Organization, Repository } from "./db/schema";
import superagent from "superagent";
import { httpPost, httpGet } from "./http-client";
import { createMirrorJob } from "./helpers";
import { db, organizations, repositories } from "./db";
import { eq } from "drizzle-orm";
@@ -181,19 +181,17 @@ export const mirrorGithubRepoToGitea = async ({
const apiUrl = `${config.giteaConfig.url}/api/v1/repos/migrate`;
const response = await superagent
.post(apiUrl)
.set("Authorization", `token ${config.giteaConfig.token}`)
.set("Content-Type", "application/json")
.send({
clone_addr: cloneAddress,
repo_name: repository.name,
mirror: true,
private: repository.isPrivate,
repo_owner: config.giteaConfig.username,
description: "",
service: "git",
});
const response = await httpPost(apiUrl, {
clone_addr: cloneAddress,
repo_name: repository.name,
mirror: true,
private: repository.isPrivate,
repo_owner: config.giteaConfig.username,
description: "",
service: "git",
}, {
"Authorization": `token ${config.giteaConfig.token}`,
});
// clone issues
if (config.githubConfig.mirrorIssues) {
@@ -229,7 +227,7 @@ export const mirrorGithubRepoToGitea = async ({
status: "mirrored",
});
return response.body;
return response.data;
} catch (error) {
console.error(
`Error while mirroring repository ${repository.name}: ${
@@ -283,6 +281,8 @@ export async function getOrCreateGiteaOrg({
}
try {
console.log(`Attempting to get or create Gitea organization: ${orgName}`);
const orgRes = await fetch(
`${config.giteaConfig.url}/api/v1/orgs/${orgName}`,
{
@@ -293,13 +293,36 @@ export async function getOrCreateGiteaOrg({
}
);
console.log(`Get org response status: ${orgRes.status} for org: ${orgName}`);
if (orgRes.ok) {
const org = await orgRes.json();
// Note: Organization events are handled by the main mirroring process
// to avoid duplicate events
return org.id;
// Check if response is actually JSON
const contentType = orgRes.headers.get("content-type");
if (!contentType || !contentType.includes("application/json")) {
console.warn(`Expected JSON response but got content-type: ${contentType}`);
const responseText = await orgRes.text();
console.warn(`Response body: ${responseText}`);
throw new Error(`Invalid response format from Gitea API. Expected JSON but got: ${contentType}`);
}
// Clone the response to handle potential JSON parsing errors
const orgResClone = orgRes.clone();
try {
const org = await orgRes.json();
console.log(`Successfully retrieved existing org: ${orgName} with ID: ${org.id}`);
// Note: Organization events are handled by the main mirroring process
// to avoid duplicate events
return org.id;
} catch (jsonError) {
const responseText = await orgResClone.text();
console.error(`Failed to parse JSON response for existing org: ${responseText}`);
throw new Error(`Failed to parse JSON response from Gitea API: ${jsonError instanceof Error ? jsonError.message : String(jsonError)}`);
}
}
console.log(`Organization ${orgName} not found, attempting to create it`);
const createRes = await fetch(`${config.giteaConfig.url}/api/v1/orgs`, {
method: "POST",
headers: {
@@ -314,21 +337,46 @@ export async function getOrCreateGiteaOrg({
}),
});
console.log(`Create org response status: ${createRes.status} for org: ${orgName}`);
if (!createRes.ok) {
throw new Error(`Failed to create Gitea org: ${await createRes.text()}`);
const errorText = await createRes.text();
console.error(`Failed to create org ${orgName}. Status: ${createRes.status}, Response: ${errorText}`);
throw new Error(`Failed to create Gitea org: ${errorText}`);
}
// Check if response is actually JSON
const createContentType = createRes.headers.get("content-type");
if (!createContentType || !createContentType.includes("application/json")) {
console.warn(`Expected JSON response but got content-type: ${createContentType}`);
const responseText = await createRes.text();
console.warn(`Response body: ${responseText}`);
throw new Error(`Invalid response format from Gitea API. Expected JSON but got: ${createContentType}`);
}
// Note: Organization creation events are handled by the main mirroring process
// to avoid duplicate events
const newOrg = await createRes.json();
return newOrg.id;
// Clone the response to handle potential JSON parsing errors
const createResClone = createRes.clone();
try {
const newOrg = await createRes.json();
console.log(`Successfully created new org: ${orgName} with ID: ${newOrg.id}`);
return newOrg.id;
} catch (jsonError) {
const responseText = await createResClone.text();
console.error(`Failed to parse JSON response for new org: ${responseText}`);
throw new Error(`Failed to parse JSON response from Gitea API: ${jsonError instanceof Error ? jsonError.message : String(jsonError)}`);
}
} catch (error) {
const errorMessage =
error instanceof Error
? error.message
: "Unknown error occurred in getOrCreateGiteaOrg.";
console.error(`Error in getOrCreateGiteaOrg for ${orgName}: ${errorMessage}`);
await createMirrorJob({
userId: config.userId,
organizationId: orgId,
@@ -410,17 +458,15 @@ export async function mirrorGitHubRepoToGiteaOrg({
const apiUrl = `${config.giteaConfig.url}/api/v1/repos/migrate`;
const migrateRes = await superagent
.post(apiUrl)
.set("Authorization", `token ${config.giteaConfig.token}`)
.set("Content-Type", "application/json")
.send({
clone_addr: cloneAddress,
uid: giteaOrgId,
repo_name: repository.name,
mirror: true,
private: repository.isPrivate,
});
const migrateRes = await httpPost(apiUrl, {
clone_addr: cloneAddress,
uid: giteaOrgId,
repo_name: repository.name,
mirror: true,
private: repository.isPrivate,
}, {
"Authorization": `token ${config.giteaConfig.token}`,
});
// Clone issues
if (config.githubConfig?.mirrorIssues) {
@@ -458,7 +504,7 @@ export async function mirrorGitHubRepoToGiteaOrg({
status: "mirrored",
});
return migrateRes.body;
return migrateRes.data;
} catch (error) {
console.error(
`Error while mirroring repository ${repository.name}: ${
@@ -751,9 +797,9 @@ export const syncGiteaRepo = async ({
// Use the actual owner where the repo was found
const apiUrl = `${config.giteaConfig.url}/api/v1/repos/${actualOwner}/${repository.name}/mirror-sync`;
const response = await superagent
.post(apiUrl)
.set("Authorization", `token ${config.giteaConfig.token}`);
const response = await httpPost(apiUrl, undefined, {
"Authorization": `token ${config.giteaConfig.token}`,
});
// Mark repo as "synced" in DB
await db
@@ -779,7 +825,7 @@ export const syncGiteaRepo = async ({
console.log(`Repository ${repository.name} synced successfully`);
return response.body;
return response.data;
} catch (error) {
console.error(
`Error while syncing repository ${repository.name}: ${
@@ -866,13 +912,14 @@ export const mirrorGitRepoIssuesToGitea = async ({
}
// Get existing labels from Gitea
const giteaLabelsRes = await superagent
.get(
`${config.giteaConfig.url}/api/v1/repos/${repoOrigin}/${repository.name}/labels`
)
.set("Authorization", `token ${config.giteaConfig.token}`);
const giteaLabelsRes = await httpGet(
`${config.giteaConfig.url}/api/v1/repos/${repoOrigin}/${repository.name}/labels`,
{
"Authorization": `token ${config.giteaConfig.token}`,
}
);
const giteaLabels = giteaLabelsRes.body;
const giteaLabels = giteaLabelsRes.data;
const labelMap = new Map<string, number>(
giteaLabels.map((label: any) => [label.name, label.id])
);
@@ -897,15 +944,16 @@ export const mirrorGitRepoIssuesToGitea = async ({
giteaLabelIds.push(labelMap.get(name)!);
} else {
try {
const created = await superagent
.post(
`${config.giteaConfig.url}/api/v1/repos/${repoOrigin}/${repository.name}/labels`
)
.set("Authorization", `token ${config.giteaConfig.token}`)
.send({ name, color: "#ededed" }); // Default color
const created = await httpPost(
`${config.giteaConfig!.url}/api/v1/repos/${repoOrigin}/${repository.name}/labels`,
{ name, color: "#ededed" }, // Default color
{
"Authorization": `token ${config.giteaConfig!.token}`,
}
);
labelMap.set(name, created.body.id);
giteaLabelIds.push(created.body.id);
labelMap.set(name, created.data.id);
giteaLabelIds.push(created.data.id);
} catch (labelErr) {
console.error(
`Failed to create label "${name}" in Gitea: ${labelErr}`
@@ -931,12 +979,13 @@ export const mirrorGitRepoIssuesToGitea = async ({
};
// Create the issue in Gitea
const createdIssue = await superagent
.post(
`${config.giteaConfig.url}/api/v1/repos/${repoOrigin}/${repository.name}/issues`
)
.set("Authorization", `token ${config.giteaConfig.token}`)
.send(issuePayload);
const createdIssue = await httpPost(
`${config.giteaConfig!.url}/api/v1/repos/${repoOrigin}/${repository.name}/issues`,
issuePayload,
{
"Authorization": `token ${config.giteaConfig!.token}`,
}
);
// Clone comments
const comments = await octokit.paginate(
@@ -955,21 +1004,22 @@ export const mirrorGitRepoIssuesToGitea = async ({
await processWithRetry(
comments,
async (comment) => {
await superagent
.post(
`${config.giteaConfig.url}/api/v1/repos/${repoOrigin}/${repository.name}/issues/${createdIssue.body.number}/comments`
)
.set("Authorization", `token ${config.giteaConfig.token}`)
.send({
await httpPost(
`${config.giteaConfig!.url}/api/v1/repos/${repoOrigin}/${repository.name}/issues/${createdIssue.data.number}/comments`,
{
body: `@${comment.user?.login} commented on GitHub:\n\n${comment.body}`,
});
},
{
"Authorization": `token ${config.giteaConfig!.token}`,
}
);
return comment;
},
{
concurrencyLimit: 5,
maxRetries: 2,
retryDelay: 1000,
onRetry: (comment, error, attempt) => {
onRetry: (_comment, error, attempt) => {
console.log(`Retrying comment (attempt ${attempt}): ${error.message}`);
}
}

204
src/lib/http-client.ts Normal file
View File

@@ -0,0 +1,204 @@
/**
* HTTP client utility functions using fetch() for consistent error handling
*/
export interface HttpResponse<T = any> {
data: T;
status: number;
statusText: string;
headers: Headers;
}
export class HttpError extends Error {
constructor(
message: string,
public status: number,
public statusText: string,
public response?: string
) {
super(message);
this.name = 'HttpError';
}
}
/**
* Enhanced fetch with consistent error handling and JSON parsing
*/
export async function httpRequest<T = any>(
url: string,
options: RequestInit = {}
): Promise<HttpResponse<T>> {
try {
const response = await fetch(url, {
...options,
headers: {
'Content-Type': 'application/json',
...options.headers,
},
});
// Clone response for error handling
const responseClone = response.clone();
if (!response.ok) {
let errorMessage = `HTTP ${response.status}: ${response.statusText}`;
let responseText = '';
try {
responseText = await responseClone.text();
if (responseText) {
errorMessage += ` - ${responseText}`;
}
} catch {
// Ignore text parsing errors
}
throw new HttpError(
errorMessage,
response.status,
response.statusText,
responseText
);
}
// Check content type for JSON responses
const contentType = response.headers.get('content-type');
let data: T;
if (contentType && contentType.includes('application/json')) {
try {
data = await response.json();
} catch (jsonError) {
const responseText = await responseClone.text();
// Enhanced JSON parsing error logging
console.error("=== JSON PARSING ERROR ===");
console.error("URL:", url);
console.error("Status:", response.status, response.statusText);
console.error("Content-Type:", contentType);
console.error("Response length:", responseText.length);
console.error("Response preview (first 500 chars):", responseText.substring(0, 500));
console.error("JSON Error:", jsonError instanceof Error ? jsonError.message : String(jsonError));
console.error("========================");
throw new HttpError(
`Failed to parse JSON response from ${url}: ${jsonError instanceof Error ? jsonError.message : String(jsonError)}. Response: ${responseText.substring(0, 200)}${responseText.length > 200 ? '...' : ''}`,
response.status,
response.statusText,
responseText
);
}
} else {
// For non-JSON responses, return text as data
data = (await response.text()) as unknown as T;
}
return {
data,
status: response.status,
statusText: response.statusText,
headers: response.headers,
};
} catch (error) {
if (error instanceof HttpError) {
throw error;
}
// Handle network errors, etc.
throw new HttpError(
`Network error: ${error instanceof Error ? error.message : String(error)}`,
0,
'Network Error'
);
}
}
/**
* GET request
*/
export async function httpGet<T = any>(
url: string,
headers?: Record<string, string>
): Promise<HttpResponse<T>> {
return httpRequest<T>(url, {
method: 'GET',
headers,
});
}
/**
* POST request
*/
export async function httpPost<T = any>(
url: string,
body?: any,
headers?: Record<string, string>
): Promise<HttpResponse<T>> {
return httpRequest<T>(url, {
method: 'POST',
headers,
body: body ? JSON.stringify(body) : undefined,
});
}
/**
* PUT request
*/
export async function httpPut<T = any>(
url: string,
body?: any,
headers?: Record<string, string>
): Promise<HttpResponse<T>> {
return httpRequest<T>(url, {
method: 'PUT',
headers,
body: body ? JSON.stringify(body) : undefined,
});
}
/**
* DELETE request
*/
export async function httpDelete<T = any>(
url: string,
headers?: Record<string, string>
): Promise<HttpResponse<T>> {
return httpRequest<T>(url, {
method: 'DELETE',
headers,
});
}
/**
* Gitea-specific HTTP client with authentication
*/
export class GiteaHttpClient {
constructor(
private baseUrl: string,
private token: string
) {}
private getHeaders(additionalHeaders?: Record<string, string>): Record<string, string> {
return {
'Authorization': `token ${this.token}`,
'Content-Type': 'application/json',
...additionalHeaders,
};
}
async get<T = any>(endpoint: string): Promise<HttpResponse<T>> {
return httpGet<T>(`${this.baseUrl}${endpoint}`, this.getHeaders());
}
async post<T = any>(endpoint: string, body?: any): Promise<HttpResponse<T>> {
return httpPost<T>(`${this.baseUrl}${endpoint}`, body, this.getHeaders());
}
async put<T = any>(endpoint: string, body?: any): Promise<HttpResponse<T>> {
return httpPut<T>(`${this.baseUrl}${endpoint}`, body, this.getHeaders());
}
async delete<T = any>(endpoint: string): Promise<HttpResponse<T>> {
return httpDelete<T>(`${this.baseUrl}${endpoint}`, this.getHeaders());
}
}

View File

@@ -1,7 +1,6 @@
import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";
import axios from "axios";
import type { AxiosError, AxiosRequestConfig } from "axios";
import { httpRequest, HttpError } from "@/lib/http-client";
import type { RepoStatus } from "@/types/Repository";
export const API_BASE = "/api";
@@ -41,10 +40,10 @@ export function safeParse<T>(value: unknown): T | undefined {
export async function apiRequest<T>(
endpoint: string,
options: AxiosRequestConfig = {}
options: RequestInit = {}
): Promise<T> {
try {
const response = await axios<T>(`${API_BASE}${endpoint}`, {
const response = await httpRequest<T>(`${API_BASE}${endpoint}`, {
headers: {
"Content-Type": "application/json",
...(options.headers || {}),
@@ -54,10 +53,10 @@ export async function apiRequest<T>(
return response.data;
} catch (err) {
const error = err as AxiosError<{ message?: string }>;
const error = err as HttpError;
const message =
error.response?.data?.message ||
error.response ||
error.message ||
"An unknown error occurred";

View File

@@ -5,19 +5,19 @@ describe("processInParallel", () => {
test("processes items in parallel with concurrency control", async () => {
// Create an array of numbers to process
const items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
// Create a mock function to track execution
const processItem = mock(async (item: number) => {
// Simulate async work
await new Promise(resolve => setTimeout(resolve, 10));
return item * 2;
});
// Create a mock progress callback
const onProgress = mock((completed: number, total: number, result?: number) => {
// Progress tracking
});
// Process the items with a concurrency limit of 3
const results = await processInParallel(
items,
@@ -25,25 +25,25 @@ describe("processInParallel", () => {
3,
onProgress
);
// Verify results
expect(results).toEqual([2, 4, 6, 8, 10, 12, 14, 16, 18, 20]);
// Verify that processItem was called for each item
expect(processItem).toHaveBeenCalledTimes(10);
// Verify that onProgress was called for each item
expect(onProgress).toHaveBeenCalledTimes(10);
// Verify the last call to onProgress had the correct completed/total values
expect(onProgress.mock.calls[9][0]).toBe(10); // completed
expect(onProgress.mock.calls[9][1]).toBe(10); // total
});
test("handles errors in processing", async () => {
// Create an array of numbers to process
const items = [1, 2, 3, 4, 5];
// Create a mock function that throws an error for item 3
const processItem = mock(async (item: number) => {
if (item === 3) {
@@ -51,24 +51,24 @@ describe("processInParallel", () => {
}
return item * 2;
});
// Create a spy for console.error
const originalConsoleError = console.error;
const consoleErrorMock = mock(() => {});
console.error = consoleErrorMock;
try {
// Process the items
const results = await processInParallel(items, processItem);
// Verify results (should have 4 items, missing the one that errored)
expect(results).toEqual([2, 4, 8, 10]);
// Verify that processItem was called for each item
expect(processItem).toHaveBeenCalledTimes(5);
// Verify that console.error was called once
expect(consoleErrorMock).toHaveBeenCalledTimes(1);
// Verify that console.error was called (enhanced logging calls it multiple times)
expect(consoleErrorMock).toHaveBeenCalled();
} finally {
// Restore console.error
console.error = originalConsoleError;
@@ -80,51 +80,51 @@ describe("processWithRetry", () => {
test("retries failed operations", async () => {
// Create an array of numbers to process
const items = [1, 2, 3];
// Create a counter to track retry attempts
const attemptCounts: Record<number, number> = { 1: 0, 2: 0, 3: 0 };
// Create a mock function that fails on first attempt for item 2
const processItem = mock(async (item: number) => {
attemptCounts[item]++;
if (item === 2 && attemptCounts[item] === 1) {
throw new Error("Temporary error");
}
return item * 2;
});
// Create a mock for the onRetry callback
const onRetry = mock((item: number, error: Error, attempt: number) => {
// Retry tracking
});
// Process the items with retry
const results = await processWithRetry(items, processItem, {
maxRetries: 2,
retryDelay: 10,
onRetry,
});
// Verify results
expect(results).toEqual([2, 4, 6]);
// Verify that item 2 was retried once
expect(attemptCounts[1]).toBe(1); // No retries
expect(attemptCounts[2]).toBe(2); // One retry
expect(attemptCounts[3]).toBe(1); // No retries
// Verify that onRetry was called once
expect(onRetry).toHaveBeenCalledTimes(1);
expect(onRetry.mock.calls[0][0]).toBe(2); // item
expect(onRetry.mock.calls[0][2]).toBe(1); // attempt
});
test("gives up after max retries", async () => {
// Create an array of numbers to process
const items = [1, 2];
// Create a mock function that always fails for item 2
const processItem = mock(async (item: number) => {
if (item === 2) {
@@ -132,17 +132,17 @@ describe("processWithRetry", () => {
}
return item * 2;
});
// Create a mock for the onRetry callback
const onRetry = mock((item: number, error: Error, attempt: number) => {
// Retry tracking
});
// Create a spy for console.error
const originalConsoleError = console.error;
const consoleErrorMock = mock(() => {});
console.error = consoleErrorMock;
try {
// Process the items with retry
const results = await processWithRetry(items, processItem, {
@@ -150,15 +150,15 @@ describe("processWithRetry", () => {
retryDelay: 10,
onRetry,
});
// Verify results (should have 1 item, missing the one that errored)
expect(results).toEqual([2]);
// Verify that onRetry was called twice (for 2 retry attempts)
expect(onRetry).toHaveBeenCalledTimes(2);
// Verify that console.error was called once
expect(consoleErrorMock).toHaveBeenCalledTimes(1);
// Verify that console.error was called (enhanced logging calls it multiple times)
expect(consoleErrorMock).toHaveBeenCalled();
} finally {
// Restore console.error
console.error = originalConsoleError;

View File

@@ -46,11 +46,25 @@ export async function processInParallel<T, R>(
const batchResults = await Promise.allSettled(batchPromises);
// Process results and handle errors
for (const result of batchResults) {
for (let j = 0; j < batchResults.length; j++) {
const result = batchResults[j];
if (result.status === 'fulfilled') {
results.push(result.value);
} else {
console.error('Error processing item:', result.reason);
const itemIndex = i + j;
console.error("=== BATCH ITEM PROCESSING ERROR ===");
console.error("Batch index:", Math.floor(i / concurrencyLimit));
console.error("Item index in batch:", j);
console.error("Global item index:", itemIndex);
console.error("Error type:", result.reason?.constructor?.name);
console.error("Error message:", result.reason instanceof Error ? result.reason.message : String(result.reason));
if (result.reason instanceof Error && result.reason.message.includes('JSON')) {
console.error("🚨 JSON parsing error in batch processing");
console.error("This indicates an API response issue from Gitea");
}
console.error("==================================");
}
}
}
@@ -139,6 +153,21 @@ export async function processWithRetry<T, R>(
const delay = retryDelay * Math.pow(2, attempt - 1);
await new Promise(resolve => setTimeout(resolve, delay));
} else {
// Enhanced error logging for final failure
console.error("=== ITEM PROCESSING FAILED (MAX RETRIES EXCEEDED) ===");
console.error("Item:", getItemId ? getItemId(item) : 'unknown');
console.error("Error type:", lastError.constructor.name);
console.error("Error message:", lastError.message);
console.error("Attempts made:", maxRetries + 1);
if (lastError.message.includes('JSON')) {
console.error("🚨 JSON-related error detected in item processing");
console.error("This suggests an issue with API responses from Gitea");
}
console.error("Stack trace:", lastError.stack);
console.error("================================================");
throw lastError;
}
}

View File

@@ -1,5 +1,4 @@
import { describe, test, expect, mock, beforeEach, afterEach } from "bun:test";
import axios from "axios";
// Mock the POST function
const mockPOST = mock(async ({ request }) => {

View File

@@ -1,5 +1,5 @@
import type { APIRoute } from 'astro';
import axios from 'axios';
import { httpGet, HttpError } from '@/lib/http-client';
export const POST: APIRoute = async ({ request }) => {
try {
@@ -25,11 +25,9 @@ export const POST: APIRoute = async ({ request }) => {
const baseUrl = url.endsWith('/') ? url.slice(0, -1) : url;
// Test the connection by fetching the authenticated user
const response = await axios.get(`${baseUrl}/api/v1/user`, {
headers: {
'Authorization': `token ${token}`,
'Accept': 'application/json',
},
const response = await httpGet(`${baseUrl}/api/v1/user`, {
'Authorization': `token ${token}`,
'Accept': 'application/json',
});
const data = response.data;
@@ -72,8 +70,8 @@ export const POST: APIRoute = async ({ request }) => {
console.error('Gitea connection test failed:', error);
// Handle specific error types
if (axios.isAxiosError(error) && error.response) {
if (error.response.status === 401) {
if (error instanceof HttpError) {
if (error.status === 401) {
return new Response(
JSON.stringify({
success: false,
@@ -86,7 +84,7 @@ export const POST: APIRoute = async ({ request }) => {
},
}
);
} else if (error.response.status === 404) {
} else if (error.status === 404) {
return new Response(
JSON.stringify({
success: false,
@@ -99,25 +97,23 @@ export const POST: APIRoute = async ({ request }) => {
},
}
);
} else if (error.status === 0) {
// Network error
return new Response(
JSON.stringify({
success: false,
message: 'Could not connect to Gitea server. Please check the URL.',
}),
{
status: 500,
headers: {
'Content-Type': 'application/json',
},
}
);
}
}
// Handle connection errors
if (axios.isAxiosError(error) && (error.code === 'ECONNREFUSED' || error.code === 'ENOTFOUND')) {
return new Response(
JSON.stringify({
success: false,
message: 'Could not connect to Gitea server. Please check the URL.',
}),
{
status: 500,
headers: {
'Content-Type': 'application/json',
},
}
);
}
// Generic error response
return new Response(
JSON.stringify({

View File

@@ -4,7 +4,7 @@ import { db } from "@/lib/db";
import { ENV } from "@/lib/config";
import { getRecoveryStatus, hasJobsNeedingRecovery } from "@/lib/recovery";
import os from "os";
import axios from "axios";
import { httpGet } from "@/lib/http-client";
// Track when the server started
const serverStartTime = new Date();
@@ -197,9 +197,9 @@ async function checkLatestVersion(): Promise<string> {
try {
// Fetch the latest release from GitHub
const response = await axios.get(
const response = await httpGet(
'https://api.github.com/repos/arunavo4/gitea-mirror/releases/latest',
{ headers: { 'Accept': 'application/vnd.github.v3+json' } }
{ 'Accept': 'application/vnd.github.v3+json' }
);
// Extract version from tag_name (remove 'v' prefix if present)

View File

@@ -165,11 +165,43 @@ export const POST: APIRoute = async ({ request }) => {
headers: { "Content-Type": "application/json" },
});
} catch (error) {
console.error("Error mirroring repositories:", error);
// Enhanced error logging for better debugging
console.error("=== ERROR MIRRORING REPOSITORIES ===");
console.error("Error type:", error?.constructor?.name);
console.error("Error message:", error instanceof Error ? error.message : String(error));
if (error instanceof Error) {
console.error("Error stack:", error.stack);
}
// Log additional context
console.error("Request details:");
console.error("- URL:", request.url);
console.error("- Method:", request.method);
console.error("- Headers:", Object.fromEntries(request.headers.entries()));
// If it's a JSON parsing error, provide more context
if (error instanceof SyntaxError && error.message.includes('JSON')) {
console.error("🚨 JSON PARSING ERROR DETECTED:");
console.error("This suggests the response from Gitea API is not valid JSON");
console.error("Common causes:");
console.error("- Gitea server returned HTML error page instead of JSON");
console.error("- Network connection interrupted");
console.error("- Gitea server is down or misconfigured");
console.error("- Authentication token is invalid");
console.error("Check your Gitea server logs and configuration");
}
console.error("=====================================");
return new Response(
JSON.stringify({
error:
error instanceof Error ? error.message : "An unknown error occurred",
error: error instanceof Error ? error.message : "An unknown error occurred",
errorType: error?.constructor?.name || "Unknown",
timestamp: new Date().toISOString(),
troubleshooting: error instanceof SyntaxError && error.message.includes('JSON')
? "JSON parsing error detected. Check Gitea server status and logs. Ensure Gitea is returning valid JSON responses."
: "Check application logs for more details"
}),
{ status: 500, headers: { "Content-Type": "application/json" } }
);