diff --git a/src/components/git/PullRequestDialog.tsx b/src/components/git/PullRequestDialog.tsx index 27b273bc..7e2a2d64 100644 --- a/src/components/git/PullRequestDialog.tsx +++ b/src/components/git/PullRequestDialog.tsx @@ -273,11 +273,73 @@ export function PullRequestDialog({ ); } - // If fork is needed, ensure we have it set up + // If fork is needed, we need to push the branch to the fork first let headRef = currentBranch; - if (forkInfo.needsFork && forkInfo.forkOwner) { + if (forkInfo.needsFork && forkInfo.forkOwner && forkInfo.forkUrl) { // For cross-fork PRs, head ref format is "owner:branch" headRef = `${forkInfo.forkOwner}:${currentBranch}`; + + console.log(`Setting up fork workflow for ${forkInfo.forkOwner}/${forkInfo.forkRepo}`); + + // Check current remotes + const remotes = await git.listRemotes({ dir: projectPath }); + console.log('Current remotes:', remotes); + + const originRemote = remotes.find(r => r.remote === 'origin'); + const forkRemote = remotes.find(r => r.url === forkInfo.forkUrl); + + // Determine which remote to use for pushing + let pushRemote = 'origin'; + + if (originRemote?.url !== forkInfo.forkUrl) { + // Origin doesn't point to the fork - need to set it up + console.log('Origin does not point to fork, checking for fork remote'); + + if (forkRemote) { + pushRemote = forkRemote.remote; + console.log(`Using existing remote '${pushRemote}' for fork`); + } else { + // Add fork as a new remote + console.log('Adding fork as new remote: fork'); + try { + await git.addRemote({ + dir: projectPath, + remote: 'fork', + url: forkInfo.forkUrl, + }); + pushRemote = 'fork'; + } catch (addRemoteError) { + console.error('Failed to add fork remote:', addRemoteError); + // Continue anyway, might already exist + pushRemote = 'fork'; + } + } + } + + // Push the current branch to the fork + console.log(`Pushing branch ${currentBranch} to ${pushRemote} (${forkInfo.forkUrl})`); + + try { + await git.push({ + dir: projectPath, + remote: pushRemote, + ref: currentBranch, + onAuth: () => credentials, + }); + console.log('Successfully pushed to fork'); + } catch (pushError) { + console.error('Failed to push to fork:', pushError); + const errorMsg = pushError instanceof Error ? pushError.message : 'Unknown error'; + throw new Error( + `Failed to push branch '${currentBranch}' to your fork at ${forkInfo.forkUrl}.\n\n` + + `This usually means:\n` + + `1. The branch hasn't been committed yet\n` + + `2. Network/authentication issues\n` + + `3. The fork URL is incorrect\n\n` + + `Error details: ${errorMsg}\n\n` + + `Try pushing manually first, then create the PR.` + ); + } } // Create PR based on platform @@ -465,7 +527,23 @@ export function PullRequestDialog({ error = { message: errorText || 'Unknown error' }; } - throw new Error(error.message || `GitHub API error: ${response.status} - ${response.statusText}`); + // GitHub validation errors often include detailed field-specific messages + let errorMessage = error.message || `GitHub API error: ${response.status} - ${response.statusText}`; + + if (error.errors && Array.isArray(error.errors)) { + const errorDetails = error.errors.map((e: any) => { + if (e.message) return e.message; + if (e.field && e.code) return `${e.field}: ${e.code}`; + return JSON.stringify(e); + }).join('; '); + errorMessage = `${errorMessage}\nDetails: ${errorDetails}`; + } + + if (error.documentation_url) { + errorMessage = `${errorMessage}\nSee: ${error.documentation_url}`; + } + + throw new Error(errorMessage); } const data = await response.json(); @@ -642,7 +720,7 @@ export function PullRequestDialog({ {error && ( - {error} + {error} )} diff --git a/src/components/git/PullRequestForm.tsx b/src/components/git/PullRequestForm.tsx index 10197655..97e8b0d2 100644 --- a/src/components/git/PullRequestForm.tsx +++ b/src/components/git/PullRequestForm.tsx @@ -185,11 +185,55 @@ export function PullRequestForm({ projectId, currentBranch, remoteUrl }: PullReq throw new Error('No credentials found. Please add credentials in Settings > Git'); } - // Create PR (simplified - full implementation in PullRequestDialog) - const headRef = forkInfo.needsFork && forkInfo.forkOwner + // If fork is needed, push the branch first + const headRef = forkInfo.needsFork && forkInfo.forkOwner ? `${forkInfo.forkOwner}:${currentBranch}` : currentBranch; + if (forkInfo.needsFork && forkInfo.forkUrl) { + console.log(`Pushing branch ${currentBranch} to fork ${forkInfo.forkUrl}`); + + // Check current remotes + const remotes = await git.listRemotes({ dir: projectPath }); + const originRemote = remotes.find(r => r.remote === 'origin'); + const forkRemote = remotes.find(r => r.url === forkInfo.forkUrl); + + let pushRemote = 'origin'; + + if (originRemote?.url !== forkInfo.forkUrl) { + if (forkRemote) { + pushRemote = forkRemote.remote; + } else { + await git.addRemote({ + dir: projectPath, + remote: 'fork', + url: forkInfo.forkUrl, + }); + pushRemote = 'fork'; + } + } + + try { + await git.push({ + dir: projectPath, + remote: pushRemote, + ref: currentBranch, + onAuth: () => credentials, + }); + console.log('Successfully pushed to fork'); + } catch (pushError) { + const errorMsg = pushError instanceof Error ? pushError.message : 'Unknown error'; + throw new Error( + `Failed to push branch '${currentBranch}' to your fork.\n\n` + + `This usually means:\n` + + `1. The branch hasn't been committed yet\n` + + `2. Network/authentication issues\n\n` + + `Error: ${errorMsg}\n\n` + + `Try committing and pushing manually first.` + ); + } + } + const url = `${remoteInfo.apiUrl}/repos/${remoteInfo.owner}/${remoteInfo.repo}/pulls`; const response = await fetch(url, { method: 'POST', @@ -208,7 +252,32 @@ export function PullRequestForm({ projectId, currentBranch, remoteUrl }: PullReq if (!response.ok) { const errorText = await response.text(); - throw new Error(errorText || 'Failed to create PR'); + console.error('PR creation failed:', errorText); + + let error; + try { + error = JSON.parse(errorText); + } catch { + error = { message: errorText || 'Unknown error' }; + } + + // Extract detailed error messages + let errorMessage = error.message || `API error: ${response.status} - ${response.statusText}`; + + if (error.errors && Array.isArray(error.errors)) { + const errorDetails = error.errors.map((e: any) => { + if (e.message) return e.message; + if (e.field && e.code) return `${e.field}: ${e.code}`; + return JSON.stringify(e); + }).join('; '); + errorMessage = `${errorMessage}\n\nDetails: ${errorDetails}`; + } + + if (error.documentation_url) { + errorMessage = `${errorMessage}\n\nSee: ${error.documentation_url}`; + } + + throw new Error(errorMessage); } const data = await response.json(); @@ -311,7 +380,7 @@ export function PullRequestForm({ projectId, currentBranch, remoteUrl }: PullReq {error && ( - {error} + {error} )}