diff --git a/src/GitHub.App/Services/DialogService.cs b/src/GitHub.App/Services/DialogService.cs index e3f2c40f95..1bf666c7d2 100644 --- a/src/GitHub.App/Services/DialogService.cs +++ b/src/GitHub.App/Services/DialogService.cs @@ -2,6 +2,7 @@ using System.ComponentModel.Composition; using System.Threading.Tasks; using GitHub.Api; +using GitHub.Exports; using GitHub.Extensions; using GitHub.Factories; using GitHub.Models; @@ -38,7 +39,13 @@ public async Task ShowCloneDialog(IConnection connection, str if (string.IsNullOrEmpty(url)) { var clipboardContext = gitHubContextService.FindContextFromClipboard(); - url = clipboardContext?.Url; + switch (clipboardContext?.LinkType) + { + case LinkType.Blob: + case LinkType.Repository: + url = clipboardContext?.Url; + break; + } } var viewModel = factory.CreateViewModel(); diff --git a/src/GitHub.App/Services/GitHubContextService.cs b/src/GitHub.App/Services/GitHubContextService.cs index 8891995f31..4cbae0167b 100644 --- a/src/GitHub.App/Services/GitHubContextService.cs +++ b/src/GitHub.App/Services/GitHubContextService.cs @@ -124,7 +124,27 @@ public GitHubContext FindContextFromUrl(string url) Url = uri }; - var repositoryPrefix = uri.ToRepositoryUrl().ToString() + "/"; + if (uri.Owner == null) + { + context.LinkType = LinkType.Unknown; + return context; + } + + if (uri.RepositoryName == null) + { + context.LinkType = LinkType.Unknown; + return context; + } + + var repositoryUrl = uri.ToRepositoryUrl().ToString(); + if (string.Equals(url, repositoryUrl, StringComparison.OrdinalIgnoreCase) || + string.Equals(url, repositoryUrl + ".git", StringComparison.OrdinalIgnoreCase)) + { + context.LinkType = LinkType.Repository; + return context; + } + + var repositoryPrefix = repositoryUrl + "/"; if (!url.StartsWith(repositoryPrefix, StringComparison.OrdinalIgnoreCase)) { return context; diff --git a/src/GitHub.Exports/Exports/ExportMetadata.cs b/src/GitHub.Exports/Exports/ExportMetadata.cs index bdab361eb2..74ecf29ba3 100644 --- a/src/GitHub.Exports/Exports/ExportMetadata.cs +++ b/src/GitHub.Exports/Exports/ExportMetadata.cs @@ -21,7 +21,8 @@ public enum LinkType { Unknown, Blob, - Blame + Blame, + Repository } /// diff --git a/test/GitHub.App.UnitTests/Services/GitHubContextServiceTests.cs b/test/GitHub.App.UnitTests/Services/GitHubContextServiceTests.cs index 5ebf5458ed..3444bd1141 100644 --- a/test/GitHub.App.UnitTests/Services/GitHubContextServiceTests.cs +++ b/test/GitHub.App.UnitTests/Services/GitHubContextServiceTests.cs @@ -133,7 +133,12 @@ public void Url_EqualTo(string url, string expectUrl) Assert.That(context.Url?.ToString(), Is.EqualTo(expectUrl)); } + [TestCase("https://github.com/github/VisualStudio", LinkType.Repository)] + [TestCase("https://github.com/github/VisualStudio.git", LinkType.Repository)] + [TestCase("https://github.com/github/VisualStudio/unknown/master/README.md", LinkType.Unknown)] [TestCase("https://github.com/github/VisualStudio/blob/master/README.md", LinkType.Blob)] + [TestCase("https://github.com", LinkType.Unknown)] + [TestCase("https://github.com/github", LinkType.Unknown)] [TestCase("https://github.com/github/VisualStudio/unknown/master/README.md", LinkType.Unknown)] public void LinkType_EqualTo(string url, LinkType expectLinkType) {