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

Commit 057e8cf

Browse files
committed
Merge pull request #122 from github/shana/121-url-conversion-should-never-throw
ToRepositoryUrl should never throw, it's a conversion helper
2 parents 8b134ab + f6bd5b6 commit 057e8cf

File tree

5 files changed

+66
-4
lines changed

5 files changed

+66
-4
lines changed

src/GitHub.Exports/Primitives/UriString.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ bool ParseScpSyntax(string scpString)
108108
Host = match.Groups["host"].Value.ToNullIfEmpty();
109109
Owner = match.Groups["owner"].Value.ToNullIfEmpty();
110110
RepositoryName = GetRepositoryName(match.Groups["repo"].Value);
111+
IsScpUri = true;
111112
return true;
112113
}
113114
return false;
@@ -123,15 +124,18 @@ bool ParseScpSyntax(string scpString)
123124

124125
public bool IsFileUri { get; private set; }
125126

127+
public bool IsScpUri { get; private set; }
128+
126129
public bool IsValidUri => url != null;
127130

128131
/// <summary>
129132
/// Attempts a best-effort to convert the remote origin to a GitHub Repository URL.
130133
/// </summary>
131-
/// <returns></returns>
134+
/// <returns>A converted uri, or the existing one if we can't convert it (which might be null)</returns>
132135
public Uri ToRepositoryUrl()
133136
{
134-
if (url != null && IsFileUri) return url;
137+
// we only want to process urls that represent network resources
138+
if (!IsScpUri && (!IsValidUri || IsFileUri)) return url;
135139

136140
var scheme = url != null && IsHypertextTransferProtocol
137141
? url.Scheme

src/GitHub.Exports/Services/GitService.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,7 @@ public static UriString GetOriginUri(IRepository repo)
167167
{
168168
return repo
169169
?.Network
170-
.Remotes
171-
.FirstOrDefault(x => x.Name.Equals("origin", StringComparison.Ordinal))
170+
.Remotes["origin"]
172171
?.Url;
173172
}
174173

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using GitHub.Services;
3+
using LibGit2Sharp;
4+
using NSubstitute;
5+
using Xunit;
6+
using System.Linq;
7+
using System.Collections;
8+
using System.Collections.Generic;
9+
10+
public class GitServiceTests : TestBaseClass
11+
{
12+
[Theory]
13+
[InlineData("asdf", null)]
14+
[InlineData("", null)]
15+
[InlineData(null, null)]
16+
[InlineData("file:///C:/dev/exp/foo", "file:///C:/dev/exp/foo")]
17+
[InlineData("http://example.com/", "http://example.com/")]
18+
[InlineData("http://[email protected]/foo/bar", "http://example.com/foo/bar")]
19+
[InlineData("https://github.com/github/Windows", "https://github.com/github/Windows")]
20+
[InlineData("https://github.com/github/Windows.git", "https://github.com/github/Windows")]
21+
[InlineData("https://[email protected]/github/Windows.git", "https://github.com/github/Windows")]
22+
[InlineData("http://example.com:4000/github/Windows", "http://example.com:4000/github/Windows")]
23+
[InlineData("[email protected]:github/Windows.git", "https://192.168.1.2/github/Windows")]
24+
[InlineData("[email protected]:org/repo.git", "https://example.com/org/repo")]
25+
[InlineData("ssh://[email protected]:443/shana/cef", "https://github.com/shana/cef")]
26+
[InlineData("ssh://[email protected]:23/haacked/encourage", "https://example.com:23/haacked/encourage")]
27+
public void GetUriShouldNotThrow(string url, string expected)
28+
{
29+
var origin = Substitute.For<Remote>();
30+
origin.Url.Returns(url);
31+
var repository = Substitute.For<IRepository>();
32+
repository.Network.Remotes["origin"].Returns(origin);
33+
34+
var gitservice = new GitService();
35+
Assert.Equal(expected, gitservice.GetUri(repository)?.ToString());
36+
}
37+
}

src/UnitTests/GitHub.Primitives/UriStringTests.cs

+21
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,27 @@ public void ConvertsToWebUrl(string uriString, string expected)
177177
{
178178
Assert.Equal(new Uri(expected), new UriString(uriString).ToRepositoryUrl());
179179
}
180+
181+
[Theory]
182+
[InlineData("asdf", null)]
183+
[InlineData("", null)]
184+
[InlineData("file:///C:/dev/exp/foo", "file:///C:/dev/exp/foo")]
185+
[InlineData("http://example.com/", "http://example.com/")]
186+
[InlineData("http://[email protected]/foo/bar", "http://example.com/foo/bar")]
187+
[InlineData("https://github.com/github/Windows", "https://github.com/github/Windows")]
188+
[InlineData("https://github.com/github/Windows.git", "https://github.com/github/Windows")]
189+
[InlineData("https://[email protected]/github/Windows.git", "https://github.com/github/Windows")]
190+
[InlineData("http://example.com:4000/github/Windows", "http://example.com:4000/github/Windows")]
191+
[InlineData("[email protected]:github/Windows.git", "https://192.168.1.2/github/Windows")]
192+
[InlineData("[email protected]:org/repo.git", "https://example.com/org/repo")]
193+
[InlineData("ssh://[email protected]:443/shana/cef", "https://github.com/shana/cef")]
194+
[InlineData("ssh://[email protected]:23/haacked/encourage", "https://example.com:23/haacked/encourage")]
195+
public void ShouldNeverThrow(string url, string expected)
196+
{
197+
Uri uri;
198+
Uri.TryCreate(expected, UriKind.Absolute, out uri);
199+
Assert.Equal(uri, new UriString(url).ToRepositoryUrl());
200+
}
180201
}
181202

182203
public class TheAdditionOperator

src/UnitTests/UnitTests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@
155155
<Compile Include="GitHub.App\ViewModels\RepositoryCreationViewModelTests.cs" />
156156
<Compile Include="GitHub.App\ViewModels\RepositoryPublishViewModelTests.cs" />
157157
<Compile Include="GitHub.Exports.Reactive\Caches\AccountCacheItemTests.cs" />
158+
<Compile Include="GitHub.Exports\GitServiceTests.cs" />
158159
<Compile Include="GitHub.Exports\VSServicesTests.cs" />
159160
<Compile Include="GitHub.Extensions\UriExtensionTests.cs" />
160161
<Compile Include="GitHub.Primitives\UriStringTests.cs" />

0 commit comments

Comments
 (0)