mirror of
https://github.com/RayLabsHQ/gitea-mirror.git
synced 2025-12-08 04:26:44 +03:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4d8d75c8a6 | ||
|
|
e6c4ca0731 | ||
|
|
f03405b87a | ||
|
|
ce367e3761 | ||
|
|
cfe65cadca | ||
|
|
68108b8383 |
21
CHANGELOG.md
21
CHANGELOG.md
@@ -7,6 +7,27 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
|
## [2.17.0] - 2025-06-24
|
||||||
|
|
||||||
|
### Added
|
||||||
|
- Custom destination control for individual repositories with inline editing
|
||||||
|
- Organization-level destination overrides with visual destination editor
|
||||||
|
- Personal repositories organization override configuration option
|
||||||
|
- Visual indicators for starred repositories (⭐ icon) in repository list
|
||||||
|
- Repository-level destination override API endpoint
|
||||||
|
- Destination customization priority hierarchy system
|
||||||
|
- "View on Gitea" buttons for organizations with smart tooltip states
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
- Enhanced repository table with destination column showing both GitHub org and Gitea destination
|
||||||
|
- Updated organization cards to display custom destinations with visual indicators
|
||||||
|
- Improved getGiteaRepoOwnerAsync to support repository-level destination overrides
|
||||||
|
|
||||||
|
### Improved
|
||||||
|
- Better visual feedback for custom destinations with badges and inline editing
|
||||||
|
- Enhanced user experience with hover-based edit buttons
|
||||||
|
- Comprehensive destination customization documentation in README
|
||||||
|
|
||||||
## [2.16.3] - 2025-06-20
|
## [2.16.3] - 2025-06-20
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|||||||
42
README.md
42
README.md
@@ -37,6 +37,7 @@ See the [LXC Container Deployment Guide](scripts/README-lxc.md).
|
|||||||
|
|
||||||
- 🔁 Sync public, private, or starred GitHub repos to Gitea
|
- 🔁 Sync public, private, or starred GitHub repos to Gitea
|
||||||
- 🏢 Mirror entire organizations with flexible organization strategies
|
- 🏢 Mirror entire organizations with flexible organization strategies
|
||||||
|
- 🎯 Custom destination control for both organizations and individual repositories
|
||||||
- 🐞 Optional mirroring of issues and labels
|
- 🐞 Optional mirroring of issues and labels
|
||||||
- 🌟 Mirror your starred repositories to a dedicated organization
|
- 🌟 Mirror your starred repositories to a dedicated organization
|
||||||
- 🕹️ Modern user interface with toast notifications and smooth experience
|
- 🕹️ Modern user interface with toast notifications and smooth experience
|
||||||
@@ -317,14 +318,14 @@ Key configuration options include:
|
|||||||
> [!IMPORTANT]
|
> [!IMPORTANT]
|
||||||
> **SQLite is the only database required for Gitea Mirror**, handling both data storage and real-time event notifications.
|
> **SQLite is the only database required for Gitea Mirror**, handling both data storage and real-time event notifications.
|
||||||
|
|
||||||
### Mirror Strategies
|
### Mirror Strategies & Destination Customization
|
||||||
|
|
||||||
Gitea Mirror offers three flexible strategies for organizing your repositories in Gitea:
|
Gitea Mirror offers three flexible strategies for organizing your repositories in Gitea, with fine-grained control over destinations:
|
||||||
|
|
||||||
#### 1. **Preserve GitHub Structure** (Default)
|
#### 1. **Preserve GitHub Structure** (Default)
|
||||||
- Personal repositories → Your Gitea username
|
- Personal repositories → Your Gitea username (or custom organization)
|
||||||
- Organization repositories → Same organization name in Gitea
|
- Organization repositories → Same organization name in Gitea (with individual overrides)
|
||||||
- Maintains the exact structure from GitHub
|
- Maintains the exact structure from GitHub with optional customization
|
||||||
|
|
||||||
#### 2. **Single Organization**
|
#### 2. **Single Organization**
|
||||||
- All repositories → One designated organization
|
- All repositories → One designated organization
|
||||||
@@ -336,8 +337,37 @@ Gitea Mirror offers three flexible strategies for organizing your repositories i
|
|||||||
- No organizations needed
|
- No organizations needed
|
||||||
- Simplest approach for personal use
|
- Simplest approach for personal use
|
||||||
|
|
||||||
|
#### Destination Customization
|
||||||
|
|
||||||
|
**Organization-Level Overrides:**
|
||||||
|
- Click the edit button on any organization card to set a custom destination
|
||||||
|
- All repositories from that GitHub organization will be mirrored to your specified Gitea organization
|
||||||
|
- Visual indicators show when custom destinations are active
|
||||||
|
|
||||||
|
**Repository-Level Overrides:**
|
||||||
|
- Fine-tune individual repository destinations in the repository table
|
||||||
|
- Click the edit button in the "Destination" column to customize where a specific repo is mirrored
|
||||||
|
- Overrides organization-level settings for maximum flexibility
|
||||||
|
- Starred repositories display a ⭐ icon and always go to the configured starred repos organization
|
||||||
|
|
||||||
|
**Priority Hierarchy:**
|
||||||
|
1. Starred repositories → Always go to `starredReposOrg` (not editable)
|
||||||
|
2. Repository-level custom destination (highest priority for non-starred)
|
||||||
|
3. Organization-level custom destination
|
||||||
|
4. Personal repos override (for non-organization repos)
|
||||||
|
5. Default strategy rules (lowest priority)
|
||||||
|
|
||||||
> [!NOTE]
|
> [!NOTE]
|
||||||
> **Starred Repositories**: Regardless of the chosen strategy, starred repositories are always mirrored to a separate organization (default: "starred") to keep them organized separately from your own repositories.
|
> **Starred Repositories**: Repositories you've starred on GitHub are automatically organized into a separate organization (default: "starred") and cannot have custom destinations. They're marked with a ⭐ icon for easy identification.
|
||||||
|
|
||||||
|
> [!TIP]
|
||||||
|
> **Example Use Cases**:
|
||||||
|
> - Mirror personal repos to `personal-archive` organization
|
||||||
|
> - Redirect `work-org` repos to `company-mirror` in Gitea
|
||||||
|
> - Override a single important repo to go to a special organization
|
||||||
|
> - Keep `company-org` repos in their own `company-org` organization
|
||||||
|
> - Override `community-scripts` to go to `community-mirrors` organization
|
||||||
|
> - This gives you complete control while maintaining GitHub's structure as the default
|
||||||
|
|
||||||
## 🚀 Development
|
## 🚀 Development
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "gitea-mirror",
|
"name": "gitea-mirror",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"version": "2.16.3",
|
"version": "2.17.0",
|
||||||
"engines": {
|
"engines": {
|
||||||
"bun": ">=1.2.9"
|
"bun": ">=1.2.9"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -97,6 +97,11 @@ export function ConfigTabs() {
|
|||||||
return isGitHubValid && isGiteaValid;
|
return isGitHubValid && isGiteaValid;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const isGitHubConfigValid = (): boolean => {
|
||||||
|
const { githubConfig } = config;
|
||||||
|
return !!(githubConfig.username.trim() && githubConfig.token.trim());
|
||||||
|
};
|
||||||
|
|
||||||
// Removed the problematic useEffect that was causing circular dependencies
|
// Removed the problematic useEffect that was causing circular dependencies
|
||||||
// The lastRun and nextRun should be managed by the backend and fetched via API
|
// The lastRun and nextRun should be managed by the backend and fetched via API
|
||||||
|
|
||||||
@@ -571,10 +576,10 @@ export function ConfigTabs() {
|
|||||||
<div className="flex gap-x-4">
|
<div className="flex gap-x-4">
|
||||||
<Button
|
<Button
|
||||||
onClick={handleImportGitHubData}
|
onClick={handleImportGitHubData}
|
||||||
disabled={isSyncing || !isConfigFormValid()}
|
disabled={isSyncing || !isGitHubConfigValid()}
|
||||||
title={
|
title={
|
||||||
!isConfigFormValid()
|
!isGitHubConfigValid()
|
||||||
? 'Please fill all required GitHub and Gitea fields'
|
? 'Please fill GitHub username and token fields'
|
||||||
: isSyncing
|
: isSyncing
|
||||||
? 'Import in progress'
|
? 'Import in progress'
|
||||||
: 'Import GitHub Data'
|
: 'Import GitHub Data'
|
||||||
|
|||||||
@@ -217,6 +217,7 @@ export function GiteaConfigForm({ config, setConfig, onAutoSave, isAutoSaving, g
|
|||||||
strategy={mirrorStrategy}
|
strategy={mirrorStrategy}
|
||||||
destinationOrg={config.organization}
|
destinationOrg={config.organization}
|
||||||
starredReposOrg={config.starredReposOrg}
|
starredReposOrg={config.starredReposOrg}
|
||||||
|
personalReposOrg={config.personalReposOrg}
|
||||||
visibility={config.visibility}
|
visibility={config.visibility}
|
||||||
onDestinationOrgChange={(org) => {
|
onDestinationOrgChange={(org) => {
|
||||||
const newConfig = { ...config, organization: org };
|
const newConfig = { ...config, organization: org };
|
||||||
@@ -228,6 +229,11 @@ export function GiteaConfigForm({ config, setConfig, onAutoSave, isAutoSaving, g
|
|||||||
setConfig(newConfig);
|
setConfig(newConfig);
|
||||||
if (onAutoSave) onAutoSave(newConfig);
|
if (onAutoSave) onAutoSave(newConfig);
|
||||||
}}
|
}}
|
||||||
|
onPersonalReposOrgChange={(org) => {
|
||||||
|
const newConfig = { ...config, personalReposOrg: org };
|
||||||
|
setConfig(newConfig);
|
||||||
|
if (onAutoSave) onAutoSave(newConfig);
|
||||||
|
}}
|
||||||
onVisibilityChange={(visibility) => {
|
onVisibilityChange={(visibility) => {
|
||||||
const newConfig = { ...config, visibility };
|
const newConfig = { ...config, visibility };
|
||||||
setConfig(newConfig);
|
setConfig(newConfig);
|
||||||
|
|||||||
@@ -15,9 +15,11 @@ interface OrganizationConfigurationProps {
|
|||||||
strategy: MirrorStrategy;
|
strategy: MirrorStrategy;
|
||||||
destinationOrg?: string;
|
destinationOrg?: string;
|
||||||
starredReposOrg?: string;
|
starredReposOrg?: string;
|
||||||
|
personalReposOrg?: string;
|
||||||
visibility: GiteaOrgVisibility;
|
visibility: GiteaOrgVisibility;
|
||||||
onDestinationOrgChange: (org: string) => void;
|
onDestinationOrgChange: (org: string) => void;
|
||||||
onStarredReposOrgChange: (org: string) => void;
|
onStarredReposOrgChange: (org: string) => void;
|
||||||
|
onPersonalReposOrgChange: (org: string) => void;
|
||||||
onVisibilityChange: (visibility: GiteaOrgVisibility) => void;
|
onVisibilityChange: (visibility: GiteaOrgVisibility) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,9 +33,11 @@ export const OrganizationConfiguration: React.FC<OrganizationConfigurationProps>
|
|||||||
strategy,
|
strategy,
|
||||||
destinationOrg,
|
destinationOrg,
|
||||||
starredReposOrg,
|
starredReposOrg,
|
||||||
|
personalReposOrg,
|
||||||
visibility,
|
visibility,
|
||||||
onDestinationOrgChange,
|
onDestinationOrgChange,
|
||||||
onStarredReposOrgChange,
|
onStarredReposOrgChange,
|
||||||
|
onPersonalReposOrgChange,
|
||||||
onVisibilityChange,
|
onVisibilityChange,
|
||||||
}) => {
|
}) => {
|
||||||
return (
|
return (
|
||||||
@@ -75,7 +79,7 @@ export const OrganizationConfiguration: React.FC<OrganizationConfigurationProps>
|
|||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Right column - shows destination org for single-org, empty div for others */}
|
{/* Right column - shows destination org for single-org, personal repos org for preserve, empty div for others */}
|
||||||
{strategy === "single-org" ? (
|
{strategy === "single-org" ? (
|
||||||
<div className="space-y-1">
|
<div className="space-y-1">
|
||||||
<Label htmlFor="destinationOrg" className="text-sm font-normal flex items-center gap-2">
|
<Label htmlFor="destinationOrg" className="text-sm font-normal flex items-center gap-2">
|
||||||
@@ -102,6 +106,32 @@ export const OrganizationConfiguration: React.FC<OrganizationConfigurationProps>
|
|||||||
Organization for consolidated repositories
|
Organization for consolidated repositories
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
) : strategy === "preserve" ? (
|
||||||
|
<div className="space-y-1">
|
||||||
|
<Label htmlFor="personalReposOrg" className="text-sm font-normal flex items-center gap-2">
|
||||||
|
Personal Repos Organization
|
||||||
|
<TooltipProvider>
|
||||||
|
<Tooltip>
|
||||||
|
<TooltipTrigger>
|
||||||
|
<Info className="h-3.5 w-3.5 text-muted-foreground" />
|
||||||
|
</TooltipTrigger>
|
||||||
|
<TooltipContent>
|
||||||
|
<p>Override where your personal repositories are mirrored (leave empty to use your username)</p>
|
||||||
|
</TooltipContent>
|
||||||
|
</Tooltip>
|
||||||
|
</TooltipProvider>
|
||||||
|
</Label>
|
||||||
|
<Input
|
||||||
|
id="personalReposOrg"
|
||||||
|
value={personalReposOrg || ""}
|
||||||
|
onChange={(e) => onPersonalReposOrgChange(e.target.value)}
|
||||||
|
placeholder="my-personal-mirrors"
|
||||||
|
className=""
|
||||||
|
/>
|
||||||
|
<p className="text-xs text-muted-foreground mt-1">
|
||||||
|
Override destination for your personal repos
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
) : (
|
) : (
|
||||||
<div className="hidden md:block" />
|
<div className="hidden md:block" />
|
||||||
)}
|
)}
|
||||||
|
|||||||
193
src/components/organizations/MirrorDestinationEditor.tsx
Normal file
193
src/components/organizations/MirrorDestinationEditor.tsx
Normal file
@@ -0,0 +1,193 @@
|
|||||||
|
import { useState } from "react";
|
||||||
|
import { ArrowRight, Edit3, RotateCcw, CheckCircle2, XCircle, Building2 } from "lucide-react";
|
||||||
|
import { Button } from "@/components/ui/button";
|
||||||
|
import { Input } from "@/components/ui/input";
|
||||||
|
import { Label } from "@/components/ui/label";
|
||||||
|
import {
|
||||||
|
Popover,
|
||||||
|
PopoverContent,
|
||||||
|
PopoverTrigger,
|
||||||
|
} from "@/components/ui/popover";
|
||||||
|
import { Badge } from "@/components/ui/badge";
|
||||||
|
import { toast } from "sonner";
|
||||||
|
import { cn } from "@/lib/utils";
|
||||||
|
|
||||||
|
interface MirrorDestinationEditorProps {
|
||||||
|
organizationId: string;
|
||||||
|
organizationName: string;
|
||||||
|
currentDestination?: string;
|
||||||
|
onUpdate: (newDestination: string | null) => Promise<void>;
|
||||||
|
isUpdating?: boolean;
|
||||||
|
className?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function MirrorDestinationEditor({
|
||||||
|
organizationId,
|
||||||
|
organizationName,
|
||||||
|
currentDestination,
|
||||||
|
onUpdate,
|
||||||
|
isUpdating = false,
|
||||||
|
className,
|
||||||
|
}: MirrorDestinationEditorProps) {
|
||||||
|
const [isOpen, setIsOpen] = useState(false);
|
||||||
|
const [editValue, setEditValue] = useState(currentDestination || "");
|
||||||
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
|
||||||
|
const hasOverride = currentDestination && currentDestination !== organizationName;
|
||||||
|
const effectiveDestination = currentDestination || organizationName;
|
||||||
|
|
||||||
|
const handleSave = async () => {
|
||||||
|
const trimmedValue = editValue.trim();
|
||||||
|
const newDestination = trimmedValue === "" || trimmedValue === organizationName
|
||||||
|
? null
|
||||||
|
: trimmedValue;
|
||||||
|
|
||||||
|
setIsLoading(true);
|
||||||
|
try {
|
||||||
|
await onUpdate(newDestination);
|
||||||
|
setIsOpen(false);
|
||||||
|
toast.success(
|
||||||
|
newDestination
|
||||||
|
? `Destination updated to: ${newDestination}`
|
||||||
|
: "Destination reset to default"
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
toast.error("Failed to update destination");
|
||||||
|
} finally {
|
||||||
|
setIsLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleReset = async () => {
|
||||||
|
setEditValue("");
|
||||||
|
await handleSave();
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleCancel = () => {
|
||||||
|
setEditValue(currentDestination || "");
|
||||||
|
setIsOpen(false);
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className={cn("flex items-center gap-2", className)}>
|
||||||
|
<div className="flex items-center gap-1.5 text-xs text-muted-foreground">
|
||||||
|
<Building2 className="h-3 w-3" />
|
||||||
|
<span className="font-medium">{organizationName}</span>
|
||||||
|
<ArrowRight className="h-3 w-3" />
|
||||||
|
<span className={cn(
|
||||||
|
"font-medium",
|
||||||
|
hasOverride && "text-orange-600 dark:text-orange-400"
|
||||||
|
)}>
|
||||||
|
{effectiveDestination}
|
||||||
|
</span>
|
||||||
|
{hasOverride && (
|
||||||
|
<Badge variant="outline" className="h-4 px-1 text-[10px] border-orange-600 text-orange-600 dark:border-orange-400 dark:text-orange-400">
|
||||||
|
custom
|
||||||
|
</Badge>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<Popover open={isOpen} onOpenChange={setIsOpen}>
|
||||||
|
<PopoverTrigger asChild>
|
||||||
|
<Button
|
||||||
|
size="sm"
|
||||||
|
variant="ghost"
|
||||||
|
className="h-6 w-6 p-0 opacity-60 hover:opacity-100"
|
||||||
|
title="Edit mirror destination"
|
||||||
|
disabled={isUpdating || isLoading}
|
||||||
|
>
|
||||||
|
<Edit3 className="h-3 w-3" />
|
||||||
|
</Button>
|
||||||
|
</PopoverTrigger>
|
||||||
|
<PopoverContent className="w-80" align="end">
|
||||||
|
<div className="space-y-4">
|
||||||
|
<div>
|
||||||
|
<h4 className="font-medium text-sm mb-1">Mirror Destination</h4>
|
||||||
|
<p className="text-xs text-muted-foreground">
|
||||||
|
Customize where this organization's repositories are mirrored to in Gitea.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="space-y-3">
|
||||||
|
{/* Visual Preview */}
|
||||||
|
<div className="rounded-md bg-muted/50 p-3 space-y-2">
|
||||||
|
<div className="text-xs font-medium text-muted-foreground">Preview</div>
|
||||||
|
<div className="flex items-center gap-2 text-sm">
|
||||||
|
<div className="flex items-center gap-1.5">
|
||||||
|
<Building2 className="h-4 w-4 text-muted-foreground" />
|
||||||
|
<span>{organizationName}</span>
|
||||||
|
</div>
|
||||||
|
<ArrowRight className="h-4 w-4 text-muted-foreground" />
|
||||||
|
<div className="flex items-center gap-1.5">
|
||||||
|
<Building2 className="h-4 w-4 text-primary" />
|
||||||
|
<span className="font-medium text-primary">
|
||||||
|
{editValue.trim() || organizationName}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Input Field */}
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label htmlFor="destination" className="text-xs">
|
||||||
|
Destination Organization
|
||||||
|
</Label>
|
||||||
|
<Input
|
||||||
|
id="destination"
|
||||||
|
value={editValue}
|
||||||
|
onChange={(e) => setEditValue(e.target.value)}
|
||||||
|
placeholder={organizationName}
|
||||||
|
className="h-8"
|
||||||
|
disabled={isLoading}
|
||||||
|
/>
|
||||||
|
<p className="text-xs text-muted-foreground">
|
||||||
|
Leave empty to use the default GitHub organization name
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Quick Actions */}
|
||||||
|
{hasOverride && (
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
size="sm"
|
||||||
|
onClick={handleReset}
|
||||||
|
disabled={isLoading}
|
||||||
|
className="w-full h-8 text-xs"
|
||||||
|
>
|
||||||
|
<RotateCcw className="h-3 w-3 mr-2" />
|
||||||
|
Reset to Default ({organizationName})
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Action Buttons */}
|
||||||
|
<div className="flex gap-2 justify-end">
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
size="sm"
|
||||||
|
onClick={handleCancel}
|
||||||
|
disabled={isLoading}
|
||||||
|
>
|
||||||
|
Cancel
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
size="sm"
|
||||||
|
onClick={handleSave}
|
||||||
|
disabled={isLoading || (editValue.trim() === (currentDestination || ""))}
|
||||||
|
>
|
||||||
|
{isLoading ? (
|
||||||
|
<>
|
||||||
|
<CheckCircle2 className="h-3 w-3 mr-2 animate-spin" />
|
||||||
|
Saving...
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
"Save"
|
||||||
|
)}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</PopoverContent>
|
||||||
|
</Popover>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -404,6 +404,7 @@ export function Organization() {
|
|||||||
loadingOrgIds={loadingOrgIds}
|
loadingOrgIds={loadingOrgIds}
|
||||||
onMirror={handleMirrorOrg}
|
onMirror={handleMirrorOrg}
|
||||||
onAddOrganization={() => setIsDialogOpen(true)}
|
onAddOrganization={() => setIsDialogOpen(true)}
|
||||||
|
onRefresh={() => fetchOrganizations(false)}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<AddOrganizationDialog
|
<AddOrganizationDialog
|
||||||
|
|||||||
@@ -3,12 +3,14 @@ import { Card } from "@/components/ui/card";
|
|||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
import { Badge } from "@/components/ui/badge";
|
import { Badge } from "@/components/ui/badge";
|
||||||
import { Plus, RefreshCw, Building2, Check, AlertCircle, Clock } from "lucide-react";
|
import { Plus, RefreshCw, Building2, Check, AlertCircle, Clock } from "lucide-react";
|
||||||
import { SiGithub } from "react-icons/si";
|
import { SiGithub, SiGitea } from "react-icons/si";
|
||||||
import type { Organization } from "@/lib/db/schema";
|
import type { Organization } from "@/lib/db/schema";
|
||||||
import type { FilterParams } from "@/types/filter";
|
import type { FilterParams } from "@/types/filter";
|
||||||
import Fuse from "fuse.js";
|
import Fuse from "fuse.js";
|
||||||
import { Skeleton } from "@/components/ui/skeleton";
|
import { Skeleton } from "@/components/ui/skeleton";
|
||||||
import { cn } from "@/lib/utils";
|
import { cn } from "@/lib/utils";
|
||||||
|
import { MirrorDestinationEditor } from "./MirrorDestinationEditor";
|
||||||
|
import { useGiteaConfig } from "@/hooks/useGiteaConfig";
|
||||||
|
|
||||||
interface OrganizationListProps {
|
interface OrganizationListProps {
|
||||||
organizations: Organization[];
|
organizations: Organization[];
|
||||||
@@ -18,6 +20,7 @@ interface OrganizationListProps {
|
|||||||
onMirror: ({ orgId }: { orgId: string }) => Promise<void>;
|
onMirror: ({ orgId }: { orgId: string }) => Promise<void>;
|
||||||
loadingOrgIds: Set<string>;
|
loadingOrgIds: Set<string>;
|
||||||
onAddOrganization?: () => void;
|
onAddOrganization?: () => void;
|
||||||
|
onRefresh?: () => Promise<void>;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper function to get status badge variant and icon
|
// Helper function to get status badge variant and icon
|
||||||
@@ -44,7 +47,59 @@ export function OrganizationList({
|
|||||||
onMirror,
|
onMirror,
|
||||||
loadingOrgIds,
|
loadingOrgIds,
|
||||||
onAddOrganization,
|
onAddOrganization,
|
||||||
|
onRefresh,
|
||||||
}: OrganizationListProps) {
|
}: OrganizationListProps) {
|
||||||
|
const { giteaConfig } = useGiteaConfig();
|
||||||
|
|
||||||
|
// Helper function to construct Gitea organization URL
|
||||||
|
const getGiteaOrgUrl = (organization: Organization): string | null => {
|
||||||
|
if (!giteaConfig?.url) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only provide Gitea links for organizations that have been mirrored
|
||||||
|
const validStatuses = ['mirroring', 'mirrored'];
|
||||||
|
if (!validStatuses.includes(organization.status || '')) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use destinationOrg if available, otherwise use the organization name
|
||||||
|
const orgName = organization.destinationOrg || organization.name;
|
||||||
|
if (!orgName) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure the base URL doesn't have a trailing slash
|
||||||
|
const baseUrl = giteaConfig.url.endsWith('/')
|
||||||
|
? giteaConfig.url.slice(0, -1)
|
||||||
|
: giteaConfig.url;
|
||||||
|
|
||||||
|
return `${baseUrl}/${orgName}`;
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleUpdateDestination = async (orgId: string, newDestination: string | null) => {
|
||||||
|
// Call API to update organization destination
|
||||||
|
const response = await fetch(`/api/organizations/${orgId}`, {
|
||||||
|
method: "PATCH",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
destinationOrg: newDestination,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const errorData = await response.json();
|
||||||
|
throw new Error(errorData.error || "Failed to update organization");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Refresh organizations data
|
||||||
|
if (onRefresh) {
|
||||||
|
await onRefresh();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const hasAnyFilter = Object.values(filter).some(
|
const hasAnyFilter = Object.values(filter).some(
|
||||||
(val) => val?.toString().trim() !== ""
|
(val) => val?.toString().trim() !== ""
|
||||||
);
|
);
|
||||||
@@ -140,6 +195,17 @@ export function OrganizationList({
|
|||||||
{org.membershipRole}
|
{org.membershipRole}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{/* Destination override section */}
|
||||||
|
<div className="mt-2">
|
||||||
|
<MirrorDestinationEditor
|
||||||
|
organizationId={org.id!}
|
||||||
|
organizationName={org.name!}
|
||||||
|
currentDestination={org.destinationOrg}
|
||||||
|
onUpdate={(newDestination) => handleUpdateDestination(org.id!, newDestination)}
|
||||||
|
isUpdating={isLoading}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<Badge variant={statusBadge.variant} className="ml-2">
|
<Badge variant={statusBadge.variant} className="ml-2">
|
||||||
{StatusIcon && <StatusIcon className={cn(
|
{StatusIcon && <StatusIcon className={cn(
|
||||||
@@ -250,16 +316,54 @@ export function OrganizationList({
|
|||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<Button variant="ghost" size="icon" asChild>
|
<div className="flex items-center gap-1">
|
||||||
<a
|
{(() => {
|
||||||
href={`https://github.com/${org.name}`}
|
const giteaUrl = getGiteaOrgUrl(org);
|
||||||
target="_blank"
|
|
||||||
rel="noopener noreferrer"
|
// Determine tooltip based on status and configuration
|
||||||
title="View on GitHub"
|
let tooltip: string;
|
||||||
>
|
if (!giteaConfig?.url) {
|
||||||
<SiGithub className="h-4 w-4" />
|
tooltip = "Gitea not configured";
|
||||||
</a>
|
} else if (org.status === 'imported') {
|
||||||
</Button>
|
tooltip = "Organization not yet mirrored to Gitea";
|
||||||
|
} else if (org.status === 'failed') {
|
||||||
|
tooltip = "Organization mirroring failed";
|
||||||
|
} else if (org.status === 'mirroring') {
|
||||||
|
tooltip = "Organization is being mirrored to Gitea";
|
||||||
|
} else if (giteaUrl) {
|
||||||
|
tooltip = "View on Gitea";
|
||||||
|
} else {
|
||||||
|
tooltip = "Gitea organization not available";
|
||||||
|
}
|
||||||
|
|
||||||
|
return giteaUrl ? (
|
||||||
|
<Button variant="ghost" size="icon" asChild>
|
||||||
|
<a
|
||||||
|
href={giteaUrl}
|
||||||
|
target="_blank"
|
||||||
|
rel="noopener noreferrer"
|
||||||
|
title={tooltip}
|
||||||
|
>
|
||||||
|
<SiGitea className="h-4 w-4" />
|
||||||
|
</a>
|
||||||
|
</Button>
|
||||||
|
) : (
|
||||||
|
<Button variant="ghost" size="icon" disabled title={tooltip}>
|
||||||
|
<SiGitea className="h-4 w-4" />
|
||||||
|
</Button>
|
||||||
|
);
|
||||||
|
})()}
|
||||||
|
<Button variant="ghost" size="icon" asChild>
|
||||||
|
<a
|
||||||
|
href={`https://github.com/${org.name}`}
|
||||||
|
target="_blank"
|
||||||
|
rel="noopener noreferrer"
|
||||||
|
title="View on GitHub"
|
||||||
|
>
|
||||||
|
<SiGithub className="h-4 w-4" />
|
||||||
|
</a>
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</Card>
|
</Card>
|
||||||
);
|
);
|
||||||
|
|||||||
187
src/components/repositories/InlineDestinationEditor.tsx
Normal file
187
src/components/repositories/InlineDestinationEditor.tsx
Normal file
@@ -0,0 +1,187 @@
|
|||||||
|
import { useState, useRef, useEffect } from "react";
|
||||||
|
import { Edit3, Check, X } from "lucide-react";
|
||||||
|
import { Button } from "@/components/ui/button";
|
||||||
|
import { Input } from "@/components/ui/input";
|
||||||
|
import { Badge } from "@/components/ui/badge";
|
||||||
|
import { cn } from "@/lib/utils";
|
||||||
|
import type { Repository } from "@/lib/db/schema";
|
||||||
|
|
||||||
|
interface InlineDestinationEditorProps {
|
||||||
|
repository: Repository;
|
||||||
|
giteaConfig: any;
|
||||||
|
onUpdate: (repoId: string, newDestination: string | null) => Promise<void>;
|
||||||
|
isUpdating?: boolean;
|
||||||
|
className?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function InlineDestinationEditor({
|
||||||
|
repository,
|
||||||
|
giteaConfig,
|
||||||
|
onUpdate,
|
||||||
|
isUpdating = false,
|
||||||
|
className,
|
||||||
|
}: InlineDestinationEditorProps) {
|
||||||
|
const [isEditing, setIsEditing] = useState(false);
|
||||||
|
const [editValue, setEditValue] = useState("");
|
||||||
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
const inputRef = useRef<HTMLInputElement>(null);
|
||||||
|
|
||||||
|
// Determine the default destination based on repository properties and config
|
||||||
|
const getDefaultDestination = () => {
|
||||||
|
// Starred repos always go to the configured starredReposOrg
|
||||||
|
if (repository.isStarred && giteaConfig?.starredReposOrg) {
|
||||||
|
return giteaConfig.starredReposOrg;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check mirror strategy
|
||||||
|
const strategy = giteaConfig?.mirrorStrategy || 'preserve';
|
||||||
|
|
||||||
|
if (strategy === 'single-org' && giteaConfig?.organization) {
|
||||||
|
// All repos go to a single organization
|
||||||
|
return giteaConfig.organization;
|
||||||
|
} else if (strategy === 'flat-user') {
|
||||||
|
// All repos go under the user account
|
||||||
|
return giteaConfig?.username || repository.owner;
|
||||||
|
} else {
|
||||||
|
// 'preserve' strategy or default
|
||||||
|
// For organization repos, use the organization name
|
||||||
|
if (repository.organization) {
|
||||||
|
return repository.organization;
|
||||||
|
}
|
||||||
|
// For personal repos, check if personalReposOrg is configured
|
||||||
|
if (!repository.organization && giteaConfig?.personalReposOrg) {
|
||||||
|
return giteaConfig.personalReposOrg;
|
||||||
|
}
|
||||||
|
// Default to the gitea username or owner
|
||||||
|
return giteaConfig?.username || repository.owner;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const defaultDestination = getDefaultDestination();
|
||||||
|
const currentDestination = repository.destinationOrg || defaultDestination;
|
||||||
|
const hasOverride = repository.destinationOrg && repository.destinationOrg !== defaultDestination;
|
||||||
|
const isStarredRepo = repository.isStarred && giteaConfig?.starredReposOrg;
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (isEditing && inputRef.current) {
|
||||||
|
inputRef.current.focus();
|
||||||
|
inputRef.current.select();
|
||||||
|
}
|
||||||
|
}, [isEditing]);
|
||||||
|
|
||||||
|
const handleStartEdit = () => {
|
||||||
|
if (isStarredRepo) return; // Don't allow editing starred repos
|
||||||
|
setEditValue(currentDestination);
|
||||||
|
setIsEditing(true);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleSave = async () => {
|
||||||
|
const trimmedValue = editValue.trim();
|
||||||
|
const newDestination = trimmedValue === defaultDestination ? null : trimmedValue;
|
||||||
|
|
||||||
|
if (trimmedValue === currentDestination) {
|
||||||
|
setIsEditing(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setIsLoading(true);
|
||||||
|
try {
|
||||||
|
await onUpdate(repository.id!, newDestination);
|
||||||
|
setIsEditing(false);
|
||||||
|
} catch (error) {
|
||||||
|
// Revert on error
|
||||||
|
setEditValue(currentDestination);
|
||||||
|
} finally {
|
||||||
|
setIsLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleCancel = () => {
|
||||||
|
setEditValue(currentDestination);
|
||||||
|
setIsEditing(false);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleKeyDown = (e: React.KeyboardEvent) => {
|
||||||
|
if (e.key === "Enter") {
|
||||||
|
e.preventDefault();
|
||||||
|
handleSave();
|
||||||
|
} else if (e.key === "Escape") {
|
||||||
|
e.preventDefault();
|
||||||
|
handleCancel();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (isEditing) {
|
||||||
|
return (
|
||||||
|
<div className={cn("flex items-center gap-1", className)}>
|
||||||
|
<Input
|
||||||
|
ref={inputRef}
|
||||||
|
value={editValue}
|
||||||
|
onChange={(e) => setEditValue(e.target.value)}
|
||||||
|
onKeyDown={handleKeyDown}
|
||||||
|
onBlur={handleCancel}
|
||||||
|
className="h-6 text-sm px-2 py-0 w-24"
|
||||||
|
disabled={isLoading}
|
||||||
|
/>
|
||||||
|
<Button
|
||||||
|
size="sm"
|
||||||
|
variant="ghost"
|
||||||
|
className="h-5 w-5 p-0"
|
||||||
|
onClick={handleSave}
|
||||||
|
disabled={isLoading}
|
||||||
|
>
|
||||||
|
<Check className="h-3 w-3" />
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
size="sm"
|
||||||
|
variant="ghost"
|
||||||
|
className="h-5 w-5 p-0"
|
||||||
|
onClick={handleCancel}
|
||||||
|
disabled={isLoading}
|
||||||
|
>
|
||||||
|
<X className="h-3 w-3" />
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className={cn("flex flex-col gap-0.5", className)}>
|
||||||
|
{/* Show GitHub org if exists */}
|
||||||
|
{repository.organization && (
|
||||||
|
<span className="text-xs text-muted-foreground">
|
||||||
|
{repository.organization}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Show Gitea destination */}
|
||||||
|
<div className="flex items-center gap-1 group">
|
||||||
|
<span className="text-sm">
|
||||||
|
{currentDestination || "-"}
|
||||||
|
</span>
|
||||||
|
{hasOverride && (
|
||||||
|
<Badge variant="outline" className="h-4 px-1 text-[10px] ml-1">
|
||||||
|
custom
|
||||||
|
</Badge>
|
||||||
|
)}
|
||||||
|
{isStarredRepo && (
|
||||||
|
<Badge variant="secondary" className="h-4 px-1 text-[10px] ml-1">
|
||||||
|
starred
|
||||||
|
</Badge>
|
||||||
|
)}
|
||||||
|
{!isStarredRepo && (
|
||||||
|
<Button
|
||||||
|
size="sm"
|
||||||
|
variant="ghost"
|
||||||
|
className="h-4 w-4 p-0 opacity-0 group-hover:opacity-60 hover:opacity-100 ml-1"
|
||||||
|
onClick={handleStartEdit}
|
||||||
|
disabled={isUpdating || isLoading}
|
||||||
|
title="Edit destination"
|
||||||
|
>
|
||||||
|
<Edit3 className="h-3 w-3" />
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -721,6 +721,7 @@ export default function Repository() {
|
|||||||
loadingRepoIds={loadingRepoIds}
|
loadingRepoIds={loadingRepoIds}
|
||||||
selectedRepoIds={selectedRepoIds}
|
selectedRepoIds={selectedRepoIds}
|
||||||
onSelectionChange={setSelectedRepoIds}
|
onSelectionChange={setSelectedRepoIds}
|
||||||
|
onRefresh={() => fetchRepositories(false)}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { useMemo, useRef } from "react";
|
import { useMemo, useRef } from "react";
|
||||||
import Fuse from "fuse.js";
|
import Fuse from "fuse.js";
|
||||||
import { useVirtualizer } from "@tanstack/react-virtual";
|
import { useVirtualizer } from "@tanstack/react-virtual";
|
||||||
import { FlipHorizontal, GitFork, RefreshCw, RotateCcw } from "lucide-react";
|
import { FlipHorizontal, GitFork, RefreshCw, RotateCcw, Star } from "lucide-react";
|
||||||
import { SiGithub, SiGitea } from "react-icons/si";
|
import { SiGithub, SiGitea } from "react-icons/si";
|
||||||
import type { Repository } from "@/lib/db/schema";
|
import type { Repository } from "@/lib/db/schema";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
@@ -16,6 +16,7 @@ import {
|
|||||||
TooltipProvider,
|
TooltipProvider,
|
||||||
TooltipTrigger,
|
TooltipTrigger,
|
||||||
} from "@/components/ui/tooltip";
|
} from "@/components/ui/tooltip";
|
||||||
|
import { InlineDestinationEditor } from "./InlineDestinationEditor";
|
||||||
|
|
||||||
interface RepositoryTableProps {
|
interface RepositoryTableProps {
|
||||||
repositories: Repository[];
|
repositories: Repository[];
|
||||||
@@ -29,6 +30,7 @@ interface RepositoryTableProps {
|
|||||||
loadingRepoIds: Set<string>;
|
loadingRepoIds: Set<string>;
|
||||||
selectedRepoIds: Set<string>;
|
selectedRepoIds: Set<string>;
|
||||||
onSelectionChange: (selectedIds: Set<string>) => void;
|
onSelectionChange: (selectedIds: Set<string>) => void;
|
||||||
|
onRefresh?: () => Promise<void>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function RepositoryTable({
|
export default function RepositoryTable({
|
||||||
@@ -43,10 +45,34 @@ export default function RepositoryTable({
|
|||||||
loadingRepoIds,
|
loadingRepoIds,
|
||||||
selectedRepoIds,
|
selectedRepoIds,
|
||||||
onSelectionChange,
|
onSelectionChange,
|
||||||
|
onRefresh,
|
||||||
}: RepositoryTableProps) {
|
}: RepositoryTableProps) {
|
||||||
const tableParentRef = useRef<HTMLDivElement>(null);
|
const tableParentRef = useRef<HTMLDivElement>(null);
|
||||||
const { giteaConfig } = useGiteaConfig();
|
const { giteaConfig } = useGiteaConfig();
|
||||||
|
|
||||||
|
const handleUpdateDestination = async (repoId: string, newDestination: string | null) => {
|
||||||
|
// Call API to update repository destination
|
||||||
|
const response = await fetch(`/api/repositories/${repoId}`, {
|
||||||
|
method: "PATCH",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
destinationOrg: newDestination,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const errorData = await response.json();
|
||||||
|
throw new Error(errorData.error || "Failed to update repository");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Refresh repositories data
|
||||||
|
if (onRefresh) {
|
||||||
|
await onRefresh();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// Helper function to construct Gitea repository URL
|
// Helper function to construct Gitea repository URL
|
||||||
const getGiteaRepoUrl = (repository: Repository): string | null => {
|
const getGiteaRepoUrl = (repository: Repository): string | null => {
|
||||||
if (!giteaConfig?.url) {
|
if (!giteaConfig?.url) {
|
||||||
@@ -296,8 +322,13 @@ export default function RepositoryTable({
|
|||||||
{/* Repository */}
|
{/* Repository */}
|
||||||
<div className="h-full p-3 flex items-center gap-2 flex-[2.5]">
|
<div className="h-full p-3 flex items-center gap-2 flex-[2.5]">
|
||||||
<GitFork className="h-4 w-4 text-muted-foreground" />
|
<GitFork className="h-4 w-4 text-muted-foreground" />
|
||||||
<div>
|
<div className="flex-1">
|
||||||
<div className="font-medium">{repo.name}</div>
|
<div className="font-medium flex items-center gap-1">
|
||||||
|
{repo.name}
|
||||||
|
{repo.isStarred && (
|
||||||
|
<Star className="h-3 w-3 fill-yellow-500 text-yellow-500" />
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
<div className="text-xs text-muted-foreground">
|
<div className="text-xs text-muted-foreground">
|
||||||
{repo.fullName}
|
{repo.fullName}
|
||||||
</div>
|
</div>
|
||||||
@@ -321,7 +352,12 @@ export default function RepositoryTable({
|
|||||||
|
|
||||||
{/* Organization */}
|
{/* Organization */}
|
||||||
<div className="h-full p-3 flex items-center flex-[1]">
|
<div className="h-full p-3 flex items-center flex-[1]">
|
||||||
<p className="text-sm"> {repo.organization || "-"}</p>
|
<InlineDestinationEditor
|
||||||
|
repository={repo}
|
||||||
|
giteaConfig={giteaConfig}
|
||||||
|
onUpdate={handleUpdateDestination}
|
||||||
|
isUpdating={loadingRepoIds.has(repo.id ?? "")}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Last Mirrored */}
|
{/* Last Mirrored */}
|
||||||
|
|||||||
@@ -28,11 +28,44 @@ try {
|
|||||||
|
|
||||||
// Ensure all required tables exist
|
// Ensure all required tables exist
|
||||||
ensureTablesExist(sqlite);
|
ensureTablesExist(sqlite);
|
||||||
|
|
||||||
|
// Run migrations
|
||||||
|
runMigrations(sqlite);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error opening database:", error);
|
console.error("Error opening database:", error);
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run database migrations
|
||||||
|
*/
|
||||||
|
function runMigrations(db: Database) {
|
||||||
|
try {
|
||||||
|
// Migration 1: Add destination_org column to organizations table
|
||||||
|
const orgTableInfo = db.query("PRAGMA table_info(organizations)").all() as Array<{name: string}>;
|
||||||
|
const hasDestinationOrg = orgTableInfo.some(col => col.name === 'destination_org');
|
||||||
|
|
||||||
|
if (!hasDestinationOrg) {
|
||||||
|
console.log("🔄 Running migration: Adding destination_org column to organizations table");
|
||||||
|
db.exec("ALTER TABLE organizations ADD COLUMN destination_org TEXT");
|
||||||
|
console.log("✅ Migration completed: destination_org column added");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Migration 2: Add destination_org column to repositories table
|
||||||
|
const repoTableInfo = db.query("PRAGMA table_info(repositories)").all() as Array<{name: string}>;
|
||||||
|
const hasRepoDestinationOrg = repoTableInfo.some(col => col.name === 'destination_org');
|
||||||
|
|
||||||
|
if (!hasRepoDestinationOrg) {
|
||||||
|
console.log("🔄 Running migration: Adding destination_org column to repositories table");
|
||||||
|
db.exec("ALTER TABLE repositories ADD COLUMN destination_org TEXT");
|
||||||
|
console.log("✅ Migration completed: destination_org column added to repositories");
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("❌ Error running migrations:", error);
|
||||||
|
// Don't throw - migrations should be non-breaking
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Ensure all required tables exist in the database
|
* Ensure all required tables exist in the database
|
||||||
*/
|
*/
|
||||||
@@ -159,6 +192,7 @@ function createTable(db: Database, tableName: string) {
|
|||||||
last_mirrored INTEGER,
|
last_mirrored INTEGER,
|
||||||
error_message TEXT,
|
error_message TEXT,
|
||||||
repository_count INTEGER NOT NULL DEFAULT 0,
|
repository_count INTEGER NOT NULL DEFAULT 0,
|
||||||
|
destination_org TEXT,
|
||||||
created_at INTEGER NOT NULL DEFAULT (strftime('%s','now')),
|
created_at INTEGER NOT NULL DEFAULT (strftime('%s','now')),
|
||||||
updated_at INTEGER NOT NULL DEFAULT (strftime('%s','now')),
|
updated_at INTEGER NOT NULL DEFAULT (strftime('%s','now')),
|
||||||
FOREIGN KEY (user_id) REFERENCES users(id),
|
FOREIGN KEY (user_id) REFERENCES users(id),
|
||||||
@@ -437,6 +471,9 @@ export const organizations = sqliteTable("organizations", {
|
|||||||
.notNull()
|
.notNull()
|
||||||
.default(true),
|
.default(true),
|
||||||
|
|
||||||
|
// Override destination organization for this GitHub org's repos
|
||||||
|
destinationOrg: text("destination_org"),
|
||||||
|
|
||||||
status: text("status").notNull().default("imported"),
|
status: text("status").notNull().default("imported"),
|
||||||
lastMirrored: integer("last_mirrored", { mode: "timestamp" }),
|
lastMirrored: integer("last_mirrored", { mode: "timestamp" }),
|
||||||
errorMessage: text("error_message"),
|
errorMessage: text("error_message"),
|
||||||
|
|||||||
@@ -45,6 +45,7 @@ export const configSchema = z.object({
|
|||||||
starredReposOrg: z.string().default("github"),
|
starredReposOrg: z.string().default("github"),
|
||||||
preserveOrgStructure: z.boolean().default(false),
|
preserveOrgStructure: z.boolean().default(false),
|
||||||
mirrorStrategy: z.enum(["preserve", "single-org", "flat-user"]).optional(),
|
mirrorStrategy: z.enum(["preserve", "single-org", "flat-user"]).optional(),
|
||||||
|
personalReposOrg: z.string().optional(), // Override destination for personal repos
|
||||||
}),
|
}),
|
||||||
include: z.array(z.string()).default(["*"]),
|
include: z.array(z.string()).default(["*"]),
|
||||||
exclude: z.array(z.string()).default([]),
|
exclude: z.array(z.string()).default([]),
|
||||||
@@ -100,6 +101,7 @@ export const repositorySchema = z.object({
|
|||||||
errorMessage: z.string().optional(),
|
errorMessage: z.string().optional(),
|
||||||
|
|
||||||
mirroredLocation: z.string().default(""), // Store the full Gitea path where repo was mirrored
|
mirroredLocation: z.string().default(""), // Store the full Gitea path where repo was mirrored
|
||||||
|
destinationOrg: z.string().optional(), // Custom destination organization override
|
||||||
|
|
||||||
createdAt: z.date().default(() => new Date()),
|
createdAt: z.date().default(() => new Date()),
|
||||||
updatedAt: z.date().default(() => new Date()),
|
updatedAt: z.date().default(() => new Date()),
|
||||||
@@ -158,6 +160,9 @@ export const organizationSchema = z.object({
|
|||||||
privateRepositoryCount: z.number().optional(),
|
privateRepositoryCount: z.number().optional(),
|
||||||
forkRepositoryCount: z.number().optional(),
|
forkRepositoryCount: z.number().optional(),
|
||||||
|
|
||||||
|
// Override destination organization for this GitHub org's repos
|
||||||
|
destinationOrg: z.string().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()),
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
import { describe, test, expect, mock, beforeEach, afterEach } from "bun:test";
|
import { describe, test, expect, mock, beforeEach, afterEach } from "bun:test";
|
||||||
import { Octokit } from "@octokit/rest";
|
import { Octokit } from "@octokit/rest";
|
||||||
import { repoStatusEnum } from "@/types/Repository";
|
import { repoStatusEnum } from "@/types/Repository";
|
||||||
import { getOrCreateGiteaOrg } from "./gitea";
|
import { getOrCreateGiteaOrg, getGiteaRepoOwner, getGiteaRepoOwnerAsync } from "./gitea";
|
||||||
|
import type { Config, Repository, Organization } from "./db/schema";
|
||||||
|
|
||||||
// Mock the isRepoPresentInGitea function
|
// Mock the isRepoPresentInGitea function
|
||||||
const mockIsRepoPresentInGitea = mock(() => Promise.resolve(false));
|
const mockIsRepoPresentInGitea = mock(() => Promise.resolve(false));
|
||||||
@@ -291,3 +292,90 @@ describe("Gitea Repository Mirroring", () => {
|
|||||||
expect(mockGetOrCreateGiteaOrg).toHaveBeenCalled();
|
expect(mockGetOrCreateGiteaOrg).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("getGiteaRepoOwner - Organization Override Tests", () => {
|
||||||
|
const baseConfig: Partial<Config> = {
|
||||||
|
githubConfig: {
|
||||||
|
username: "testuser",
|
||||||
|
token: "token",
|
||||||
|
preserveOrgStructure: false,
|
||||||
|
skipForks: false,
|
||||||
|
privateRepositories: false,
|
||||||
|
mirrorIssues: false,
|
||||||
|
mirrorWiki: false,
|
||||||
|
mirrorStarred: false,
|
||||||
|
useSpecificUser: false,
|
||||||
|
includeOrgs: [],
|
||||||
|
excludeOrgs: [],
|
||||||
|
mirrorPublicOrgs: false,
|
||||||
|
publicOrgs: [],
|
||||||
|
skipStarredIssues: false
|
||||||
|
},
|
||||||
|
giteaConfig: {
|
||||||
|
username: "giteauser",
|
||||||
|
url: "https://gitea.example.com",
|
||||||
|
token: "gitea-token",
|
||||||
|
organization: "github-mirrors",
|
||||||
|
visibility: "public",
|
||||||
|
starredReposOrg: "starred",
|
||||||
|
preserveOrgStructure: false,
|
||||||
|
mirrorStrategy: "preserve"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const baseRepo: Repository = {
|
||||||
|
id: "repo-id",
|
||||||
|
userId: "user-id",
|
||||||
|
configId: "config-id",
|
||||||
|
name: "test-repo",
|
||||||
|
fullName: "testuser/test-repo",
|
||||||
|
url: "https://github.com/testuser/test-repo",
|
||||||
|
cloneUrl: "https://github.com/testuser/test-repo.git",
|
||||||
|
owner: "testuser",
|
||||||
|
isPrivate: false,
|
||||||
|
isForked: false,
|
||||||
|
hasIssues: true,
|
||||||
|
isStarred: false,
|
||||||
|
isArchived: false,
|
||||||
|
size: 1000,
|
||||||
|
hasLFS: false,
|
||||||
|
hasSubmodules: false,
|
||||||
|
defaultBranch: "main",
|
||||||
|
visibility: "public",
|
||||||
|
status: "imported",
|
||||||
|
mirroredLocation: "",
|
||||||
|
createdAt: new Date(),
|
||||||
|
updatedAt: new Date()
|
||||||
|
};
|
||||||
|
|
||||||
|
test("starred repos go to starredReposOrg", () => {
|
||||||
|
const repo = { ...baseRepo, isStarred: true };
|
||||||
|
const result = getGiteaRepoOwner({ config: baseConfig, repository: repo });
|
||||||
|
expect(result).toBe("starred");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("preserve strategy: personal repos use personalReposOrg override", () => {
|
||||||
|
const configWithOverride = {
|
||||||
|
...baseConfig,
|
||||||
|
giteaConfig: {
|
||||||
|
...baseConfig.giteaConfig!,
|
||||||
|
personalReposOrg: "my-personal-mirrors"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const repo = { ...baseRepo, organization: undefined };
|
||||||
|
const result = getGiteaRepoOwner({ config: configWithOverride, repository: repo });
|
||||||
|
expect(result).toBe("my-personal-mirrors");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("preserve strategy: personal repos fallback to username when no override", () => {
|
||||||
|
const repo = { ...baseRepo, organization: undefined };
|
||||||
|
const result = getGiteaRepoOwner({ config: baseConfig, repository: repo });
|
||||||
|
expect(result).toBe("giteauser");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("preserve strategy: org repos go to same org name", () => {
|
||||||
|
const repo = { ...baseRepo, organization: "myorg" };
|
||||||
|
const result = getGiteaRepoOwner({ config: baseConfig, repository: repo });
|
||||||
|
expect(result).toBe("myorg");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
@@ -9,7 +9,87 @@ import type { Organization, Repository } from "./db/schema";
|
|||||||
import { httpPost, httpGet } from "./http-client";
|
import { httpPost, httpGet } from "./http-client";
|
||||||
import { createMirrorJob } from "./helpers";
|
import { createMirrorJob } from "./helpers";
|
||||||
import { db, organizations, repositories } from "./db";
|
import { db, organizations, repositories } from "./db";
|
||||||
import { eq } from "drizzle-orm";
|
import { eq, and } from "drizzle-orm";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function to get organization configuration including destination override
|
||||||
|
*/
|
||||||
|
export const getOrganizationConfig = async ({
|
||||||
|
orgName,
|
||||||
|
userId,
|
||||||
|
}: {
|
||||||
|
orgName: string;
|
||||||
|
userId: string;
|
||||||
|
}): Promise<Organization | null> => {
|
||||||
|
try {
|
||||||
|
const [orgConfig] = await db
|
||||||
|
.select()
|
||||||
|
.from(organizations)
|
||||||
|
.where(and(eq(organizations.name, orgName), eq(organizations.userId, userId)))
|
||||||
|
.limit(1);
|
||||||
|
|
||||||
|
return orgConfig || null;
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Error fetching organization config for ${orgName}:`, error);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enhanced async version of getGiteaRepoOwner that supports organization overrides
|
||||||
|
*/
|
||||||
|
export const getGiteaRepoOwnerAsync = async ({
|
||||||
|
config,
|
||||||
|
repository,
|
||||||
|
}: {
|
||||||
|
config: Partial<Config>;
|
||||||
|
repository: Repository;
|
||||||
|
}): Promise<string> => {
|
||||||
|
if (!config.githubConfig || !config.giteaConfig) {
|
||||||
|
throw new Error("GitHub or Gitea config is required.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!config.giteaConfig.username) {
|
||||||
|
throw new Error("Gitea username is required.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!config.userId) {
|
||||||
|
throw new Error("User ID is required for organization overrides.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if repository is starred - starred repos always go to starredReposOrg (highest priority)
|
||||||
|
if (repository.isStarred && config.giteaConfig.starredReposOrg) {
|
||||||
|
return config.giteaConfig.starredReposOrg;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for repository-specific override (second highest priority)
|
||||||
|
if (repository.destinationOrg) {
|
||||||
|
console.log(`Using repository override: ${repository.fullName} -> ${repository.destinationOrg}`);
|
||||||
|
return repository.destinationOrg;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for organization-specific override
|
||||||
|
if (repository.organization) {
|
||||||
|
const orgConfig = await getOrganizationConfig({
|
||||||
|
orgName: repository.organization,
|
||||||
|
userId: config.userId,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (orgConfig?.destinationOrg) {
|
||||||
|
console.log(`Using organization override: ${repository.organization} -> ${orgConfig.destinationOrg}`);
|
||||||
|
return orgConfig.destinationOrg;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for personal repos override (when it's user's repo, not an organization)
|
||||||
|
if (!repository.organization && config.giteaConfig.personalReposOrg) {
|
||||||
|
console.log(`Using personal repos override: ${config.giteaConfig.personalReposOrg}`);
|
||||||
|
return config.giteaConfig.personalReposOrg;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fall back to existing strategy logic
|
||||||
|
return getGiteaRepoOwner({ config, repository });
|
||||||
|
};
|
||||||
|
|
||||||
export const getGiteaRepoOwner = ({
|
export const getGiteaRepoOwner = ({
|
||||||
config,
|
config,
|
||||||
@@ -37,11 +117,12 @@ export const getGiteaRepoOwner = ({
|
|||||||
|
|
||||||
switch (mirrorStrategy) {
|
switch (mirrorStrategy) {
|
||||||
case "preserve":
|
case "preserve":
|
||||||
// Keep GitHub structure - org repos go to same org, personal repos to user
|
// Keep GitHub structure - org repos go to same org, personal repos to user (or override)
|
||||||
if (repository.organization) {
|
if (repository.organization) {
|
||||||
return repository.organization;
|
return repository.organization;
|
||||||
}
|
}
|
||||||
return config.giteaConfig.username;
|
// Use personal repos override if configured, otherwise use username
|
||||||
|
return config.giteaConfig.personalReposOrg || config.giteaConfig.username;
|
||||||
|
|
||||||
case "single-org":
|
case "single-org":
|
||||||
// All non-starred repos go to the destination organization
|
// All non-starred repos go to the destination organization
|
||||||
@@ -160,8 +241,8 @@ export const mirrorGithubRepoToGitea = async ({
|
|||||||
throw new Error("Gitea username is required.");
|
throw new Error("Gitea username is required.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the correct owner based on the strategy
|
// Get the correct owner based on the strategy (with organization overrides)
|
||||||
const repoOwner = getGiteaRepoOwner({ config, repository });
|
const repoOwner = await getGiteaRepoOwnerAsync({ config, repository });
|
||||||
|
|
||||||
const isExisting = await isRepoPresentInGitea({
|
const isExisting = await isRepoPresentInGitea({
|
||||||
config,
|
config,
|
||||||
@@ -987,8 +1068,8 @@ export const syncGiteaRepo = async ({
|
|||||||
status: repoStatusEnum.parse("syncing"),
|
status: repoStatusEnum.parse("syncing"),
|
||||||
});
|
});
|
||||||
|
|
||||||
// Get the expected owner based on current config
|
// Get the expected owner based on current config (with organization overrides)
|
||||||
const repoOwner = getGiteaRepoOwner({ config, repository });
|
const repoOwner = await getGiteaRepoOwnerAsync({ config, repository });
|
||||||
|
|
||||||
// Check if repo exists at the expected location or alternate location
|
// Check if repo exists at the expected location or alternate location
|
||||||
const { present, actualOwner } = await checkRepoLocation({
|
const { present, actualOwner } = await checkRepoLocation({
|
||||||
@@ -1289,7 +1370,7 @@ export async function mirrorGitHubReleasesToGitea({
|
|||||||
throw new Error("Gitea config is incomplete for mirroring releases.");
|
throw new Error("Gitea config is incomplete for mirroring releases.");
|
||||||
}
|
}
|
||||||
|
|
||||||
const repoOwner = getGiteaRepoOwner({
|
const repoOwner = await getGiteaRepoOwnerAsync({
|
||||||
config,
|
config,
|
||||||
repository,
|
repository,
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ interface DbGiteaConfig {
|
|||||||
starredReposOrg: string;
|
starredReposOrg: string;
|
||||||
preserveOrgStructure: boolean;
|
preserveOrgStructure: boolean;
|
||||||
mirrorStrategy?: "preserve" | "single-org" | "flat-user";
|
mirrorStrategy?: "preserve" | "single-org" | "flat-user";
|
||||||
|
personalReposOrg?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -106,6 +107,7 @@ export function mapDbToUiConfig(dbConfig: any): {
|
|||||||
starredReposOrg: dbConfig.giteaConfig?.starredReposOrg || "github",
|
starredReposOrg: dbConfig.giteaConfig?.starredReposOrg || "github",
|
||||||
preserveOrgStructure: dbConfig.giteaConfig?.preserveOrgStructure || false,
|
preserveOrgStructure: dbConfig.giteaConfig?.preserveOrgStructure || false,
|
||||||
mirrorStrategy: dbConfig.giteaConfig?.mirrorStrategy,
|
mirrorStrategy: dbConfig.giteaConfig?.mirrorStrategy,
|
||||||
|
personalReposOrg: dbConfig.giteaConfig?.personalReposOrg,
|
||||||
};
|
};
|
||||||
|
|
||||||
const mirrorOptions: MirrorOptions = {
|
const mirrorOptions: MirrorOptions = {
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import { repositoryVisibilityEnum, repoStatusEnum } from "@/types/Repository";
|
|||||||
import {
|
import {
|
||||||
mirrorGithubRepoToGitea,
|
mirrorGithubRepoToGitea,
|
||||||
mirrorGitHubOrgRepoToGiteaOrg,
|
mirrorGitHubOrgRepoToGiteaOrg,
|
||||||
getGiteaRepoOwner,
|
getGiteaRepoOwnerAsync,
|
||||||
} from "@/lib/gitea";
|
} from "@/lib/gitea";
|
||||||
import { createGitHubClient } from "@/lib/github";
|
import { createGitHubClient } from "@/lib/github";
|
||||||
import { processWithResilience } from "@/lib/utils/concurrency";
|
import { processWithResilience } from "@/lib/utils/concurrency";
|
||||||
@@ -97,8 +97,8 @@ export const POST: APIRoute = async ({ request }) => {
|
|||||||
// Log the start of mirroring
|
// Log the start of mirroring
|
||||||
console.log(`Starting mirror for repository: ${repo.name}`);
|
console.log(`Starting mirror for repository: ${repo.name}`);
|
||||||
|
|
||||||
// Determine where the repository should be mirrored
|
// Determine where the repository should be mirrored (with organization overrides)
|
||||||
const owner = getGiteaRepoOwner({
|
const owner = await getGiteaRepoOwnerAsync({
|
||||||
config,
|
config,
|
||||||
repository: repoData,
|
repository: repoData,
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import type { APIRoute } from "astro";
|
import type { APIRoute } from "astro";
|
||||||
import { db, configs, repositories } from "@/lib/db";
|
import { db, configs, repositories } from "@/lib/db";
|
||||||
import { eq, inArray } from "drizzle-orm";
|
import { eq, inArray } from "drizzle-orm";
|
||||||
import { getGiteaRepoOwner, isRepoPresentInGitea } from "@/lib/gitea";
|
import { getGiteaRepoOwnerAsync, isRepoPresentInGitea } from "@/lib/gitea";
|
||||||
import {
|
import {
|
||||||
mirrorGithubRepoToGitea,
|
mirrorGithubRepoToGitea,
|
||||||
mirrorGitHubOrgRepoToGiteaOrg,
|
mirrorGitHubOrgRepoToGiteaOrg,
|
||||||
@@ -109,8 +109,8 @@ export const POST: APIRoute = async ({ request }) => {
|
|||||||
status: "imported",
|
status: "imported",
|
||||||
});
|
});
|
||||||
|
|
||||||
// Determine if the repository exists in Gitea
|
// Determine if the repository exists in Gitea (with organization overrides)
|
||||||
let owner = getGiteaRepoOwner({
|
let owner = await getGiteaRepoOwnerAsync({
|
||||||
config,
|
config,
|
||||||
repository: repoData,
|
repository: repoData,
|
||||||
});
|
});
|
||||||
|
|||||||
82
src/pages/api/organizations/[id].ts
Normal file
82
src/pages/api/organizations/[id].ts
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
import type { APIRoute } from "astro";
|
||||||
|
import { db, organizations } from "@/lib/db";
|
||||||
|
import { eq, and } from "drizzle-orm";
|
||||||
|
import { createSecureErrorResponse } from "@/lib/utils";
|
||||||
|
import jwt from "jsonwebtoken";
|
||||||
|
|
||||||
|
const JWT_SECRET = process.env.JWT_SECRET || "your-secret-key";
|
||||||
|
|
||||||
|
export const PATCH: APIRoute = async ({ request, params, cookies }) => {
|
||||||
|
try {
|
||||||
|
// Get token from Authorization header or cookies
|
||||||
|
const authHeader = request.headers.get("Authorization");
|
||||||
|
const token = authHeader?.split(" ")[1] || cookies.get("token")?.value;
|
||||||
|
|
||||||
|
if (!token) {
|
||||||
|
return new Response(JSON.stringify({ error: "Unauthorized" }), {
|
||||||
|
status: 401,
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify token and get user ID
|
||||||
|
let userId: string;
|
||||||
|
try {
|
||||||
|
const decoded = jwt.verify(token, JWT_SECRET) as { id: string };
|
||||||
|
userId = decoded.id;
|
||||||
|
} catch (error) {
|
||||||
|
return new Response(JSON.stringify({ error: "Invalid token" }), {
|
||||||
|
status: 401,
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const orgId = params.id;
|
||||||
|
if (!orgId) {
|
||||||
|
return new Response(JSON.stringify({ error: "Organization ID is required" }), {
|
||||||
|
status: 400,
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const body = await request.json();
|
||||||
|
const { destinationOrg } = body;
|
||||||
|
|
||||||
|
// Validate that the organization belongs to the user
|
||||||
|
const [existingOrg] = await db
|
||||||
|
.select()
|
||||||
|
.from(organizations)
|
||||||
|
.where(and(eq(organizations.id, orgId), eq(organizations.userId, userId)))
|
||||||
|
.limit(1);
|
||||||
|
|
||||||
|
if (!existingOrg) {
|
||||||
|
return new Response(JSON.stringify({ error: "Organization not found" }), {
|
||||||
|
status: 404,
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the organization's destination override
|
||||||
|
await db
|
||||||
|
.update(organizations)
|
||||||
|
.set({
|
||||||
|
destinationOrg: destinationOrg || null,
|
||||||
|
updatedAt: new Date(),
|
||||||
|
})
|
||||||
|
.where(eq(organizations.id, orgId));
|
||||||
|
|
||||||
|
return new Response(
|
||||||
|
JSON.stringify({
|
||||||
|
success: true,
|
||||||
|
message: "Organization destination updated successfully",
|
||||||
|
destinationOrg: destinationOrg || null,
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
status: 200,
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
return createSecureErrorResponse(error, "Update organization destination", 500);
|
||||||
|
}
|
||||||
|
};
|
||||||
82
src/pages/api/repositories/[id].ts
Normal file
82
src/pages/api/repositories/[id].ts
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
import type { APIRoute } from "astro";
|
||||||
|
import { db, repositories } from "@/lib/db";
|
||||||
|
import { eq, and } from "drizzle-orm";
|
||||||
|
import { createSecureErrorResponse } from "@/lib/utils";
|
||||||
|
import jwt from "jsonwebtoken";
|
||||||
|
|
||||||
|
const JWT_SECRET = process.env.JWT_SECRET || "your-secret-key";
|
||||||
|
|
||||||
|
export const PATCH: APIRoute = async ({ request, params, cookies }) => {
|
||||||
|
try {
|
||||||
|
// Get token from Authorization header or cookies
|
||||||
|
const authHeader = request.headers.get("Authorization");
|
||||||
|
const token = authHeader?.split(" ")[1] || cookies.get("token")?.value;
|
||||||
|
|
||||||
|
if (!token) {
|
||||||
|
return new Response(JSON.stringify({ error: "Unauthorized" }), {
|
||||||
|
status: 401,
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify token and get user ID
|
||||||
|
let userId: string;
|
||||||
|
try {
|
||||||
|
const decoded = jwt.verify(token, JWT_SECRET) as { id: string };
|
||||||
|
userId = decoded.id;
|
||||||
|
} catch (error) {
|
||||||
|
return new Response(JSON.stringify({ error: "Invalid token" }), {
|
||||||
|
status: 401,
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const repoId = params.id;
|
||||||
|
if (!repoId) {
|
||||||
|
return new Response(JSON.stringify({ error: "Repository ID is required" }), {
|
||||||
|
status: 400,
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const body = await request.json();
|
||||||
|
const { destinationOrg } = body;
|
||||||
|
|
||||||
|
// Validate that the repository belongs to the user
|
||||||
|
const [existingRepo] = await db
|
||||||
|
.select()
|
||||||
|
.from(repositories)
|
||||||
|
.where(and(eq(repositories.id, repoId), eq(repositories.userId, userId)))
|
||||||
|
.limit(1);
|
||||||
|
|
||||||
|
if (!existingRepo) {
|
||||||
|
return new Response(JSON.stringify({ error: "Repository not found" }), {
|
||||||
|
status: 404,
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the repository's destination override
|
||||||
|
await db
|
||||||
|
.update(repositories)
|
||||||
|
.set({
|
||||||
|
destinationOrg: destinationOrg || null,
|
||||||
|
updatedAt: new Date(),
|
||||||
|
})
|
||||||
|
.where(eq(repositories.id, repoId));
|
||||||
|
|
||||||
|
return new Response(
|
||||||
|
JSON.stringify({
|
||||||
|
success: true,
|
||||||
|
message: "Repository destination updated successfully",
|
||||||
|
destinationOrg: destinationOrg || null,
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
status: 200,
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
return createSecureErrorResponse(error, "Update repository destination", 500);
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -12,6 +12,7 @@ export interface GiteaConfig {
|
|||||||
starredReposOrg: string;
|
starredReposOrg: string;
|
||||||
preserveOrgStructure: boolean;
|
preserveOrgStructure: boolean;
|
||||||
mirrorStrategy?: MirrorStrategy; // New field for the strategy
|
mirrorStrategy?: MirrorStrategy; // New field for the strategy
|
||||||
|
personalReposOrg?: string; // Override destination for personal repos
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ScheduleConfig {
|
export interface ScheduleConfig {
|
||||||
|
|||||||
Reference in New Issue
Block a user