Skip to content

Commit 704675f

Browse files
Copilotalexr00
andcommitted
Add createFromTemplate setting with none, first, and prompt options
Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
1 parent 28cd193 commit 704675f

5 files changed

Lines changed: 78 additions & 1 deletion

File tree

package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,21 @@
158158
"default": "template",
159159
"description": "%githubPullRequests.pullRequestDescription.description%"
160160
},
161+
"githubPullRequests.createFromTemplate": {
162+
"type": "string",
163+
"enum": [
164+
"none",
165+
"first",
166+
"prompt"
167+
],
168+
"enumDescriptions": [
169+
"%githubPullRequests.createFromTemplate.none%",
170+
"%githubPullRequests.createFromTemplate.first%",
171+
"%githubPullRequests.createFromTemplate.prompt%"
172+
],
173+
"default": "first",
174+
"description": "%githubPullRequests.createFromTemplate.description%"
175+
},
161176
"githubPullRequests.defaultCreateOption": {
162177
"type": "string",
163178
"enum": [

package.nls.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
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.createFromTemplate.description": "When multiple pull request templates are available, choose which template to use.",
10+
"githubPullRequests.createFromTemplate.none": "Do not use any template",
11+
"githubPullRequests.createFromTemplate.first": "Use the first template found",
12+
"githubPullRequests.createFromTemplate.prompt": "Prompt to choose which template to use",
913
"githubPullRequests.defaultCreateOption.description": "The create option that the \"Create\" button will default to when creating a pull request.",
1014
"githubPullRequests.defaultCreateOption.lastUsed": "The most recently used create option.",
1115
"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 CREATE_FROM_TEMPLATE = 'createFromTemplate';
2122
export const NOTIFICATION_SETTING = 'notifications';
2223
export type NotificationVariants = 'off' | 'pullRequests';
2324
export const POST_CREATE = 'postCreate';

src/github/createPRViewProvider.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,41 @@ export class CreatePullRequestViewProvider extends BaseCreatePullRequestViewProv
757757
}
758758

759759
private async getPullRequestTemplate(): Promise<string | undefined> {
760-
return this._folderRepositoryManager.getPullRequestTemplateBody(this.model.baseOwner);
760+
const createFromTemplate = vscode.workspace.getConfiguration(PR_SETTINGS_NAMESPACE).get<'none' | 'first' | 'prompt'>('createFromTemplate', 'first');
761+
762+
if (createFromTemplate === 'none') {
763+
return undefined;
764+
}
765+
766+
const templates = await this._folderRepositoryManager.getAllPullRequestTemplates(this.model.baseOwner);
767+
768+
if (!templates || templates.length === 0) {
769+
return undefined;
770+
}
771+
772+
if (templates.length === 1 || createFromTemplate === 'first') {
773+
return templates[0];
774+
}
775+
776+
// createFromTemplate === 'prompt' and multiple templates exist
777+
const selectedTemplate = await vscode.window.showQuickPick(
778+
templates.map((template, index) => {
779+
// Try to extract a meaningful name from the template (first line or first few chars)
780+
const firstLine = template.split('\n')[0].trim();
781+
const label = firstLine || `Template ${index + 1}`;
782+
return {
783+
label: label.substring(0, 50) + (label.length > 50 ? '...' : ''),
784+
description: `${template.length} characters`,
785+
template: template
786+
};
787+
}),
788+
{
789+
placeHolder: vscode.l10n.t('Select a pull request template'),
790+
ignoreFocusOut: true
791+
}
792+
);
793+
794+
return selectedTemplate?.template;
761795
}
762796

763797
protected async detectBaseMetadata(defaultCompareBranch: Branch): Promise<BaseBranchMetadata | undefined> {

src/github/folderRepositoryManager.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,6 +1366,29 @@ export class FolderRepositoryManager extends Disposable {
13661366
}
13671367
}
13681368

1369+
async getAllPullRequestTemplates(owner: string): Promise<string[] | undefined> {
1370+
try {
1371+
const repository = this.gitHubRepositories.find(repo => repo.remote.owner === owner);
1372+
if (!repository) {
1373+
return undefined;
1374+
}
1375+
const templates = await repository.getPullRequestTemplates();
1376+
if (templates && templates.length > 0) {
1377+
return templates;
1378+
}
1379+
1380+
// If there's no local template, look for owner-wide templates
1381+
const githubRepository = await this.createGitHubRepositoryFromOwnerName(owner, '.github');
1382+
if (!githubRepository) {
1383+
return undefined;
1384+
}
1385+
return githubRepository.getPullRequestTemplates();
1386+
} catch (e) {
1387+
Logger.error(`Error fetching pull request templates for ${owner}: ${e instanceof Error ? e.message : e}`, this.id);
1388+
return undefined;
1389+
}
1390+
}
1391+
13691392
private async getPullRequestTemplateWithCache(owner: string): Promise<string | undefined> {
13701393
const cacheLocation = `${CACHED_TEMPLATE_BODY}+${this.repository.rootUri.toString()}`;
13711394

0 commit comments

Comments
 (0)