8000 Master PR for refactoring connections by grokys · Pull Request #1277 · github/VisualStudio · GitHub
[go: up one dir, main page]

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

Master PR for refactoring connections #1277

Merged
merged 43 commits into from
Nov 21, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
b790d96
ILoginCache -> IKeychain
grokys Aug 25, 2017
1c81104
Use IKeychain for credentials.
grokys Aug 25, 2017
21f888e
Updated parameter name.
grokys Aug 25, 2017
7e061d1
Removed unneeded usings.
grokys Aug 25, 2017
c8b8dd7
Updated variable names.
grokys Aug 25, 2017
95f2c0b
Remove `IObservableKeychainAdapter`.
grokys Aug 25, 2017
7b4080f
Merge branch 'master' into fixes/1176-duplicate-login-classes
grokys Aug 30, 2017
3b13736
Store "master" credential.
grokys Aug 30, 2017
a142423
Merge branch 'master' into fixes/1176-duplicate-login-classes
grokys Sep 7, 2017
bcfbd79
Fix broken merge.
grokys Sep 7, 2017
c52a261
Merge branch 'fixes/1176-duplicate-login-classes' into fixes/harden-c…
grokys Sep 7, 2017
e6c5d89
Merge branch 'master' into fixes/1176-duplicate-login-classes
grokys Oct 20, 2017
17c845e
Merge branch 'fixes/1176-duplicate-login-classes' into fixes/harden-c…
grokys Oct 20, 2017
a627271
Merge branch 'master' into refactor/connections-master
grokys Oct 20, 2017
470ea13
Removed `IConnection.Repositories`.
grokys Oct 20, 2017
d98ec51
Refactored ConnectionManager/Connection.
grokys Oct 23, 2017
aa78f03
Refactored RepositoryHost(s).
grokys Oct 23, 2017
7cf89c9
Added DebuggerDisplay back in.
grokys Oct 23, 2017
213a7fc
Moved LocalRepositories to GitHub.VisualStudio.
grokys Oct 24, 2017
90c35c5
Track login metrics.
grokys Oct 26, 2017
9db05a7
Fix failing tests.
grokys Oct 26, 2017
f854b79
WIP: Removing RepositoryHost(s)
grokys Oct 26, 2017
3240372
WIP: Still removing RepositoryHost(s)
grokys Oct 26, 2017
038f232
WIP: Removing RepositoryHost(s) from tests.
grokys Oct 26, 2017
a57a759
Finished migrating tests (I think).
grokys Oct 27, 2017
95cd0b1
Added IModelServiceFactory implemenation.
grokys Oct 27, 2017
6034643
Added IGlobalConnection.
grokys Oct 27, 2017
17aebf8
Removed ConnectionManager.ConnectionCreated.
grokys Oct 27, 2017
00232f0
Merge branch 'master' into refactor/connections-master
grokys Nov 3, 2017
01aad33
Merge branch 'refactor/connections-master' into refactor/connections/…
grokys Nov 3, 2017
217e415
Merge branch 'refactor/connections/local-repositories' into refactor/…
grokys Nov 3, 2017
7e57621
Insert AccountCacheItem for connection.
grokys Nov 7, 2017
e45d0c6
Merge branch 'master' into refactor/connections-master
grokys Nov 7, 2017
8f6d218
Merge branch 'refactor/connections-master' into refactor/connections/…
grokys Nov 7, 2017
32f073a
Merge branch 'refactor/connections/local-repositories' into refactor/…
grokys Nov 7, 2017
9f15db6
Merge branch 'refactor/connection-manager' into refactor/connections/…
grokys Nov 7, 2017
cd546bb
Fix CA errors.
grokys Nov 7, 2017
def6070
Merge pull request #1283 from github/refactor/connections/remove-repo…
grokys Nov 8, 2017
fc57163
Merge branch 'master' into refactor/connections-master
grokys Nov 8, 2017
d9c2a2f
Removed empty file.
grokys Nov 8, 2017
cefd4b9
Merge branch 'master' into refactor/connections-master
grokys Nov 14, 2017
7d7b188
Merge branch 'master' into refactor/connections-master
grokys Nov 20, 2017
4701dd0
Merge branch 'master' into refactor/connections-master
grokys Nov 20, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactored RepositoryHost(s).
  • Loading branch information
grokys committed Oct 23, 10000 2017
commit aa78f0343fa7840f262c0d7de9f88b14834e4cb0
14 changes: 10 additions & 4 deletions src/GitHub.App/Controllers/UIController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Reactive.Threading.Tasks;
using System.Windows;

namespace GitHub.Controllers
Expand Down Expand Up @@ -219,7 +220,13 @@ public void Start()
else // sanity check: it makes zero sense to pass a connection in when calling the auth flow
Debug.Assert(false, "Calling the auth flow with a connection makes no sense!");

Fire(Trigger.Next);
hosts.EnsureInitialized().ToObservable()
.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(_ => { }, () =>
{
Debug.WriteLine("Start ({0})", GetHashCode());
Fire(Trigger.Next);
});
}
else
{
Expand Down Expand Up @@ -438,8 +445,7 @@ void ConfigureSingleViewLogic(UIControllerFlow flow, UIViewType type)

UIControllerFlow SelectActiveFlow()
{
var host = connection != null ? hosts.LookupHost(connection.HostAddress) : null;
var loggedIn = host?.IsLoggedIn ?? false;
var loggedIn = connection?.IsLoggedIn ?? false;
return loggedIn ? selectedFlow : UIControllerFlow.Authentication;
}

Expand Down Expand Up @@ -704,7 +710,7 @@ public void Dispose()
public bool IsStopped => uiStateMachine.IsInState(UIViewType.None) || stopping;
public UIControllerFlow CurrentFlow => activeFlow;
public UIControllerFlow SelectedFlow => selectedFlow;
bool LoggedIn => connection != null && hosts.LookupHost(connection.HostAddress).IsLoggedIn;
bool LoggedIn => connection?.IsLoggedIn ?? false;
bool? Success { get; set; }
}
}
49 changes: 6 additions & 43 deletions src/GitHub.App/Factories/RepositoryHostFactory.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
using System;
using System.ComponentModel.Composition;
using GitHub.Caches;
using System.ComponentModel.Composition;
using System.Threading.Tasks;
using GitHub.Models;
using GitHub.Primitives;
using GitHub.Services;
using System.Reactive.Disposables;
using System.Threading.Tasks;
using GitHub.Api;

namespace GitHub.Factories
{
Expand All @@ -16,59 +11,27 @@ public class RepositoryHostFactory : IRepositoryHostFactory
{
readonly IApiClientFactory apiClientFactory;
readonly IHostCacheFactory hostCacheFactory;
readonly ILoginManager loginManager;
readonly IKeychain keychain;
readonly IAvatarProvider avatarProvider;
readonly CompositeDisposable hosts = new CompositeDisposable();
readonly IUsageTracker usage;

[ImportingConstructor]
public RepositoryHostFactory(
IApiClientFactory apiClientFactory,
IHostCacheFactory hostCacheFactory,
ILoginManager loginManager,
IKeychain keychain,
IAvatarProvider avatarProvider,
IUsageTracker usage)
IAvatarProvider avatarProvider)
{
this.apiClientFactory = apiClientFactory;
this.hostCacheFactory = hostCacheFactory;
this.loginManager = loginManager;
this.keychain = keychain;
this.avatarProvider = avatarProvider;
this.usage = usage;
}

public async Task<IRepositoryHost> Create(HostAddress hostAddress)
public async Task<IRepositoryHost> Create(IConnection connection)
{
var hostAddress = connection.HostAddress;
var apiClient = await apiClientFactory.Create(hostAddress);
var hostCache = await hostCacheFactory.Create(hostAddress);
var modelService = new ModelService(apiClient, hostCache, avatarProvider);
var host = new RepositoryHost(apiClient, modelService, loginManager, keychain, usage);
hosts.Add(host);
var host = new RepositoryHost(connection, apiClient, modelService);
return host;
}

public void Remove(IRepositoryHost host)
{
hosts.Remove(host);
}

bool disposed;
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (disposed) return;
disposed = true;
hosts.Dispose();
}
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
}
5 changes: 0 additions & 5 deletions src/GitHub.App/Models/DisconnectedRepositoryHosts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,5 @@ public IObservable<Unit> LogOut()
{
return Observable.Return(Unit.Default);
}

public void Dispose()
{
GC.SuppressFinalize(this);
}
}
}
158 changes: 13 additions & 145 deletions src/GitHub.App/Models/RepositoryHost.cs
Original file line number Diff line number Diff line change
@@ -1,168 +1,36 @@
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using System.Reactive.Threading.Tasks;
using System.Threading.Tasks;
using GitHub.Api;
using GitHub.Authentication;
using GitHub.Caches;
using GitHub.Extensions;
using GitHub.Primitives;
using GitHub.Services;
using NLog;
using Octokit;
using ReactiveUI;

namespace GitHub.Models
{
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class RepositoryHost : ReactiveObject, IRepositoryHost
{
static readonly Logger log = LogManager.GetCurrentClassLogger();

readonly ILoginManager loginManager;
readonly HostAddress hostAddress;
readonly IKeychain keychain;
readonly IUsageTracker usage;

bool isLoggedIn;
readonly IConnection connection;

public RepositoryHost(
IConnection connection,
IApiClient apiClient,
IModelService modelService,
ILoginManager loginManager,
IKeychain keychain,
IUsageTracker usage)
IModelService modelService)
{
Guard.ArgumentNotNull(connection, nameof(connection));
Guard.ArgumentNotNull(apiClient, nameof(apiClient));
Guard.ArgumentNotNull(modelService, nameof(modelService));

this.connection = connection;
ApiClient = apiClient;
ModelService = modelService;
this.loginManager = loginManager;
this.keychain = keychain;
this.usage = usage;

Debug.Assert(apiClient.HostAddress != null, "HostAddress of an api client shouldn't be null");
Address = apiClient.HostAddress;
hostAddress = apiClient.HostAddress;
Title = apiClient.HostAddress.Title;
}

public HostAddress Address { get; private set; }

public IApiClient ApiClient { get; private set; }

public bool IsLoggedIn
{
get { return isLoggedIn; }
private set { this.RaiseAndSetIfChanged(ref isLoggedIn, value); }
}

public string Title { get; private set; }

[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
public IObservable<AuthenticationResult> LogInFromCache()
{
Func<Task<AuthenticationResult>> f = async () =>
{
try
{
var user = await loginManager.LoginFromCache(Address, ApiClient.GitHubClient);
var accountCacheItem = new AccountCacheItem(user);
usage.IncrementLoginCount().Forget();
await ModelService.InsertUser(accountCacheItem);

if (user != null)
{
IsLoggedIn = true;
return AuthenticationResult.Success;
}
else
{
return AuthenticationResult.VerificationFailure;
}
}
catch (AuthorizationException)
{
return AuthenticationResult.CredentialFailure;
}
};

return f().ToObservable();
}

public IObservable<AuthenticationResult> LogIn(string usernameOrEmail, string password)
{
Guard.ArgumentNotEmptyString(usernameOrEmail, nameof(usernameOrEmail));
Guard.ArgumentNotEmptyString(password, nameof(password));

return Observable.Defer(async () =>
{
var user = await loginManager.Login(Address, ApiClient.GitHubClient, usernameOrEmail, password);
var accountCacheItem = new AccountCacheItem(user);
usage.IncrementLoginCount().Forget();
await ModelService.InsertUser(accountCacheItem);

if (user != null)
{
IsLoggedIn = true;
return Observable.Return(AuthenticationResult.Success);
}
else
{
return Observable.Return(AuthenticationResult.VerificationFailure);
}
});
}

public IObservable<Unit> LogOut()
{
if (!IsLoggedIn) return Observable.Return(Unit.Default);

log.Info(CultureInfo.InvariantCulture, "Logged off of host '{0}'", hostAddress.ApiUri);

return keychain.Delete(Address).ToObservable()
.Catch<Unit, Exception>(e =>
{
log.Warn("ASSERT! Failed to erase login. Going to invalidate cache anyways.", e);
return Observable.Return(Unit.Default);
})
.SelectMany(_ => ModelService.InvalidateAll())
.Catch<Unit, Exception>(e =>
{
log.Warn("ASSERT! Failed to invaldiate caches", e);
return Observable.Return(Unit.Default);
})
.ObserveOn(RxApp.MainThreadScheduler)
.Finally(() =>
{
IsLoggedIn = false;
});
}

protected virtual void Dispose(bool disposing)
{}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

internal string DebuggerDisplay
{
get
{
return string.Format(CultureInfo.InvariantCulture, "RepositoryHost: {0} {1}", Title, hostAddress.ApiUri);
}
}

public IModelService ModelService
{
get;
private set;
}
public HostAddress Address => ApiClient.HostAddress;
public IApiClient ApiClient { get;}
public bool IsLoggedIn => connection.IsLoggedIn;
public IModelService ModelService { get; }
public string Title => ApiClient.HostAddress.Title;
}
}
Loading
0