From 844e3fea1521ed889ad172b80eff1ad69c20f0dd Mon Sep 17 00:00:00 2001 From: Uong Dong Hung Date: Tue, 1 Dec 2026 15:55:11 +0700 Subject: [PATCH 1/2] fix: prevent crash when currentUser is undefined on logout (#8187) When switching accounts or logging out of GitHub, currentUser can temporarily be undefined. Accessing currentUser.login caused an unhandled TypeError: "Cannot read properties of undefined (reading 'login')" which triggered an error notification dialog in the active pull request view. Changes: - Add safe checks for currentUser before accessing .login in activityBarViewProvider, pullRequestReviewCommon, pullRequestModel, and issueOverview - Add unit test covering undefined currentUser in activityBarViewProvider.test.ts --- src/github/activityBarViewProvider.ts | 4 ++-- src/github/issueOverview.ts | 4 ++-- src/github/pullRequestModel.ts | 4 ++-- src/github/pullRequestReviewCommon.ts | 5 ++++- src/test/github/activityBarViewProvider.test.ts | 12 ++++++++++++ 5 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/github/activityBarViewProvider.ts b/src/github/activityBarViewProvider.ts index 485658de3f..a2b87e5d15 100644 --- a/src/github/activityBarViewProvider.ts +++ b/src/github/activityBarViewProvider.ts @@ -164,7 +164,7 @@ export class PullRequestViewProvider extends WebviewViewBase implements vscode.W }); } - private getCurrentUserReviewState(reviewers: ReviewState[], currentUser: IAccount): string | undefined { + private getCurrentUserReviewState(reviewers: ReviewState[], currentUser: IAccount | undefined): string | undefined { return PullRequestReviewCommon.getCurrentUserReviewState(reviewers, currentUser); } @@ -386,7 +386,7 @@ export class PullRequestViewProvider extends WebviewViewBase implements vscode.W repositoryDefaultBranch: defaultBranch, doneCheckoutBranch, isIssue: false, - isAuthor: currentUser.login === pullRequest.author.login, + isAuthor: !!currentUser && !!pullRequest.author && currentUser.login === pullRequest.author.login, reviewers: this._existingReviewers, continueOnGitHub, isDarkTheme: vscode.window.activeColorTheme.kind === vscode.ColorThemeKind.Dark, diff --git a/src/github/issueOverview.ts b/src/github/issueOverview.ts index 75efee67a3..001555d960 100644 --- a/src/github/issueOverview.ts +++ b/src/github/issueOverview.ts @@ -323,7 +323,7 @@ export class IssueOverviewPanel extends W } } - protected async getInitializeContext(currentUser: IAccount, issue: IssueModel, timelineEvents: TimelineEvent[], repositoryAccess: RepoAccessAndMergeMethods, viewerCanEdit: boolean, assignableUsers: IAccount[]): Promise { + protected async getInitializeContext(currentUser: IAccount | undefined, issue: IssueModel, timelineEvents: TimelineEvent[], repositoryAccess: RepoAccessAndMergeMethods, viewerCanEdit: boolean, assignableUsers: IAccount[]): Promise { const hasWritePermission = repositoryAccess.hasWritePermission; const canEdit = hasWritePermission || viewerCanEdit; const labels = issue.item.labels.map(label => ({ @@ -362,7 +362,7 @@ export class IssueOverviewPanel extends W canAssignCopilot: assignableUsers.find(user => COPILOT_ACCOUNTS[user.login]) !== undefined, canRequestCopilotReview: false, reactions: issue.item.reactions, - isAuthor: issue.author.login === currentUser.login, + isAuthor: !!currentUser && !!issue.author && issue.author.login === currentUser.login, isDescriptionCollapsed: this.isDescriptionCollapsed(), }; diff --git a/src/github/pullRequestModel.ts b/src/github/pullRequestModel.ts index 27042dcd5d..0278637631 100644 --- a/src/github/pullRequestModel.ts +++ b/src/github/pullRequestModel.ts @@ -2961,8 +2961,8 @@ export class PullRequestModel extends IssueModel implements IPullRe } } -export async function isCopilotOnMyBehalf(pullRequestModel: PullRequestModel, currentUser: IAccount, coAuthors?: IAccount[]): Promise { - if (!COPILOT_ACCOUNTS[pullRequestModel.author.login]) { +export async function isCopilotOnMyBehalf(pullRequestModel: PullRequestModel, currentUser: IAccount | undefined, coAuthors?: IAccount[]): Promise { + if (!currentUser || !pullRequestModel.author || !COPILOT_ACCOUNTS[pullRequestModel.author.login]) { return false; } diff --git a/src/github/pullRequestReviewCommon.ts b/src/github/pullRequestReviewCommon.ts index 9fb2a032b8..9b9ed2bf0e 100644 --- a/src/github/pullRequestReviewCommon.ts +++ b/src/github/pullRequestReviewCommon.ts @@ -74,7 +74,10 @@ export namespace PullRequestReviewCommon { /** * Find currently configured user's review status for the current PR */ - export function getCurrentUserReviewState(reviewers: ReviewState[], currentUser: IAccount): string | undefined { + export function getCurrentUserReviewState(reviewers: ReviewState[], currentUser: IAccount | undefined): string | undefined { + if (!currentUser) { + return undefined; + } const review = reviewers.find(r => reviewerId(r.reviewer) === currentUser.login); // There will always be a review. If not then the PR shouldn't have been or fetched/shown for the current user return review?.state; diff --git a/src/test/github/activityBarViewProvider.test.ts b/src/test/github/activityBarViewProvider.test.ts index 65e812561c..ca6e5f265d 100644 --- a/src/test/github/activityBarViewProvider.test.ts +++ b/src/test/github/activityBarViewProvider.test.ts @@ -414,6 +414,18 @@ describe('Active pull request stack changes', function () { assert.match(confirm.firstCall.args[0], /Rebase and push 3 pull requests in this stack\?/); assert(reply.calledOnceWithExactly(message, { updatedPullRequests: [] })); }); + + it('handles undefined currentUser gracefully without throwing TypeError (#8187)', async function () { + (folderManager.getCurrentUser as SinonStub).resolves(undefined); + await provider.updatePullRequest(model); + await settleUpdates(); + + const initialize = provider.messages.find(message => message.command === 'pr.initialize'); + assert(initialize); + assert.strictEqual(initialize?.pullrequest?.isAuthor, false); + assert.strictEqual(initialize?.pullrequest?.currentUserReviewState, undefined); + assert.strictEqual(initialize?.pullrequest?.isCopilotOnMyBehalf, false); + }); }); describe('Merge queue', function () { From 720bd6b7eaa89efe2a459fa1171df66da45aa68f Mon Sep 17 00:00:00 2001 From: Uong Dong Hung Date: Fri, 4 Dec 2026 14:06:31 +0700 Subject: [PATCH 2/2] chore: address review comments on optional chaining, comment, and issueOverview test --- src/github/activityBarViewProvider.ts | 2 +- src/github/issueOverview.ts | 2 +- src/github/pullRequestReviewCommon.ts | 2 +- src/test/github/issueOverview.test.ts | 9 +++++++++ 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/github/activityBarViewProvider.ts b/src/github/activityBarViewProvider.ts index a2b87e5d15..e948e347b9 100644 --- a/src/github/activityBarViewProvider.ts +++ b/src/github/activityBarViewProvider.ts @@ -386,7 +386,7 @@ export class PullRequestViewProvider extends WebviewViewBase implements vscode.W repositoryDefaultBranch: defaultBranch, doneCheckoutBranch, isIssue: false, - isAuthor: !!currentUser && !!pullRequest.author && currentUser.login === pullRequest.author.login, + isAuthor: currentUser?.login !== undefined && currentUser.login === pullRequest.author?.login, reviewers: this._existingReviewers, continueOnGitHub, isDarkTheme: vscode.window.activeColorTheme.kind === vscode.ColorThemeKind.Dark, diff --git a/src/github/issueOverview.ts b/src/github/issueOverview.ts index 001555d960..58439c1bc5 100644 --- a/src/github/issueOverview.ts +++ b/src/github/issueOverview.ts @@ -362,7 +362,7 @@ export class IssueOverviewPanel extends W canAssignCopilot: assignableUsers.find(user => COPILOT_ACCOUNTS[user.login]) !== undefined, canRequestCopilotReview: false, reactions: issue.item.reactions, - isAuthor: !!currentUser && !!issue.author && issue.author.login === currentUser.login, + isAuthor: currentUser?.login !== undefined && issue.author?.login === currentUser.login, isDescriptionCollapsed: this.isDescriptionCollapsed(), }; diff --git a/src/github/pullRequestReviewCommon.ts b/src/github/pullRequestReviewCommon.ts index 9b9ed2bf0e..c372897c8b 100644 --- a/src/github/pullRequestReviewCommon.ts +++ b/src/github/pullRequestReviewCommon.ts @@ -79,7 +79,7 @@ export namespace PullRequestReviewCommon { return undefined; } const review = reviewers.find(r => reviewerId(r.reviewer) === currentUser.login); - // There will always be a review. If not then the PR shouldn't have been or fetched/shown for the current user + // Returns undefined when currentUser is unavailable or has no review entry return review?.state; } diff --git a/src/test/github/issueOverview.test.ts b/src/test/github/issueOverview.test.ts index a341cc9635..2e173ef794 100644 --- a/src/test/github/issueOverview.test.ts +++ b/src/test/github/issueOverview.test.ts @@ -67,6 +67,10 @@ class TestIssueOverviewPanel extends IssueOverviewPanel { public override _throwError(message: IRequestMessage | undefined, error: string): Promise { return super._throwError(message, error); } + + public callGetInitializeContext(currentUser: Parameters[0], issue: IssueModel) { + return super.getInitializeContext(currentUser, issue, [], { hasWritePermission: false, mergeMethodsAvailability: { merge: false, squash: false, rebase: false } } as any, false, []); + } } describe('IssueOverview remote-only manager upgrades', function () { @@ -288,4 +292,9 @@ describe('IssueOverview remote-only manager upgrades', function () { sandbox.assert.notCalled(lookup); sandbox.assert.notCalled(postMessage); }); + + it('initializes issue context with isAuthor=false when currentUser is undefined (#8187)', async function () { + const result = await panel.callGetInitializeContext(undefined, issue); + assert.strictEqual(result.isAuthor, false); + }); });