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.
This commit is contained in:
shakespeare.diy committed 2025-10-14 15:15:17 -05:00
1 parent 7b5415a5f1
commit f43ce5cb5b
1 file changed
+17 -4
+17 -4
View File
@@ -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({