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

Commit b4304d9

Browse files
authored
Merge pull request #1369 from github/refactor/taskize-connectionmanagerextensions
Task-ize ConnectionManagerExtensions
2 parents 2353ab1 + a1a233e commit b4304d9

File tree

4 files changed

+53
-63
lines changed

4 files changed

+53
-63
lines changed
Lines changed: 2 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,20 @@
11
using System;
2-
using System.Linq;
3-
using System.Reactive.Linq;
4-
using System.Reactive.Threading.Tasks;
52
using System.Threading.Tasks;
63
using GitHub.Factories;
74
using GitHub.Models;
8-
using GitHub.Primitives;
95
using GitHub.Services;
106

117
namespace GitHub.Extensions
128
{
139
public static class ConnectionManagerExtensions
1410
{
15-
public static IObservable<bool> IsLoggedIn(this IConnectionManager cm)
16-
{
17-
Guard.ArgumentNotNull(cm, nameof(cm));
18-
19-
return Observable.FromAsync(async () =>
20-
{
21-
var connections = await cm.GetLoadedConnections();
22-
return connections.Any(x => x.ConnectionError == null);
23-
});
24-
}
25-
26-
public static IObservable<bool> IsLoggedIn(this IConnectionManager cm, HostAddress address)
27-
{
28-
Guard.ArgumentNotNull(cm, nameof(cm));
29-
Guard.ArgumentNotNull(address, nameof(address));
30-
31-
return Observable.FromAsync(async () =>
32-
{
33-
var connections = await cm.GetLoadedConnections();
34-
return connections.Any(x => x.HostAddress == address && x.ConnectionError == null);
35-
});
36-
}
37-
38-
public static IObservable<bool> IsLoggedIn(this IConnection connection)
39-
{
40-
Guard.ArgumentNotNull(connection, nameof(connection));
41-
42-
return Observable.Return(connection?.IsLoggedIn ?? false);
43-
}
44-
45-
public static IObservable<IConnection> GetConnection(this IConnectionManager cm, HostAddress address)
46-
{
47-
Guard.ArgumentNotNull(cm, nameof(cm));
48-
Guard.ArgumentNotNull(address, nameof(address));
49-
50-
return cm.GetLoadedConnections()
51-
.ToObservable()
52-
.Select(x => x.FirstOrDefault(y => y.HostAddress == address));
53-
}
54-
55-
public static IObservable<IConnection> GetLoggedInConnections(this IConnectionManager cm)
56-
{
57-
Guard.ArgumentNotNull(cm, nameof(cm));
58-
59-
return cm.GetLoadedConnections()
60-
.ToObservable()
61-
.Select(x => x.FirstOrDefault(y => y.IsLoggedIn));
62-
}
63-
6411
public static async Task<IModelService> GetModelService(
6512
this IConnectionManager cm,
6613
ILocalRepositoryModel repository,
6714
IModelServiceFactory factory)
6815
{
69-
var connection = await cm.LookupConnection(repository);
70-
return connection != null ? await factory.CreateAsync(connection) : null;
71-
}
72-
73-
public static IObservable<IConnection> LookupConnection(this IConnectionManager cm, ILocalRepositoryModel repository)
74-
{
75-
return Observable.Return(repository?.CloneUrl != null
76-
? cm.Connections.FirstOrDefault(c => c.HostAddress.Equals(HostAddress.Create(repository.CloneUrl)))
77-
: null);
16+
var connection = await cm.GetConnection(repository);
17+
return connection?.IsLoggedIn == true ? await factory.CreateAsync(connection) : null;
7818
}
7919
}
8020
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using GitHub.Models;
5+
using GitHub.Primitives;
6+
using GitHub.Services;
7+
8+
namespace GitHub.Extensions
9+
{
10+
public static class ConnectionManagerExtensions
11+
{
12+
public static async Task<bool> IsLoggedIn(this IConnectionManager cm)
13+
{
14+
Guard.ArgumentNotNull(cm, nameof(cm));
15+
16+
var connections = await cm.GetLoadedConnections();
17+
return connections.Any(x => x.ConnectionError == null);
18+
}
19+
20+
public static async Task<bool> IsLoggedIn(this IConnectionManager cm, HostAddress address)
21+
{
22+
Guard.ArgumentNotNull(cm, nameof(cm));
23+
Guard.ArgumentNotNull(address, nameof(address));
24+
25+
var connections = await cm.GetLoadedConnections();
26+
return connections.Any(x => x.HostAddress == address && x.ConnectionError == null);
27+
}
28+
29+
public static async Task<IConnection> GetFirstLoggedInConnection(this IConnectionManager cm)
30+
{
31+
Guard.ArgumentNotNull(cm, nameof(cm));
32+
33+
var connections = await cm.GetLoadedConnections();
34+
return connections.FirstOrDefault(x => x.IsLoggedIn);
35+
}
36+
37+
public static Task<IConnection> GetConnection(this IConnectionManager cm, IRepositoryModel repository)
38+
{
39+
if (repository?.CloneUrl != null)
40+
{
41+
var hostAddress = HostAddress.Create(repository.CloneUrl);
42+
return cm.GetConnection(hostAddress);
43+
}
44+
45+
return null;
46+
}
47+
}
48+
}

src/GitHub.Exports/GitHub.Exports.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@
140140
</ItemGroup>
141141
<ItemGroup>
142142
<Compile Include="Exports\ExportForProcess.cs" />
143+
<Compile Include="Extensions\ConnectionManagerExtensions.cs" />
143144
<Compile Include="GitHubLogicException.cs" />
144145
<Compile Include="Models\CommitMessage.cs" />
145146
<Compile Include="Models\DiffChangeType.cs" />

test/GitHub.InlineReviews.UnitTests/Services/PullRequestSessionManagerTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -951,9 +951,10 @@ IConnectionManager CreateConnectionManager()
951951
{
952952
var connection = Substitute.For<IConnection>();
953953
connection.HostAddress.Returns(HostAddress.Create("https://github.com"));
954+
connection.IsLoggedIn.Returns(true);
954955

955956
var result = Substitute.For<IConnectionManager>();
956-
result.Connections.Returns(new ObservableCollectionEx<IConnection>(new[] { connection }));
957+
result.GetConnection(connection.HostAddress).Returns(connection);
957958
return result;
958959
}
959960

0 commit comments

Comments
 (0)