|
4 | 4 | *--------------------------------------------------------------------------------------------*/ |
5 | 5 |
|
6 | 6 | import { default as assert } from 'assert'; |
| 7 | +import * as vscode from 'vscode'; |
7 | 8 |
|
8 | 9 | import { MockRepository } from '../mocks/mockRepository'; |
9 | 10 | import { PullRequestGitHelper } from '../../github/pullRequestGitHelper'; |
@@ -188,4 +189,101 @@ describe('PullRequestGitHelper', function () { |
188 | 189 | assert.strictEqual(await repository.getConfig('branch.pr/me/100.github-pr-owner-number'), 'owner#name#100'); |
189 | 190 | }); |
190 | 191 | }); |
| 192 | + |
| 193 | + describe('calculateUniqueBranchNameForPR', function () { |
| 194 | + it('uses default template to create branch name', async function () { |
| 195 | + const url = 'git@github.com:owner/name.git'; |
| 196 | + const remote = new GitHubRemote('origin', url, new Protocol(url), GitHubServerType.GitHubDotCom); |
| 197 | + const gitHubRepository = new MockGitHubRepository(remote, credentialStore, telemetry, sinon); |
| 198 | + |
| 199 | + const prItem = convertRESTPullRequestToRawPullRequest( |
| 200 | + new PullRequestBuilder() |
| 201 | + .number(42) |
| 202 | + .user(u => u.login('testuser')) |
| 203 | + .title('Add new feature') |
| 204 | + .build(), |
| 205 | + gitHubRepository, |
| 206 | + ); |
| 207 | + |
| 208 | + const pullRequest = new PullRequestModel(credentialStore, telemetry, gitHubRepository, remote, prItem); |
| 209 | + |
| 210 | + const branchName = await PullRequestGitHelper.calculateUniqueBranchNameForPR(repository, pullRequest); |
| 211 | + assert.strictEqual(branchName, 'pr/testuser/42', 'Should use default template'); |
| 212 | + }); |
| 213 | + |
| 214 | + it('uses custom template with ${owner} and ${number}', async function () { |
| 215 | + const url = 'git@github.com:owner/name.git'; |
| 216 | + const remote = new GitHubRemote('origin', url, new Protocol(url), GitHubServerType.GitHubDotCom); |
| 217 | + const gitHubRepository = new MockGitHubRepository(remote, credentialStore, telemetry, sinon); |
| 218 | + |
| 219 | + const prItem = convertRESTPullRequestToRawPullRequest( |
| 220 | + new PullRequestBuilder() |
| 221 | + .number(42) |
| 222 | + .user(u => u.login('testuser')) |
| 223 | + .title('Add new feature') |
| 224 | + .build(), |
| 225 | + gitHubRepository, |
| 226 | + ); |
| 227 | + |
| 228 | + const pullRequest = new PullRequestModel(credentialStore, telemetry, gitHubRepository, remote, prItem); |
| 229 | + |
| 230 | + // Set custom template |
| 231 | + const configStub = sinon.stub().returns('feature/${owner}-${number}'); |
| 232 | + sinon.stub(vscode.workspace, 'getConfiguration').returns({ |
| 233 | + get: configStub, |
| 234 | + } as any); |
| 235 | + |
| 236 | + const branchName = await PullRequestGitHelper.calculateUniqueBranchNameForPR(repository, pullRequest); |
| 237 | + assert.strictEqual(branchName, 'feature/testuser-42', 'Should use custom template with ${owner} and ${number}'); |
| 238 | + }); |
| 239 | + |
| 240 | + it('uses custom template with ${title}', async function () { |
| 241 | + const url = 'git@github.com:owner/name.git'; |
| 242 | + const remote = new GitHubRemote('origin', url, new Protocol(url), GitHubServerType.GitHubDotCom); |
| 243 | + const gitHubRepository = new MockGitHubRepository(remote, credentialStore, telemetry, sinon); |
| 244 | + |
| 245 | + const prItem = convertRESTPullRequestToRawPullRequest( |
| 246 | + new PullRequestBuilder() |
| 247 | + .number(42) |
| 248 | + .user(u => u.login('testuser')) |
| 249 | + .title('Add new feature') |
| 250 | + .build(), |
| 251 | + gitHubRepository, |
| 252 | + ); |
| 253 | + |
| 254 | + const pullRequest = new PullRequestModel(credentialStore, telemetry, gitHubRepository, remote, prItem); |
| 255 | + |
| 256 | + // Set custom template |
| 257 | + const configStub = sinon.stub().returns('pr-${number}-${title}'); |
| 258 | + sinon.stub(vscode.workspace, 'getConfiguration').returns({ |
| 259 | + get: configStub, |
| 260 | + } as any); |
| 261 | + |
| 262 | + const branchName = await PullRequestGitHelper.calculateUniqueBranchNameForPR(repository, pullRequest); |
| 263 | + assert.strictEqual(branchName, 'pr-42-Add-new-feature', 'Should use custom template with sanitized title'); |
| 264 | + }); |
| 265 | + |
| 266 | + it('creates unique branch name when branch already exists', async function () { |
| 267 | + const url = 'git@github.com:owner/name.git'; |
| 268 | + const remote = new GitHubRemote('origin', url, new Protocol(url), GitHubServerType.GitHubDotCom); |
| 269 | + const gitHubRepository = new MockGitHubRepository(remote, credentialStore, telemetry, sinon); |
| 270 | + |
| 271 | + const prItem = convertRESTPullRequestToRawPullRequest( |
| 272 | + new PullRequestBuilder() |
| 273 | + .number(42) |
| 274 | + .user(u => u.login('testuser')) |
| 275 | + .title('Add new feature') |
| 276 | + .build(), |
| 277 | + gitHubRepository, |
| 278 | + ); |
| 279 | + |
| 280 | + const pullRequest = new PullRequestModel(credentialStore, telemetry, gitHubRepository, remote, prItem); |
| 281 | + |
| 282 | + // Create existing branch with the same name |
| 283 | + await repository.createBranch('pr/testuser/42', false, 'some-commit-hash'); |
| 284 | + |
| 285 | + const branchName = await PullRequestGitHelper.calculateUniqueBranchNameForPR(repository, pullRequest); |
| 286 | + assert.strictEqual(branchName, 'pr/testuser/42-1', 'Should append -1 when branch exists'); |
| 287 | + }); |
| 288 | + }); |
191 | 289 | }); |
0 commit comments