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

Commit ac526ca

Browse files
committed
Use UriString and remove duplicate parsing logic
We have some places where we used custom parsing logic for extracting owner and repo from a repository URL. Instead, we should use the robust logic from UriString.
1 parent f401897 commit ac526ca

File tree

13 files changed

+48
-111
lines changed

13 files changed

+48
-111
lines changed

src/GitHub.Api/SimpleApiClient.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Diagnostics;
33
using System.Threading;
44
using System.Threading.Tasks;
5-
using GitHub.Extensions;
65
using GitHub.Primitives;
76
using GitHub.Services;
87
using Octokit;
@@ -11,8 +10,8 @@ namespace GitHub.Api
1110
{
1211
public class SimpleApiClient : ISimpleApiClient
1312
{
14-
public HostAddress HostAddress { get; private set; }
15-
public Uri OriginalUrl { get; private set; }
13+
public HostAddress HostAddress { get; }
14+
public UriString OriginalUrl { get; }
1615

1716
readonly GitHubClient client;
1817
readonly Lazy<IEnterpriseProbeTask> enterpriseProbe;
@@ -24,7 +23,7 @@ public class SimpleApiClient : ISimpleApiClient
2423
bool? isEnterprise;
2524
bool? hasWiki;
2625

27-
internal SimpleApiClient(HostAddress hostAddress, Uri repoUrl, GitHubClient githubClient,
26+
internal SimpleApiClient(HostAddress hostAddress, UriString repoUrl, GitHubClient githubClient,
2827
Lazy<IEnterpriseProbeTask> enterpriseProbe, Lazy<IWikiProbe> wikiProbe)
2928
{
3029
HostAddress = hostAddress;
@@ -51,19 +50,19 @@ async Task<Repository> GetRepositoryInternal()
5150
{
5251
if (owner == null && OriginalUrl != null)
5352
{
54-
var own = OriginalUrl.GetUser();
55-
var name = OriginalUrl.GetRepo();
53+
var ownerLogin = OriginalUrl.Owner;
54+
var repositoryName = OriginalUrl.RepositoryName;
5655

57-
if (own != null && name != null)
56+
if (ownerLogin != null && repositoryName != null)
5857
{
59-
var repo = await client.Repository.Get(own, name);
58+
var repo = await client.Repository.Get(ownerLogin, repositoryName);
6059
if (repo != null)
6160
{
6261
hasWiki = await HasWikiInternal(repo);
6362
isEnterprise = await IsEnterpriseInternal();
6463
repositoryCache = repo;
6564
}
66-
owner = own;
65+
owner = ownerLogin;
6766
}
6867
}
6968
}

src/GitHub.Api/SimpleApiClientFactory.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class SimpleApiClientFactory : ISimpleApiClientFactory
1616
readonly Lazy<IEnterpriseProbeTask> lazyEnterpriseProbe;
1717
readonly Lazy<IWikiProbe> lazyWikiProbe;
1818

19-
static readonly Dictionary<Uri, ISimpleApiClient> cache = new Dictionary<Uri, ISimpleApiClient>();
19+
static readonly Dictionary<UriString, ISimpleApiClient> cache = new Dictionary<UriString, ISimpleApiClient>();
2020

2121
[ImportingConstructor]
2222
public SimpleApiClientFactory(IProgram program, Lazy<IEnterpriseProbeTask> enterpriseProbe, Lazy<IWikiProbe> wikiProbe)
@@ -26,21 +26,21 @@ public SimpleApiClientFactory(IProgram program, Lazy<IEnterpriseProbeTask> enter
2626
lazyWikiProbe = wikiProbe;
2727
}
2828

29-
public ISimpleApiClient Create(Uri repoUrl)
29+
public ISimpleApiClient Create(UriString repositoryUrl)
3030
{
31-
var contains = cache.ContainsKey(repoUrl);
31+
var contains = cache.ContainsKey(repositoryUrl);
3232
if (contains)
33-
return cache[repoUrl];
33+
return cache[repositoryUrl];
3434

3535
lock (cache)
3636
{
37-
if (!cache.ContainsKey(repoUrl))
37+
if (!cache.ContainsKey(repositoryUrl))
3838
{
39-
var hostAddress = HostAddress.Create(repoUrl);
39+
var hostAddress = HostAddress.Create(repositoryUrl);
4040
var apiBaseUri = hostAddress.ApiUri;
41-
cache.Add(repoUrl, new SimpleApiClient(hostAddress, repoUrl, new GitHubClient(productHeader, new SimpleCredentialStore(hostAddress), apiBaseUri), lazyEnterpriseProbe, lazyWikiProbe));
41+
cache.Add(repositoryUrl, new SimpleApiClient(hostAddress, repositoryUrl, new GitHubClient(productHeader, new SimpleCredentialStore(hostAddress), apiBaseUri), lazyEnterpriseProbe, lazyWikiProbe));
4242
}
43-
return cache[repoUrl];
43+
return cache[repositoryUrl];
4444
}
4545
}
4646

src/GitHub.Exports/Api/ISimpleApiClient.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
using System;
2-
using System.Threading.Tasks;
1+
using System.Threading.Tasks;
32
using GitHub.Primitives;
4-
using GitHub.Services;
53
using Octokit;
64

75
namespace GitHub.Api
86
{
97
public interface ISimpleApiClient
108
{
119
HostAddress HostAddress { get; }
12-
Uri OriginalUrl { get; }
10+
UriString OriginalUrl { get; }
1311
Task<Repository> GetRepository();
1412
bool HasWiki();
1513
bool IsEnterprise();
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
using System;
1+
using GitHub.Primitives;
22

33
namespace GitHub.Api
44
{
55
public interface ISimpleApiClientFactory
66
{
7-
ISimpleApiClient Create(Uri repoUrl);
7+
ISimpleApiClient Create(UriString repositoryUrl);
88
void ClearFromCache(ISimpleApiClient client);
99
}
1010
}

src/GitHub.Exports/Services/ITeamExplorerServiceHolder.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
using Microsoft.VisualStudio.TeamFoundation.Git.Extensibility;
2-
using System;
1+
using System;
2+
using GitHub.Primitives;
3+
using Microsoft.VisualStudio.TeamFoundation.Git.Extensibility;
34

45
namespace GitHub.Services
56
{
@@ -50,7 +51,7 @@ public interface IGitAwareItem
5051
/// <summary>
5152
/// Represents the web URL of the repository on GitHub.com, even if the origin is an SSH address.
5253
/// </summary>
53-
Uri ActiveRepoUri { get; }
54+
UriString ActiveRepoUri { get; }
5455
string ActiveRepoName { get; }
5556
}
5657
}
Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using NullGuard;
2-
using System;
1+
using System;
32

43
namespace GitHub.Extensions
54
{
@@ -30,46 +29,5 @@ public static bool IsSameHost(this Uri uri, Uri compareUri)
3029
{
3130
return uri.Host.Equals(compareUri.Host, StringComparison.OrdinalIgnoreCase);
3231
}
33-
34-
public static Uri WithAbsolutePath(this Uri uri, string absolutePath)
35-
{
36-
absolutePath = absolutePath.EnsureStartsWith('/');
37-
38-
return new Uri(uri, new Uri(absolutePath, UriKind.Relative));
39-
}
40-
41-
public static string ToUpperInvariantString(this Uri uri)
42-
{
43-
return uri == null ? "" : uri.ToString().ToUpperInvariant();
44-
}
45-
46-
[return: AllowNull]
47-
public static string GetUser(this Uri uri)
48-
{
49-
var parts = uri.Segments;
50-
// only parse urls in the format domain/user/repo
51-
if (parts.Length < 3)
52-
return null;
53-
var u = parts[1];
54-
if (u == null)
55-
return null;
56-
u = u.TrimEnd('/');
57-
return u;
58-
}
59-
60-
[return: AllowNull]
61-
public static string GetRepo(this Uri uri)
62-
{
63-
var parts = uri.Segments;
64-
// only parse urls in the format domain/user/repo
65-
if (parts.Length < 3)
66-
return null;
67-
var name = parts[2];
68-
if (name == null)
69-
return null;
70-
if (name.EndsWith(".git", StringComparison.Ordinal))
71-
name = name.Remove(name.Length - 4);
72-
return name;
73-
}
7432
}
7533
}

src/GitHub.VisualStudio/Base/TeamExplorerGitRepoInfo.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
using GitHub.Api;
1+
using GitHub.Primitives;
22
using GitHub.Services;
33
using Microsoft.VisualStudio.TeamFoundation.Git.Extensibility;
44
using NullGuard;
5-
using System;
65

76
namespace GitHub.VisualStudio.Base
87
{
@@ -31,7 +30,7 @@ public IGitRepositoryInfo ActiveRepo
3130
/// Represents the web URL of the repository on GitHub.com, even if the origin is an SSH address.
3231
/// </summary>
3332
[AllowNull]
34-
public Uri ActiveRepoUri { [return: AllowNull] get; set; }
33+
public UriString ActiveRepoUri { [return: AllowNull] get; set; }
3534
public string ActiveRepoName { get; set; }
3635
}
3736
}

src/GitHub.VisualStudio/Base/TeamExplorerItemBase.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
using System.ComponentModel.Composition;
2+
using System.Threading.Tasks;
3+
using GitHub.Api;
24
using GitHub.Models;
5+
using GitHub.Primitives;
6+
using GitHub.Services;
37
using GitHub.VisualStudio.Helpers;
48
using NullGuard;
59
using Octokit;
6-
using System;
7-
using GitHub.Services;
8-
using GitHub.Api;
9-
using System.Threading.Tasks;
10-
using GitHub.Primitives;
11-
using GitHub.Extensions;
1210

1311
namespace GitHub.VisualStudio.Base
1412
{
@@ -54,7 +52,7 @@ protected virtual void RepoChanged()
5452
var name = uri.RepositoryName;
5553
if (name != null)
5654
{
57-
ActiveRepoUri = uri.ToWebUri();
55+
ActiveRepoUri = uri;
5856
ActiveRepoName = uri.NameWithOwner;
5957
}
6058
}
@@ -68,7 +66,7 @@ protected async Task<bool> IsAGitHubRepo()
6866

6967
SimpleApiClient = apiFactory.Create(uri);
7068

71-
if (!HostAddress.IsGitHubDotComUri(uri))
69+
if (!HostAddress.IsGitHubDotComUri(uri.ToWebUri()))
7270
{
7371
var repo = await SimpleApiClient.GetRepository();
7472
return repo.FullName == ActiveRepoName && SimpleApiClient.IsEnterprise();

src/GitHub.VisualStudio/Base/TeamExplorerNavigationItemBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ protected void OpenInBrowser(Lazy<IVisualStudioBrowser> browser, string endpoint
7474
if (uri == null)
7575
return;
7676
#endif
77-
var browseUrl = uri.Append(endpoint);
77+
var browseUrl = uri.ToWebUri().Append(endpoint);
7878

7979
OpenInBrowser(browser, browseUrl);
8080
}

src/UnitTests/GitHub.Extensions/URITests.cs

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)