From f43ce5cb5bd2b8b5f50acd54c4d322ca19e5aefa Mon Sep 17 00:00:00 2001 From: "shakespeare.diy" Date: Tue, 14 Oct 2025 15:15:17 -0500 Subject: [PATCH] Fix GitLab PR creation 401 error - use correct credential field and auth header The PullRequestDialog was trying to access `credentials.token` which doesn't exist in the GitCredential interface. The actual token is stored in the `password` field. Additionally, GitLab's API requires the `PRIVATE-TOKEN` header for Personal Access Tokens, not `Authorization: Bearer`. Updated both the GitHub and GitLab API calls to use the correct credential field and proper authentication headers. Changes: - Changed credentials.token to credentials.password for both GitHub and GitLab - Updated GitLab API calls to use PRIVATE-TOKEN header instead of Authorization Bearer - Added comprehensive logging to help debug credential issues - Added validation to check if token is empty and provide helpful error message This fixes the "401 Unauthorized" error when creating merge requests on GitLab. --- src/components/git/PullRequestDialog.tsx | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/components/git/PullRequestDialog.tsx b/src/components/git/PullRequestDialog.tsx index 23a8ab4f..716e0da0 100644 --- a/src/components/git/PullRequestDialog.tsx +++ b/src/components/git/PullRequestDialog.tsx @@ -209,6 +209,12 @@ export function PullRequestDialog({ // 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( @@ -216,13 +222,19 @@ export function PullRequestDialog({ ); } + 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.token); + const prUrl = await createGitHubPR(remoteInfo, credentials.password); setCreatedPrUrl(prUrl); } else if (remoteInfo.platform === 'gitlab') { - const prUrl = await createGitLabMR(remoteInfo, credentials.token); + const prUrl = await createGitLabMR(remoteInfo, credentials.password); setCreatedPrUrl(prUrl); } @@ -288,10 +300,11 @@ export function PullRequestDialog({ // 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: { - 'Authorization': `Bearer ${token}`, + 'PRIVATE-TOKEN': token, 'Content-Type': 'application/json', }, }); @@ -315,7 +328,7 @@ export function PullRequestDialog({ const response = await fetch(mrUrl, { method: 'POST', headers: { - 'Authorization': `Bearer ${token}`, + 'PRIVATE-TOKEN': token, 'Content-Type': 'application/json', }, body: JSON.stringify({