From 9bb633aaf79329f3f42812ce5fed241dd67776c2 Mon Sep 17 00:00:00 2001 From: Ladislau Szomoru <3372902+lszomoru@users.noreply.github.com> Date: Fri, 10 Jan 2025 13:52:01 +0100 Subject: [PATCH] GitHub - add "Open on GitHub" action to the SCM graph (#237635) --- extensions/github/package.json | 22 ++++++++++-- extensions/github/src/commands.ts | 34 +++++++++++++++++-- extensions/github/tsconfig.json | 1 + .../contrib/scm/browser/scmHistoryViewPane.ts | 3 +- 4 files changed, 55 insertions(+), 5 deletions(-) diff --git a/extensions/github/package.json b/extensions/github/package.json index f99a41d5979..5ad7b749348 100644 --- a/extensions/github/package.json +++ b/extensions/github/package.json @@ -27,9 +27,11 @@ } }, "enabledApiProposals": [ - "contribShareMenu", - "contribEditSessions", "canonicalUriProvider", + "contribEditSessions", + "contribShareMenu", + "contribSourceControlHistoryItemMenu", + "scmHistoryProvider", "shareProvider" ], "contributes": { @@ -54,6 +56,11 @@ "command": "github.openOnVscodeDev", "title": "Open in vscode.dev", "icon": "$(globe)" + }, + { + "command": "github.openOnGitHub2", + "title": "Open on GitHub", + "icon": "$(github)" } ], "continueEditSession": [ @@ -71,6 +78,10 @@ "command": "github.publish", "when": "git-base.gitEnabled && workspaceFolderCount != 0 && remoteName != 'codespaces'" }, + { + "command": "github.openOnGitHub2", + "when": "false" + }, { "command": "github.copyVscodeDevLink", "when": "false" @@ -127,6 +138,13 @@ "when": "github.hasGitHubRepo && resourceScheme != untitled && remoteName != 'codespaces'", "group": "0_vscode@0" } + ], + "scm/historyItem/context": [ + { + "command": "github.openOnGitHub2", + "when": "github.hasGitHubRepo", + "group": "0_view@2" + } ] }, "configuration": [ diff --git a/extensions/github/src/commands.ts b/extensions/github/src/commands.ts index 2fc47e096f6..0889628cf0d 100644 --- a/extensions/github/src/commands.ts +++ b/extensions/github/src/commands.ts @@ -4,9 +4,9 @@ *--------------------------------------------------------------------------------------------*/ import * as vscode from 'vscode'; -import { API as GitAPI } from './typings/git'; +import { API as GitAPI, RefType } from './typings/git'; import { publishRepository } from './publish'; -import { DisposableStore } from './util'; +import { DisposableStore, getRepositoryFromUrl } from './util'; import { LinkContext, getCommitLink, getLink, getVscodeDevHost } from './links'; async function copyVscodeDevLink(gitAPI: GitAPI, useSelection: boolean, context: LinkContext, includeRange = true) { @@ -62,6 +62,36 @@ export function registerCommands(gitAPI: GitAPI): vscode.Disposable { vscode.env.openExternal(vscode.Uri.parse(link)); })); + disposables.add(vscode.commands.registerCommand('github.openOnGitHub2', async (repository: vscode.SourceControl, historyItem: vscode.SourceControlHistoryItem) => { + if (!repository || !historyItem) { + return; + } + + const apiRepository = gitAPI.repositories.find(r => r.rootUri.fsPath === repository.rootUri?.fsPath); + if (!apiRepository) { + return; + } + + // Get the unique remotes that contain the commit + const branches = await apiRepository.getBranches({ contains: historyItem.id }); + const remoteNames = new Set(branches.filter(b => b.type === RefType.RemoteHead && b.remote).map(b => b.remote!)); + + // GitHub remotes that contain the commit + const remotes = apiRepository.state.remotes + .filter(r => remoteNames.has(r.name) && r.fetchUrl && getRepositoryFromUrl(r.fetchUrl)); + + if (remotes.length === 0) { + vscode.window.showInformationMessage(vscode.l10n.t('No GitHub remotes found that contain this commit.')); + return; + } + + // Default remote (origin, or the first remote) + const defaultRemote = remotes.find(r => r.name === 'origin') ?? remotes[0]; + + const link = getCommitLink(defaultRemote.fetchUrl!, historyItem.id); + vscode.env.openExternal(vscode.Uri.parse(link)); + })); + disposables.add(vscode.commands.registerCommand('github.openOnVscodeDev', async () => { return openVscodeDevLink(gitAPI); })); diff --git a/extensions/github/tsconfig.json b/extensions/github/tsconfig.json index 452c74b8bc4..6b9a6e7db0a 100644 --- a/extensions/github/tsconfig.json +++ b/extensions/github/tsconfig.json @@ -11,6 +11,7 @@ "src/**/*", "../../src/vscode-dts/vscode.d.ts", "../../src/vscode-dts/vscode.proposed.canonicalUriProvider.d.ts", + "../../src/vscode-dts/vscode.proposed.scmHistoryProvider.d.ts", "../../src/vscode-dts/vscode.proposed.shareProvider.d.ts" ] } diff --git a/src/vs/workbench/contrib/scm/browser/scmHistoryViewPane.ts b/src/vs/workbench/contrib/scm/browser/scmHistoryViewPane.ts index 1c3f71ac1ca..436a0276295 100644 --- a/src/vs/workbench/contrib/scm/browser/scmHistoryViewPane.ts +++ b/src/vs/workbench/contrib/scm/browser/scmHistoryViewPane.ts @@ -243,8 +243,9 @@ registerAction2(class extends Action2 { menu: [ { id: MenuId.SCMChangesContext, + when: ContextKeyExpr.equals('config.multiDiffEditor.experimental.enabled', true), group: '0_view', - when: ContextKeyExpr.equals('config.multiDiffEditor.experimental.enabled', true) + order: 1 } ] });