Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit a82390f

Browse files
committed
Add support for opening from topmost browser
If there is no URL in the paste buffer, use the GitHub context from the topmost browser.
1 parent 70d9d90 commit a82390f

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/GitHub.App/Services/GitHubContextService.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
using System.Text;
33
using System.Linq;
44
using System.Collections.Generic;
5+
using System.ComponentModel.Composition;
56
using System.Text.RegularExpressions;
67
using System.Runtime.InteropServices;
78
using GitHub.Primitives;
89

910
namespace GitHub.App.Services
1011
{
12+
[Export(typeof(GitHubContextService))]
1113
public class GitHubContextService
1214
{
1315
// USERID_REGEX = /[a-z0-9][a-z0-9\-\_]*/i
@@ -37,7 +39,7 @@ public GitHubContext FindContextFromUrl(UriString url)
3739
{
3840
Host = url.Host,
3941
Owner = url.Owner,
40-
RepositoryName = url.RepositoryName
42+
RepositoryName = url.RepositoryName,
4143
};
4244
}
4345

src/GitHub.VisualStudio/Commands/OpenFromUrlCommand.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using GitHub.Commands;
77
using GitHub.Services;
88
using GitHub.Primitives;
9+
using GitHub.App.Services;
910
using GitHub.Services.Vssdk.Commands;
1011
using EnvDTE;
1112
using Microsoft.VisualStudio;
@@ -15,6 +16,7 @@ namespace GitHub.VisualStudio.Commands
1516
[Export(typeof(IOpenFromUrlCommand))]
1617
public class OpenFromUrlCommand : VsCommand<string>, IOpenFromUrlCommand
1718
{
19+
readonly Lazy<GitHubContextService> gitHubContextService;
1820
readonly Lazy<IRepositoryCloneService> repositoryCloneService;
1921
readonly Lazy<IPullRequestEditorService> pullRequestEditorService;
2022
readonly Lazy<IGitHubToolWindowManager> gitHubToolWindowManager;
@@ -32,11 +34,13 @@ public class OpenFromUrlCommand : VsCommand<string>, IOpenFromUrlCommand
3234

3335
[ImportingConstructor]
3436
public OpenFromUrlCommand(
37+
Lazy<GitHubContextService> gitHubContextService,
3538
Lazy<IRepositoryCloneService> repositoryCloneService,
3639
Lazy<IPullRequestEditorService> pullRequestEditorService,
3740
[Import(typeof(Microsoft.VisualStudio.Shell.SVsServiceProvider))] IServiceProvider sp) :
3841
base(CommandSet, CommandId)
3942
{
43+
this.gitHubContextService = gitHubContextService;
4044
this.repositoryCloneService = repositoryCloneService;
4145
this.pullRequestEditorService = pullRequestEditorService;
4246
dte = new Lazy<DTE>(() => (DTE)sp.GetService(typeof(DTE)));
@@ -56,6 +60,21 @@ public override async Task Execute(string url)
5660

5761
var gitHubUrl = new UriString(url);
5862
if (!gitHubUrl.IsValidUri || !gitHubUrl.IsHypertextTransferProtocol)
63+
{
64+
gitHubUrl = null;
65+
}
66+
67+
if (gitHubUrl == null)
68+
{
69+
// HACK: Reconstruct a GitHub URL from the topmost browser window
70+
var context = gitHubContextService.Value.FindContextFromBrowser();
71+
if (context != null)
72+
{
73+
gitHubUrl = $"https://github.com/{context.Owner}/{context.RepositoryName}";
74+
}
75+
}
76+
77+
if (gitHubUrl == null)
5978
{
6079
return;
6180
}

0 commit comments

Comments
 (0)