mirror of
https://github.com/RayLabsHQ/gitea-mirror.git
synced 2025-12-10 05:26:44 +03:00
update default configs
This commit is contained in:
@@ -195,13 +195,14 @@ export function AutomationSettings({
|
|||||||
<Clock className="h-3.5 w-3.5" />
|
<Clock className="h-3.5 w-3.5" />
|
||||||
Last sync
|
Last sync
|
||||||
</span>
|
</span>
|
||||||
<span className="font-medium">
|
<span className="font-medium text-muted-foreground">
|
||||||
{scheduleConfig.lastRun
|
{scheduleConfig.lastRun
|
||||||
? formatDate(scheduleConfig.lastRun)
|
? formatDate(scheduleConfig.lastRun)
|
||||||
: "Never"}
|
: "Never"}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
{scheduleConfig.enabled && scheduleConfig.nextRun && (
|
{scheduleConfig.enabled ? (
|
||||||
|
scheduleConfig.nextRun && (
|
||||||
<div className="flex items-center justify-between text-xs">
|
<div className="flex items-center justify-between text-xs">
|
||||||
<span className="flex items-center gap-1.5">
|
<span className="flex items-center gap-1.5">
|
||||||
<Calendar className="h-3.5 w-3.5" />
|
<Calendar className="h-3.5 w-3.5" />
|
||||||
@@ -211,6 +212,11 @@ export function AutomationSettings({
|
|||||||
{formatDate(scheduleConfig.nextRun)}
|
{formatDate(scheduleConfig.nextRun)}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
|
)
|
||||||
|
) : (
|
||||||
|
<div className="text-xs text-muted-foreground">
|
||||||
|
Enable automatic syncing to schedule periodic repository updates
|
||||||
|
</div>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -307,24 +313,28 @@ export function AutomationSettings({
|
|||||||
<Clock className="h-3.5 w-3.5" />
|
<Clock className="h-3.5 w-3.5" />
|
||||||
Last cleanup
|
Last cleanup
|
||||||
</span>
|
</span>
|
||||||
<span className="font-medium">
|
<span className="font-medium text-muted-foreground">
|
||||||
{cleanupConfig.lastRun
|
{cleanupConfig.lastRun
|
||||||
? formatDate(cleanupConfig.lastRun)
|
? formatDate(cleanupConfig.lastRun)
|
||||||
: "Never"}
|
: "Never"}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
{cleanupConfig.enabled && cleanupConfig.nextRun && (
|
{cleanupConfig.enabled ? (
|
||||||
|
cleanupConfig.nextRun && (
|
||||||
<div className="flex items-center justify-between text-xs">
|
<div className="flex items-center justify-between text-xs">
|
||||||
<span className="flex items-center gap-1.5">
|
<span className="flex items-center gap-1.5">
|
||||||
<Calendar className="h-3.5 w-3.5" />
|
<Calendar className="h-3.5 w-3.5" />
|
||||||
Next cleanup
|
Next cleanup
|
||||||
</span>
|
</span>
|
||||||
<span className="font-medium">
|
<span className="font-medium">
|
||||||
{cleanupConfig.nextRun
|
{formatDate(cleanupConfig.nextRun)}
|
||||||
? formatDate(cleanupConfig.nextRun)
|
|
||||||
: "Calculating..."}
|
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
|
)
|
||||||
|
) : (
|
||||||
|
<div className="text-xs text-muted-foreground">
|
||||||
|
Enable automatic cleanup to optimize database storage
|
||||||
|
</div>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -51,11 +51,11 @@ export function ConfigTabs() {
|
|||||||
},
|
},
|
||||||
scheduleConfig: {
|
scheduleConfig: {
|
||||||
enabled: false,
|
enabled: false,
|
||||||
interval: 3600,
|
interval: 86400, // Default to daily (24 hours) instead of hourly
|
||||||
},
|
},
|
||||||
cleanupConfig: {
|
cleanupConfig: {
|
||||||
enabled: false,
|
enabled: false,
|
||||||
retentionDays: 604800, // 7 days in seconds
|
retentionDays: 604800, // 7 days in seconds - Default retention period
|
||||||
},
|
},
|
||||||
mirrorOptions: {
|
mirrorOptions: {
|
||||||
mirrorReleases: false,
|
mirrorReleases: false,
|
||||||
|
|||||||
126
src/lib/utils/config-defaults.ts
Normal file
126
src/lib/utils/config-defaults.ts
Normal file
@@ -0,0 +1,126 @@
|
|||||||
|
import { db, configs } from "@/lib/db";
|
||||||
|
import { eq } from "drizzle-orm";
|
||||||
|
import { v4 as uuidv4 } from "uuid";
|
||||||
|
import { encrypt } from "@/lib/utils/encryption";
|
||||||
|
|
||||||
|
export interface DefaultConfigOptions {
|
||||||
|
userId: string;
|
||||||
|
envOverrides?: {
|
||||||
|
githubToken?: string;
|
||||||
|
githubUsername?: string;
|
||||||
|
giteaUrl?: string;
|
||||||
|
giteaToken?: string;
|
||||||
|
giteaUsername?: string;
|
||||||
|
scheduleEnabled?: boolean;
|
||||||
|
scheduleInterval?: number;
|
||||||
|
cleanupEnabled?: boolean;
|
||||||
|
cleanupRetentionDays?: number;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a default configuration for a new user with sensible defaults
|
||||||
|
* Environment variables can override these defaults
|
||||||
|
*/
|
||||||
|
export async function createDefaultConfig({ userId, envOverrides = {} }: DefaultConfigOptions) {
|
||||||
|
// Check if config already exists
|
||||||
|
const existingConfig = await db
|
||||||
|
.select()
|
||||||
|
.from(configs)
|
||||||
|
.where(eq(configs.userId, userId))
|
||||||
|
.limit(1);
|
||||||
|
|
||||||
|
if (existingConfig.length > 0) {
|
||||||
|
return existingConfig[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read environment variables for overrides
|
||||||
|
const githubToken = envOverrides.githubToken || process.env.GITHUB_TOKEN || "";
|
||||||
|
const githubUsername = envOverrides.githubUsername || process.env.GITHUB_USERNAME || "";
|
||||||
|
const giteaUrl = envOverrides.giteaUrl || process.env.GITEA_URL || "";
|
||||||
|
const giteaToken = envOverrides.giteaToken || process.env.GITEA_TOKEN || "";
|
||||||
|
const giteaUsername = envOverrides.giteaUsername || process.env.GITEA_USERNAME || "";
|
||||||
|
|
||||||
|
// Schedule config from env
|
||||||
|
const scheduleEnabled = envOverrides.scheduleEnabled ??
|
||||||
|
(process.env.SCHEDULE_ENABLED === "true" ? true : false);
|
||||||
|
const scheduleInterval = envOverrides.scheduleInterval ??
|
||||||
|
(process.env.SCHEDULE_INTERVAL ? parseInt(process.env.SCHEDULE_INTERVAL, 10) : 86400); // Default: daily
|
||||||
|
|
||||||
|
// Cleanup config from env
|
||||||
|
const cleanupEnabled = envOverrides.cleanupEnabled ??
|
||||||
|
(process.env.CLEANUP_ENABLED === "true" ? true : false);
|
||||||
|
const cleanupRetentionDays = envOverrides.cleanupRetentionDays ??
|
||||||
|
(process.env.CLEANUP_RETENTION_DAYS ? parseInt(process.env.CLEANUP_RETENTION_DAYS, 10) * 86400 : 604800); // Default: 7 days
|
||||||
|
|
||||||
|
// Create default configuration
|
||||||
|
const configId = uuidv4();
|
||||||
|
const defaultConfig = {
|
||||||
|
id: configId,
|
||||||
|
userId,
|
||||||
|
name: "Default Configuration",
|
||||||
|
isActive: true,
|
||||||
|
githubConfig: {
|
||||||
|
owner: githubUsername,
|
||||||
|
type: "personal",
|
||||||
|
token: githubToken ? encrypt(githubToken) : "",
|
||||||
|
includeStarred: false,
|
||||||
|
includeForks: true,
|
||||||
|
includeArchived: false,
|
||||||
|
includePrivate: false,
|
||||||
|
includePublic: true,
|
||||||
|
includeOrganizations: [],
|
||||||
|
starredReposOrg: "starred",
|
||||||
|
mirrorStrategy: "preserve",
|
||||||
|
defaultOrg: "github-mirrors",
|
||||||
|
},
|
||||||
|
giteaConfig: {
|
||||||
|
url: giteaUrl,
|
||||||
|
token: giteaToken ? encrypt(giteaToken) : "",
|
||||||
|
defaultOwner: giteaUsername,
|
||||||
|
mirrorInterval: "8h",
|
||||||
|
lfs: false,
|
||||||
|
wiki: false,
|
||||||
|
visibility: "public",
|
||||||
|
createOrg: true,
|
||||||
|
addTopics: true,
|
||||||
|
preserveVisibility: false,
|
||||||
|
forkStrategy: "reference",
|
||||||
|
},
|
||||||
|
include: [],
|
||||||
|
exclude: [],
|
||||||
|
scheduleConfig: {
|
||||||
|
enabled: scheduleEnabled,
|
||||||
|
interval: scheduleInterval,
|
||||||
|
concurrent: false,
|
||||||
|
batchSize: 10,
|
||||||
|
lastRun: null,
|
||||||
|
nextRun: scheduleEnabled ? new Date(Date.now() + scheduleInterval * 1000) : null,
|
||||||
|
},
|
||||||
|
cleanupConfig: {
|
||||||
|
enabled: cleanupEnabled,
|
||||||
|
retentionDays: cleanupRetentionDays,
|
||||||
|
lastRun: null,
|
||||||
|
nextRun: cleanupEnabled ? new Date(Date.now() + getCleanupInterval(cleanupRetentionDays) * 1000) : null,
|
||||||
|
},
|
||||||
|
createdAt: new Date(),
|
||||||
|
updatedAt: new Date(),
|
||||||
|
};
|
||||||
|
|
||||||
|
// Insert the default config
|
||||||
|
await db.insert(configs).values(defaultConfig);
|
||||||
|
|
||||||
|
return defaultConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculate cleanup interval based on retention period
|
||||||
|
*/
|
||||||
|
function getCleanupInterval(retentionSeconds: number): number {
|
||||||
|
const days = retentionSeconds / 86400;
|
||||||
|
if (days <= 1) return 21600; // 6 hours
|
||||||
|
if (days <= 3) return 43200; // 12 hours
|
||||||
|
if (days <= 7) return 86400; // 24 hours
|
||||||
|
if (days <= 30) return 172800; // 48 hours
|
||||||
|
return 604800; // 1 week
|
||||||
|
}
|
||||||
@@ -13,6 +13,7 @@ import {
|
|||||||
mapDbCleanupToUi
|
mapDbCleanupToUi
|
||||||
} from "@/lib/utils/config-mapper";
|
} from "@/lib/utils/config-mapper";
|
||||||
import { encrypt, decrypt, migrateToken } from "@/lib/utils/encryption";
|
import { encrypt, decrypt, migrateToken } from "@/lib/utils/encryption";
|
||||||
|
import { createDefaultConfig } from "@/lib/utils/config-defaults";
|
||||||
|
|
||||||
export const POST: APIRoute = async ({ request }) => {
|
export const POST: APIRoute = async ({ request }) => {
|
||||||
try {
|
try {
|
||||||
@@ -188,58 +189,20 @@ export const GET: APIRoute = async ({ request }) => {
|
|||||||
.limit(1);
|
.limit(1);
|
||||||
|
|
||||||
if (config.length === 0) {
|
if (config.length === 0) {
|
||||||
// Return a default empty configuration with database structure
|
// Create default configuration for the user
|
||||||
const defaultDbConfig = {
|
const defaultConfig = await createDefaultConfig({ userId });
|
||||||
githubConfig: {
|
|
||||||
owner: "",
|
|
||||||
type: "personal",
|
|
||||||
token: "",
|
|
||||||
includeStarred: false,
|
|
||||||
includeForks: true,
|
|
||||||
includeArchived: false,
|
|
||||||
includePrivate: false,
|
|
||||||
includePublic: true,
|
|
||||||
includeOrganizations: [],
|
|
||||||
starredReposOrg: "starred",
|
|
||||||
mirrorStrategy: "preserve",
|
|
||||||
defaultOrg: "github-mirrors",
|
|
||||||
},
|
|
||||||
giteaConfig: {
|
|
||||||
url: "",
|
|
||||||
token: "",
|
|
||||||
defaultOwner: "",
|
|
||||||
mirrorInterval: "8h",
|
|
||||||
lfs: false,
|
|
||||||
wiki: false,
|
|
||||||
visibility: "public",
|
|
||||||
createOrg: true,
|
|
||||||
addTopics: true,
|
|
||||||
preserveVisibility: false,
|
|
||||||
forkStrategy: "reference",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const uiConfig = mapDbToUiConfig(defaultDbConfig);
|
// Map the created config to UI format
|
||||||
|
const uiConfig = mapDbToUiConfig(defaultConfig);
|
||||||
|
const uiScheduleConfig = mapDbScheduleToUi(defaultConfig.scheduleConfig);
|
||||||
|
const uiCleanupConfig = mapDbCleanupToUi(defaultConfig.cleanupConfig);
|
||||||
|
|
||||||
return new Response(
|
return new Response(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
id: null,
|
...defaultConfig,
|
||||||
userId: userId,
|
|
||||||
name: "Default Configuration",
|
|
||||||
isActive: true,
|
|
||||||
...uiConfig,
|
...uiConfig,
|
||||||
scheduleConfig: {
|
scheduleConfig: uiScheduleConfig,
|
||||||
enabled: false,
|
cleanupConfig: uiCleanupConfig,
|
||||||
interval: 3600,
|
|
||||||
lastRun: null,
|
|
||||||
nextRun: null,
|
|
||||||
},
|
|
||||||
cleanupConfig: {
|
|
||||||
enabled: false,
|
|
||||||
retentionDays: 604800, // 7 days in seconds
|
|
||||||
lastRun: null,
|
|
||||||
nextRun: null,
|
|
||||||
},
|
|
||||||
}),
|
}),
|
||||||
{
|
{
|
||||||
status: 200,
|
status: 200,
|
||||||
|
|||||||
Reference in New Issue
Block a user