|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as vscode from 'vscode'; |
| 7 | +import { IssueOverviewPanel } from './issueOverview'; |
| 8 | +import { PullRequestOverviewPanel } from './pullRequestOverview'; |
| 9 | +import { RepositoriesManager } from './repositoriesManager'; |
| 10 | +import { parseGitHubIssueOrPullRequestUri } from '../common/externalUri'; |
| 11 | +import { ITelemetry } from '../common/telemetry'; |
| 12 | +import { EXTENSION_ID } from '../constants'; |
| 13 | + |
| 14 | +export function registerGitHubIssueOrPullRequestExternalUriOpener( |
| 15 | + extensionUri: vscode.Uri, |
| 16 | + repositoriesManager: RepositoriesManager, |
| 17 | + telemetry: ITelemetry, |
| 18 | +): vscode.Disposable { |
| 19 | + return vscode.window.registerExternalUriOpener(`${EXTENSION_ID}.issueOrPullRequest`, { |
| 20 | + canOpenExternalUri(uri) { |
| 21 | + if (!parseGitHubIssueOrPullRequestUri(uri)) { |
| 22 | + return vscode.ExternalUriOpenerPriority.None; |
| 23 | + } |
| 24 | + return vscode.ExternalUriOpenerPriority.Preferred; |
| 25 | + }, |
| 26 | + async openExternalUri(_resolvedUri, openContext, token) { |
| 27 | + const identity = parseGitHubIssueOrPullRequestUri(openContext.sourceUri); |
| 28 | + if (!identity || token.isCancellationRequested) { |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + const folderRepositoryManager = repositoriesManager.getManagerForRepository(identity.owner, identity.repo) |
| 33 | + ?? repositoriesManager.folderManagers[0]; |
| 34 | + if (!folderRepositoryManager) { |
| 35 | + await vscode.window.showErrorMessage(vscode.l10n.t('Unable to open issue or pull request #{0}: no GitHub repository is available.', identity.number)); |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + if (identity.kind === 'pullRequest') { |
| 40 | + const pullRequest = await folderRepositoryManager.resolvePullRequest(identity.owner, identity.repo, identity.number, true); |
| 41 | + if (token.isCancellationRequested) { |
| 42 | + return; |
| 43 | + } |
| 44 | + if (!pullRequest) { |
| 45 | + await vscode.window.showErrorMessage(vscode.l10n.t('Unable to find pull request #{0} in {1}/{2}.', identity.number, identity.owner, identity.repo)); |
| 46 | + return; |
| 47 | + } |
| 48 | + await PullRequestOverviewPanel.createOrShow( |
| 49 | + telemetry, |
| 50 | + extensionUri, |
| 51 | + folderRepositoryManager, |
| 52 | + identity, |
| 53 | + pullRequest, |
| 54 | + ); |
| 55 | + } else { |
| 56 | + const issue = await folderRepositoryManager.resolveIssue(identity.owner, identity.repo, identity.number, true, true); |
| 57 | + if (token.isCancellationRequested) { |
| 58 | + return; |
| 59 | + } |
| 60 | + if (!issue) { |
| 61 | + await vscode.window.showErrorMessage(vscode.l10n.t('Unable to find issue #{0} in {1}/{2}.', identity.number, identity.owner, identity.repo)); |
| 62 | + return; |
| 63 | + } |
| 64 | + await IssueOverviewPanel.createOrShow( |
| 65 | + telemetry, |
| 66 | + extensionUri, |
| 67 | + folderRepositoryManager, |
| 68 | + identity, |
| 69 | + issue, |
| 70 | + ); |
| 71 | + } |
| 72 | + }, |
| 73 | + }, { |
| 74 | + schemes: ['http', 'https'], |
| 75 | + label: vscode.l10n.t('Open GitHub Issue or Pull Request'), |
| 76 | + }); |
| 77 | +} |
0 commit comments