diff --git a/src/github/pullRequestOverview.ts b/src/github/pullRequestOverview.ts index 290eb69da3..e424178d72 100644 --- a/src/github/pullRequestOverview.ts +++ b/src/github/pullRequestOverview.ts @@ -555,20 +555,37 @@ export class PullRequestOverviewPanel extends IssueOverviewPanel this.isDisposed || updateSequence !== this._updateSequence + || overviewIdentity !== this._identity || this._pendingUpdate !== undefined; this._stackLoaded = false; this._isBranchUpToDateWithBase = undefined; const branchStatusSequence = ++this._branchStatusSequence; const updateStart = performance.now(); this._stackPrefetch = undefined; - if (this._panel.visible) { - // The stack only needs the pull request, so it loads while the rest of the page does, and is shown once the page is. - const data = this.fetchStackData(pullRequestModel); - // A failure is reported when the stack is shown. - data.catch(() => undefined); - this._stackPrefetch = { updateSequence, data }; - } try { + // PR list models may contain Markdown without rendered description HTML. + // Resolve it before the initial webview context is sent. + if (pullRequestModel.body?.trim() && !pullRequestModel.bodyHTML?.trim()) { + // Preview data is detached from the shared model cache until freshness is checked. + const description = await pullRequestModel.githubRepository.getPullRequestPreview(pullRequestModel.number); + if (isStale()) { + return; + } + pullRequestModel.bodyHTML = description.bodyHTML; + pullRequestModel.update({ ...pullRequestModel.item, body: description.body, bodyHTML: description.bodyHTML }); + } + + if (this._panel.visible) { + // The stack only needs the pull request, so it loads while the rest of the page does, and is shown once the page is. + const data = this.fetchStackData(pullRequestModel); + // A failure is reported when the stack is shown. + data.catch(() => undefined); + this._stackPrefetch = { updateSequence, data }; + } + const dataTimings = new Map(); const measure = async (name: string, promise: Promise): Promise => { const start = performance.now(); @@ -592,6 +609,9 @@ export class PullRequestOverviewPanel extends IssueOverviewPanel void; const blockedInitialization = new Promise(resolve => releaseInitialization = resolve); @@ -819,6 +824,8 @@ describe('PullRequestOverview', function () { const firstModel = new PullRequestModel(credentialStore, telemetry, repo, remote, firstItem); const updatedItem = convertRESTPullRequestToRawPullRequest(new PullRequestBuilder().number(1000).title('Updated title').build(), repo); const updatedModel = new PullRequestModel(credentialStore, telemetry, repo, remote, updatedItem); + firstModel.bodyHTML = '

Please merge thx

'; + updatedModel.bodyHTML = '

Please merge thx

'; const identity = { owner: firstModel.remote.owner, repo: firstModel.remote.repositoryName, number: firstModel.number }; const staleEvents: TimelineEvent[] = []; let resolveTimeline: (events: TimelineEvent[]) => void; @@ -875,6 +882,7 @@ describe('PullRequestOverview', function () { }); const prItem0 = convertRESTPullRequestToRawPullRequest(new PullRequestBuilder().number(1000).build(), repo); + prItem0.bodyHTML = '

Please merge thx

'; const prModel0 = new PullRequestModel(credentialStore, telemetry, repo, remote, prItem0); const identity0 = { owner: prModel0.remote.owner, repo: prModel0.remote.repositoryName, number: prModel0.number }; const resolveStub = sinon.stub(pullRequestManager, 'resolvePullRequest').resolves(prModel0); @@ -889,6 +897,7 @@ describe('PullRequestOverview', function () { assert.strictEqual(createWebviewPanel.callCount, 1); const prItem1 = convertRESTPullRequestToRawPullRequest(new PullRequestBuilder().number(2000).build(), repo); + prItem1.bodyHTML = '

Please merge thx

'; const prModel1 = new PullRequestModel(credentialStore, telemetry, repo, remote, prItem1); const identity1 = { owner: prModel1.remote.owner, repo: prModel1.remote.repositoryName, number: prModel1.number }; resolveStub.resolves(prModel1); @@ -932,7 +941,8 @@ describe('PullRequestOverview', function () { beforeEach(function () { const prItem = convertRESTPullRequestToRawPullRequest(new PullRequestBuilder().number(1000).build(), repo); - prModel = new PullRequestModel(credentialStore, telemetry, repo, remote, prItem); + prItem.bodyHTML = '

Please merge thx

'; + prModel = repo.createOrUpdatePullRequestModel(prItem); getRepository = sinon.stub(pullRequestManager, 'createGitHubRepositoryFromOwnerName').resolves(repo); getPreview = sinon.stub(repo, 'getPullRequestPreview').resolves(preview); getRepositoryAccess = sinon.stub(pullRequestManager, 'getPullRequestRepositoryAccessAndMergeMethods').resolves({ @@ -987,6 +997,97 @@ describe('PullRequestOverview', function () { previewLoadingTests(() => ({ model: prModel, preview, getPreview, getRepository, getRepositoryAccess, getAssignableUsers, openPanel, messages, webviewPanel })); + for (const bodyHTML of [undefined, '', ' ']) { + it(`loads missing description HTML before initialization (${JSON.stringify(bodyHTML)})`, async function () { + prModel.bodyHTML = bodyHTML; + let resolveDescription!: (description: PullRequestPreview) => void; + const getDescription = getPreview.returns(new Promise(resolve => resolveDescription = resolve)); + const opening = openPanel(); + await new Promise(resolve => setImmediate(resolve)); + assert.strictEqual(messages.some(message => message.command === 'pr.initialize'), false); + sinon.assert.calledOnce(getDescription); + sinon.assert.calledWithExactly(getDescription, prModel.number); + resolveDescription({ ...preview, body: prModel.body, bodyHTML: '

Please merge thx

' }); + await opening; + assert.strictEqual(messages.find(message => message.command === 'pr.initialize')?.pullrequest?.bodyHTML, prModel.bodyHTML); + }); + } + + it('initializes with description HTML parsed from a GitHub GraphQL response', async function () { + prModel.bodyHTML = undefined; + getPreview.restore(); + const query = sinon.stub(repo, 'query').resolves({ + loading: false, stale: false, networkStatus: NetworkStatus.ready, + data: { repository: { pullRequest: { + ...preview, body: 'Actual GitHub Markdown', bodyHTML: '

Actual GitHub Markdown

', + author: null, baseRefName: 'main', headRefName: 'feature', + baseRepository: { owner: { login: remote.owner } }, headRepository: null, + } } }, + }); + await openPanel(); + sinon.assert.calledWithMatch(query, { query: repo.schema.PullRequestPreview }); + assert.strictEqual(messages.find(message => message.command === 'pr.initialize')?.pullrequest?.bodyHTML, + '

Actual GitHub Markdown

'); + }); + + it('ignores description lookups that finish after the overview closes', async function () { + prModel.bodyHTML = undefined; + let resolveDescription!: (description: PullRequestPreview) => void; + getPreview.returns(new Promise(resolve => resolveDescription = resolve)); + const opening = openPanel(); + await new Promise(resolve => setImmediate(resolve)); + webviewPanel.dispose(); + resolveDescription({ ...preview, bodyHTML: '

Late description

' }); + await opening; + assert.strictEqual(messages.some(message => message.command === 'pr.initialize'), false); + }); + + it('does not overwrite a newer overview when an older description lookup finishes', async function () { + prModel.bodyHTML = undefined; + let resolveDescription!: (description: PullRequestPreview) => void; + const getDescription = getPreview.returns(new Promise(resolve => resolveDescription = resolve)); + const firstOpening = openPanel(); + await new Promise(resolve => setImmediate(resolve)); + sinon.assert.calledOnce(getDescription); + const cachedModel = repo.createOrUpdatePullRequestModel(prModel.item); + assert.strictEqual(cachedModel, prModel); + cachedModel.update({ ...cachedModel.item, body: 'New description', bodyHTML: '

New description

' }); + const secondOpening = openPanel(cachedModel); + resolveDescription({ ...preview, body: 'Old description', bodyHTML: '

Old description

' }); + await Promise.all([firstOpening, secondOpening]); + const initializations = messages.filter(message => message.command === 'pr.initialize'); + assert.strictEqual(cachedModel.body, 'New description'); + assert.strictEqual(cachedModel.bodyHTML, '

New description

'); + assert.strictEqual(initializations.length, 1); + assert.strictEqual(initializations[0].pullrequest?.bodyHTML, '

New description

'); + }); + + it('does not resolve a description that already has HTML', async function () { + const getDescription = getPreview; + await openPanel(); + sinon.assert.notCalled(getDescription); + assert.strictEqual(messages.find(message => message.command === 'pr.initialize')?.pullrequest?.bodyHTML, prModel.bodyHTML); + }); + + it('does not resolve an empty description', async function () { + prModel.body = ''; + prModel.bodyHTML = undefined; + const getDescription = getPreview; + await openPanel(); + sinon.assert.notCalled(getDescription); + assert.ok(messages.some(message => message.command === 'pr.initialize')); + }); + + it('reports a description lookup failure instead of initializing a blank body', async function () { + prModel.bodyHTML = undefined; + getPreview.rejects(new Error('Description lookup failed')); + const showError = sinon.stub(vscode.window, 'showErrorMessage').resolves(undefined); + await openPanel(); + assert.strictEqual(showError.firstCall.args[0], 'Error updating pull request description: Description lookup failed'); + assert.strictEqual(messages.some(message => message.command === 'pr.initialize'), false); + }); + + describe('branch labels', function () { const initialized = () => messages.find(message => message.command === 'pr.initialize')?.pullrequest; const branchLabels = () => [initialized()?.base, initialized()?.head]; @@ -1620,6 +1721,7 @@ describe('PullRequestOverview', function () { } function stubModelServices(model: PullRequestModel) { + model.bodyHTML ??= '

Please merge thx

'; sinon.stub(model, 'canEdit').resolves(true); sinon.stub(model, 'getReviewRequests').resolves([]); const timeline = sinon.stub(model, 'getTimelineEvents').resolves([]);