|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { default as assert } from 'assert'; |
| 7 | +import { createSandbox, SinonSandbox } from 'sinon'; |
| 8 | +import { Protocol } from '../../common/protocol'; |
| 9 | +import { GitHubServerType } from '../../common/authentication'; |
| 10 | +import { GitHubRemote } from '../../common/remote'; |
| 11 | +import { CredentialStore } from '../../github/credentials'; |
| 12 | +import { GithubItemStateEnum } from '../../github/interface'; |
| 13 | +import { PullRequestModel } from '../../github/pullRequestModel'; |
| 14 | +import { addPullRequestsToStack, isStackablePullRequest, orderStackablePullRequests } from '../../github/pullRequestStack'; |
| 15 | +import { convertRESTPullRequestToRawPullRequest } from '../../github/utils'; |
| 16 | +import { PullRequestBuilder } from '../builders/rest/pullRequestBuilder'; |
| 17 | +import { getAddToStackConfirmation } from '../../view/prsTreeDataProvider'; |
| 18 | +import { MockCommandRegistry } from '../mocks/mockCommandRegistry'; |
| 19 | +import { MockExtensionContext } from '../mocks/mockExtensionContext'; |
| 20 | +import { MockGitHubRepository } from '../mocks/mockGitHubRepository'; |
| 21 | +import { MockTelemetry } from '../mocks/mockTelemetry'; |
| 22 | + |
| 23 | +describe('Pull request stack selection', function () { |
| 24 | + let sinon: SinonSandbox; |
| 25 | + let context: MockExtensionContext; |
| 26 | + let credentials: CredentialStore; |
| 27 | + let repository: MockGitHubRepository; |
| 28 | + let remote: GitHubRemote; |
| 29 | + let telemetry: MockTelemetry; |
| 30 | + |
| 31 | + beforeEach(function () { |
| 32 | + sinon = createSandbox(); |
| 33 | + MockCommandRegistry.install(sinon); |
| 34 | + context = new MockExtensionContext(); |
| 35 | + telemetry = new MockTelemetry(); |
| 36 | + credentials = new CredentialStore(telemetry, context); |
| 37 | + const url = 'https://github.com/owner/repo'; |
| 38 | + remote = new GitHubRemote('origin', url, new Protocol(url), GitHubServerType.GitHubDotCom); |
| 39 | + repository = new MockGitHubRepository(remote, credentials, telemetry, sinon); |
| 40 | + }); |
| 41 | + |
| 42 | + afterEach(function () { |
| 43 | + repository.dispose(); |
| 44 | + credentials.dispose(); |
| 45 | + context.dispose(); |
| 46 | + sinon.restore(); |
| 47 | + }); |
| 48 | + |
| 49 | + function pullRequest(number: number, base: string, head: string, state: 'open' | 'closed' = 'open', headOwner: string = remote.owner): PullRequestModel { |
| 50 | + const rest = new PullRequestBuilder().number(number).state(state) |
| 51 | + .base(ref => ref.ref(base)) |
| 52 | + .head(ref => ref.ref(head)).build(); |
| 53 | + for (const ref of [rest.base, rest.head]) { |
| 54 | + ref.repo.owner.login = remote.owner; |
| 55 | + ref.repo.name = remote.repositoryName; |
| 56 | + ref.repo.clone_url = `https://github.com/${remote.owner}/${remote.repositoryName}.git`; |
| 57 | + } |
| 58 | + rest.head.repo.owner.login = headOwner; |
| 59 | + rest.head.repo.clone_url = `https://github.com/${headOwner}/${remote.repositoryName}.git`; |
| 60 | + return new PullRequestModel(credentials, telemetry, repository, remote, convertRESTPullRequestToRawPullRequest(rest, repository)); |
| 61 | + } |
| 62 | + |
| 63 | + it('orders a branch chain from the target base toward the top', function () { |
| 64 | + const bottom = pullRequest(1, 'main', 'D1'); |
| 65 | + const middle = pullRequest(2, 'D1', 'D2'); |
| 66 | + const top = pullRequest(3, 'D2', 'D3'); |
| 67 | + assert.deepStrictEqual(orderStackablePullRequests([top, bottom, middle]), [bottom, middle, top]); |
| 68 | + }); |
| 69 | + |
| 70 | + it('distinguishes creating a stack from adding PRs to an existing stack', function () { |
| 71 | + const bottom = pullRequest(1, 'main', 'D1'); |
| 72 | + const middle = pullRequest(2, 'D1', 'D2'); |
| 73 | + const top = pullRequest(3, 'D2', 'D3'); |
| 74 | + assert.deepStrictEqual(getAddToStackConfirmation([bottom, middle], { |
| 75 | + parentPullRequestNumber: 1, size: 1, url: bottom.html_url, |
| 76 | + }), { |
| 77 | + message: 'Create a stack with 2 pull requests?', |
| 78 | + detail: '#1 New feature\n#2 New feature', |
| 79 | + action: 'Create Stack', |
| 80 | + }); |
| 81 | + assert.deepStrictEqual(getAddToStackConfirmation([bottom, middle], { |
| 82 | + parentPullRequestNumber: 1, stackNumber: 10, size: 3, url: bottom.html_url, |
| 83 | + }), { |
| 84 | + message: 'Add 1 pull request to an existing stack?', |
| 85 | + detail: 'Adding #2 New feature\nto #1 New feature', |
| 86 | + action: 'Add to Stack', |
| 87 | + }); |
| 88 | + assert.deepStrictEqual(getAddToStackConfirmation([bottom, middle, top], { |
| 89 | + parentPullRequestNumber: 1, stackNumber: 10, size: 3, url: bottom.html_url, |
| 90 | + }), { |
| 91 | + message: 'Add 2 pull requests to an existing stack?', |
| 92 | + detail: 'Adding #2 New feature\n#3 New feature\nto #1 New feature', |
| 93 | + action: 'Add to Stack', |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + it('hides the action for unrelated, duplicate, closed and forked PRs', function () { |
| 98 | + const bottom = pullRequest(1, 'main', 'D1'); |
| 99 | + const top = pullRequest(2, 'D1', 'D2'); |
| 100 | + assert.strictEqual(orderStackablePullRequests([bottom]) === undefined, true, 'one PR is insufficient'); |
| 101 | + assert.strictEqual(orderStackablePullRequests([bottom, bottom]) === undefined, true, 'duplicate PRs are invalid'); |
| 102 | + assert.strictEqual(orderStackablePullRequests([bottom, pullRequest(3, 'other', 'D3')]) === undefined, true, 'disconnected branches are invalid'); |
| 103 | + assert.strictEqual(orderStackablePullRequests([bottom, top, pullRequest(3, 'D1', 'D2')]) === undefined, true, 'duplicate head branches are invalid'); |
| 104 | + assert.strictEqual(orderStackablePullRequests([bottom, pullRequest(3, 'D1', 'D3', 'closed')]) === undefined, true, 'closed PRs are invalid'); |
| 105 | + const forked = pullRequest(4, 'D1', 'D4', 'open', 'another'); |
| 106 | + assert.strictEqual(isStackablePullRequest(forked), false); |
| 107 | + assert.strictEqual(orderStackablePullRequests([bottom, forked]) === undefined, true, 'forked heads are invalid'); |
| 108 | + assert.strictEqual(orderStackablePullRequests([bottom, top])?.map(pr => pr.number).join(','), '1,2'); |
| 109 | + }); |
| 110 | + |
| 111 | + it('creates a new stack with selected PRs in branch order after refreshing them', async function () { |
| 112 | + const bottom = pullRequest(1, 'main', 'D1'); |
| 113 | + const middle = pullRequest(2, 'D1', 'D2'); |
| 114 | + const top = pullRequest(3, 'D2', 'D3'); |
| 115 | + const refresh = sinon.stub(repository, 'getPullRequest').callsFake(async number => [bottom, middle, top].find(pr => pr.number === number)); |
| 116 | + sinon.stub(repository, 'getStackCandidate').resolves({ parentPullRequestNumber: 1, size: 1, url: bottom.html_url }); |
| 117 | + sinon.stub(middle, 'getStack').resolves(undefined); |
| 118 | + sinon.stub(top, 'getStack').resolves(undefined); |
| 119 | + const add = sinon.stub(repository, 'addPullRequestsToStack').resolves(); |
| 120 | + |
| 121 | + assert.deepStrictEqual(await addPullRequestsToStack([top, bottom, middle]), [1, 2, 3]); |
| 122 | + assert(refresh.calledThrice); |
| 123 | + assert(add.calledOnceWithExactly({ parentPullRequestNumber: 1, size: 1, url: bottom.html_url }, [2, 3])); |
| 124 | + }); |
| 125 | + |
| 126 | + it('appends to an existing stack only when the bottom PR is its top', async function () { |
| 127 | + const bottom = pullRequest(1, 'main', 'D1'); |
| 128 | + const top = pullRequest(2, 'D1', 'D2'); |
| 129 | + sinon.stub(repository, 'getPullRequest').callsFake(async number => [bottom, top].find(pr => pr.number === number)); |
| 130 | + const candidate = { parentPullRequestNumber: 1, stackNumber: 10, size: 3, url: bottom.html_url }; |
| 131 | + sinon.stub(repository, 'getStackCandidate').resolves(candidate); |
| 132 | + sinon.stub(top, 'getStack').resolves(undefined); |
| 133 | + const add = sinon.stub(repository, 'addPullRequestsToStack').resolves(); |
| 134 | + |
| 135 | + assert.deepStrictEqual(await addPullRequestsToStack([top, bottom]), [1, 2]); |
| 136 | + assert(add.calledOnceWithExactly(candidate, [2])); |
| 137 | + }); |
| 138 | + |
| 139 | + it('rejects stale branch chains and PRs already in another stack before writing', async function () { |
| 140 | + const bottom = pullRequest(1, 'main', 'D1'); |
| 141 | + const top = pullRequest(2, 'D1', 'D2'); |
| 142 | + const moved = pullRequest(2, 'other', 'D2'); |
| 143 | + const refresh = sinon.stub(repository, 'getPullRequest').callsFake(async number => number === 1 ? bottom : moved); |
| 144 | + const add = sinon.stub(repository, 'addPullRequestsToStack').resolves(); |
| 145 | + await assert.rejects(addPullRequestsToStack([top, bottom]), /branches have changed/); |
| 146 | + assert(add.notCalled); |
| 147 | + |
| 148 | + refresh.callsFake(async number => number === 1 ? bottom : top); |
| 149 | + sinon.stub(repository, 'getStackCandidate').resolves({ parentPullRequestNumber: 1, size: 1, url: bottom.html_url }); |
| 150 | + sinon.stub(top, 'getStack').resolves({ |
| 151 | + position: 1, size: 1, base: 'main', pullRequests: [{ |
| 152 | + position: 1, number: 2, title: top.title, url: top.html_url, head: 'D2', |
| 153 | + state: GithubItemStateEnum.Open, isDraft: false, mergeable: top.item.mergeable!, |
| 154 | + }], |
| 155 | + }); |
| 156 | + await assert.rejects(addPullRequestsToStack([bottom, top]), /already in a stack/); |
| 157 | + assert(add.notCalled); |
| 158 | + }); |
| 159 | + |
| 160 | + it('rejects a changed head branch when refreshing reuses the selected model', async function () { |
| 161 | + const bottom = pullRequest(1, 'main', 'D1'); |
| 162 | + const top = pullRequest(2, 'D1', 'D2'); |
| 163 | + sinon.stub(repository, 'getPullRequest').callsFake(async number => { |
| 164 | + if (number === top.number) { |
| 165 | + top.head!.ref = 'D3'; |
| 166 | + } |
| 167 | + return number === bottom.number ? bottom : top; |
| 168 | + }); |
| 169 | + const add = sinon.stub(repository, 'addPullRequestsToStack').resolves(); |
| 170 | + |
| 171 | + await assert.rejects(addPullRequestsToStack([bottom, top]), /branches have changed/); |
| 172 | + assert(add.notCalled); |
| 173 | + }); |
| 174 | +}); |
0 commit comments