|
8 | 8 | */ |
9 | 9 | import * as vscode from 'vscode'; |
10 | 10 | import { IResolvedPullRequestModel, PullRequestModel } from './pullRequestModel'; |
| 11 | +import { sanitizeIssueTitle } from './utils'; |
11 | 12 | import { Branch, Repository } from '../api/api'; |
12 | 13 | import Logger from '../common/logger'; |
13 | 14 | import { Protocol } from '../common/protocol'; |
14 | 15 | import { parseRepositoryRemotes, Remote } from '../common/remote'; |
15 | | -import { PR_SETTINGS_NAMESPACE, PULL_PR_BRANCH_BEFORE_CHECKOUT, PullPRBranchVariants } from '../common/settingKeys'; |
| 16 | +import { PR_SETTINGS_NAMESPACE, PULL_PR_BRANCH_BEFORE_CHECKOUT, PULL_REQUEST_CHECKOUT_BRANCH_TITLE, PullPRBranchVariants } from '../common/settingKeys'; |
16 | 17 |
|
17 | 18 | const PullRequestRemoteMetadataKey = 'github-pr-remote'; |
18 | 19 | export const PullRequestMetadataKey = 'github-pr-owner-number'; |
@@ -367,11 +368,36 @@ export class PullRequestGitHelper { |
367 | 368 | } |
368 | 369 | } |
369 | 370 |
|
| 371 | + private static prBranchNameVariableSubstitution( |
| 372 | + template: string, |
| 373 | + pullRequest: PullRequestModel, |
| 374 | + ): string { |
| 375 | + const VARIABLE_PATTERN = /\$\{([^}]*?)\}/g; |
| 376 | + return template.replace(VARIABLE_PATTERN, (match: string, variable: string) => { |
| 377 | + switch (variable) { |
| 378 | + case 'owner': |
| 379 | + return pullRequest.author.login; |
| 380 | + case 'number': |
| 381 | + return `${pullRequest.number}`; |
| 382 | + case 'title': |
| 383 | + return sanitizeIssueTitle(pullRequest.title); |
| 384 | + case 'sanitizedLowercaseTitle': |
| 385 | + return sanitizeIssueTitle(pullRequest.title).toLowerCase(); |
| 386 | + default: |
| 387 | + return match; |
| 388 | + } |
| 389 | + }); |
| 390 | + } |
| 391 | + |
370 | 392 | static async calculateUniqueBranchNameForPR( |
371 | 393 | repository: Repository, |
372 | 394 | pullRequest: PullRequestModel, |
373 | 395 | ): Promise<string> { |
374 | | - const branchName = `pr/${pullRequest.author.login}/${pullRequest.number}`; |
| 396 | + const template = vscode.workspace |
| 397 | + .getConfiguration(PR_SETTINGS_NAMESPACE) |
| 398 | + .get<string>(PULL_REQUEST_CHECKOUT_BRANCH_TITLE)!; |
| 399 | + |
| 400 | + const branchName = PullRequestGitHelper.prBranchNameVariableSubstitution(template, pullRequest); |
375 | 401 | let result = branchName; |
376 | 402 | let number = 1; |
377 | 403 |
|
|
0 commit comments