-
Notifications
You must be signed in to change notification settings - Fork 731
Expand file tree
/
Copy pathpullRequestGitHelper.test.ts
More file actions
289 lines (236 loc) · 11.6 KB
/
pullRequestGitHelper.test.ts
File metadata and controls
289 lines (236 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { default as assert } from 'assert';
import * as vscode from 'vscode';
import { MockRepository } from '../mocks/mockRepository';
import { PullRequestGitHelper } from '../../github/pullRequestGitHelper';
import { PullRequestModel } from '../../github/pullRequestModel';
import { MockGitHubRepository } from '../mocks/mockGitHubRepository';
import { GitHubRemote, Remote } from '../../common/remote';
import { Protocol } from '../../common/protocol';
import { CredentialStore } from '../../github/credentials';
import { MockTelemetry } from '../mocks/mockTelemetry';
import { MockCommandRegistry } from '../mocks/mockCommandRegistry';
import { SinonSandbox, createSandbox } from 'sinon';
import { convertRESTPullRequestToRawPullRequest } from '../../github/utils';
import { PullRequestBuilder } from '../builders/rest/pullRequestBuilder';
import { RefType } from '../../api/api1';
import { RepositoryBuilder } from '../builders/rest/repoBuilder';
import { MockExtensionContext } from '../mocks/mockExtensionContext';
import { GitHubServerType } from '../../common/authentication';
describe('PullRequestGitHelper', function () {
let sinon: SinonSandbox;
let repository: MockRepository;
let telemetry: MockTelemetry;
let credentialStore: CredentialStore;
let context: MockExtensionContext;
beforeEach(function () {
sinon = createSandbox();
MockCommandRegistry.install(sinon);
repository = new MockRepository();
telemetry = new MockTelemetry();
context = new MockExtensionContext();
credentialStore = new CredentialStore(telemetry, context);
});
afterEach(function () {
sinon.restore();
});
describe('fetchAndCheckout', function () {
it('creates a unique branch when local branch exists with different commit to preserve user work', async function () {
const url = 'git@github.com:owner/name.git';
const remote = new GitHubRemote('origin', url, new Protocol(url), GitHubServerType.GitHubDotCom);
const gitHubRepository = new MockGitHubRepository(remote, credentialStore, telemetry, sinon);
const prItem = convertRESTPullRequestToRawPullRequest(
new PullRequestBuilder()
.number(100)
.user(u => u.login('me'))
.base(b => {
(b.repo)(r => (<RepositoryBuilder>r).clone_url('git@github.com:owner/name.git'));
})
.head(h => {
h.repo(r => (<RepositoryBuilder>r).clone_url('git@github.com:owner/name.git'));
h.ref('my-branch');
})
.build(),
gitHubRepository,
);
const pullRequest = new PullRequestModel(credentialStore, telemetry, gitHubRepository, remote, prItem);
// Setup: local branch exists with different commit than remote
await repository.createBranch('my-branch', false, 'local-commit-hash');
// Setup: remote branch has different commit
await repository.createBranch('refs/remotes/origin/my-branch', false, 'remote-commit-hash');
const remotes = [remote];
// Expect fetch to be called
repository.expectFetch('origin', 'my-branch');
await PullRequestGitHelper.fetchAndCheckout(repository, remotes, pullRequest, { report: () => undefined });
// Verify that the original local branch is preserved
const originalBranch = await repository.getBranch('my-branch');
assert.strictEqual(originalBranch.commit, 'local-commit-hash', 'Original branch should be preserved');
// Verify that a unique branch was created with the correct commit
const uniqueBranch = await repository.getBranch('pr/me/100');
assert.strictEqual(uniqueBranch.commit, 'remote-commit-hash', 'Unique branch should have remote commit');
assert.strictEqual(repository.state.HEAD?.name, 'pr/me/100', 'Should check out the unique branch');
});
it('creates a unique branch even when currently checked out on conflicting local branch', async function () {
const url = 'git@github.com:owner/name.git';
const remote = new GitHubRemote('origin', url, new Protocol(url), GitHubServerType.GitHubDotCom);
const gitHubRepository = new MockGitHubRepository(remote, credentialStore, telemetry, sinon);
const prItem = convertRESTPullRequestToRawPullRequest(
new PullRequestBuilder()
.number(100)
.user(u => u.login('me'))
.base(b => {
(b.repo)(r => (<RepositoryBuilder>r).clone_url('git@github.com:owner/name.git'));
})
.head(h => {
h.repo(r => (<RepositoryBuilder>r).clone_url('git@github.com:owner/name.git'));
h.ref('my-branch');
})
.build(),
gitHubRepository,
);
const pullRequest = new PullRequestModel(credentialStore, telemetry, gitHubRepository, remote, prItem);
// Setup: local branch exists with different commit than remote AND is currently checked out
await repository.createBranch('my-branch', true, 'local-commit-hash'); // checkout = true
// Setup: remote branch has different commit
await repository.createBranch('refs/remotes/origin/my-branch', false, 'remote-commit-hash');
const remotes = [remote];
// Expect fetch to be called
repository.expectFetch('origin', 'my-branch');
await PullRequestGitHelper.fetchAndCheckout(repository, remotes, pullRequest, { report: () => undefined });
// Verify that the original local branch is preserved with its commit
const originalBranch = await repository.getBranch('my-branch');
assert.strictEqual(originalBranch.commit, 'local-commit-hash', 'Original branch should be preserved');
// Verify that a unique branch was created and checked out
const uniqueBranch = await repository.getBranch('pr/me/100');
assert.strictEqual(uniqueBranch.commit, 'remote-commit-hash', 'Unique branch should have remote commit');
assert.strictEqual(repository.state.HEAD?.name, 'pr/me/100', 'Should check out the unique branch');
});
});
describe('checkoutFromFork', function () {
it('fetches, checks out, and configures a branch from a fork', async function () {
const url = 'git@github.com:owner/name.git';
const remote = new GitHubRemote('elsewhere', url, new Protocol(url), GitHubServerType.GitHubDotCom);
const gitHubRepository = new MockGitHubRepository(remote, credentialStore, telemetry, sinon);
const prItem = convertRESTPullRequestToRawPullRequest(
new PullRequestBuilder()
.number(100)
.user(u => u.login('me'))
.base(b => {
(b.repo)(r => (<RepositoryBuilder>r).clone_url('git@github.com:owner/name.git'));
})
.head(h => {
h.repo(r => (<RepositoryBuilder>r).clone_url('git@github.com:you/name.git'));
h.ref('my-branch');
})
.build(),
gitHubRepository,
);
repository.expectFetch('you', 'my-branch:pr/me/100');
repository.expectPull(true);
const pullRequest = new PullRequestModel(credentialStore, telemetry, gitHubRepository, remote, prItem);
if (!pullRequest.isResolved()) {
assert(false, 'pull request head not resolved successfully');
}
await PullRequestGitHelper.checkoutFromFork(repository, pullRequest, undefined, { report: () => undefined });
assert.deepStrictEqual(repository.state.remotes, [
{
name: 'you',
fetchUrl: 'git@github.com:you/name',
pushUrl: 'git@github.com:you/name',
isReadOnly: false,
},
]);
assert.deepStrictEqual(repository.state.HEAD, {
type: RefType.Head,
name: 'pr/me/100',
commit: undefined,
upstream: {
remote: 'you',
name: 'my-branch',
},
});
assert.strictEqual(await repository.getConfig('branch.pr/me/100.github-pr-owner-number'), 'owner#name#100');
});
});
describe('calculateUniqueBranchNameForPR', function () {
it('uses default template to create branch name', async function () {
const url = 'git@github.com:owner/name.git';
const remote = new GitHubRemote('origin', url, new Protocol(url), GitHubServerType.GitHubDotCom);
const gitHubRepository = new MockGitHubRepository(remote, credentialStore, telemetry, sinon);
const prItem = convertRESTPullRequestToRawPullRequest(
new PullRequestBuilder()
.number(42)
.user(u => u.login('testuser'))
.title('Add new feature')
.build(),
gitHubRepository,
);
const pullRequest = new PullRequestModel(credentialStore, telemetry, gitHubRepository, remote, prItem);
const branchName = await PullRequestGitHelper.calculateUniqueBranchNameForPR(repository, pullRequest);
assert.strictEqual(branchName, 'pr/testuser/42', 'Should use default template');
});
it('uses custom template with ${owner} and ${number}', async function () {
const url = 'git@github.com:owner/name.git';
const remote = new GitHubRemote('origin', url, new Protocol(url), GitHubServerType.GitHubDotCom);
const gitHubRepository = new MockGitHubRepository(remote, credentialStore, telemetry, sinon);
const prItem = convertRESTPullRequestToRawPullRequest(
new PullRequestBuilder()
.number(42)
.user(u => u.login('testuser'))
.title('Add new feature')
.build(),
gitHubRepository,
);
const pullRequest = new PullRequestModel(credentialStore, telemetry, gitHubRepository, remote, prItem);
// Set custom template
const configStub = sinon.stub().returns('feature/${owner}-${number}');
sinon.stub(vscode.workspace, 'getConfiguration').returns({
get: configStub,
} as any);
const branchName = await PullRequestGitHelper.calculateUniqueBranchNameForPR(repository, pullRequest);
assert.strictEqual(branchName, 'feature/testuser-42', 'Should use custom template with ${owner} and ${number}');
});
it('uses custom template with ${title}', async function () {
const url = 'git@github.com:owner/name.git';
const remote = new GitHubRemote('origin', url, new Protocol(url), GitHubServerType.GitHubDotCom);
const gitHubRepository = new MockGitHubRepository(remote, credentialStore, telemetry, sinon);
const prItem = convertRESTPullRequestToRawPullRequest(
new PullRequestBuilder()
.number(42)
.user(u => u.login('testuser'))
.title('Add new feature')
.build(),
gitHubRepository,
);
const pullRequest = new PullRequestModel(credentialStore, telemetry, gitHubRepository, remote, prItem);
// Set custom template
const configStub = sinon.stub().returns('pr-${number}-${title}');
sinon.stub(vscode.workspace, 'getConfiguration').returns({
get: configStub,
} as any);
const branchName = await PullRequestGitHelper.calculateUniqueBranchNameForPR(repository, pullRequest);
assert.strictEqual(branchName, 'pr-42-Add-new-feature', 'Should use custom template with sanitized title');
});
it('creates unique branch name when branch already exists', async function () {
const url = 'git@github.com:owner/name.git';
const remote = new GitHubRemote('origin', url, new Protocol(url), GitHubServerType.GitHubDotCom);
const gitHubRepository = new MockGitHubRepository(remote, credentialStore, telemetry, sinon);
const prItem = convertRESTPullRequestToRawPullRequest(
new PullRequestBuilder()
.number(42)
.user(u => u.login('testuser'))
.title('Add new feature')
.build(),
gitHubRepository,
);
const pullRequest = new PullRequestModel(credentialStore, telemetry, gitHubRepository, remote, prItem);
// Create existing branch with the same name
await repository.createBranch('pr/testuser/42', false, 'some-commit-hash');
const branchName = await PullRequestGitHelper.calculateUniqueBranchNameForPR(repository, pullRequest);
assert.strictEqual(branchName, 'pr/testuser/42-1', 'Should append -1 when branch exists');
});
});
});