Repository navigation
Fix/GitHub integration hanging and jit fetch #98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,9 +44,10 @@ type EncryptedData = EncryptedDataV1 | EncryptedDataV2 | EncryptedDataV3; | |
|
|
||
| // Encrypt content for a specific recipient | ||
| export async function encryptContent( | ||
| content: string, | ||
| recipientName: string | null = null, | ||
| usePgp: boolean = false | ||
| content: string, | ||
| recipientName: string | null = null, | ||
| usePgp: boolean = false, | ||
| refreshGithubKeys: boolean = false | ||
| ): Promise<Buffer> { | ||
| try { | ||
| let publicKey: string; | ||
|
|
@@ -60,16 +61,38 @@ export async function encryptContent( | |
| // If not found and it starts with "github:", try to fetch it just-in-time | ||
| if (!friendKey && recipientName.startsWith('github:')) { | ||
| const username = recipientName.replace('github:', ''); | ||
| console.log(`GitHub key not found locally, fetching from GitHub...`); | ||
|
|
||
| try { | ||
| const { ensureGitHubKey } = await import('./githubUtils.js'); | ||
| await ensureGitHubKey(username, false); | ||
| await ensureGitHubKey(username, false, refreshGithubKeys); | ||
|
|
||
| // Try to get the key again after fetching | ||
| friendKey = await getKey('any', recipientName); | ||
| } catch (error: any) { | ||
| throw new Error(`Failed to fetch GitHub key for ${username}: ${error.message}`); | ||
| console.error(`\nError: Failed to fetch GitHub key for user "${username}"`); | ||
| console.error(`Details: ${error.message}`); | ||
| console.error(`\nPlease verify:`); | ||
| console.error(` 1. The GitHub username is correct`); | ||
| console.error(` 2. The user has a GPG key uploaded to GitHub`); | ||
| console.error(` 3. You have internet connectivity`); | ||
| console.error(`\nTo manually add a GitHub user's key, run:`); | ||
| console.error(` dedpaste keys --github ${username}`); | ||
| throw new Error(`Failed to fetch GitHub key for ${username}`); | ||
| } | ||
| } | ||
|
|
||
| // Also check if we should refresh an existing GitHub key | ||
| if (friendKey && recipientName.startsWith('github:') && refreshGithubKeys) { | ||
|
||
| const username = recipientName.replace('github:', ''); | ||
|
|
||
| try { | ||
| const { ensureGitHubKey } = await import('./githubUtils.js'); | ||
| await ensureGitHubKey(username, false, true); | ||
|
|
||
| // Reload the key after refreshing | ||
| friendKey = await getKey('any', recipientName); | ||
| } catch (error: any) { | ||
| console.warn(`Warning: Failed to refresh GitHub key, using cached version`); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -155,15 +155,9 @@ export async function addGitHubKey( | |
| } | ||
|
|
||
| // Fetch GPG key | ||
| if (!silent) { | ||
| console.log(`Fetching GPG key for GitHub user: ${sanitizedUsername}...`); | ||
| } | ||
| const gpgKeyData = await fetchGitHubGpgKey(sanitizedUsername); | ||
|
|
||
| // Import and validate PGP key | ||
| if (!silent) { | ||
| console.log(`Importing PGP key...`); | ||
| } | ||
| const keyInfo = await importPgpKey(gpgKeyData); | ||
|
|
||
| // Generate key name | ||
|
|
@@ -196,35 +190,38 @@ export async function addGitHubKey( | |
|
|
||
| /** | ||
| * Fetch GitHub key just-in-time (used during encryption) | ||
| * Checks if key exists in database, fetches if not | ||
| * Checks if key exists in database, fetches if not or if stale | ||
| * @param username GitHub username (without github: prefix) | ||
| * @param silent Whether to suppress console output | ||
| * @param forceRefresh Whether to force refresh even if cached | ||
| * @returns Key name and metadata | ||
| */ | ||
| export async function ensureGitHubKey( | ||
| username: string, | ||
| silent: boolean = false | ||
| silent: boolean = false, | ||
| forceRefresh: boolean = false | ||
| ): Promise<{ name: string; fingerprint: string; email?: string }> { | ||
| const keyName = `github:${username}`; | ||
|
|
||
| // Check if key already exists | ||
| const { getKey } = await import('./keyManager.js'); | ||
| const existingKey = await getKey('github', keyName); | ||
|
|
||
| if (existingKey) { | ||
| if (!silent) { | ||
| console.log(`Using cached GitHub key: ${keyName}`); | ||
| } | ||
| return { | ||
| name: keyName, | ||
| fingerprint: existingKey.fingerprint, | ||
| email: existingKey.email | ||
| }; | ||
| } | ||
| if (existingKey && !forceRefresh) { | ||
| // Check if key is stale (older than 24 hours) | ||
| const lastFetched = existingKey.lastFetched ? new Date(existingKey.lastFetched) : null; | ||
| const now = new Date(); | ||
| const maxAge = 24 * 60 * 60 * 1000; // 24 hours in milliseconds | ||
|
||
|
|
||
| // Key doesn't exist, fetch it | ||
| if (!silent) { | ||
| console.log(`GitHub key not found locally, fetching from GitHub...`); | ||
| const isStale = lastFetched ? (now.getTime() - lastFetched.getTime() > maxAge) : true; | ||
|
|
||
| if (!isStale) { | ||
| return { | ||
| name: keyName, | ||
| fingerprint: existingKey.fingerprint, | ||
| email: existingKey.email | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| return await addGitHubKey(username, undefined, false, silent); | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This error messaging logic is duplicated from the existing error handling patterns. Consider extracting this into a shared error formatting utility function to maintain consistency and reduce duplication.