edit repos for gh connector

This commit is contained in:
Adamsmith6300 2025-04-16 20:29:50 -07:00
parent ae8c74a5aa
commit 5176569e30
5 changed files with 506 additions and 30 deletions

View file

@ -15,7 +15,7 @@ from sqlalchemy.future import select
from sqlalchemy.exc import IntegrityError
from typing import List, Dict, Any
from app.db import get_async_session, User, SearchSourceConnector, SearchSourceConnectorType, SearchSpace, async_session_maker
from app.schemas import SearchSourceConnectorCreate, SearchSourceConnectorUpdate, SearchSourceConnectorRead
from app.schemas import SearchSourceConnectorCreate, SearchSourceConnectorUpdate, SearchSourceConnectorRead, SearchSourceConnectorBase
from app.users import current_active_user
from app.utils.check_ownership import check_ownership
from pydantic import ValidationError, BaseModel, Field
@ -159,54 +159,84 @@ async def update_search_source_connector(
):
"""
Update a search source connector.
Each user can have only one connector of each type (SERPER_API, TAVILY_API, SLACK_CONNECTOR).
The config must contain the appropriate keys for the connector type.
Handles partial updates, including merging changes into the 'config' field.
"""
try:
db_connector = await check_ownership(session, SearchSourceConnector, connector_id, user)
db_connector = await check_ownership(session, SearchSourceConnector, connector_id, user)
# Convert the sparse update data (only fields present in request) to a dict
update_data = connector_update.model_dump(exclude_unset=True)
# Special handling for 'config' field
if "config" in update_data:
incoming_config = update_data["config"] # Config data from the request
existing_config = db_connector.config if db_connector.config else {} # Current config from DB
# If connector type is being changed, check if one of that type already exists
if connector_update.connector_type != db_connector.connector_type:
# Merge incoming config into existing config
# This preserves existing keys (like GITHUB_PAT) if they are not in the incoming data
merged_config = existing_config.copy()
merged_config.update(incoming_config)
# -- Validation after merging --
# Validate the *merged* config based on the connector type
# We need the connector type - use the one from the update if provided, else the existing one
current_connector_type = connector_update.connector_type if connector_update.connector_type is not None else db_connector.connector_type
try:
# We can reuse the base validator by creating a temporary base model instance
# Note: This assumes 'name' and 'is_indexable' are not crucial for config validation itself
temp_data_for_validation = {
"name": db_connector.name, # Use existing name
"connector_type": current_connector_type,
"is_indexable": db_connector.is_indexable, # Use existing value
"last_indexed_at": db_connector.last_indexed_at, # Not used by validator
"config": merged_config
}
SearchSourceConnectorBase.model_validate(temp_data_for_validation)
except ValidationError as e:
# Raise specific validation error for the merged config
raise HTTPException(
status_code=422,
detail=f"Validation error for merged config: {str(e)}"
)
# If validation passes, update the main update_data dict with the merged config
update_data["config"] = merged_config
# Apply all updates (including the potentially merged config)
for key, value in update_data.items():
# Prevent changing connector_type if it causes a duplicate (check moved here)
if key == "connector_type" and value != db_connector.connector_type:
result = await session.execute(
select(SearchSourceConnector)
.filter(
SearchSourceConnector.user_id == user.id,
SearchSourceConnector.connector_type == connector_update.connector_type,
SearchSourceConnector.connector_type == value,
SearchSourceConnector.id != connector_id
)
)
existing_connector = result.scalars().first()
if existing_connector:
raise HTTPException(
status_code=409,
detail=f"A connector with type {connector_update.connector_type} already exists. Each user can have only one connector of each type."
detail=f"A connector with type {value} already exists. Each user can have only one connector of each type."
)
update_data = connector_update.model_dump(exclude_unset=True)
for key, value in update_data.items():
setattr(db_connector, key, value)
setattr(db_connector, key, value)
try:
await session.commit()
await session.refresh(db_connector)
return db_connector
except ValidationError as e:
await session.rollback()
raise HTTPException(
status_code=422,
detail=f"Validation error: {str(e)}"
)
except IntegrityError as e:
await session.rollback()
# This might occur if connector_type constraint is violated somehow after the check
raise HTTPException(
status_code=409,
detail=f"Integrity error: A connector with this type already exists. {str(e)}"
detail=f"Database integrity error during update: {str(e)}"
)
except HTTPException:
await session.rollback()
raise
except Exception as e:
await session.rollback()
logger.error(f"Failed to update search source connector {connector_id}: {e}", exc_info=True)
raise HTTPException(
status_code=500,
detail=f"Failed to update search source connector: {str(e)}"

View file

@ -1,6 +1,6 @@
from datetime import datetime
import uuid
from typing import Dict, Any
from typing import Dict, Any, Optional
from pydantic import BaseModel, field_validator
from .base import IDModel, TimestampModel
from app.db import SearchSourceConnectorType
@ -9,7 +9,7 @@ class SearchSourceConnectorBase(BaseModel):
name: str
connector_type: SearchSourceConnectorType
is_indexable: bool
last_indexed_at: datetime | None
last_indexed_at: Optional[datetime] = None
config: Dict[str, Any]
@field_validator('config')
@ -77,8 +77,12 @@ class SearchSourceConnectorBase(BaseModel):
class SearchSourceConnectorCreate(SearchSourceConnectorBase):
pass
class SearchSourceConnectorUpdate(SearchSourceConnectorBase):
pass
class SearchSourceConnectorUpdate(BaseModel):
name: Optional[str] = None
connector_type: Optional[SearchSourceConnectorType] = None
is_indexable: Optional[bool] = None
last_indexed_at: Optional[datetime] = None
config: Optional[Dict[str, Any]] = None
class SearchSourceConnectorRead(SearchSourceConnectorBase, IDModel, TimestampModel):
user_id: uuid.UUID

View file

@ -205,7 +205,7 @@ export default function ConnectorsPage() {
<Button
variant="outline"
size="sm"
onClick={() => router.push(`/dashboard/${searchSpaceId}/connectors/${connector.id}`)}
onClick={() => router.push(`/dashboard/${searchSpaceId}/connectors/${connector.id}/edit`)}
>
<Edit className="h-4 w-4" />
<span className="sr-only">Edit</span>

View file

@ -0,0 +1,442 @@
"use client";
import { useState, useEffect } from "react";
import { useRouter, useParams } from "next/navigation";
import { motion } from "framer-motion";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import * as z from "zod";
import { toast } from "sonner";
import { ArrowLeft, Check, Info, Loader2, Github, CircleAlert, ListChecks, Edit, KeyRound } from "lucide-react";
import { useSearchSourceConnectors, SearchSourceConnector } from "@/hooks/useSearchSourceConnectors";
import {
Form,
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form";
import { Input } from "@/components/ui/input";
import { Button } from "@/components/ui/button";
import {
Card,
CardContent,
CardDescription,
CardFooter,
CardHeader,
CardTitle,
} from "@/components/ui/card";
import {
Alert,
AlertDescription,
AlertTitle,
} from "@/components/ui/alert";
import { Checkbox } from "@/components/ui/checkbox";
import { Skeleton } from "@/components/ui/skeleton";
// Schema for PAT input when editing repos
const githubPatSchema = z.object({
github_pat: z.string()
.min(20, { message: "GitHub Personal Access Token seems too short." })
.refine(pat => pat.startsWith('ghp_') || pat.startsWith('github_pat_'), {
message: "GitHub PAT should start with 'ghp_' or 'github_pat_'",
}),
});
type GithubPatFormValues = z.infer<typeof githubPatSchema>;
// Schema for main edit form (just the name for now)
const editConnectorSchema = z.object({
name: z.string().min(3, { message: "Connector name must be at least 3 characters." }),
});
type EditConnectorFormValues = z.infer<typeof editConnectorSchema>;
interface GithubRepo {
id: number;
name: string;
full_name: string;
private: boolean;
url: string;
description: string | null;
last_updated: string | null;
}
type EditMode = 'viewing' | 'editing_repos';
export default function EditGithubConnectorPage() {
const router = useRouter();
const params = useParams();
const searchSpaceId = params.search_space_id as string;
const connectorId = parseInt(params.connector_id as string, 10);
const { connectors, updateConnector, isLoading: connectorsLoading } = useSearchSourceConnectors();
const [connector, setConnector] = useState<SearchSourceConnector | null>(null);
const [currentSelectedRepos, setCurrentSelectedRepos] = useState<string[]>([]);
const [originalPat, setOriginalPat] = useState<string>(""); // State to hold the initial PAT
const [editMode, setEditMode] = useState<EditMode>('viewing');
const [fetchedRepos, setFetchedRepos] = useState<GithubRepo[] | null>(null); // Null indicates not fetched yet for edit
const [newSelectedRepos, setNewSelectedRepos] = useState<string[]>([]); // Tracks selections *during* edit
const [isFetchingRepos, setIsFetchingRepos] = useState(false);
const [isSaving, setIsSaving] = useState(false);
// Form for just the PAT input
const patForm = useForm<GithubPatFormValues>({
resolver: zodResolver(githubPatSchema),
defaultValues: { github_pat: "" }, // Default empty, will be reset
});
// Form for the main connector details (e.g., name)
const editForm = useForm<EditConnectorFormValues>({
resolver: zodResolver(editConnectorSchema),
defaultValues: { name: "" },
});
// Effect to find and set the current connector details on load
useEffect(() => {
if (!connectorsLoading && connectors.length > 0 && !connector) { // Added !connector check to prevent loop
const currentConnector = connectors.find(c => c.id === connectorId);
if (currentConnector && currentConnector.connector_type === 'GITHUB_CONNECTOR') {
setConnector(currentConnector);
const savedRepos = currentConnector.config?.repo_full_names || [];
const savedPat = currentConnector.config?.GITHUB_PAT || "";
setCurrentSelectedRepos(savedRepos);
setNewSelectedRepos(savedRepos);
setOriginalPat(savedPat); // Store the original PAT
editForm.reset({ name: currentConnector.name });
patForm.reset({ github_pat: savedPat }); // Also reset PAT form initially
} else if (currentConnector) {
toast.error("This connector is not a GitHub connector.");
router.push(`/dashboard/${searchSpaceId}/connectors`);
} else {
toast.error("Connector not found.");
router.push(`/dashboard/${searchSpaceId}/connectors`);
}
}
}, [connectorId, connectors, connectorsLoading, router, searchSpaceId, connector]); // Removed editForm, patForm from dependencies
// Fetch repositories using the entered PAT
const handleFetchRepositories = async (values: GithubPatFormValues) => {
setIsFetchingRepos(true);
setFetchedRepos(null);
// No need for patInputValue state, values.github_pat has the submitted value
try {
const token = localStorage.getItem('surfsense_bearer_token');
if (!token) throw new Error('No authentication token found');
const response = await fetch(
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/github/repositories/`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify({ github_pat: values.github_pat })
}
);
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.detail || `Failed to fetch repositories: ${response.statusText}`);
}
const data: GithubRepo[] = await response.json();
setFetchedRepos(data);
// Reset selection based on currently SAVED repos when fetching
setNewSelectedRepos(currentSelectedRepos);
toast.success(`Found ${data.length} repositories. Select which ones to index.`);
} catch (error) {
console.error("Error fetching GitHub repositories:", error);
toast.error(error instanceof Error ? error.message : "Failed to fetch repositories.");
// Don't clear PAT form on error, let user fix it
} finally {
setIsFetchingRepos(false);
}
};
// Handle checkbox changes during editing
const handleRepoSelectionChange = (repoFullName: string, checked: boolean) => {
setNewSelectedRepos(prev =>
checked
? [...prev, repoFullName]
: prev.filter(name => name !== repoFullName)
);
};
// Save all changes (name and potentially repo selection + PAT)
const handleSaveChanges = async (formData: EditConnectorFormValues) => {
if (!connector) return;
setIsSaving(true);
const updatePayload: Partial<SearchSourceConnector> = {};
let configChanged = false;
// 1. Check if name changed
if (formData.name !== connector.name) {
updatePayload.name = formData.name;
}
// 2. Check PAT and Repo changes
const currentPatInForm = patForm.getValues('github_pat');
let patChanged = false;
// Check if PAT input field was actually edited
if (editMode === 'editing_repos' && currentPatInForm !== originalPat) {
patChanged = true;
}
// Check if repo selection was modified
const initialRepoSet = new Set(currentSelectedRepos);
const newRepoSet = new Set(newSelectedRepos);
const reposChanged = initialRepoSet.size !== newRepoSet.size || ![...initialRepoSet].every(repo => newRepoSet.has(repo));
// If PAT was changed OR repos were changed (implying PAT was involved)
if (patChanged || (editMode === 'editing_repos' && reposChanged && fetchedRepos !== null)) {
// Validate the PAT from the form before including it
if (!currentPatInForm || !(currentPatInForm.startsWith('ghp_') || currentPatInForm.startsWith('github_pat_'))) {
toast.error("Invalid GitHub PAT format in the input field. Cannot save config changes.");
setIsSaving(false);
return;
}
updatePayload.config = {
// Use the PAT value currently in the form field
GITHUB_PAT: currentPatInForm,
// Use the latest repo selection state
repo_full_names: newSelectedRepos,
};
configChanged = true; // Mark config as changed
if (reposChanged && newSelectedRepos.length === 0) {
toast.warning("Warning: You haven't selected any repositories. The connector won't index anything.");
}
}
// 3. Check if there are actual changes to save
if (Object.keys(updatePayload).length === 0) {
toast.info("No changes detected.");
setIsSaving(false);
setEditMode('viewing');
// Reset PAT form to original value if returning to view mode without saving PAT change
patForm.reset({ github_pat: originalPat });
return;
}
// 4. Proceed with update API call
try {
await updateConnector(connectorId, updatePayload);
toast.success("Connector updated successfully!");
// Update local state based on what was *actually* saved
if (updatePayload.config) {
setCurrentSelectedRepos(updatePayload.config.repo_full_names || []);
setOriginalPat(updatePayload.config.GITHUB_PAT || "");
// Reset PAT form with the newly saved PAT
patForm.reset({ github_pat: updatePayload.config.GITHUB_PAT || "" });
} else {
// If config wasn't in payload, ensure PAT form is reset to original value
patForm.reset({ github_pat: originalPat });
}
// Update connector name state if it changed (or rely on hook refresh)
if (updatePayload.name) {
setConnector(prev => prev ? { ...prev, name: updatePayload.name! } : null);
}
// Reset edit state
setEditMode('viewing');
setFetchedRepos(null);
// Reset working selection to match saved state (use the updated currentSelectedRepos)
setNewSelectedRepos(updatePayload.config?.repo_full_names || currentSelectedRepos);
// Optionally redirect or rely on hook refresh
// router.push(`/dashboard/${searchSpaceId}/connectors`);
} catch (error) {
console.error("Error updating connector:", error);
toast.error(error instanceof Error ? error.message : "Failed to update connector.");
} finally {
setIsSaving(false);
}
};
if (connectorsLoading || !connector) {
return (
<div className="container mx-auto py-8 max-w-3xl">
<Skeleton className="h-8 w-48 mb-6" />
<Card className="border-2 border-border">
<CardHeader>
<Skeleton className="h-7 w-3/4 mb-2" />
<Skeleton className="h-4 w-full" />
</CardHeader>
<CardContent className="space-y-4">
<Skeleton className="h-10 w-full" />
<Skeleton className="h-20 w-full" />
</CardContent>
</Card>
</div>
);
}
return (
<div className="container mx-auto py-8 max-w-3xl">
<Button
variant="ghost"
className="mb-6"
onClick={() => router.push(`/dashboard/${searchSpaceId}/connectors`)}
>
<ArrowLeft className="mr-2 h-4 w-4" />
Back to Connectors
</Button>
<motion.div
initial={{ opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.5 }}
>
<Card className="border-2 border-border">
<CardHeader>
<CardTitle className="text-2xl font-bold flex items-center gap-2"><Github className="h-6 w-6" /> Edit GitHub Connector</CardTitle>
<CardDescription>
Modify the connector name and repository selections. To change repository selections, you need to re-enter your PAT.
</CardDescription>
</CardHeader>
{/* Use editForm for the main form structure */}
<Form {...editForm}>
<form onSubmit={editForm.handleSubmit(handleSaveChanges)} className="space-y-6">
<CardContent className="space-y-6">
{/* Connector Name Field */}
<FormField
control={editForm.control}
name="name"
render={({ field }) => (
<FormItem>
<FormLabel>Connector Name</FormLabel>
<FormControl>
<Input {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<hr />
{/* Repository Selection Section */}
<div className="space-y-4">
<h3 className="text-lg font-semibold flex items-center gap-2"><ListChecks className="h-5 w-5" /> Repository Selection</h3>
{editMode === 'viewing' && (
<div className="space-y-3 p-4 border rounded-md bg-muted/50">
<FormLabel>Currently Indexed Repositories:</FormLabel>
{currentSelectedRepos.length > 0 ? (
<ul className="list-disc pl-5 text-sm">
{currentSelectedRepos.map(repo => <li key={repo}>{repo}</li>)}
</ul>
) : (
<p className="text-sm text-muted-foreground">(No repositories currently selected for indexing)</p>
)}
<Button type="button" variant="outline" size="sm" onClick={() => setEditMode('editing_repos')}>
<Edit className="mr-2 h-4 w-4" />
Change Selection
</Button>
<FormDescription>Click "Change Selection" to re-enter your PAT and update the list.</FormDescription>
</div>
)}
{editMode === 'editing_repos' && (
<div className="space-y-4 p-4 border rounded-md">
{/* PAT Input Section (No nested Form provider) */}
{/* We still use patForm fields but trigger validation manually */}
<div className="flex items-end gap-4 p-4 border rounded-md bg-muted/90">
<FormField
// Associate with patForm instance for control/state
control={patForm.control}
name="github_pat"
render={({ field }) => (
<FormItem className="flex-grow">
<FormLabel className="flex items-center gap-1"><KeyRound className="h-4 w-4" /> Re-enter PAT to Fetch Repos</FormLabel>
<FormControl>
<Input type="password" placeholder="ghp_... or github_pat_..." {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<Button
type="button" // Changed from submit to button
disabled={isFetchingRepos}
size="sm"
onClick={async () => { // Added async onClick handler
const isValid = await patForm.trigger('github_pat'); // Trigger validation
if (isValid) {
handleFetchRepositories(patForm.getValues()); // Call fetch if valid
}
}}
>
{isFetchingRepos ? <Loader2 className="h-4 w-4 animate-spin" /> : "Fetch"}
</Button>
</div>
{/* Fetched Repository List (shown after fetch) */}
{isFetchingRepos && <Skeleton className="h-40 w-full" />}
{!isFetchingRepos && fetchedRepos !== null && (
fetchedRepos.length === 0 ? (
<Alert variant="destructive">
<CircleAlert className="h-4 w-4" />
<AlertTitle>No Repositories Found</AlertTitle>
<AlertDescription>Check the PAT and permissions.</AlertDescription>
</Alert>
) : (
<div className="space-y-2">
<FormLabel>Select Repositories to Index ({newSelectedRepos.length} selected):</FormLabel>
<div className="h-64 w-full rounded-md border p-4 overflow-y-auto">
{fetchedRepos.map((repo) => (
<div key={repo.id} className="flex items-center space-x-2 mb-2 py-1">
<Checkbox
id={`repo-${repo.id}`}
checked={newSelectedRepos.includes(repo.full_name)}
onCheckedChange={(checked) => handleRepoSelectionChange(repo.full_name, !!checked)}
/>
<label
htmlFor={`repo-${repo.id}`}
className="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
>
{repo.full_name} {repo.private && "(Private)"}
</label>
</div>
))}
</div>
</div>
)
)}
<Button type="button" variant="ghost" size="sm" onClick={() => {
setEditMode('viewing');
setFetchedRepos(null);
setNewSelectedRepos(currentSelectedRepos);
patForm.reset({ github_pat: originalPat }); // Reset PAT form on cancel
}}>
Cancel Repo Change
</Button>
</div>
)}
</div>
</CardContent>
<CardFooter className="border-t pt-6">
<Button type="submit" disabled={isSaving} className="w-full sm:w-auto">
{isSaving ? <Loader2 className="mr-2 h-4 w-4 animate-spin" /> : <Check className="mr-2 h-4 w-4" />}
Save Changes
</Button>
</CardFooter>
</form>
</Form>
</Card>
</motion.div>
</div>
);
}

View file

@ -70,7 +70,7 @@ export default function EditConnectorPage() {
const [connector, setConnector] = useState<SearchSourceConnector | null>(null);
const [isLoading, setIsLoading] = useState(true);
const [isSubmitting, setIsSubmitting] = useState(false);
console.log("connector", connector);
// Initialize the form
const form = useForm<ApiConnectorFormValues>({
resolver: zodResolver(apiConnectorFormSchema),