Skip to content

Transform issue/pr body to have extension handled urls #6960

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/common/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class Protocol {
public readonly url: vscode.Uri;
constructor(uriString: string) {
if (this.parseSshProtocol(uriString)) {
this.url = vscode.Uri.from({ scheme: 'ssh', authority: this.host, path: `/${this.nameWithOwner}` });
return;
}

Expand Down
8 changes: 4 additions & 4 deletions src/github/folderRepositoryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1181,8 +1181,8 @@ export class FolderRepositoryManager extends Disposable {
const hasMorePages = !!headers.link && headers.link.indexOf('rel="next"') > -1;
const pullRequestResponses = await Promise.all(promises);

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

Logger.debug(`Fetch pull request category ${categoryQuery} - done`, this.id);
Expand Down
16 changes: 8 additions & 8 deletions src/github/githubRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ export class GitHubRepository extends Disposable {
Logger.debug(`Fetch pull requests for branch - done`, this.id);

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

const issues: Issue[] = [];
if (data && data.search.edges) {
data.search.edges.forEach(raw => {
await Promise.all(data.search.edges.map(async raw => {
if (raw.node.id) {
issues.push(parseGraphQLIssue(raw.node, this));
issues.push(await parseGraphQLIssue(raw.node, this));
}
});
}));
}
return {
items: issues,
Expand Down Expand Up @@ -944,7 +944,7 @@ export class GitHubRepository extends Disposable {
if (!data) {
throw new Error('Failed to create pull request.');
}
return this.createOrUpdatePullRequestModel(parseGraphQLPullRequest(data.createPullRequest.pullRequest, this));
return this.createOrUpdatePullRequestModel(await parseGraphQLPullRequest(data.createPullRequest.pullRequest, this));
} catch (e) {
Logger.error(`Unable to create PR: ${e}`, this.id);
throw e;
Expand All @@ -971,7 +971,7 @@ export class GitHubRepository extends Disposable {
if (!data) {
throw new Error('Failed to create revert pull request.');
}
return this.createOrUpdatePullRequestModel(parseGraphQLPullRequest(data.revertPullRequest.revertPullRequest, this));
return this.createOrUpdatePullRequestModel(await parseGraphQLPullRequest(data.revertPullRequest.revertPullRequest, this));
} catch (e) {
Logger.error(`Unable to create revert PR: ${e}`, this.id);
throw e;
Expand All @@ -997,7 +997,7 @@ export class GitHubRepository extends Disposable {
}

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

return new IssueModel(this, remote, parseGraphQLIssue(data.repository.issue, this));
return new IssueModel(this, remote, await parseGraphQLIssue(data.repository.issue, this));
} catch (e) {
Logger.error(`Unable to fetch issue: ${e}`, this.id);
return;
Expand Down
24 changes: 18 additions & 6 deletions src/github/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { Resource } from '../common/resources';
import { GITHUB_ENTERPRISE, OVERRIDE_DEFAULT_BRANCH, PR_SETTINGS_NAMESPACE, URI } from '../common/settingKeys';
import * as Common from '../common/timelineEvent';
import { DataUri, toOpenIssueWebviewUri, toOpenPullRequestWebviewUri } from '../common/uri';
import { gitHubLabelColor, uniqBy } from '../common/utils';
import { gitHubLabelColor, stringReplaceAsync, uniqBy } from '../common/utils';
import { OctokitCommon } from './common';
import { FolderRepositoryManager, PullRequestDefaults } from './folderRepositoryManager';
import { GitHubRepository, ViewerPermission } from './githubRepository';
Expand Down Expand Up @@ -307,6 +307,18 @@ export function convertRESTHeadToIGitHubRef(head: OctokitCommon.PullsListRespons
};
}

async function transformHtmlUrlsToExtensionUrls(body: string, githubRepository: GitHubRepository): Promise<string> {
const issueRegex = new RegExp(
`href="https?:\/\/${githubRepository.remote.gitProtocol.url.authority}\\/${githubRepository.remote.owner}\\/${githubRepository.remote.repositoryName}\\/(issues|pull)\\/([0-9]+)"`);
return stringReplaceAsync(body, issueRegex, async (match: string, issuesOrPull: string, number: string) => {
if (issuesOrPull === 'issues') {
return `href="${(await toOpenIssueWebviewUri({ owner: githubRepository.remote.owner, repo: githubRepository.remote.repositoryName, issueNumber: Number(number) })).toString()}""`;
} else {
return `href="${(await toOpenPullRequestWebviewUri({ owner: githubRepository.remote.owner, repo: githubRepository.remote.repositoryName, pullRequestNumber: Number(number) })).toString()}"`;
}
});
}

export function convertRESTPullRequestToRawPullRequest(
pullRequest:
| OctokitCommon.PullsGetResponseData
Expand Down Expand Up @@ -765,18 +777,18 @@ export function parseMergeability(mergeability: 'UNKNOWN' | 'MERGEABLE' | 'CONFL
return parsed;
}

export function parseGraphQLPullRequest(
export async function parseGraphQLPullRequest(
graphQLPullRequest: GraphQL.PullRequest,
githubRepository: GitHubRepository,
): PullRequest {
): Promise<PullRequest> {
const pr: PullRequest = {
id: graphQLPullRequest.databaseId,
graphNodeId: graphQLPullRequest.id,
url: graphQLPullRequest.url,
number: graphQLPullRequest.number,
state: graphQLPullRequest.state,
body: graphQLPullRequest.body,
bodyHTML: graphQLPullRequest.bodyHTML,
bodyHTML: await transformHtmlUrlsToExtensionUrls(graphQLPullRequest.bodyHTML, githubRepository),
title: graphQLPullRequest.title,
titleHTML: graphQLPullRequest.titleHTML,
createdAt: graphQLPullRequest.createdAt,
Expand Down Expand Up @@ -892,15 +904,15 @@ function parseComments(comments: GraphQL.AbbreviatedIssueComment[] | undefined,
return parsedComments;
}

export function parseGraphQLIssue(issue: GraphQL.Issue, githubRepository: GitHubRepository): Issue {
export async function parseGraphQLIssue(issue: GraphQL.Issue, githubRepository: GitHubRepository): Promise<Issue> {
return {
id: issue.databaseId,
graphNodeId: issue.id,
url: issue.url,
number: issue.number,
state: issue.state,
body: issue.body,
bodyHTML: issue.bodyHTML,
bodyHTML: await transformHtmlUrlsToExtensionUrls(issue.bodyHTML, githubRepository),
title: issue.title,
titleHTML: issue.titleHTML,
createdAt: issue.createdAt,
Expand Down
4 changes: 2 additions & 2 deletions src/test/view/prsTree.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ describe('GitHub Pull Requests view', function () {
);
});
}).pullRequest;
const prItem0 = parseGraphQLPullRequest(pr0.repository.pullRequest, gitHubRepository);
const prItem0 = await parseGraphQLPullRequest(pr0.repository.pullRequest, gitHubRepository);
const pullRequest0 = new PullRequestModel(credentialStore, telemetry, gitHubRepository, remote, prItem0);

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

const repository = new MockRepository();
Expand Down