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

Commit f401897

Browse files
committed
Preserve HTTP scheme for enterprise repositories
Fixes #13 We need to preserve the format of the origin for non github.com repositories (such as GitHub Enterprise instances). Many of them are not set up with HTTPS because they're behind a firewall etc.
1 parent 7cfd1a1 commit f401897

File tree

7 files changed

+71
-32
lines changed

7 files changed

+71
-32
lines changed

src/GitHub.Exports/Services/ITeamExplorerServiceHolder.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ public interface ITeamExplorerServiceHolder
4646
public interface IGitAwareItem
4747
{
4848
IGitRepositoryInfo ActiveRepo { get; }
49+
50+
/// <summary>
51+
/// Represents the web URL of the repository on GitHub.com, even if the origin is an SSH address.
52+
/// </summary>
4953
Uri ActiveRepoUri { get; }
5054
string ActiveRepoName { get; }
5155
}

src/GitHub.Exports/Services/Services.cs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Microsoft.VisualStudio.TeamFoundation.Git.Extensibility;
1313
using GitHub.Models;
1414
using GitHub.Info;
15+
using GitHub.Primitives;
1516

1617
namespace GitHub.VisualStudio
1718
{
@@ -127,7 +128,7 @@ public static Uri GetRepoUrlFromSolution(IVsSolution solution)
127128
return null;
128129
using (var repo = new Repository(repoPath))
129130
{
130-
return GetUriFromRepository(repo);
131+
return GetUriFromRepository(repo)?.ToWebUri();
131132
}
132133
}
133134

@@ -144,21 +145,13 @@ public static Repository GetRepoFromSolution(this IVsSolution solution)
144145
return new Repository(repoPath);
145146
}
146147

147-
public static Uri GetUriFromRepository(Repository repo)
148+
public static UriString GetUriFromRepository(Repository repo)
148149
{
149-
if (repo == null)
150-
return null;
151-
var remote = repo.Network.Remotes.FirstOrDefault(x => x.Name.Equals("origin", StringComparison.Ordinal));
152-
if (remote == null)
153-
return null;
154-
Uri uri;
155-
var url = remote.Url;
156-
// fixup ssh urls
157-
if (url.StartsWith("[email protected]:", StringComparison.Ordinal))
158-
url = url.Replace("[email protected]:", "https://github.com/");
159-
if (!Uri.TryCreate(url, UriKind.Absolute, out uri))
160-
return null;
161-
return uri;
150+
return repo
151+
?.Network
152+
.Remotes
153+
.FirstOrDefault(x => x.Name.Equals("origin", StringComparison.Ordinal))
154+
?.Url;
162155
}
163156

164157
public static Repository GetRepoFromIGit(this IGitRepositoryInfo repoInfo)

src/GitHub.VisualStudio/Base/TeamExplorerGitRepoInfo.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ public IGitRepositoryInfo ActiveRepo
2727
}
2828
}
2929

30+
/// <summary>
31+
/// Represents the web URL of the repository on GitHub.com, even if the origin is an SSH address.
32+
/// </summary>
3033
[AllowNull]
3134
public Uri ActiveRepoUri { [return: AllowNull] get; set; }
3235
public string ActiveRepoName { get; set; }

src/GitHub.VisualStudio/Base/TeamExplorerItemBase.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,13 @@ protected virtual void RepoChanged()
4949
var repo = ActiveRepo;
5050
if (repo != null)
5151
{
52-
var gitRepo = Services.GetRepoFromIGit(repo);
52+
var gitRepo = repo.GetRepoFromIGit();
5353
var uri = Services.GetUriFromRepository(gitRepo);
54-
if (uri != null)
54+
var name = uri.RepositoryName;
55+
if (name != null)
5556
{
56-
var name = uri.GetRepo();
57-
if (name != null)
58-
{
59-
ActiveRepoUri = uri;
60-
ActiveRepoName = ActiveRepoUri.GetUser() + "/" + ActiveRepoUri.GetRepo();
61-
}
57+
ActiveRepoUri = uri.ToWebUri();
58+
ActiveRepoName = uri.NameWithOwner;
6259
}
6360
}
6461
}

src/GitHub.VisualStudio/Base/TeamExplorerNavigationItemBase.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,14 @@ public TeamExplorerNavigationItemBase(ISimpleApiClientFactory apiFactory, ITeamE
2828
IsVisible = false;
2929
IsEnabled = true;
3030

31-
OnThemeChanged();
31+
try
32+
{
33+
OnThemeChanged();
34+
}
35+
catch (ArgumentNullException)
36+
{
37+
// This throws in the unit test runner.
38+
}
3239
VSColorTheme.ThemeChanged += _ =>
3340
{
3441
OnThemeChanged();
@@ -63,17 +70,13 @@ protected void OpenInBrowser(Lazy<IVisualStudioBrowser> browser, string endpoint
6370
{
6471
var uri = ActiveRepoUri;
6572
Debug.Assert(uri != null, "OpenInBrowser: uri should never be null");
73+
#if !DEBUG
6674
if (uri == null)
6775
return;
76+
#endif
77+
var browseUrl = uri.Append(endpoint);
6878

69-
var https = uri.ToHttps();
70-
if (https == null)
71-
return;
72-
73-
if (!Uri.TryCreate(https.ToString() + "/" + endpoint, UriKind.Absolute, out uri))
74-
return;
75-
76-
OpenInBrowser(browser, uri);
79+
OpenInBrowser(browser, browseUrl);
7780
}
7881

7982
void Unsubscribe()
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using GitHub.Api;
3+
using GitHub.Services;
4+
using GitHub.VisualStudio.TeamExplorer.Home;
5+
using NSubstitute;
6+
using Xunit;
7+
8+
public class GraphsNavigationItemTests
9+
{
10+
public class TheExecuteMethod
11+
{
12+
[Theory]
13+
[InlineData("https://github.com/foo/bar", "https://github.com/foo/bar/graphs")]
14+
[InlineData("https://github.com/foo/bar/", "https://github.com/foo/bar/graphs")]
15+
public void BrowsesToTheCorrectURL(string origin, string expectedUrl)
16+
{
17+
var apiFactory = Substitute.For<ISimpleApiClientFactory>();
18+
var browser = Substitute.For<IVisualStudioBrowser>();
19+
var lazyBrowser = new Lazy<IVisualStudioBrowser>(() => browser);
20+
var holder = Substitute.For<ITeamExplorerServiceHolder>();
21+
var graphsNavigationItem = new GraphsNavigationItem(apiFactory, lazyBrowser, holder)
22+
{
23+
ActiveRepoUri = new Uri(origin)
24+
};
25+
26+
graphsNavigationItem.Execute();
27+
28+
browser.Received().OpenUrl(new Uri(expectedUrl));
29+
}
30+
}
31+
}

src/UnitTests/UnitTests.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,17 @@
4242
<HintPath>..\..\packages\Rx-Testing.2.2.5-custom\lib\net45\Microsoft.Reactive.Testing.dll</HintPath>
4343
<Private>True</Private>
4444
</Reference>
45+
<Reference Include="Microsoft.TeamFoundation.Controls, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
46+
<SpecificVersion>False</SpecificVersion>
47+
<HintPath>..\..\lib\Microsoft.TeamFoundation.Controls.dll</HintPath>
48+
</Reference>
4549
<Reference Include="Microsoft.TeamFoundation.Git.Controls, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
4650
<SpecificVersion>False</SpecificVersion>
4751
<HintPath>..\..\lib\Microsoft.TeamFoundation.Git.Controls.dll</HintPath>
4852
</Reference>
53+
<Reference Include="Microsoft.TeamFoundation.Git.Provider">
54+
<HintPath>..\..\lib\Microsoft.TeamFoundation.Git.Provider.dll</HintPath>
55+
</Reference>
4956
<Reference Include="Microsoft.VisualStudio.ComponentModelHost, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
5057
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
5158
<Reference Include="Microsoft.VisualStudio.Text.Data, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
@@ -147,6 +154,7 @@
147154
<Compile Include="GitHub.UI\TwoFactorInputTests.cs" />
148155
<Compile Include="GitHub.VisualStudio\Services\ConnectionManagerTests.cs" />
149156
<Compile Include="GitHub.VisualStudio\Services\RepositoryPublishServiceTests.cs" />
157+
<Compile Include="GitHub.VisualStudio\TeamExplorer\Home\GraphsNavigationItemTests.cs" />
150158
<Compile Include="GitHubPackageTests.cs" />
151159
<Compile Include="Helpers\CommandTestHelpers.cs" />
152160
<Compile Include="Helpers\LazySubstitute.cs" />

0 commit comments

Comments
 (0)