Skip to content

Commit 5af8268

Browse files
authored
Many ripgrep (#5937)
Fixes #5923
1 parent 916697b commit 5af8268

2 files changed

Lines changed: 31 additions & 69 deletions

File tree

src/github/folderRepositoryManager.ts

Lines changed: 30 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ enum PagedDataType {
161161
IssueSearch,
162162
}
163163

164-
const CACHED_TEMPLATE_URI = 'templateUri';
164+
const CACHED_TEMPLATE_BODY = 'templateBody';
165165

166166
export class FolderRepositoryManager implements vscode.Disposable {
167167
static ID = 'FolderRepositoryManager';
@@ -1197,99 +1197,61 @@ export class FolderRepositoryManager implements vscode.Disposable {
11971197
}
11981198

11991199
async getPullRequestTemplateBody(owner: string): Promise<string | undefined> {
1200-
// First try for a local template
1201-
const templates = await this.getPullRequestTemplatesWithCache();
1202-
if (templates.length > 0) {
1203-
const templateUri = templates[0];
1204-
if (templateUri) {
1205-
try {
1206-
const templateContent = await vscode.workspace.fs.readFile(templateUri);
1207-
return new TextDecoder('utf-8').decode(templateContent);
1208-
} catch (e) {
1209-
Logger.warn(`Reading pull request template failed: ${e}`);
1210-
}
1200+
try {
1201+
const template = await this.getPullRequestTemplateWithCache(owner);
1202+
if (template) {
1203+
return template;
12111204
}
1212-
}
12131205

1214-
// If there's no local template, look for a owner-wide template
1215-
return this.getOwnerPullRequestTemplates(owner);
1206+
// If there's no local template, look for a owner-wide template
1207+
return this.getOwnerPullRequestTemplate(owner);
1208+
} catch (e) {
1209+
Logger.error(`Error fetching pull request template for ${owner}: ${e instanceof Error ? e.message : e}`, this.id);
1210+
}
12161211
}
12171212

1218-
async getPullRequestTemplatesWithCache(): Promise<vscode.Uri[]> {
1219-
const cacheLocation = `${CACHED_TEMPLATE_URI}+${this.repository.rootUri.toString()}`;
1213+
private async getPullRequestTemplateWithCache(owner: string): Promise<string | undefined> {
1214+
const cacheLocation = `${CACHED_TEMPLATE_BODY}+${this.repository.rootUri.toString()}`;
12201215

1221-
const findTemplate = this.getPullRequestTemplates().then((templates) => {
1216+
const findTemplate = this.getPullRequestTemplate(owner).then((template) => {
12221217
//update cache
1223-
if (templates.length > 0) {
1224-
this.context.workspaceState.update(cacheLocation, templates[0].toString());
1218+
if (template) {
1219+
this.context.workspaceState.update(cacheLocation, template);
12251220
} else {
12261221
this.context.workspaceState.update(cacheLocation, null);
12271222
}
1228-
return templates;
1223+
return template;
12291224
});
12301225
const hasCachedTemplate = this.context.workspaceState.keys().includes(cacheLocation);
1231-
const cachedTemplateLocation = this.context.workspaceState.get<string | null>(cacheLocation);
1226+
const cachedTemplate = this.context.workspaceState.get<string | null>(cacheLocation);
12321227
if (hasCachedTemplate) {
1233-
if (cachedTemplateLocation === null) {
1234-
return [];
1235-
} else if (cachedTemplateLocation) {
1236-
return [vscode.Uri.parse(cachedTemplateLocation)];
1228+
if (cachedTemplate === null) {
1229+
return undefined;
1230+
} else if (cachedTemplate) {
1231+
return cachedTemplate;
12371232
}
12381233
}
12391234
return findTemplate;
12401235
}
12411236

1242-
private async getOwnerPullRequestTemplates(owner: string): Promise<string | undefined> {
1237+
private async getOwnerPullRequestTemplate(owner: string): Promise<string | undefined> {
12431238
const githubRepository = await this.createGitHubRepositoryFromOwnerName(owner, '.github');
12441239
if (!githubRepository) {
12451240
return undefined;
12461241
}
1247-
const templates = await githubRepository.getOwnerPullRequestTemplates();
1242+
const templates = await githubRepository.getPullRequestTemplates();
12481243
if (templates && templates?.length > 0) {
12491244
return templates[0];
12501245
}
12511246
}
12521247

1253-
private async getPullRequestTemplates(): Promise<vscode.Uri[]> {
1254-
/**
1255-
* Places a PR template can be:
1256-
* - At the root, the docs folder, or the.github folder, named pull_request_template.md or PULL_REQUEST_TEMPLATE.md
1257-
* - At the same folder locations under a PULL_REQUEST_TEMPLATE folder with any name
1258-
*/
1259-
const pattern1 = '{pull_request_template,PULL_REQUEST_TEMPLATE}.{md,txt}';
1260-
const templatesPattern1 = vscode.workspace.findFiles(
1261-
new vscode.RelativePattern(this._repository.rootUri, pattern1)
1262-
);
1263-
1264-
const pattern2 = '{docs,.github}/{pull_request_template,PULL_REQUEST_TEMPLATE}.{md,txt}';
1265-
const templatesPattern2 = vscode.workspace.findFiles(
1266-
new vscode.RelativePattern(this._repository.rootUri, pattern2), null
1267-
);
1268-
1269-
const pattern3 = '{pull_request_template,PULL_REQUEST_TEMPLATE}';
1270-
const templatesPattern3 = vscode.workspace.findFiles(
1271-
new vscode.RelativePattern(this._repository.rootUri, pattern3)
1272-
);
1273-
1274-
const pattern4 = '{docs,.github}/{pull_request_template,PULL_REQUEST_TEMPLATE}';
1275-
const templatesPattern4 = vscode.workspace.findFiles(
1276-
new vscode.RelativePattern(this._repository.rootUri, pattern4), null
1277-
);
1278-
1279-
const pattern5 = 'PULL_REQUEST_TEMPLATE/*.md';
1280-
const templatesPattern5 = vscode.workspace.findFiles(
1281-
new vscode.RelativePattern(this._repository.rootUri, pattern5)
1282-
);
1283-
1284-
const pattern6 = '{docs,.github}/PULL_REQUEST_TEMPLATE/*.md';
1285-
const templatesPattern6 = vscode.workspace.findFiles(
1286-
new vscode.RelativePattern(this._repository.rootUri, pattern6), null
1287-
);
1288-
1289-
const allResults = await Promise.all([templatesPattern1, templatesPattern2, templatesPattern3, templatesPattern4, templatesPattern5, templatesPattern6]);
1290-
1291-
const result = [...allResults[0], ...allResults[1], ...allResults[2], ...allResults[3], ...allResults[4], ...allResults[5]];
1292-
return result;
1248+
private async getPullRequestTemplate(owner: string): Promise<string | undefined> {
1249+
const repository = this.gitHubRepositories.find(repo => repo.remote.owner === owner);
1250+
if (!repository) {
1251+
return;
1252+
}
1253+
const templates = await repository.getPullRequestTemplates();
1254+
return templates ? templates[0] : undefined;
12931255
}
12941256

12951257
async getPullRequestDefaults(branch?: Branch): Promise<PullRequestDefaults> {

src/github/githubRepository.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ export class GitHubRepository implements vscode.Disposable {
426426
return 'master';
427427
}
428428

429-
async getOwnerPullRequestTemplates(): Promise<string[] | undefined> {
429+
async getPullRequestTemplates(): Promise<string[] | undefined> {
430430
try {
431431
Logger.debug('Fetch pull request templates - enter', GitHubRepository.ID);
432432
const { query, remote, schema } = await this.ensure();

0 commit comments

Comments
 (0)