import { useState, useEffect } from 'react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Textarea } from '@/components/ui/textarea'; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger, DialogFooter, } from '@/components/ui/dialog'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select'; import { Alert, AlertDescription } from '@/components/ui/alert'; import { ScrollArea } from '@/components/ui/scroll-area'; import { Badge } from '@/components/ui/badge'; import { GitPullRequest, Loader2, AlertCircle, CheckCircle, ExternalLink, GitCompare, } from 'lucide-react'; import { useGit } from '@/hooks/useGit'; import { useGitSettings } from '@/hooks/useGitSettings'; import { useToast } from '@/hooks/useToast'; import { cn } from '@/lib/utils'; import { findCredentialsForRepo } from '@/lib/gitCredentials'; interface PullRequestDialogProps { projectId: string; currentBranch: string | null; remoteUrl?: string; open?: boolean; onOpenChange?: (open: boolean) => void; } interface RemoteInfo { owner: string; repo: string; platform: 'github' | 'gitlab'; apiUrl: string; } export function PullRequestDialog({ projectId, currentBranch, remoteUrl, open, onOpenChange, }: PullRequestDialogProps) { const [isOpen, setIsOpen] = useState(open ?? false); const [title, setTitle] = useState(''); const [description, setDescription] = useState(''); const [targetBranch, setTargetBranch] = useState('main'); const [remoteBranches, setRemoteBranches] = useState([]); const [isLoadingBranches, setIsLoadingBranches] = useState(false); const [isCreating, setIsCreating] = useState(false); const [remoteInfo, setRemoteInfo] = useState(null); const [createdPrUrl, setCreatedPrUrl] = useState(null); const [error, setError] = useState(null); const { git } = useGit(); const { settings } = useGitSettings(); const { toast } = useToast(); const projectPath = `/projects/${projectId}`; useEffect(() => { if (open !== undefined) { setIsOpen(open); } }, [open]); useEffect(() => { if (isOpen && remoteUrl) { parseRemoteUrl(remoteUrl); loadRemoteBranches(); } }, [isOpen, remoteUrl]); const parseRemoteUrl = (url: string) => { setError(null); try { // Parse GitHub/GitLab URLs let match; let owner = ''; let repo = ''; let platform: 'github' | 'gitlab' = 'github'; let apiUrl = ''; console.log('Parsing remote URL:', url); // Normalize URL - remove .git suffix if present const normalizedUrl = url.replace(/\.git$/, ''); // GitHub SSH: git@github.com:owner/repo match = normalizedUrl.match(/git@github\.com:([^/]+)\/(.+)$/); if (match) { owner = match[1]; repo = match[2]; platform = 'github'; apiUrl = 'https://api.github.com'; console.log('Detected GitHub SSH:', { owner, repo }); } // GitHub HTTPS: https://github.com/owner/repo if (!match) { match = normalizedUrl.match(/https?:\/\/github\.com\/([^/]+)\/(.+)$/); if (match) { owner = match[1]; repo = match[2]; platform = 'github'; apiUrl = 'https://api.github.com'; console.log('Detected GitHub HTTPS:', { owner, repo }); } } // GitLab SSH: git@gitlab.com:owner/repo if (!match) { match = normalizedUrl.match(/git@gitlab\.com:([^/]+)\/(.+)$/); if (match) { owner = match[1]; repo = match[2]; platform = 'gitlab'; apiUrl = 'https://gitlab.com/api/v4'; console.log('Detected GitLab SSH:', { owner, repo }); } } // GitLab HTTPS: https://gitlab.com/owner/repo if (!match) { match = normalizedUrl.match(/https?:\/\/gitlab\.com\/([^/]+)\/(.+)$/); if (match) { owner = match[1]; repo = match[2]; platform = 'gitlab'; apiUrl = 'https://gitlab.com/api/v4'; console.log('Detected GitLab HTTPS:', { owner, repo }); } } if (owner && repo) { console.log('Remote info set:', { owner, repo, platform, apiUrl }); setRemoteInfo({ owner, repo, platform, apiUrl }); } else { console.error('Failed to parse URL - no owner/repo found:', url); setError('Unsupported remote URL format. Only GitHub and GitLab URLs are supported (e.g., https://github.com/user/repo.git)'); } } catch (err) { console.error('Error parsing remote URL:', err); setError('Failed to parse remote URL: ' + (err instanceof Error ? err.message : 'Unknown error')); } }; const loadRemoteBranches = async () => { setIsLoadingBranches(true); try { const branches = await git.listBranches({ dir: projectPath, remote: 'origin', }); setRemoteBranches(branches); // Set default target branch if (branches.includes('main')) { setTargetBranch('main'); } else if (branches.includes('master')) { setTargetBranch('master'); } else if (branches.length > 0) { setTargetBranch(branches[0]); } } catch (err) { console.warn('Failed to load remote branches:', err); // Set default branches if we can't fetch setRemoteBranches(['main', 'master', 'develop']); } finally { setIsLoadingBranches(false); } }; const createPullRequest = async () => { if (!remoteInfo || !currentBranch) return; if (!title.trim()) { toast({ title: 'Title required', description: 'Please enter a title for the pull request', variant: 'destructive', }); return; } setIsCreating(true); setError(null); try { console.log('Creating PR for:', remoteInfo); console.log('Remote URL:', remoteUrl); // Get credentials const credentials = findCredentialsForRepo(remoteUrl || '', settings.credentials); console.log('Found credentials:', credentials ? 'Yes' : 'No'); console.log('Available credential origins:', Object.keys(settings.credentials)); if (credentials) { console.log('Credential username:', credentials.username); console.log('Credential password length:', credentials.password?.length || 0); } if (!credentials) { throw new Error( `No credentials found for ${remoteInfo.platform === 'github' ? 'GitHub' : 'GitLab'}. Please add credentials in Settings > Git for ${remoteUrl}` ); } if (!credentials.password || credentials.password.trim() === '') { throw new Error( `Credentials found but token is empty. Please check your ${remoteInfo.platform === 'github' ? 'GitHub' : 'GitLab'} token in Settings > Git` ); } // Create PR based on platform console.log(`Creating ${remoteInfo.platform} PR...`); if (remoteInfo.platform === 'github') { const prUrl = await createGitHubPR(remoteInfo, credentials.password); setCreatedPrUrl(prUrl); } else if (remoteInfo.platform === 'gitlab') { const prUrl = await createGitLabMR(remoteInfo, credentials.password); setCreatedPrUrl(prUrl); } toast({ title: 'Pull request created', description: 'Successfully created pull request', }); } catch (err) { const errorMessage = err instanceof Error ? err.message : 'Unknown error'; setError(errorMessage); toast({ title: 'Failed to create pull request', description: errorMessage, variant: 'destructive', }); } finally { setIsCreating(false); } }; const createGitHubPR = async (info: RemoteInfo, token: string): Promise => { const url = `${info.apiUrl}/repos/${info.owner}/${info.repo}/pulls`; console.log('GitHub PR URL:', url); console.log('GitHub PR data:', { title, body: description, head: currentBranch, base: targetBranch }); const response = await fetch(url, { method: 'POST', headers: { 'Authorization': `token ${token}`, 'Content-Type': 'application/json', 'Accept': 'application/vnd.github.v3+json', }, body: JSON.stringify({ title, body: description, head: currentBranch, base: targetBranch, }), }); console.log('GitHub response status:', response.status); if (!response.ok) { const errorText = await response.text(); console.error('GitHub API error response:', errorText); let error; try { error = JSON.parse(errorText); } catch { error = { message: errorText || 'Unknown error' }; } throw new Error(error.message || `GitHub API error: ${response.status} - ${response.statusText}`); } const data = await response.json(); console.log('GitHub PR created:', data.html_url); return data.html_url; }; const createGitLabMR = async (info: RemoteInfo, token: string): Promise => { // Get project ID first const projectUrl = `${info.apiUrl}/projects/${encodeURIComponent(`${info.owner}/${info.repo}`)}`; console.log('GitLab project URL:', projectUrl); console.log('Using token (first 10 chars):', token.substring(0, 10) + '...'); const projectResponse = await fetch(projectUrl, { headers: { 'PRIVATE-TOKEN': token, 'Content-Type': 'application/json', }, }); console.log('GitLab project response status:', projectResponse.status); if (!projectResponse.ok) { const errorText = await projectResponse.text(); console.error('GitLab project error response:', errorText); throw new Error(`Failed to get GitLab project: ${projectResponse.status} - ${errorText}`); } const project = await projectResponse.json(); console.log('GitLab project ID:', project.id); // Create merge request const mrUrl = `${info.apiUrl}/projects/${project.id}/merge_requests`; console.log('GitLab MR URL:', mrUrl); console.log('GitLab MR data:', { source_branch: currentBranch, target_branch: targetBranch, title, description }); const response = await fetch(mrUrl, { method: 'POST', headers: { 'PRIVATE-TOKEN': token, 'Content-Type': 'application/json', }, body: JSON.stringify({ source_branch: currentBranch, target_branch: targetBranch, title, description, }), }); console.log('GitLab MR response status:', response.status); if (!response.ok) { const errorText = await response.text(); console.error('GitLab MR error response:', errorText); let error; try { error = JSON.parse(errorText); } catch { error = { message: errorText || 'Unknown error' }; } throw new Error(error.message || `GitLab API error: ${response.status} - ${response.statusText}`); } const data = await response.json(); console.log('GitLab MR created:', data.web_url); return data.web_url; }; const handleOpenChange = (newOpen: boolean) => { setIsOpen(newOpen); onOpenChange?.(newOpen); // Reset form when closing if (!newOpen) { setTitle(''); setDescription(''); setCreatedPrUrl(null); setError(null); } }; if (!remoteUrl) { return null; } return ( Create Pull Request Create a pull request to merge your changes {createdPrUrl ? ( // Success state

Pull Request Created!

Your pull request has been created successfully

) : ( // Form
{/* Branch comparison */} {currentBranch && (
{targetBranch} {currentBranch}

Merge {currentBranch} into {targetBranch}

)} {/* Error alert */} {error && ( {error} )} {/* Target branch selection */}

The branch you want to merge your changes into

{/* Title */}
setTitle(e.target.value)} disabled={isCreating} />
{/* Description */}