mirror of
https://github.com/RayLabsHQ/gitea-mirror.git
synced 2025-12-08 20:46:44 +03:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b34ed5595b | ||
|
|
cbc11155ef | ||
|
|
941f61830f | ||
|
|
5b60cffaae |
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "gitea-mirror",
|
"name": "gitea-mirror",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"version": "2.9.3",
|
"version": "2.10.0",
|
||||||
"engines": {
|
"engines": {
|
||||||
"bun": ">=1.2.9"
|
"bun": ">=1.2.9"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -56,14 +56,18 @@ export function ConfigTabs() {
|
|||||||
retentionDays: 604800, // 7 days in seconds
|
retentionDays: 604800, // 7 days in seconds
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
const { user, refreshUser } = useAuth();
|
const { user } = useAuth();
|
||||||
const [isLoading, setIsLoading] = useState(true);
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
const [isSyncing, setIsSyncing] = useState<boolean>(false);
|
const [isSyncing, setIsSyncing] = useState<boolean>(false);
|
||||||
const [isConfigSaved, setIsConfigSaved] = useState<boolean>(false);
|
|
||||||
const [isAutoSavingSchedule, setIsAutoSavingSchedule] = useState<boolean>(false);
|
const [isAutoSavingSchedule, setIsAutoSavingSchedule] = useState<boolean>(false);
|
||||||
const [isAutoSavingCleanup, setIsAutoSavingCleanup] = useState<boolean>(false);
|
const [isAutoSavingCleanup, setIsAutoSavingCleanup] = useState<boolean>(false);
|
||||||
|
const [isAutoSavingGitHub, setIsAutoSavingGitHub] = useState<boolean>(false);
|
||||||
|
const [isAutoSavingGitea, setIsAutoSavingGitea] = useState<boolean>(false);
|
||||||
const autoSaveScheduleTimeoutRef = useRef<NodeJS.Timeout | null>(null);
|
const autoSaveScheduleTimeoutRef = useRef<NodeJS.Timeout | null>(null);
|
||||||
const autoSaveCleanupTimeoutRef = useRef<NodeJS.Timeout | null>(null);
|
const autoSaveCleanupTimeoutRef = useRef<NodeJS.Timeout | null>(null);
|
||||||
|
const autoSaveGitHubTimeoutRef = useRef<NodeJS.Timeout | null>(null);
|
||||||
|
const autoSaveGiteaTimeoutRef = useRef<NodeJS.Timeout | null>(null);
|
||||||
|
|
||||||
const isConfigFormValid = (): boolean => {
|
const isConfigFormValid = (): boolean => {
|
||||||
const { githubConfig, giteaConfig } = config;
|
const { githubConfig, giteaConfig } = config;
|
||||||
@@ -109,44 +113,9 @@ export function ConfigTabs() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSaveConfig = async () => {
|
|
||||||
if (!user?.id) return;
|
|
||||||
const reqPayload: SaveConfigApiRequest = {
|
|
||||||
userId: user.id,
|
|
||||||
githubConfig: config.githubConfig,
|
|
||||||
giteaConfig: config.giteaConfig,
|
|
||||||
scheduleConfig: config.scheduleConfig,
|
|
||||||
cleanupConfig: config.cleanupConfig,
|
|
||||||
};
|
|
||||||
try {
|
|
||||||
const response = await fetch('/api/config', {
|
|
||||||
method: 'POST',
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify(reqPayload),
|
|
||||||
});
|
|
||||||
const result: SaveConfigApiResponse = await response.json();
|
|
||||||
if (result.success) {
|
|
||||||
await refreshUser();
|
|
||||||
setIsConfigSaved(true);
|
|
||||||
// Invalidate config cache so other components get fresh data
|
|
||||||
invalidateConfigCache();
|
|
||||||
toast.success(
|
|
||||||
'Configuration saved successfully! Now import your GitHub data to begin.',
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
showErrorToast(
|
|
||||||
`Failed to save configuration: ${result.message || 'Unknown error'}`,
|
|
||||||
toast
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
showErrorToast(error, toast);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Auto-save function specifically for schedule config changes
|
// Auto-save function specifically for schedule config changes
|
||||||
const autoSaveScheduleConfig = useCallback(async (scheduleConfig: ScheduleConfig) => {
|
const autoSaveScheduleConfig = useCallback(async (scheduleConfig: ScheduleConfig) => {
|
||||||
if (!user?.id || !isConfigSaved) return; // Only auto-save if config was previously saved
|
if (!user?.id) return;
|
||||||
|
|
||||||
// Clear any existing timeout
|
// Clear any existing timeout
|
||||||
if (autoSaveScheduleTimeoutRef.current) {
|
if (autoSaveScheduleTimeoutRef.current) {
|
||||||
@@ -206,11 +175,11 @@ export function ConfigTabs() {
|
|||||||
setIsAutoSavingSchedule(false);
|
setIsAutoSavingSchedule(false);
|
||||||
}
|
}
|
||||||
}, 500); // 500ms debounce
|
}, 500); // 500ms debounce
|
||||||
}, [user?.id, isConfigSaved, config.githubConfig, config.giteaConfig, config.cleanupConfig]);
|
}, [user?.id, config.githubConfig, config.giteaConfig, config.cleanupConfig]);
|
||||||
|
|
||||||
// Auto-save function specifically for cleanup config changes
|
// Auto-save function specifically for cleanup config changes
|
||||||
const autoSaveCleanupConfig = useCallback(async (cleanupConfig: DatabaseCleanupConfig) => {
|
const autoSaveCleanupConfig = useCallback(async (cleanupConfig: DatabaseCleanupConfig) => {
|
||||||
if (!user?.id || !isConfigSaved) return; // Only auto-save if config was previously saved
|
if (!user?.id) return;
|
||||||
|
|
||||||
// Clear any existing timeout
|
// Clear any existing timeout
|
||||||
if (autoSaveCleanupTimeoutRef.current) {
|
if (autoSaveCleanupTimeoutRef.current) {
|
||||||
@@ -269,7 +238,101 @@ export function ConfigTabs() {
|
|||||||
setIsAutoSavingCleanup(false);
|
setIsAutoSavingCleanup(false);
|
||||||
}
|
}
|
||||||
}, 500); // 500ms debounce
|
}, 500); // 500ms debounce
|
||||||
}, [user?.id, isConfigSaved, config.githubConfig, config.giteaConfig, config.scheduleConfig]);
|
}, [user?.id, config.githubConfig, config.giteaConfig, config.scheduleConfig]);
|
||||||
|
|
||||||
|
// Auto-save function specifically for GitHub config changes
|
||||||
|
const autoSaveGitHubConfig = useCallback(async (githubConfig: GitHubConfig) => {
|
||||||
|
if (!user?.id) return;
|
||||||
|
|
||||||
|
// Clear any existing timeout
|
||||||
|
if (autoSaveGitHubTimeoutRef.current) {
|
||||||
|
clearTimeout(autoSaveGitHubTimeoutRef.current);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Debounce the auto-save to prevent excessive API calls
|
||||||
|
autoSaveGitHubTimeoutRef.current = setTimeout(async () => {
|
||||||
|
setIsAutoSavingGitHub(true);
|
||||||
|
|
||||||
|
const reqPayload: SaveConfigApiRequest = {
|
||||||
|
userId: user.id!,
|
||||||
|
githubConfig: githubConfig,
|
||||||
|
giteaConfig: config.giteaConfig,
|
||||||
|
scheduleConfig: config.scheduleConfig,
|
||||||
|
cleanupConfig: config.cleanupConfig,
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch('/api/config', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify(reqPayload),
|
||||||
|
});
|
||||||
|
const result: SaveConfigApiResponse = await response.json();
|
||||||
|
|
||||||
|
if (result.success) {
|
||||||
|
// Silent success - no toast for auto-save
|
||||||
|
// Invalidate config cache so other components get fresh data
|
||||||
|
invalidateConfigCache();
|
||||||
|
} else {
|
||||||
|
showErrorToast(
|
||||||
|
`Auto-save failed: ${result.message || 'Unknown error'}`,
|
||||||
|
toast
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
showErrorToast(error, toast);
|
||||||
|
} finally {
|
||||||
|
setIsAutoSavingGitHub(false);
|
||||||
|
}
|
||||||
|
}, 500); // 500ms debounce
|
||||||
|
}, [user?.id, config.giteaConfig, config.scheduleConfig, config.cleanupConfig]);
|
||||||
|
|
||||||
|
// Auto-save function specifically for Gitea config changes
|
||||||
|
const autoSaveGiteaConfig = useCallback(async (giteaConfig: GiteaConfig) => {
|
||||||
|
if (!user?.id) return;
|
||||||
|
|
||||||
|
// Clear any existing timeout
|
||||||
|
if (autoSaveGiteaTimeoutRef.current) {
|
||||||
|
clearTimeout(autoSaveGiteaTimeoutRef.current);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Debounce the auto-save to prevent excessive API calls
|
||||||
|
autoSaveGiteaTimeoutRef.current = setTimeout(async () => {
|
||||||
|
setIsAutoSavingGitea(true);
|
||||||
|
|
||||||
|
const reqPayload: SaveConfigApiRequest = {
|
||||||
|
userId: user.id!,
|
||||||
|
githubConfig: config.githubConfig,
|
||||||
|
giteaConfig: giteaConfig,
|
||||||
|
scheduleConfig: config.scheduleConfig,
|
||||||
|
cleanupConfig: config.cleanupConfig,
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch('/api/config', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify(reqPayload),
|
||||||
|
});
|
||||||
|
const result: SaveConfigApiResponse = await response.json();
|
||||||
|
|
||||||
|
if (result.success) {
|
||||||
|
// Silent success - no toast for auto-save
|
||||||
|
// Invalidate config cache so other components get fresh data
|
||||||
|
invalidateConfigCache();
|
||||||
|
} else {
|
||||||
|
showErrorToast(
|
||||||
|
`Auto-save failed: ${result.message || 'Unknown error'}`,
|
||||||
|
toast
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
showErrorToast(error, toast);
|
||||||
|
} finally {
|
||||||
|
setIsAutoSavingGitea(false);
|
||||||
|
}
|
||||||
|
}, 500); // 500ms debounce
|
||||||
|
}, [user?.id, config.githubConfig, config.scheduleConfig, config.cleanupConfig]);
|
||||||
|
|
||||||
// Cleanup timeouts on unmount
|
// Cleanup timeouts on unmount
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -280,6 +343,12 @@ export function ConfigTabs() {
|
|||||||
if (autoSaveCleanupTimeoutRef.current) {
|
if (autoSaveCleanupTimeoutRef.current) {
|
||||||
clearTimeout(autoSaveCleanupTimeoutRef.current);
|
clearTimeout(autoSaveCleanupTimeoutRef.current);
|
||||||
}
|
}
|
||||||
|
if (autoSaveGitHubTimeoutRef.current) {
|
||||||
|
clearTimeout(autoSaveGitHubTimeoutRef.current);
|
||||||
|
}
|
||||||
|
if (autoSaveGiteaTimeoutRef.current) {
|
||||||
|
clearTimeout(autoSaveGiteaTimeoutRef.current);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
@@ -304,7 +373,7 @@ export function ConfigTabs() {
|
|||||||
cleanupConfig:
|
cleanupConfig:
|
||||||
response.cleanupConfig || config.cleanupConfig,
|
response.cleanupConfig || config.cleanupConfig,
|
||||||
});
|
});
|
||||||
if (response.id) setIsConfigSaved(true);
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.warn(
|
console.warn(
|
||||||
@@ -401,10 +470,10 @@ export function ConfigTabs() {
|
|||||||
<div className="flex gap-x-4">
|
<div className="flex gap-x-4">
|
||||||
<Button
|
<Button
|
||||||
onClick={handleImportGitHubData}
|
onClick={handleImportGitHubData}
|
||||||
disabled={isSyncing || !isConfigSaved}
|
disabled={isSyncing || !isConfigFormValid()}
|
||||||
title={
|
title={
|
||||||
!isConfigSaved
|
!isConfigFormValid()
|
||||||
? 'Save configuration first'
|
? 'Please fill all required GitHub and Gitea fields'
|
||||||
: isSyncing
|
: isSyncing
|
||||||
? 'Import in progress'
|
? 'Import in progress'
|
||||||
: 'Import GitHub Data'
|
: 'Import GitHub Data'
|
||||||
@@ -422,17 +491,6 @@ export function ConfigTabs() {
|
|||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
</Button>
|
</Button>
|
||||||
<Button
|
|
||||||
onClick={handleSaveConfig}
|
|
||||||
disabled={!isConfigFormValid()}
|
|
||||||
title={
|
|
||||||
!isConfigFormValid()
|
|
||||||
? 'Please fill all required fields'
|
|
||||||
: 'Save Configuration'
|
|
||||||
}
|
|
||||||
>
|
|
||||||
Save Configuration
|
|
||||||
</Button>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -450,6 +508,8 @@ export function ConfigTabs() {
|
|||||||
: update,
|
: update,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
onAutoSave={autoSaveGitHubConfig}
|
||||||
|
isAutoSaving={isAutoSavingGitHub}
|
||||||
/>
|
/>
|
||||||
<GiteaConfigForm
|
<GiteaConfigForm
|
||||||
config={config.giteaConfig}
|
config={config.giteaConfig}
|
||||||
@@ -462,6 +522,8 @@ export function ConfigTabs() {
|
|||||||
: update,
|
: update,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
onAutoSave={autoSaveGiteaConfig}
|
||||||
|
isAutoSaving={isAutoSavingGitea}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex gap-x-4">
|
<div className="flex gap-x-4">
|
||||||
|
|||||||
@@ -20,9 +20,11 @@ import { Tooltip, TooltipContent, TooltipTrigger } from "../ui/tooltip";
|
|||||||
interface GitHubConfigFormProps {
|
interface GitHubConfigFormProps {
|
||||||
config: GitHubConfig;
|
config: GitHubConfig;
|
||||||
setConfig: React.Dispatch<React.SetStateAction<GitHubConfig>>;
|
setConfig: React.Dispatch<React.SetStateAction<GitHubConfig>>;
|
||||||
|
onAutoSave?: (githubConfig: GitHubConfig) => Promise<void>;
|
||||||
|
isAutoSaving?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function GitHubConfigForm({ config, setConfig }: GitHubConfigFormProps) {
|
export function GitHubConfigForm({ config, setConfig, onAutoSave, isAutoSaving }: GitHubConfigFormProps) {
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
|
||||||
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
@@ -43,10 +45,17 @@ export function GitHubConfigForm({ config, setConfig }: GitHubConfigFormProps) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
setConfig({
|
const newConfig = {
|
||||||
...config,
|
...config,
|
||||||
[name]: type === "checkbox" ? checked : value,
|
[name]: type === "checkbox" ? checked : value,
|
||||||
});
|
};
|
||||||
|
|
||||||
|
setConfig(newConfig);
|
||||||
|
|
||||||
|
// Auto-save for all field changes
|
||||||
|
if (onAutoSave) {
|
||||||
|
onAutoSave(newConfig);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const testConnection = async () => {
|
const testConnection = async () => {
|
||||||
|
|||||||
@@ -21,19 +21,27 @@ import { toast } from "sonner";
|
|||||||
interface GiteaConfigFormProps {
|
interface GiteaConfigFormProps {
|
||||||
config: GiteaConfig;
|
config: GiteaConfig;
|
||||||
setConfig: React.Dispatch<React.SetStateAction<GiteaConfig>>;
|
setConfig: React.Dispatch<React.SetStateAction<GiteaConfig>>;
|
||||||
|
onAutoSave?: (giteaConfig: GiteaConfig) => Promise<void>;
|
||||||
|
isAutoSaving?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function GiteaConfigForm({ config, setConfig }: GiteaConfigFormProps) {
|
export function GiteaConfigForm({ config, setConfig, onAutoSave, isAutoSaving }: GiteaConfigFormProps) {
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
|
||||||
const handleChange = (
|
const handleChange = (
|
||||||
e: React.ChangeEvent<HTMLInputElement | HTMLSelectElement>
|
e: React.ChangeEvent<HTMLInputElement | HTMLSelectElement>
|
||||||
) => {
|
) => {
|
||||||
const { name, value } = e.target;
|
const { name, value } = e.target;
|
||||||
setConfig({
|
const newConfig = {
|
||||||
...config,
|
...config,
|
||||||
[name]: value,
|
[name]: value,
|
||||||
});
|
};
|
||||||
|
setConfig(newConfig);
|
||||||
|
|
||||||
|
// Auto-save for all field changes
|
||||||
|
if (onAutoSave) {
|
||||||
|
onAutoSave(newConfig);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const testConnection = async () => {
|
const testConnection = async () => {
|
||||||
|
|||||||
@@ -81,6 +81,11 @@ export function RepositoryList({ repositories }: RepositoryListProps) {
|
|||||||
Private
|
Private
|
||||||
</span>
|
</span>
|
||||||
)}
|
)}
|
||||||
|
{repo.isForked && (
|
||||||
|
<span className="rounded-full bg-muted px-2 py-0.5 text-xs">
|
||||||
|
Fork
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
<div className="flex items-center gap-2 mt-1">
|
<div className="flex items-center gap-2 mt-1">
|
||||||
<span className="text-xs text-muted-foreground">
|
<span className="text-xs text-muted-foreground">
|
||||||
|
|||||||
@@ -118,10 +118,38 @@ export function OrganizationList({
|
|||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<p className="text-sm text-muted-foreground mb-4">
|
<div className="text-sm text-muted-foreground mb-4">
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<span className="font-medium">
|
||||||
{org.repositoryCount}{" "}
|
{org.repositoryCount}{" "}
|
||||||
{org.repositoryCount === 1 ? "repository" : "repositories"}
|
{org.repositoryCount === 1 ? "repository" : "repositories"}
|
||||||
</p>
|
</span>
|
||||||
|
</div>
|
||||||
|
{(org.publicRepositoryCount !== undefined ||
|
||||||
|
org.privateRepositoryCount !== undefined ||
|
||||||
|
org.forkRepositoryCount !== undefined) && (
|
||||||
|
<div className="flex gap-4 mt-2 text-xs">
|
||||||
|
{org.publicRepositoryCount !== undefined && (
|
||||||
|
<span className="flex items-center gap-1">
|
||||||
|
<div className="h-2 w-2 rounded-full bg-green-500" />
|
||||||
|
{org.publicRepositoryCount} public
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
{org.privateRepositoryCount !== undefined && org.privateRepositoryCount > 0 && (
|
||||||
|
<span className="flex items-center gap-1">
|
||||||
|
<div className="h-2 w-2 rounded-full bg-orange-500" />
|
||||||
|
{org.privateRepositoryCount} private
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
{org.forkRepositoryCount !== undefined && org.forkRepositoryCount > 0 && (
|
||||||
|
<span className="flex items-center gap-1">
|
||||||
|
<div className="h-2 w-2 rounded-full bg-blue-500" />
|
||||||
|
{org.forkRepositoryCount} fork{org.forkRepositoryCount !== 1 ? 's' : ''}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
<div className="flex items-center justify-between">
|
<div className="flex items-center justify-between">
|
||||||
<div className="flex items-center">
|
<div className="flex items-center">
|
||||||
|
|||||||
@@ -249,6 +249,11 @@ export default function RepositoryTable({
|
|||||||
Private
|
Private
|
||||||
</span>
|
</span>
|
||||||
)}
|
)}
|
||||||
|
{repo.isForked && (
|
||||||
|
<span className="ml-2 rounded-full bg-muted px-2 py-0.5 text-xs">
|
||||||
|
Fork
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Owner */}
|
{/* Owner */}
|
||||||
|
|||||||
@@ -152,6 +152,9 @@ export const organizationSchema = z.object({
|
|||||||
errorMessage: z.string().optional(),
|
errorMessage: z.string().optional(),
|
||||||
|
|
||||||
repositoryCount: z.number().default(0),
|
repositoryCount: z.number().default(0),
|
||||||
|
publicRepositoryCount: z.number().optional(),
|
||||||
|
privateRepositoryCount: z.number().optional(),
|
||||||
|
forkRepositoryCount: z.number().optional(),
|
||||||
|
|
||||||
createdAt: z.date().default(() => new Date()),
|
createdAt: z.date().default(() => new Date()),
|
||||||
updatedAt: z.date().default(() => new Date()),
|
updatedAt: z.date().default(() => new Date()),
|
||||||
|
|||||||
@@ -204,4 +204,90 @@ describe("Gitea Repository Mirroring", () => {
|
|||||||
global.fetch = originalFetch;
|
global.fetch = originalFetch;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("mirrorGitHubOrgToGitea handles empty organizations correctly", async () => {
|
||||||
|
// Mock the createMirrorJob function
|
||||||
|
const mockCreateMirrorJob = mock(() => Promise.resolve("job-id"));
|
||||||
|
|
||||||
|
// Mock the getOrCreateGiteaOrg function
|
||||||
|
const mockGetOrCreateGiteaOrg = mock(() => Promise.resolve("gitea-org-id"));
|
||||||
|
|
||||||
|
// Create a test version of the function with mocked dependencies
|
||||||
|
const testMirrorGitHubOrgToGitea = async ({
|
||||||
|
organization,
|
||||||
|
config,
|
||||||
|
}: {
|
||||||
|
organization: any;
|
||||||
|
config: any;
|
||||||
|
}) => {
|
||||||
|
// Simulate the function logic for empty organization
|
||||||
|
console.log(`Mirroring organization ${organization.name}`);
|
||||||
|
|
||||||
|
// Mock: get or create Gitea org
|
||||||
|
await mockGetOrCreateGiteaOrg();
|
||||||
|
|
||||||
|
// Mock: query the db with the org name and get the repos
|
||||||
|
const orgRepos: any[] = []; // Empty array to simulate no repositories
|
||||||
|
|
||||||
|
if (orgRepos.length === 0) {
|
||||||
|
console.log(`No repositories found for organization ${organization.name} - marking as successfully mirrored`);
|
||||||
|
} else {
|
||||||
|
console.log(`Mirroring ${orgRepos.length} repositories for organization ${organization.name}`);
|
||||||
|
// Repository processing would happen here
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`Organization ${organization.name} mirrored successfully`);
|
||||||
|
|
||||||
|
// Mock: Append log for "mirrored" status
|
||||||
|
await mockCreateMirrorJob({
|
||||||
|
userId: config.userId,
|
||||||
|
organizationId: organization.id,
|
||||||
|
organizationName: organization.name,
|
||||||
|
message: `Successfully mirrored organization: ${organization.name}`,
|
||||||
|
details: orgRepos.length === 0
|
||||||
|
? `Organization ${organization.name} was processed successfully (no repositories found).`
|
||||||
|
: `Organization ${organization.name} was mirrored to Gitea with ${orgRepos.length} repositories.`,
|
||||||
|
status: "mirrored",
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
// Create mock organization
|
||||||
|
const organization = {
|
||||||
|
id: "org-id",
|
||||||
|
name: "empty-org",
|
||||||
|
status: "imported"
|
||||||
|
};
|
||||||
|
|
||||||
|
// Create mock config
|
||||||
|
const config = {
|
||||||
|
id: "config-id",
|
||||||
|
userId: "user-id",
|
||||||
|
githubConfig: {
|
||||||
|
token: "github-token"
|
||||||
|
},
|
||||||
|
giteaConfig: {
|
||||||
|
url: "https://gitea.example.com",
|
||||||
|
token: "gitea-token"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Call the test function
|
||||||
|
await testMirrorGitHubOrgToGitea({
|
||||||
|
organization,
|
||||||
|
config
|
||||||
|
});
|
||||||
|
|
||||||
|
// Verify that the mirror job was created with the correct details for empty org
|
||||||
|
expect(mockCreateMirrorJob).toHaveBeenCalledWith({
|
||||||
|
userId: "user-id",
|
||||||
|
organizationId: "org-id",
|
||||||
|
organizationName: "empty-org",
|
||||||
|
message: "Successfully mirrored organization: empty-org",
|
||||||
|
details: "Organization empty-org was processed successfully (no repositories found).",
|
||||||
|
status: "mirrored",
|
||||||
|
});
|
||||||
|
|
||||||
|
// Verify that getOrCreateGiteaOrg was called
|
||||||
|
expect(mockGetOrCreateGiteaOrg).toHaveBeenCalled();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -629,10 +629,8 @@ export async function mirrorGitHubOrgToGitea({
|
|||||||
.where(eq(repositories.organization, organization.name));
|
.where(eq(repositories.organization, organization.name));
|
||||||
|
|
||||||
if (orgRepos.length === 0) {
|
if (orgRepos.length === 0) {
|
||||||
console.log(`No repositories found for organization ${organization.name}`);
|
console.log(`No repositories found for organization ${organization.name} - marking as successfully mirrored`);
|
||||||
return;
|
} else {
|
||||||
}
|
|
||||||
|
|
||||||
console.log(`Mirroring ${orgRepos.length} repositories for organization ${organization.name}`);
|
console.log(`Mirroring ${orgRepos.length} repositories for organization ${organization.name}`);
|
||||||
|
|
||||||
// Import the processWithRetry function
|
// Import the processWithRetry function
|
||||||
@@ -683,6 +681,7 @@ export async function mirrorGitHubOrgToGitea({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
console.log(`Organization ${organization.name} mirrored successfully`);
|
console.log(`Organization ${organization.name} mirrored successfully`);
|
||||||
|
|
||||||
@@ -703,7 +702,9 @@ export async function mirrorGitHubOrgToGitea({
|
|||||||
organizationId: organization.id,
|
organizationId: organization.id,
|
||||||
organizationName: organization.name,
|
organizationName: organization.name,
|
||||||
message: `Successfully mirrored organization: ${organization.name}`,
|
message: `Successfully mirrored organization: ${organization.name}`,
|
||||||
details: `Organization ${organization.name} was mirrored to Gitea.`,
|
details: orgRepos.length === 0
|
||||||
|
? `Organization ${organization.name} was processed successfully (no repositories found).`
|
||||||
|
: `Organization ${organization.name} was mirrored to Gitea with ${orgRepos.length} repositories.`,
|
||||||
status: repoStatusEnum.parse("mirrored"),
|
status: repoStatusEnum.parse("mirrored"),
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import type { APIRoute } from "astro";
|
import type { APIRoute } from "astro";
|
||||||
import { db } from "@/lib/db";
|
import { db } from "@/lib/db";
|
||||||
import { organizations } from "@/lib/db";
|
import { organizations, repositories, configs } from "@/lib/db";
|
||||||
import { eq, sql } from "drizzle-orm";
|
import { eq, sql, and, count } from "drizzle-orm";
|
||||||
import {
|
import {
|
||||||
membershipRoleEnum,
|
membershipRoleEnum,
|
||||||
type OrganizationsApiResponse,
|
type OrganizationsApiResponse,
|
||||||
@@ -25,24 +25,114 @@ export const GET: APIRoute = async ({ request }) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// Fetch the user's active configuration to respect filtering settings
|
||||||
|
const [config] = await db
|
||||||
|
.select()
|
||||||
|
.from(configs)
|
||||||
|
.where(and(eq(configs.userId, userId), eq(configs.isActive, true)));
|
||||||
|
|
||||||
|
if (!config) {
|
||||||
|
return jsonResponse({
|
||||||
|
data: {
|
||||||
|
success: false,
|
||||||
|
error: "No active configuration found for this user",
|
||||||
|
},
|
||||||
|
status: 404,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const githubConfig = config.githubConfig as {
|
||||||
|
mirrorStarred: boolean;
|
||||||
|
skipForks: boolean;
|
||||||
|
privateRepositories: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
const rawOrgs = await db
|
const rawOrgs = await db
|
||||||
.select()
|
.select()
|
||||||
.from(organizations)
|
.from(organizations)
|
||||||
.where(eq(organizations.userId, userId))
|
.where(eq(organizations.userId, userId))
|
||||||
.orderBy(sql`name COLLATE NOCASE`);
|
.orderBy(sql`name COLLATE NOCASE`);
|
||||||
|
|
||||||
const orgsWithIds: Organization[] = rawOrgs.map((org) => ({
|
// Calculate repository breakdowns for each organization
|
||||||
|
const orgsWithBreakdown = await Promise.all(
|
||||||
|
rawOrgs.map(async (org) => {
|
||||||
|
// Build base conditions for this organization (without private/fork filters)
|
||||||
|
const baseConditions = [
|
||||||
|
eq(repositories.userId, userId),
|
||||||
|
eq(repositories.organization, org.name)
|
||||||
|
];
|
||||||
|
|
||||||
|
if (!githubConfig.mirrorStarred) {
|
||||||
|
baseConditions.push(eq(repositories.isStarred, false));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get total count with all user config filters applied
|
||||||
|
const totalConditions = [...baseConditions];
|
||||||
|
if (githubConfig.skipForks) {
|
||||||
|
totalConditions.push(eq(repositories.isForked, false));
|
||||||
|
}
|
||||||
|
if (!githubConfig.privateRepositories) {
|
||||||
|
totalConditions.push(eq(repositories.isPrivate, false));
|
||||||
|
}
|
||||||
|
|
||||||
|
const [totalCount] = await db
|
||||||
|
.select({ count: count() })
|
||||||
|
.from(repositories)
|
||||||
|
.where(and(...totalConditions));
|
||||||
|
|
||||||
|
// Get public count
|
||||||
|
const publicConditions = [...baseConditions, eq(repositories.isPrivate, false)];
|
||||||
|
if (githubConfig.skipForks) {
|
||||||
|
publicConditions.push(eq(repositories.isForked, false));
|
||||||
|
}
|
||||||
|
|
||||||
|
const [publicCount] = await db
|
||||||
|
.select({ count: count() })
|
||||||
|
.from(repositories)
|
||||||
|
.where(and(...publicConditions));
|
||||||
|
|
||||||
|
// Get private count (only if private repos are enabled in config)
|
||||||
|
const [privateCount] = githubConfig.privateRepositories ? await db
|
||||||
|
.select({ count: count() })
|
||||||
|
.from(repositories)
|
||||||
|
.where(
|
||||||
|
and(
|
||||||
|
...baseConditions,
|
||||||
|
eq(repositories.isPrivate, true),
|
||||||
|
...(githubConfig.skipForks ? [eq(repositories.isForked, false)] : [])
|
||||||
|
)
|
||||||
|
) : [{ count: 0 }];
|
||||||
|
|
||||||
|
// Get fork count (only if forks are enabled in config)
|
||||||
|
const [forkCount] = !githubConfig.skipForks ? await db
|
||||||
|
.select({ count: count() })
|
||||||
|
.from(repositories)
|
||||||
|
.where(
|
||||||
|
and(
|
||||||
|
...baseConditions,
|
||||||
|
eq(repositories.isForked, true),
|
||||||
|
...(!githubConfig.privateRepositories ? [eq(repositories.isPrivate, false)] : [])
|
||||||
|
)
|
||||||
|
) : [{ count: 0 }];
|
||||||
|
|
||||||
|
return {
|
||||||
...org,
|
...org,
|
||||||
status: repoStatusEnum.parse(org.status),
|
status: repoStatusEnum.parse(org.status),
|
||||||
membershipRole: membershipRoleEnum.parse(org.membershipRole),
|
membershipRole: membershipRoleEnum.parse(org.membershipRole),
|
||||||
lastMirrored: org.lastMirrored ?? undefined,
|
lastMirrored: org.lastMirrored ?? undefined,
|
||||||
errorMessage: org.errorMessage ?? undefined,
|
errorMessage: org.errorMessage ?? undefined,
|
||||||
}));
|
repositoryCount: totalCount.count,
|
||||||
|
publicRepositoryCount: publicCount.count,
|
||||||
|
privateRepositoryCount: privateCount.count,
|
||||||
|
forkRepositoryCount: forkCount.count,
|
||||||
|
};
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
const resPayload: OrganizationsApiResponse = {
|
const resPayload: OrganizationsApiResponse = {
|
||||||
success: true,
|
success: true,
|
||||||
message: "Organizations fetched successfully",
|
message: "Organizations fetched successfully",
|
||||||
organizations: orgsWithIds,
|
organizations: orgsWithBreakdown,
|
||||||
};
|
};
|
||||||
|
|
||||||
return jsonResponse({ data: resPayload, status: 200 });
|
return jsonResponse({ data: resPayload, status: 200 });
|
||||||
|
|||||||
@@ -33,6 +33,9 @@ export interface GitOrg {
|
|||||||
isIncluded: boolean;
|
isIncluded: boolean;
|
||||||
status: RepoStatus;
|
status: RepoStatus;
|
||||||
repositoryCount: number;
|
repositoryCount: number;
|
||||||
|
publicRepositoryCount?: number;
|
||||||
|
privateRepositoryCount?: number;
|
||||||
|
forkRepositoryCount?: number;
|
||||||
createdAt: Date;
|
createdAt: Date;
|
||||||
updatedAt: Date;
|
updatedAt: Date;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user