Skip to content

Commit 5f82791

Browse files
Copilotalexr00
andcommitted
Add customizable PR checkout branch name setting
Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
1 parent 564af99 commit 5f82791

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@
158158
"default": "template",
159159
"description": "%githubPullRequests.pullRequestDescription.description%"
160160
},
161+
"githubPullRequests.pullRequestCheckoutBranchTitle": {
162+
"type": "string",
163+
"default": "pr/${owner}/${number}",
164+
"markdownDescription": "%githubPullRequests.pullRequestCheckoutBranchTitle.markdownDescription%"
165+
},
161166
"githubPullRequests.defaultCreateOption": {
162167
"type": "string",
163168
"enum": [

package.nls.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
"githubPullRequests.pullRequestDescription.commit": "Use the latest commit message only",
77
"githubPullRequests.pullRequestDescription.none": "Do not have a default description",
88
"githubPullRequests.pullRequestDescription.copilot": "Generate a pull request title and description from GitHub Copilot. Requires that the GitHub Copilot extension is installed and authenticated. Will fall back to `commit` if Copilot is not set up.",
9+
"githubPullRequests.pullRequestCheckoutBranchTitle.markdownDescription": {
10+
"message": "Advanced settings for the name of the branch that is created when you check out a pull request. \n- `${owner}` will be replaced with the pull request author's username \n- `${number}` will be replaced with the pull request number \n- `${title}` will be replaced with the pull request title, with all spaces and unsupported characters (https://git-scm.com/docs/git-check-ref-format) removed. For lowercase, use `${sanitizedLowercaseTitle}` ",
11+
"comment": [
12+
"{Locked='${...}'}",
13+
"Do not translate what's inside of the '${..}'. It is an internal syntax for the extension"
14+
]
15+
},
916
"githubPullRequests.defaultCreateOption.description": "The create option that the \"Create\" button will default to when creating a pull request.",
1017
"githubPullRequests.defaultCreateOption.lastUsed": "The most recently used create option.",
1118
"githubPullRequests.defaultCreateOption.create": "The pull request will be created.",

src/common/settingKeys.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export const NEVER_IGNORE_DEFAULT_BRANCH = 'neverIgnoreDefaultBranch';
1818
export const OVERRIDE_DEFAULT_BRANCH = 'overrideDefaultBranch';
1919
export const PULL_BRANCH = 'pullBranch';
2020
export const PULL_REQUEST_DESCRIPTION = 'pullRequestDescription';
21+
export const PULL_REQUEST_CHECKOUT_BRANCH_TITLE = 'pullRequestCheckoutBranchTitle';
2122
export const NOTIFICATION_SETTING = 'notifications';
2223
export type NotificationVariants = 'off' | 'pullRequests';
2324
export const POST_CREATE = 'postCreate';

src/github/pullRequestGitHelper.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
*/
99
import * as vscode from 'vscode';
1010
import { IResolvedPullRequestModel, PullRequestModel } from './pullRequestModel';
11+
import { sanitizeIssueTitle } from './utils';
1112
import { Branch, Repository } from '../api/api';
1213
import Logger from '../common/logger';
1314
import { Protocol } from '../common/protocol';
1415
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';
1617

1718
const PullRequestRemoteMetadataKey = 'github-pr-remote';
1819
export const PullRequestMetadataKey = 'github-pr-owner-number';
@@ -367,11 +368,36 @@ export class PullRequestGitHelper {
367368
}
368369
}
369370

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+
370392
static async calculateUniqueBranchNameForPR(
371393
repository: Repository,
372394
pullRequest: PullRequestModel,
373395
): 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);
375401
let result = branchName;
376402
let number = 1;
377403

0 commit comments

Comments
 (0)