Skip to content

Commit 3d7b8ea

Browse files
Copilotalexr00
andauthored
Add guidance and template creation when no PR templates exist (#8364)
* Initial plan * Initial commit - planning changes Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com> * Add help when no PR templates exist - show documentation link and create template option Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com> * Make template button always visible for easier template management Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com> * Improve directory creation to handle nested paths robustly Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com> * Improve error handling in directory creation - only ignore FileExists errors Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com> * Polish --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
1 parent c84c48e commit 3d7b8ea

File tree

1 file changed

+100
-1
lines changed

1 file changed

+100
-1
lines changed

src/github/createPRViewProvider.ts

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,31 @@ export class CreatePullRequestViewProvider extends BaseCreatePullRequestViewProv
808808
const templates = await this._folderRepositoryManager.getAllPullRequestTemplates(this.model.baseOwner);
809809

810810
if (!templates || templates.length === 0) {
811-
vscode.window.showQuickPick([vscode.l10n.t('No pull request templates found')]);
811+
// No templates found - show helpful options
812+
const learnMore = vscode.l10n.t('Learn More');
813+
const createTemplate = vscode.l10n.t('Create Template');
814+
const selected = await vscode.window.showQuickPick(
815+
[
816+
{
817+
label: createTemplate,
818+
description: vscode.l10n.t('Create a new pull request template')
819+
},
820+
{
821+
label: learnMore,
822+
description: vscode.l10n.t('Open GitHub documentation')
823+
}
824+
],
825+
{
826+
placeHolder: vscode.l10n.t('No pull request templates found'),
827+
ignoreFocusOut: true
828+
}
829+
);
830+
831+
if (selected?.label === learnMore) {
832+
vscode.env.openExternal(vscode.Uri.parse('https://docs.github.com/en/communities/using-templates-to-encourage-useful-issues-and-pull-requests/creating-a-pull-request-template-for-your-repository'));
833+
} else if (selected?.label === createTemplate) {
834+
await this.createPullRequestTemplate();
835+
}
812836
return this._replyMessage(message, undefined);
813837
}
814838

@@ -839,6 +863,81 @@ export class CreatePullRequestViewProvider extends BaseCreatePullRequestViewProv
839863
return this._replyMessage(message, undefined);
840864
}
841865

866+
private async createPullRequestTemplate(): Promise<void> {
867+
// Show options for where to create the template
868+
const templateLocations = [
869+
{
870+
label: '.github/pull_request_template.md',
871+
description: vscode.l10n.t('Default location for a single template')
872+
},
873+
{
874+
label: 'docs/pull_request_template.md',
875+
description: vscode.l10n.t('Alternative location in docs folder')
876+
},
877+
{
878+
label: '.github/PULL_REQUEST_TEMPLATE/template.md',
879+
description: vscode.l10n.t('For multiple templates')
880+
}
881+
];
882+
883+
const selected = await vscode.window.showQuickPick(templateLocations, {
884+
placeHolder: vscode.l10n.t('Choose where to create the pull request template'),
885+
ignoreFocusOut: true
886+
});
887+
888+
if (!selected) {
889+
return;
890+
}
891+
892+
// Get the repository root
893+
const workspaceFolder = this._folderRepositoryManager.repository.rootUri;
894+
const templatePath = vscode.Uri.joinPath(workspaceFolder, selected.label);
895+
896+
// Default template content
897+
const templateContent = `## Sample Pull Request Template Description
898+
899+
This is a sample pull request template. You can customize it to fit your project's needs.
900+
901+
Don't forget to commit your template file to the repository so that it can be used for future pull requests!
902+
`;
903+
904+
try {
905+
// Ensure all parent directories exist by creating them step by step
906+
const pathParts = selected.label.split('/');
907+
let currentPath = workspaceFolder;
908+
909+
// Create each directory in the path (excluding the file name)
910+
for (let i = 0; i < pathParts.length - 1; i++) {
911+
currentPath = vscode.Uri.joinPath(currentPath, pathParts[i]);
912+
try {
913+
await vscode.workspace.fs.createDirectory(currentPath);
914+
} catch (e) {
915+
// Re-throw if it's not a FileSystemError about the directory already existing
916+
if (e instanceof vscode.FileSystemError && e.code !== 'FileExists') {
917+
throw e;
918+
}
919+
// Directory already exists, which is fine
920+
}
921+
}
922+
923+
// Create the template file
924+
const encoder = new TextEncoder();
925+
await vscode.workspace.fs.writeFile(templatePath, encoder.encode(templateContent));
926+
927+
// Open the file for editing
928+
const document = await vscode.workspace.openTextDocument(templatePath);
929+
await vscode.window.showTextDocument(document);
930+
931+
vscode.window.showInformationMessage(
932+
vscode.l10n.t('Pull request template created at {0}', selected.label)
933+
);
934+
} catch (error) {
935+
vscode.window.showErrorMessage(
936+
vscode.l10n.t('Failed to create pull request template: {0}', error instanceof Error ? error.message : String(error))
937+
);
938+
}
939+
}
940+
842941
protected async detectBaseMetadata(defaultCompareBranch: Branch): Promise<BaseBranchMetadata | undefined> {
843942
const owner = this.model.compareOwner;
844943
const repositoryName = this.model.repositoryName;

0 commit comments

Comments
 (0)