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

Commit 8e968cc

Browse files
committed
Show alert when no GitHub URL in clipboard
1 parent dbcb792 commit 8e968cc

File tree

12 files changed

+99
-28
lines changed

12 files changed

+99
-28
lines changed

src/GitHub.App/GitHub.App.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,6 @@
224224
<Compile Include="SampleData\PullRequestReviewViewModelDesigner.cs" />
225225
<Compile Include="SampleData\PullRequestUserReviewsViewModelDesigner.cs" />
226226
<Compile Include="Services\EnterpriseCapabilitiesService.cs" />
227-
<Compile Include="Services\GitHubContext.cs" />
228227
<Compile Include="Services\GitHubContextService.cs" />
229228
<Compile Include="Services\GlobalConnection.cs" />
230229
<Compile Include="Services\RepositoryForkService.cs" />

src/GitHub.App/Services/GitHubContextService.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
using System.IO;
33
using System.Text;
44
using System.Linq;
5+
using System.Windows;
56
using System.Collections.Generic;
67
using System.ComponentModel.Composition;
78
using System.Text.RegularExpressions;
89
using System.Runtime.InteropServices;
10+
using GitHub.Services;
911
using GitHub.Primitives;
1012
using Microsoft.VisualStudio;
1113
using Microsoft.VisualStudio.Shell;
@@ -14,8 +16,8 @@
1416

1517
namespace GitHub.App.Services
1618
{
17-
[Export(typeof(GitHubContextService))]
18-
public class GitHubContextService
19+
[Export(typeof(IGitHubContextService))]
20+
public class GitHubContextService : IGitHubContextService
1921
{
2022
readonly IServiceProvider serviceProvider;
2123

@@ -56,6 +58,12 @@ public GitHubContextService([Import(typeof(SVsServiceProvider))] IServiceProvide
5658
this.serviceProvider = serviceProvider;
5759
}
5860

61+
public GitHubContext FindContextFromClipboard()
62+
{
63+
var text = Clipboard.GetText(TextDataFormat.Text);
64+
return FindContextFromUrl(text);
65+
}
66+
5967
public GitHubContext FindContextFromUrl(string url)
6068
{
6169
var uri = new UriString(url);

src/GitHub.Exports/GitHub.Exports.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,9 @@
180180
<Compile Include="Models\PullRequestReviewCommentModel.cs" />
181181
<Compile Include="Models\PullRequestReviewModel.cs" />
182182
<Compile Include="Models\PullRequestReviewThreadModel.cs" />
183+
<Compile Include="Services\GitHubContext.cs" />
183184
<Compile Include="Services\IEnterpriseCapabilitiesService.cs" />
185+
<Compile Include="Services\IGitHubContextService.cs" />
184186
<Compile Include="Services\IGlobalConnection.cs" />
185187
<Compile Include="Services\ILocalRepositories.cs" />
186188
<Compile Include="Services\ITeamExplorerContext.cs" />

src/GitHub.App/Services/GitHubContext.cs renamed to src/GitHub.Exports/Services/GitHubContext.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
2-
3-
namespace GitHub.App.Services
1+
namespace GitHub.Services
42
{
53
public class GitHubContext
64
{
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace GitHub.Services
5+
{
6+
public interface IGitHubContextService
7+
{
8+
GitHubContext FindContextFromClipboard();
9+
GitHubContext FindContextFromBrowser();
10+
GitHubContext FindContextFromUrl(string url);
11+
GitHubContext FindContextFromWindowTitle(string windowTitle);
12+
IEnumerable<string> FindWindowTitlesForClass(string className = "MozillaWindowClass");
13+
string ResolvePath(GitHubContext context);
14+
Uri ToRepositoryUrl(GitHubContext context);
15+
bool TryOpenFile(string repositoryDir, GitHubContext context);
16+
}
17+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
using System.Collections.Generic;
2-
using GitHub.Models;
1+
using Microsoft.VisualStudio;
32

43
namespace GitHub.Services
54
{
65
public interface IVSServices
76
{
87
string VSVersion { get; }
98
bool TryOpenRepository(string directory);
9+
VSConstants.MessageBoxResult ShowMessageBoxInfo(string message);
1010
}
1111
}

src/GitHub.Exports/Services/VSServices.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
using System;
22
using System.ComponentModel.Composition;
3-
using System.Diagnostics;
43
using System.Globalization;
54
using System.IO;
6-
using System.Linq;
7-
using System.Text.RegularExpressions;
85
using GitHub.Logging;
9-
using GitHub.VisualStudio;
106
using Microsoft.VisualStudio;
117
using Microsoft.VisualStudio.Setup.Configuration;
128
using Microsoft.VisualStudio.Shell;
@@ -50,6 +46,12 @@ public string VSVersion
5046
}
5147
}
5248

49+
public VSConstants.MessageBoxResult ShowMessageBoxInfo(string message)
50+
{
51+
return (VSConstants.MessageBoxResult)VsShellUtilities.ShowMessageBox(serviceProvider, message, null,
52+
OLEMSGICON.OLEMSGICON_INFO, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
53+
}
54+
5355
/// <summary>Open a repository in Team Explorer</summary>
5456
/// <remarks>
5557
/// There doesn't appear to be a command that directly opens a target repo.

src/GitHub.VisualStudio/Commands/OpenFromClipboardCommand.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using System;
2-
using System.Windows;
32
using System.ComponentModel.Composition;
43
using GitHub.Commands;
54
using GitHub.Services;
6-
using GitHub.App.Services;
75
using GitHub.Services.Vssdk.Commands;
86
using Task = System.Threading.Tasks.Task;
97

@@ -12,8 +10,11 @@ namespace GitHub.VisualStudio.Commands
1210
[Export(typeof(IOpenFromClipboardCommand))]
1311
public class OpenFromClipboardCommand : VsCommand<string>, IOpenFromClipboardCommand
1412
{
15-
readonly Lazy<GitHubContextService> gitHubContextService;
13+
public const string NoGitHubUrlMessage = "Couldn't a find a GitHub URL in clipboard";
14+
15+
readonly Lazy<IGitHubContextService> gitHubContextService;
1616
readonly Lazy<ITeamExplorerContext> teamExplorerContext;
17+
readonly Lazy<IVSServices> vsServices;
1718

1819
/// <summary>
1920
/// Gets the GUID of the group the command belongs to.
@@ -27,28 +28,25 @@ public class OpenFromClipboardCommand : VsCommand<string>, IOpenFromClipboardCom
2728

2829
[ImportingConstructor]
2930
public OpenFromClipboardCommand(
30-
Lazy<GitHubContextService> gitHubContextService,
31-
Lazy<ITeamExplorerContext> teamExplorerContext)
31+
Lazy<IGitHubContextService> gitHubContextService,
32+
Lazy<ITeamExplorerContext> teamExplorerContext,
33+
Lazy<IVSServices> vsServices)
3234
: base(CommandSet, CommandId)
3335
{
3436
this.gitHubContextService = gitHubContextService;
3537
this.teamExplorerContext = teamExplorerContext;
38+
this.vsServices = vsServices;
3639

3740
// See https://code.msdn.microsoft.com/windowsdesktop/AllowParams-2005-9442298f
3841
ParametersDescription = "u"; // accept a single url
3942
}
4043

4144
public override Task Execute(string url)
4245
{
43-
if (string.IsNullOrEmpty(url))
44-
{
45-
url = Clipboard.GetText(TextDataFormat.Text);
46-
}
47-
48-
var context = gitHubContextService.Value.FindContextFromUrl(url);
46+
var context = gitHubContextService.Value.FindContextFromClipboard();
4947
if (context == null)
5048
{
51-
// Couldn't find URL in clipboard
49+
vsServices.Value.ShowMessageBoxInfo(NoGitHubUrlMessage);
5250
return Task.CompletedTask;
5351
}
5452

src/GitHub.VisualStudio/Commands/OpenFromUrlCommand.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
using System;
22
using System.IO;
3-
using System.Linq;
43
using System.Windows;
54
using System.Threading.Tasks;
65
using System.ComponentModel.Composition;
76
using GitHub.Commands;
87
using GitHub.Services;
9-
using GitHub.App.Services;
108
using GitHub.Services.Vssdk.Commands;
119
using EnvDTE;
1210
using Microsoft.VisualStudio;
@@ -20,7 +18,7 @@ namespace GitHub.VisualStudio.Commands
2018
[Export(typeof(IOpenFromUrlCommand))]
2119
public class OpenFromUrlCommand : VsCommand<string>, IOpenFromUrlCommand
2220
{
23-
readonly Lazy<GitHubContextService> gitHubContextService;
21+
readonly Lazy<IGitHubContextService> gitHubContextService;
2422
readonly Lazy<IRepositoryCloneService> repositoryCloneService;
2523
readonly Lazy<IPullRequestEditorService> pullRequestEditorService;
2624
readonly Lazy<ITeamExplorerContext> teamExplorerContext;
@@ -40,7 +38,7 @@ public class OpenFromUrlCommand : VsCommand<string>, IOpenFromUrlCommand
4038

4139
[ImportingConstructor]
4240
public OpenFromUrlCommand(
43-
Lazy<GitHubContextService> gitHubContextService,
41+
Lazy<IGitHubContextService> gitHubContextService,
4442
Lazy<IRepositoryCloneService> repositoryCloneService,
4543
Lazy<IPullRequestEditorService> pullRequestEditorService,
4644
Lazy<ITeamExplorerContext> teamExplorerContext,

test/GitHub.App.UnitTests/Services/GitHubContextServiceTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using GitHub.Services;
23
using GitHub.App.Services;
34
using NSubstitute;
45
using NUnit.Framework;

0 commit comments

Comments
 (0)