Skip to content

Commit 6a3bf4e

Browse files
authored
Transform issue/pr body to have extension handled urls (microsoft#6960)
* Transform issue/pr body to have extension handled urls * Why wasn't this caught by the compiler? * Fix test
1 parent aa8a663 commit 6a3bf4e

File tree

5 files changed

+33
-20
lines changed

5 files changed

+33
-20
lines changed

src/common/protocol.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export class Protocol {
3232
public readonly url: vscode.Uri;
3333
constructor(uriString: string) {
3434
if (this.parseSshProtocol(uriString)) {
35+
this.url = vscode.Uri.from({ scheme: 'ssh', authority: this.host, path: `/${this.nameWithOwner}` });
3536
return;
3637
}
3738

src/github/folderRepositoryManager.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,8 +1181,8 @@ export class FolderRepositoryManager extends Disposable {
11811181
const hasMorePages = !!headers.link && headers.link.indexOf('rel="next"') > -1;
11821182
const pullRequestResponses = await Promise.all(promises);
11831183

1184-
const pullRequests = pullRequestResponses
1185-
.map(response => {
1184+
const pullRequests = (await Promise.all(pullRequestResponses
1185+
.map(async response => {
11861186
if (!response?.data.repository) {
11871187
Logger.appendLine('Pull request doesn\'t appear to exist.', this.id);
11881188
return null;
@@ -1191,9 +1191,9 @@ export class FolderRepositoryManager extends Disposable {
11911191
// Pull requests fetched with a query can be from any repo.
11921192
// We need to use the correct GitHubRepository for this PR.
11931193
return response.repo.createOrUpdatePullRequestModel(
1194-
parseGraphQLPullRequest(response.data.repository.pullRequest, response.repo),
1194+
await parseGraphQLPullRequest(response.data.repository.pullRequest, response.repo),
11951195
);
1196-
})
1196+
})))
11971197
.filter(item => item !== null) as PullRequestModel[];
11981198

11991199
Logger.debug(`Fetch pull request category ${categoryQuery} - done`, this.id);

src/github/githubRepository.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ export class GitHubRepository extends Disposable {
620620
Logger.debug(`Fetch pull requests for branch - done`, this.id);
621621

622622
if (data?.repository && data.repository.pullRequests.nodes.length > 0) {
623-
const prs = data.repository.pullRequests.nodes.map(node => parseGraphQLPullRequest(node, this)).filter(pr => pr.head?.repo.owner === headOwner);
623+
const prs = (await Promise.all(data.repository.pullRequests.nodes.map(node => parseGraphQLPullRequest(node, this)))).filter(pr => pr.head?.repo.owner === headOwner);
624624
if (prs.length === 0) {
625625
return undefined;
626626
}
@@ -775,11 +775,11 @@ export class GitHubRepository extends Disposable {
775775

776776
const issues: Issue[] = [];
777777
if (data && data.search.edges) {
778-
data.search.edges.forEach(raw => {
778+
await Promise.all(data.search.edges.map(async raw => {
779779
if (raw.node.id) {
780-
issues.push(parseGraphQLIssue(raw.node, this));
780+
issues.push(await parseGraphQLIssue(raw.node, this));
781781
}
782-
});
782+
}));
783783
}
784784
return {
785785
items: issues,
@@ -944,7 +944,7 @@ export class GitHubRepository extends Disposable {
944944
if (!data) {
945945
throw new Error('Failed to create pull request.');
946946
}
947-
return this.createOrUpdatePullRequestModel(parseGraphQLPullRequest(data.createPullRequest.pullRequest, this));
947+
return this.createOrUpdatePullRequestModel(await parseGraphQLPullRequest(data.createPullRequest.pullRequest, this));
948948
} catch (e) {
949949
Logger.error(`Unable to create PR: ${e}`, this.id);
950950
throw e;
@@ -971,7 +971,7 @@ export class GitHubRepository extends Disposable {
971971
if (!data) {
972972
throw new Error('Failed to create revert pull request.');
973973
}
974-
return this.createOrUpdatePullRequestModel(parseGraphQLPullRequest(data.revertPullRequest.revertPullRequest, this));
974+
return this.createOrUpdatePullRequestModel(await parseGraphQLPullRequest(data.revertPullRequest.revertPullRequest, this));
975975
} catch (e) {
976976
Logger.error(`Unable to create revert PR: ${e}`, this.id);
977977
throw e;
@@ -997,7 +997,7 @@ export class GitHubRepository extends Disposable {
997997
}
998998

999999
Logger.debug(`Fetch pull request ${id} - done`, this.id);
1000-
return this.createOrUpdatePullRequestModel(parseGraphQLPullRequest(data.repository.pullRequest, this));
1000+
return this.createOrUpdatePullRequestModel(await parseGraphQLPullRequest(data.repository.pullRequest, this));
10011001
} catch (e) {
10021002
Logger.error(`Unable to fetch PR: ${e}`, this.id);
10031003
return;
@@ -1024,7 +1024,7 @@ export class GitHubRepository extends Disposable {
10241024
}
10251025
Logger.debug(`Fetch issue ${id} - done`, this.id);
10261026

1027-
return new IssueModel(this, remote, parseGraphQLIssue(data.repository.issue, this));
1027+
return new IssueModel(this, remote, await parseGraphQLIssue(data.repository.issue, this));
10281028
} catch (e) {
10291029
Logger.error(`Unable to fetch issue: ${e}`, this.id);
10301030
return;

src/github/utils.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { Resource } from '../common/resources';
1919
import { GITHUB_ENTERPRISE, OVERRIDE_DEFAULT_BRANCH, PR_SETTINGS_NAMESPACE, URI } from '../common/settingKeys';
2020
import * as Common from '../common/timelineEvent';
2121
import { DataUri, toOpenIssueWebviewUri, toOpenPullRequestWebviewUri } from '../common/uri';
22-
import { gitHubLabelColor, uniqBy } from '../common/utils';
22+
import { gitHubLabelColor, stringReplaceAsync, uniqBy } from '../common/utils';
2323
import { OctokitCommon } from './common';
2424
import { FolderRepositoryManager, PullRequestDefaults } from './folderRepositoryManager';
2525
import { GitHubRepository, ViewerPermission } from './githubRepository';
@@ -307,6 +307,18 @@ export function convertRESTHeadToIGitHubRef(head: OctokitCommon.PullsListRespons
307307
};
308308
}
309309

310+
async function transformHtmlUrlsToExtensionUrls(body: string, githubRepository: GitHubRepository): Promise<string> {
311+
const issueRegex = new RegExp(
312+
`href="https?:\/\/${githubRepository.remote.gitProtocol.url.authority}\\/${githubRepository.remote.owner}\\/${githubRepository.remote.repositoryName}\\/(issues|pull)\\/([0-9]+)"`);
313+
return stringReplaceAsync(body, issueRegex, async (match: string, issuesOrPull: string, number: string) => {
314+
if (issuesOrPull === 'issues') {
315+
return `href="${(await toOpenIssueWebviewUri({ owner: githubRepository.remote.owner, repo: githubRepository.remote.repositoryName, issueNumber: Number(number) })).toString()}""`;
316+
} else {
317+
return `href="${(await toOpenPullRequestWebviewUri({ owner: githubRepository.remote.owner, repo: githubRepository.remote.repositoryName, pullRequestNumber: Number(number) })).toString()}"`;
318+
}
319+
});
320+
}
321+
310322
export function convertRESTPullRequestToRawPullRequest(
311323
pullRequest:
312324
| OctokitCommon.PullsGetResponseData
@@ -765,18 +777,18 @@ export function parseMergeability(mergeability: 'UNKNOWN' | 'MERGEABLE' | 'CONFL
765777
return parsed;
766778
}
767779

768-
export function parseGraphQLPullRequest(
780+
export async function parseGraphQLPullRequest(
769781
graphQLPullRequest: GraphQL.PullRequest,
770782
githubRepository: GitHubRepository,
771-
): PullRequest {
783+
): Promise<PullRequest> {
772784
const pr: PullRequest = {
773785
id: graphQLPullRequest.databaseId,
774786
graphNodeId: graphQLPullRequest.id,
775787
url: graphQLPullRequest.url,
776788
number: graphQLPullRequest.number,
777789
state: graphQLPullRequest.state,
778790
body: graphQLPullRequest.body,
779-
bodyHTML: graphQLPullRequest.bodyHTML,
791+
bodyHTML: await transformHtmlUrlsToExtensionUrls(graphQLPullRequest.bodyHTML, githubRepository),
780792
title: graphQLPullRequest.title,
781793
titleHTML: graphQLPullRequest.titleHTML,
782794
createdAt: graphQLPullRequest.createdAt,
@@ -892,15 +904,15 @@ function parseComments(comments: GraphQL.AbbreviatedIssueComment[] | undefined,
892904
return parsedComments;
893905
}
894906

895-
export function parseGraphQLIssue(issue: GraphQL.Issue, githubRepository: GitHubRepository): Issue {
907+
export async function parseGraphQLIssue(issue: GraphQL.Issue, githubRepository: GitHubRepository): Promise<Issue> {
896908
return {
897909
id: issue.databaseId,
898910
graphNodeId: issue.id,
899911
url: issue.url,
900912
number: issue.number,
901913
state: issue.state,
902914
body: issue.body,
903-
bodyHTML: issue.bodyHTML,
915+
bodyHTML: await transformHtmlUrlsToExtensionUrls(issue.bodyHTML, githubRepository),
904916
title: issue.title,
905917
titleHTML: issue.titleHTML,
906918
createdAt: issue.createdAt,

src/test/view/prsTree.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ describe('GitHub Pull Requests view', function () {
151151
);
152152
});
153153
}).pullRequest;
154-
const prItem0 = parseGraphQLPullRequest(pr0.repository.pullRequest, gitHubRepository);
154+
const prItem0 = await parseGraphQLPullRequest(pr0.repository.pullRequest, gitHubRepository);
155155
const pullRequest0 = new PullRequestModel(credentialStore, telemetry, gitHubRepository, remote, prItem0);
156156

157157
const pr1 = gitHubRepository.addGraphQLPullRequest(builder => {
@@ -168,7 +168,7 @@ describe('GitHub Pull Requests view', function () {
168168
);
169169
});
170170
}).pullRequest;
171-
const prItem1 = parseGraphQLPullRequest(pr1.repository.pullRequest, gitHubRepository);
171+
const prItem1 = await parseGraphQLPullRequest(pr1.repository.pullRequest, gitHubRepository);
172172
const pullRequest1 = new PullRequestModel(credentialStore, telemetry, gitHubRepository, remote, prItem1);
173173

174174
const repository = new MockRepository();

0 commit comments

Comments
 (0)