Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/github/pullRequestOverview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,8 @@ export class PullRequestOverviewPanel extends IssueOverviewPanel<PullRequestMode
}

private async processPendingUpdates(): Promise<void> {
while (this._pendingUpdate) {
// An update that was queued before the panel was closed cannot be shown.
while (this._pendingUpdate && !this.isDisposed) {
const pullRequestModel = this._pendingUpdate;
this._pendingUpdate = undefined;
await this.updateItemNow(pullRequestModel);
Expand Down Expand Up @@ -959,20 +960,28 @@ export class PullRequestOverviewPanel extends IssueOverviewPanel<PullRequestMode
}

public override async refreshPanel(): Promise<void> {
if (!this._panel.visible || this._refreshing) {
if (this.isDisposed || !this._panel.visible || this._refreshing) {
return;
}
this._refreshing = true;
try {
const pullRequest = await this.resolveModel(this._identity);
// The panel can be closed while the pull request is resolved, such as when merging it checks out the default branch.
if (this.isDisposed) {
return;
}
if (!pullRequest) {
throw new Error(
`Failed to resolve Pull Request #${this._identity.number} in ${this._identity.owner}/${this._identity.repo}`,
);
}
await this.updateItem(pullRequest);
} catch (error) {
vscode.window.showErrorMessage(`Error refreshing pull request description: ${formatError(error)}`);
if (this.isDisposed) {
Logger.debug(`Pull request description closed while refreshing: ${formatError(error)}`, PullRequestOverviewPanel.ID);
} else {
vscode.window.showErrorMessage(`Error refreshing pull request description: ${formatError(error)}`);
}
} finally {
this._refreshing = false;
}
Expand Down
55 changes: 52 additions & 3 deletions src/test/github/pullRequestOverview.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1457,6 +1457,55 @@ describe('PullRequestOverview', function () {
});
});

describe('refreshing a closed panel', function () {
it('does not report an error when merging closes the panel while the pull request is reloaded', async function () {
const { model, resolvePullRequest, showError, webviewPanel } = await createPanel();
let reloadPullRequest!: (model: PullRequestModel) => void;
resolvePullRequest.resetHistory();
resolvePullRequest.callsFake(() => new Promise(resolve => reloadPullRequest = resolve));

// Merging the pull request refreshes the panel.
model.update({ ...model.item, state: 'MERGED', merged: true });
sinon.assert.calledOnce(resolvePullRequest);
// Checking out the default branch after the merge closes the panel before the refresh finishes.
webviewPanel.dispose();
reloadPullRequest(model);
await new Promise(resolve => setImmediate(resolve));

sinon.assert.notCalled(showError);
});

it('does nothing when the panel is refreshed after it is closed', async function () {
const { panel, resolvePullRequest, showError, webviewPanel } = await createPanel();
resolvePullRequest.resetHistory();
webviewPanel.dispose();

await panel.refreshPanel();

sinon.assert.notCalled(resolvePullRequest);
sinon.assert.notCalled(showError);
});

it('does not apply an update that was queued before the panel closed', async function () {
const { panel, model, defaultBranch, showError, webviewPanel } = await createPanel();
let finishUpdate!: (defaultBranch: string) => void;
defaultBranch.returns(new Promise<string>(resolve => finishUpdate = resolve));
defaultBranch.resetHistory();
const refreshing = panel.refreshPanel();
await new Promise(resolve => setImmediate(resolve));
sinon.assert.calledOnce(defaultBranch);

const identity = { owner: remote.owner, repo: remote.repositoryName, number: model.number };
const reopening = PullRequestOverviewPanel.createOrShow(telemetry, EXTENSION_URI, pullRequestManager, identity, model, false, true, webviewPanel);
webviewPanel.dispose();
finishUpdate('main');
await Promise.all([refreshing, reopening]);

sinon.assert.calledOnce(defaultBranch);
sinon.assert.notCalled(showError);
});
});

describe('mergeStack', function () {
const stack: PullRequestStack = {
position: 2,
Expand Down Expand Up @@ -1532,7 +1581,7 @@ describe('PullRequestOverview', function () {
mergeMethodsAvailability: { merge: true, squash: true, rebase: true },
viewerCanAutoMerge: false,
});
sinon.stub(pullRequestManager, 'getPullRequestRepositoryDefaultBranch').resolves('main');
const defaultBranch = sinon.stub(pullRequestManager, 'getPullRequestRepositoryDefaultBranch').resolves('main');
const currentUser = sinon.stub(pullRequestManager, 'getCurrentUser').callsFake(async () => {
const model = models.values().next().value;
assert(model);
Expand All @@ -1549,7 +1598,7 @@ describe('PullRequestOverview', function () {
const showError = sinon.stub(vscode.window, 'showErrorMessage').resolves(undefined);
const stackablePullRequests = sinon.stub(repo, 'getStackablePullRequests').resolves({});
const query = sinon.spy(repo, 'query');
return { models, access, currentUser, branch, queueMethod, resolvePullRequest, externalUri, showError, stackablePullRequests, query };
return { models, access, defaultBranch, currentUser, branch, queueMethod, resolvePullRequest, externalUri, showError, stackablePullRequests, query };
}

/** A pull request whose branches are both in the test repository, so it can be stacked. */
Expand Down Expand Up @@ -1625,7 +1674,7 @@ describe('PullRequestOverview', function () {
{ position: 2, number: 1000, title: 'Second', url: '', head: 'D2', state: GithubItemStateEnum.Open, isDraft: false, mergeable: PullRequestMergeability.Mergeable },
],
});
return { ...services, panel, model, stackQuery, webviewPostMessage, received, onDidPost: posted.event };
return { ...services, panel, model, stackQuery, webviewPanel, webviewPostMessage, received, onDidPost: posted.event };
}

describe('stack panel fixture', function () {
Expand Down
Loading