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

Commit 0aade59

Browse files
committed
Update UriString to allow http URLs with owner
The following URL will now have an owner of "owner": https://github.com/owner Previously it would make a repository name of "owner".
1 parent 89484dc commit 0aade59

File tree

2 files changed

+20
-29
lines changed

2 files changed

+20
-29
lines changed

src/GitHub.Exports/Primitives/UriString.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,17 @@ public UriString(string uriString) : base(NormalizePath(uriString))
4343
SetFilePath(uriString);
4444
}
4545

46-
if (RepositoryName != null)
46+
if (Owner != null && RepositoryName != null)
4747
{
48-
NameWithOwner = Owner != null
49-
? string.Format(CultureInfo.InvariantCulture, "{0}/{1}", Owner, RepositoryName)
50-
: RepositoryName;
48+
NameWithOwner = string.Format(CultureInfo.InvariantCulture, "{0}/{1}", Owner, RepositoryName);
49+
}
50+
else if (Owner != null)
51+
{
52+
NameWithOwner = Owner;
53+
}
54+
else if (RepositoryName != null)
55+
{
56+
NameWithOwner = RepositoryName;
5157
}
5258
}
5359

@@ -68,13 +74,6 @@ void SetUri(Uri uri)
6874
var ownerSegment = FindSegment(uri.Segments, 0);
6975
var repositorySegment = FindSegment(uri.Segments, 1);
7076

71-
if (repositorySegment == null)
72-
{
73-
// Prioritize having a RepositoryName over an Owner
74-
repositorySegment = ownerSegment;
75-
ownerSegment = null;
76-
}
77-
7877
Host = uri.Host;
7978
Owner = ownerSegment;
8079
RepositoryName = GetRepositoryName(repositorySegment);
@@ -148,7 +147,7 @@ public Uri ToRepositoryUrl(string owner = null)
148147
? url.Scheme
149148
: Uri.UriSchemeHttps;
150149

151-
var nameWithOwner = owner != null && Owner != null ?
150+
var nameWithOwner = owner != null && RepositoryName != null ?
152151
string.Format(CultureInfo.InvariantCulture, "{0}/{1}", owner, RepositoryName) :
153152
NameWithOwner;
154153

test/GitHub.Primitives.UnitTests/UriStringTests.cs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,15 @@ public class TheConstructor : TestBaseClass
2525
[TestCase("jane;fingerprint=9e:1a:5e:27:16:4d:2a:13:90:2c:64:41:bd:25:fd:[email protected]:github/Windows.git",
2626
"foo.com", "github", "Windows")]
2727
[TestCase("https://[email protected]/haacked/test-mytest.git", "bitbucket.org", "haacked", "test-mytest")]
28-
[TestCase("https://git01.codeplex.com/nuget", "git01.codeplex.com", null, "nuget")]
28+
[TestCase("https://git01.codeplex.com/nuget", "git01.codeplex.com", "nuget", null,
29+
Description = "We assume the first component is the owner")]
2930
[TestCase("https://example.com/vpath/foo/bar", "example.com", "vpath", "foo")]
3031
[TestCase("https://example.com/vpath/foo/bar.git", "example.com", "vpath", "foo")]
3132
[TestCase("https://github.com/github/Windows.git?pr=24&branch=pr/23&filepath=relative/to/the/path.md",
3233
"github.com", "github", "Windows")]
3334
[TestCase("https://github.com/github/VisualStudio/blob/master/src/code.cs", "github.com", "github", "VisualStudio")]
35+
[TestCase("https://github.com/github", "github.com", "github", null)]
36+
[TestCase("https://github.com", "github.com", null, null)]
3437
public void ParsesWellFormedUrlComponents(string url, string expectedHost, string owner, string repositoryName)
3538
{
3639
var cloneUrl = new UriString(url);
@@ -71,13 +74,17 @@ public void ParsesLocalFileUris(string path)
7174
[TestCase("http://example.com", "example.com", null, null)]
7275
[TestCase("http://example.com?bar", "example.com", null, null)]
7376
[TestCase("https://example.com?bar", "example.com", null, null)]
74-
[TestCase("ssh://[email protected]/Windows.git", "example.com", null, "Windows")]
77+
[TestCase("ssh://[email protected]/Windows.git", "example.com", "Windows.git", null,
78+
Description = "We assume the first component is the owner even if it ends with .git")]
7579
[TestCase("[email protected]:/", "bar.com", null, null)]
7680
[TestCase("[email protected]/", "bar.com", null, null)]
7781
[TestCase("[email protected]", "bar.com", null, null)]
7882
[TestCase("[email protected]:/Windows.git", "bar.com", null, "Windows")]
7983
[TestCase("[email protected]/Windows.git", "baz.com", null, "Windows")]
8084
[TestCase("ssh://[email protected]:github/Windows.git", "github.com", "github", "Windows")]
85+
86+
// NOTE: Used by LocalRepositoryModelTests.GenerateUrl but I don't think it's a legal URL
87+
[TestCase("[email protected]/foo/bar", "github.com", null, "foo/bar")]
8188
public void ParsesWeirdUrlsAsWellAsPossible(string url, string expectedHost, string owner, string repositoryName)
8289
{
8390
var cloneUrl = new UriString(url);
@@ -202,21 +209,6 @@ public void ConvertsWithNewOwner(string uriString, string expected, string owner
202209
[TestCase("[email protected]:org/repo.git", "https://example.com/org/repo")]
203210
[TestCase("ssh://[email protected]:443/shana/cef", "https://github.com/shana/cef")]
204211
[TestCase("ssh://[email protected]:23/haacked/encourage", "https://example.com:23/haacked/encourage")]
205-
206-
[TestCase("asdf", null)]
207-
[TestCase("", null)]
208-
[TestCase("file:///C:/dev/exp/foo", "file:///C:/dev/exp/foo")]
209-
[TestCase("http://example.com/", "http://example.com/")]
210-
[TestCase("http://[email protected]/foo/bar", "http://example.com/foo/bar")]
211-
[TestCase("https://github.com/github/Windows", "https://github.com/github/Windows")]
212-
[TestCase("https://github.com/github/Windows.git", "https://github.com/github/Windows")]
213-
[TestCase("https://[email protected]/github/Windows.git", "https://github.com/github/Windows")]
214-
[TestCase("http://example.com:4000/github/Windows", "http://example.com:4000/github/Windows")]
215-
[TestCase("[email protected]:github/Windows.git", "https://192.168.1.2/github/Windows")]
216-
[TestCase("[email protected]:org/repo.git", "https://example.com/org/repo")]
217-
[TestCase("ssh://[email protected]:443/shana/cef", "https://github.com/shana/cef")]
218-
[TestCase("ssh://[email protected]:23/haacked/encourage", "https://example.com:23/haacked/encourage")]
219-
220212
public void ShouldNeverThrow(string url, string expected)
221213
{
222214
Uri uri;

0 commit comments

Comments
 (0)