Skip to content

Commit ebd3e55

Browse files
Copilotalexr00
andcommitted
Address code review feedback: add error handling for getCurrentUser
Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
1 parent 1fd8776 commit ebd3e55

File tree

3 files changed

+19
-38
lines changed

3 files changed

+19
-38
lines changed

src/github/credentials.ts

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -455,39 +455,13 @@ export class CredentialStore extends Disposable {
455455
}
456456

457457
/**
458-
* Check if there are multiple GitHub accounts signed in to VS Code.
459-
* This helps detect if the user might be using the wrong account for a repository.
458+
* Check if the user is authenticated and might benefit from checking account preferences.
459+
* Note: Due to VS Code API limitations, we cannot directly check if multiple accounts are available.
460+
* This method returns true if the user is authenticated, as they may have multiple accounts
461+
* configured in VS Code that they can switch to via the "Manage Account Preferences" command.
460462
*/
461-
public async hasMultipleAccounts(authProviderId: AuthProvider): Promise<boolean> {
462-
try {
463-
// Try different scope combinations to find all possible sessions
464-
const scopesToCheck = [SCOPES_WITH_ADDITIONAL, SCOPES_OLD, SCOPES_OLDEST];
465-
const foundSessions = new Set<string>();
466-
467-
for (const scopes of scopesToCheck) {
468-
try {
469-
const session = await vscode.authentication.getSession(authProviderId, scopes, { silent: true });
470-
if (session) {
471-
foundSessions.add(session.account.id);
472-
}
473-
} catch {
474-
// Ignore errors for individual scope checks
475-
}
476-
}
477-
478-
// If we found sessions with different account IDs, there are multiple accounts
479-
// However, the current API limitations mean we can only detect one session at a time
480-
// So we use a different approach: check if there are accounts available to switch to
481-
// by checking the account property on the session
482-
483-
// For now, we'll assume if the user is authenticated, there might be multiple accounts
484-
// The VS Code API doesn't easily expose all accounts, but the manage preferences command
485-
// will show the user if they have multiple accounts configured
486-
return foundSessions.size > 0;
487-
} catch (e) {
488-
Logger.error(`Error checking for multiple accounts: ${e}`, CredentialStore.ID);
489-
return false;
490-
}
463+
public async isAuthenticatedForAccountPreferences(authProviderId: AuthProvider): Promise<boolean> {
464+
return this.isAuthenticated(authProviderId);
491465
}
492466

493467
/**
@@ -498,8 +472,13 @@ export class CredentialStore extends Disposable {
498472
* @returns true if the user chose to manage account preferences, false otherwise
499473
*/
500474
public async showWrongAccountModal(repoName: string, authProviderId: AuthProvider): Promise<boolean> {
501-
const currentUser = await this.getCurrentUser(authProviderId);
502-
const accountName = currentUser?.login ?? vscode.l10n.t('your current account');
475+
let accountName: string;
476+
try {
477+
const currentUser = await this.getCurrentUser(authProviderId);
478+
accountName = currentUser?.login ?? vscode.l10n.t('your current account');
479+
} catch {
480+
accountName = vscode.l10n.t('your current account');
481+
}
503482

504483
const manageAccountPreferences = vscode.l10n.t('Manage Account Preferences');
505484
const result = await vscode.window.showErrorMessage(

src/github/folderRepositoryManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,8 +1362,8 @@ export class FolderRepositoryManager extends Disposable {
13621362
if (e.status === 404) {
13631363
// not found - this might be due to using the wrong account
13641364
const repoName = `${githubRepository.remote.owner}/${githubRepository.remote.repositoryName}`;
1365-
const hasMultipleAccounts = await this._credentialStore.hasMultipleAccounts(githubRepository.remote.authProviderId);
1366-
if (hasMultipleAccounts) {
1365+
const isAuthenticated = await this._credentialStore.isAuthenticatedForAccountPreferences(githubRepository.remote.authProviderId);
1366+
if (isAuthenticated) {
13671367
// Show modal suggesting the user might be using the wrong account
13681368
await this._credentialStore.showWrongAccountModal(repoName, githubRepository.remote.authProviderId);
13691369
} else {

src/view/treeNodes/categoryNode.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,10 @@ export class CategoryTreeNode extends TreeNode implements vscode.TreeItem {
310310
actions.push(vscode.l10n.t('Login again'));
311311
} else if (e.status === 403 || e.response?.status === 403) {
312312
// 403 forbidden - user might not have access with current account
313-
const hasMultipleAccounts = await this.folderRepoManager.credentialStore.hasMultipleAccounts(AuthProvider.github);
314-
if (hasMultipleAccounts) {
313+
// Check both GitHub.com and Enterprise providers since we might have repos from either
314+
const isAuthenticatedGitHub = await this.folderRepoManager.credentialStore.isAuthenticatedForAccountPreferences(AuthProvider.github);
315+
const isAuthenticatedEnterprise = await this.folderRepoManager.credentialStore.isAuthenticatedForAccountPreferences(AuthProvider.githubEnterprise);
316+
if (isAuthenticatedGitHub || isAuthenticatedEnterprise) {
315317
actions.push(vscode.l10n.t('Check Account Preferences'));
316318
}
317319
}

0 commit comments

Comments
 (0)