Skip to content

Use exact diagnostic endpoint approach for JWT token creation - #133

Merged
github-actions[bot] merged 1 commit into
mainfrom
fix/use-diagnostic-approach-for-jwt
Oct 13, 2025
Merged

github-actions[bot] merged 1 commit into
mainfrom
fix/use-diagnostic-approach-for-jwt

Conversation

@ishaileshpant

Copy link
Copy Markdown
Collaborator

🎯 Fix JWT Token Creation Using Diagnostic Approach

Problem

The comprehensive debugging revealed that JWT token creation fails during the 'listInstallations' call with 'A JSON web token could not be decoded' error. The diagnostic endpoint shows JWT creation as successful, indicating a discrepancy in approaches.

Root Cause

The bot API was using a more complex private key processing approach, while the diagnostic endpoint uses a simpler approach that works. The complex approach was causing JWT token creation to fail.

Solution

  • Use diagnostic endpoint approach: Replace complex private key processing with simple diagnostic approach
  • Simplified JWT creation: Use exact same createAppAuth call as diagnostic endpoint
  • Consistent private key handling: Handle escaped newlines the same way as diagnostic
  • Removed complex fallbacks: Eliminate alternative approaches that were causing issues

Technical Changes

Simplified Private Key Processing

// Before: Complex private key processing with reconstruction
// After: Simple diagnostic approach
let privateKey = process.env.GITHUB_APP_PRIVATE_KEY;
if (privateKey.includes('\\n')) {
    privateKey = privateKey.replace(/\\n/g, '\n');
}

Simplified JWT Token Creation

// Before: Complex createAppAuth with fallbacks
// After: Simple diagnostic approach
const { createAppAuth: createAppAuthDiag } = await import('@octokit/auth-app');
const appAuth = createAppAuthDiag({
    appId: process.env.GITHUB_APP_ID,
    privateKey: privateKey,
});
const appToken = await appAuth({ type: 'app' });

Removed Complex Fallbacks

  • Removed alternative private key format attempts
  • Removed complex error handling that was causing issues
  • Simplified to use only the working diagnostic approach

Files Modified

  • api/github/create-content-pr.js - Simplified JWT token creation to match diagnostic endpoint

Impact

  • 🎯 Uses the exact same approach as the working diagnostic endpoint
  • 🔧 Eliminates complex private key processing that was causing failures
  • 📊 Simplifies JWT token creation to proven working method
  • 🛠️ Removes fallback mechanisms that were introducing errors
  • Should resolve the 'A JSON web token could not be decoded' error

Testing

  • Simplified private key processing to match diagnostic endpoint
  • Simplified JWT token creation to match diagnostic endpoint
  • Removed complex fallback mechanisms
  • No linting errors

Expected Results

The bot API should now work because:

  1. Uses exact same approach as the working diagnostic endpoint
  2. Eliminates complex processing that was causing JWT failures
  3. Simplified JWT creation matches proven working method
  4. Should resolve the 'listInstallations' JWT token error

Next Steps

  1. Test content submission to verify JWT token creation now works
  2. Monitor logs to confirm the diagnostic approach succeeds
  3. Verify bot-only content submission works without fallbacks

## 🎯 Fix JWT Token Creation Using Diagnostic Approach

### Problem
The comprehensive debugging revealed that JWT token creation fails during the 'listInstallations' call with 'A JSON web token could not be decoded' error. The diagnostic endpoint shows JWT creation as successful, indicating a discrepancy in approaches.

### Root Cause
The bot API was using a more complex private key processing approach, while the diagnostic endpoint uses a simpler approach that works. The complex approach was causing JWT token creation to fail.

### Solution
- ✅ **Use diagnostic endpoint approach**: Replace complex private key processing with simple diagnostic approach
- ✅ **Simplified JWT creation**: Use exact same createAppAuth call as diagnostic endpoint
- ✅ **Consistent private key handling**: Handle escaped newlines the same way as diagnostic
- ✅ **Removed complex fallbacks**: Eliminate alternative approaches that were causing issues

### Technical Changes

#### **Simplified Private Key Processing**
```javascript
// Before: Complex private key processing with reconstruction
// After: Simple diagnostic approach
let privateKey = process.env.GITHUB_APP_PRIVATE_KEY;
if (privateKey.includes('\\n')) {
    privateKey = privateKey.replace(/\\n/g, '\n');
}
```

#### **Simplified JWT Token Creation**
```javascript
// Before: Complex createAppAuth with fallbacks
// After: Simple diagnostic approach
const { createAppAuth: createAppAuthDiag } = await import('@octokit/auth-app');
const appAuth = createAppAuthDiag({
    appId: process.env.GITHUB_APP_ID,
    privateKey: privateKey,
});
const appToken = await appAuth({ type: 'app' });
```

#### **Removed Complex Fallbacks**
- Removed alternative private key format attempts
- Removed complex error handling that was causing issues
- Simplified to use only the working diagnostic approach

### Files Modified
- `api/github/create-content-pr.js` - Simplified JWT token creation to match diagnostic endpoint

### Impact
- 🎯 **Uses** the exact same approach as the working diagnostic endpoint
- 🔧 **Eliminates** complex private key processing that was causing failures
- 📊 **Simplifies** JWT token creation to proven working method
- 🛠️ **Removes** fallback mechanisms that were introducing errors
- ✅ **Should resolve** the 'A JSON web token could not be decoded' error

### Testing
- [x] Simplified private key processing to match diagnostic endpoint
- [x] Simplified JWT token creation to match diagnostic endpoint
- [x] Removed complex fallback mechanisms
- [x] No linting errors

### Expected Results
The bot API should now work because:
1. **Uses exact same approach** as the working diagnostic endpoint
2. **Eliminates complex processing** that was causing JWT failures
3. **Simplified JWT creation** matches proven working method
4. **Should resolve** the 'listInstallations' JWT token error
@vercel

vercel Bot commented Oct 13, 2025

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
prepguides-dev Ready Ready Preview Comment Oct 13, 2025 3:56pm

@ishaileshpant

Copy link
Copy Markdown
Collaborator Author

/approve

@github-actions
github-actions Bot merged commit 54898f7 into main Oct 13, 2025
11 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

🚀 Admin-Approved and Auto-Merged!

This PR has been automatically merged to main after admin approval by @ishaileshpant.

✅ **Status**: Merged and deployed
🔗 **Commit**: `54898f7f20ace71938cc2d2a767f4ccb0ed6e188`
📅 **Merged at**: 2025-10-13T15:56:27.198Z
👤 **Approved by**: Admin/Maintainer/Owner

Thank you for your contribution! 🎉

@ishaileshpant
ishaileshpant deleted the fix/use-diagnostic-approach-for-jwt branch October 13, 2025 18:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant