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

Refactoring ConnectionManager/RepositoryHost(s) #1280

Merged
merged 7 commits into from
Nov 9, 2017

Conversation

grokys
Copy link
Contributor

@grokys grokys commented Oct 23, 2017

This PR is the first step on the way to removing RepositoryHosts/RepositoryHost.

Ever since #845 we've been "logging in" cached connections when ConnectionManager starts up to make sure we have valid credentials, and then "logging in" again from RepositoryHost.

This PR moves all of the logic around connections into ConnectionManager and makes RepositoryHosts a simple "wrapper" around ConnectionManager.Connections that exposes a GitHubHost and an EnterpriseHost.

As this PR is a first step, there are a few hacks needed. See inline PR comments.

Fixes #1277
Fixes #1242

Depends on #1279

@grokys grokys changed the base branch from master to refactor/connections-master October 23, 2017 14:52
@grokys grokys changed the base branch from refactor/connections-master to refactor/connections/local-repositories October 23, 2017 14:52
{
var user = await loginManager.LoginFromCache(Address, ApiClient.GitHubClient);
var accountCacheItem = new AccountCacheItem(user);
usage.IncrementLoginCount().Forget();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: Need to work out if a login from cache counts as a login, and if so put this in `ConnectionManager.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done this now in ConnectionManager.

{
var user = await loginManager.Login(Address, ApiClient.GitHubClient, usernameOrEmail, password);
var accountCacheItem = new AccountCacheItem(user);
usage.IncrementLoginCount().Forget();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: Need to put this in ConnectionManager.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done this now in ConnectionManager.

.Select(h => LogOut(h))
.Merge().ToList().Select(_ => Unit.Default).Subscribe());

connectionManager.ConnectionCreated = ConnectionAdded;
Copy link
Contributor Author

@grokys grokys Oct 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a temporary hack, that performs the same purpose as connectionManager.DoLogin, but renamed (as it no longer does a login) and using Task instead of an observable.

It's needed because creating a RepositoryHost is async meaning that for a short time a connection can exist without a repository host, but other older parts of the code get confused by this. This callback allows RepositoryHosts to hook into the creation of the connection to make sure a host is available by the time the connection is made available.

The longer-term goal is to remove RepositoryHost(s) so this can go away.

// start tracking changes to the EnterpriseHost property and persist every change to the db
disposables.Add(persistEntepriseHostObs.Subscribe());
loaded = new TaskCompletionSource<object>();
disposables.Add(connectionManager.Connections.ForEachItem(_ => { }, ConnectionRemoved, ConnectionsReset));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we'd ideally just call ConnectionAdded as the first callback in ForEachItem which would also handle initialization, but because of the hack mentioned above we have to initialize explicitly and use ConnectionCreated to create the RepositoryHost.

////{
//// log.Info("Received AuthorizationException reading pull requests", ex);
//// return repositoryHost.LogOut();
////})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this code should be removed @shana - as far as I can see there's no good reason for the PR list to be messing with removing connections?

@@ -12,33 +15,40 @@ public static class ConnectionManagerExtensions
public static IObservable<bool> IsLoggedIn(this IConnectionManager cm, IRepositoryHosts hosts)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These methods should be changed to return async Task<T> but I didn't want to have to update all the call-sites in the PR.

@@ -16,115 +16,4 @@

public class RepositoryHostTests
{
public class TheLoginMethod : TestBaseClass
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RepositoryHost no longer has any functionality, so removed these tests.

@grokys grokys changed the title WIP: Refactoring ConnectionManager/RepositoryHost(s) Refactoring ConnectionManager/RepositoryHost(s) Oct 26, 2017
@grokys grokys requested a review from shana October 26, 2017 09:19
@grokys grokys mentioned this pull request Oct 27, 2017
…connection-manager

 Conflicts:
	src/GitHub.InlineReviews/Services/PullRequestSessionManager.cs
	src/UnitTests/GitHub.VisualStudio/Services/ConnectionManagerTests.cs
…connection-manager

 Conflicts:
	src/GitHub.App/Models/RepositoryHost.cs
	src/GitHub.App/Models/RepositoryHosts.cs
	src/GitHub.App/ViewModels/PullRequestListViewModel.cs
	src/GitHub.VisualStudio/Services/ConnectionManager.cs
@grokys
Copy link
Contributor Author

grokys commented Nov 8, 2017

Merged #1283 which was based on and superseded this PR - not sure why this PR wasn't automatically marked as merged when I merged that one.

@grokys grokys closed this Nov 8, 2017
@grokys grokys deleted the refactor/connection-manager branch November 8, 2017 07:25
@shana shana merged commit 32f073a into refactor/connections-master Nov 9, 2017
@shana shana changed the base branch from refactor/connections/local-repositories to refactor/connections-master November 9, 2017 00:53
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants