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

Commit 7cedbe1

Browse files
committed
Removed UserAndScopes.
It was a nasty hack anyway, and for now it's no longer needed.
1 parent 6141d31 commit 7cedbe1

File tree

9 files changed

+17
-100
lines changed

9 files changed

+17
-100
lines changed

src/GitHub.App/Api/ApiClient.cs

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public partial class ApiClient : IApiClient
2626
const string ScopesHeader = "X-OAuth-Scopes";
2727
const string ProductName = Info.ApplicationInfo.ApplicationDescription;
2828
static readonly Logger log = LogManager.GetCurrentClassLogger();
29-
static readonly Uri userEndpoint = new Uri("user", UriKind.Relative);
3029

3130
readonly IObservableGitHubClient gitHubClient;
3231
// There are two sets of authorization scopes, old and new:
@@ -96,30 +95,9 @@ public IObservable<Gist> CreateGist(NewGist newGist)
9695
return gitHubClient.Gist.Create(newGist);
9796
}
9897

99-
public IObservable<UserAndScopes> GetUser()
98+
public IObservable<User> GetUser()
10099
{
101-
return GetUserInternal().ToObservable();
102-
}
103-
104-
async Task<UserAndScopes> GetUserInternal()
105-
{
106-
var response = await gitHubClient.Connection.Get<User>(
107-
userEndpoint, null, null).ConfigureAwait(false);
108-
var scopes = default(string[]);
109-
110-
if (response.HttpResponse.Headers.ContainsKey(ScopesHeader))
111-
{
112-
scopes = response.HttpResponse.Headers[ScopesHeader]
113-
.Split(',')
114-
.Select(x => x.Trim())
115-
.ToArray();
116-
}
117-
else
118-
{
119-
log.Error($"Error reading scopes: /user succeeded but {ScopesHeader} was not present.");
120-
}
121-
122-
return new UserAndScopes(response.Body, scopes);
100+
return gitHubClient.User.Current();
123101
}
124102

125103
public IObservable<ApplicationAuthorization> GetOrCreateApplicationAuthenticationCode(

src/GitHub.App/Models/RepositoryHost.cs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ namespace GitHub.Models
2727
public class RepositoryHost : ReactiveObject, IRepositoryHost
2828
{
2929
static readonly Logger log = LogManager.GetCurrentClassLogger();
30-
static readonly UserAndScopes unverifiedUser = new UserAndScopes(null, null);
3130

3231
readonly ILoginManager loginManager;
3332
readonly HostAddress hostAddress;
@@ -79,7 +78,7 @@ public IObservable<AuthenticationResult> LogInFromCache()
7978
usage.IncrementLoginCount().Forget();
8079
await ModelService.InsertUser(accountCacheItem);
8180

82-
if (user != unverifiedUser.User)
81+
if (user != null)
8382
{
8483
IsLoggedIn = true;
8584
return AuthenticationResult.Success;
@@ -110,7 +109,7 @@ public IObservable<AuthenticationResult> LogIn(string usernameOrEmail, string pa
110109
usage.IncrementLoginCount().Forget();
111110
await ModelService.InsertUser(accountCacheItem);
112111

113-
if (user != unverifiedUser.User)
112+
if (user != null)
114113
{
115114
IsLoggedIn = true;
116115
return Observable.Return(AuthenticationResult.Success);
@@ -147,19 +146,6 @@ public IObservable<Unit> LogOut()
147146
});
148147
}
149148

150-
static IObservable<AuthenticationResult> GetAuthenticationResultForUser(UserAndScopes account)
151-
{
152-
return Observable.Return(account == null ? AuthenticationResult.CredentialFailure
153-
: account == unverifiedUser
154-
? AuthenticationResult.VerificationFailure
155-
: AuthenticationResult.Success);
156-
}
157-
158-
IObservable<UserAndScopes> GetUserFromApi()
159-
{
160-
return Observable.Defer(() => ApiClient.GetUser());
161-
}
162-
163149
protected virtual void Dispose(bool disposing)
164150
{}
165151

src/GitHub.Exports.Reactive/Api/IApiClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public interface IApiClient
1515

1616
IObservable<Repository> CreateRepository(NewRepository repository, string login, bool isUser);
1717
IObservable<Gist> CreateGist(NewGist newGist);
18-
IObservable<UserAndScopes> GetUser();
18+
IObservable<User> GetUser();
1919
IObservable<Organization> GetOrganizations();
2020
/// <summary>
2121
/// Retrieves all repositories that belong to this user.

src/GitHub.Exports.Reactive/Caches/AccountCacheItem.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ public static AccountCacheItem Create(Account apiAccount)
1212
return new AccountCacheItem(apiAccount);
1313
}
1414

15-
public static AccountCacheItem Create(UserAndScopes userAndScopes)
16-
{
17-
return new AccountCacheItem(userAndScopes.User);
18-
}
19-
2015
public AccountCacheItem()
2116
{ }
2217

src/GitHub.Exports.Reactive/GitHub.Exports.Reactive.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@
100100
<Compile Include="Models\IPullRequestSessionFile.cs" />
101101
<Compile Include="Models\IRepositoryHosts.cs" />
102102
<Compile Include="Models\PullRequestTextBufferInfo.cs" />
103-
<Compile Include="Models\UserAndScopes.cs" />
104103
<Compile Include="Services\IModelService.cs" />
105104
<Compile Include="Services\IGistPublishService.cs" />
106105
<Compile Include="Services\IPullRequestSession.cs" />

src/GitHub.Exports.Reactive/Models/UserAndScopes.cs

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

src/UnitTests/GitHub.App/Models/ModelServiceTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public async Task CanRetrieveAndCacheUserAndAccounts()
153153
CreateOctokitOrganization("fake")
154154
};
155155
var apiClient = Substitute.For<IApiClient>();
156-
apiClient.GetUser().Returns(Observable.Return(CreateUserAndScopes("snoopy")));
156+
apiClient.GetUser().Returns(Observable.Return(CreateOctokitUser("snoopy")));
157157
apiClient.GetOrganizations().Returns(orgs.ToObservable());
158158
var cache = new InMemoryBlobCache();
159159
var modelService = new ModelService(apiClient, cache, Substitute.For<IAvatarProvider>());
@@ -207,8 +207,8 @@ public async Task OnlyRetrievesOneUserEvenIfCacheOrApiReturnsMoreThanOne()
207207
// This should be impossible, but let's pretend it does happen.
208208
var users = new[]
209209
{
210-
CreateUserAndScopes("peppermintpatty"),
211-
CreateUserAndScopes("peppermintpatty")
210+
CreateOctokitUser("peppermintpatty"),
211+
CreateOctokitUser("peppermintpatty")
212212
};
213213
var apiClient = Substitute.For<IApiClient>();
214214
apiClient.GetUser().Returns(users.ToObservable());
@@ -387,7 +387,7 @@ public async Task NonExpiredIndexReturnsCache()
387387
var apiClient = Substitute.For<IApiClient>();
388388
var modelService = new ModelService(apiClient, cache, Substitute.For<IAvatarProvider>());
389389
var user = CreateOctokitUser(username);
390-
apiClient.GetUser().Returns(Observable.Return(new UserAndScopes(user, null)));
390+
apiClient.GetUser().Returns(Observable.Return(user));
391391
apiClient.GetOrganizations().Returns(Observable.Empty<Organization>());
392392
var act = modelService.GetAccounts().ToEnumerable().First().First();
393393

@@ -438,7 +438,7 @@ public async Task ExpiredIndexReturnsLive()
438438
var apiClient = Substitute.For<IApiClient>();
439439
var modelService = new ModelService(apiClient, cache, Substitute.For<IAvatarProvider>());
440440
var user = CreateOctokitUser(username);
441-
apiClient.GetUser().Returns(Observable.Return(new UserAndScopes(user, null)));
441+
apiClient.GetUser().Returns(Observable.Return(user));
442442
apiClient.GetOrganizations().Returns(Observable.Empty<Organization>());
443443
var act = modelService.GetAccounts().ToEnumerable().First().First();
444444

@@ -506,7 +506,7 @@ public async Task ExpiredIndexClearsItems()
506506
var apiClient = Substitute.For<IApiClient>();
507507
var modelService = new ModelService(apiClient, cache, Substitute.For<IAvatarProvider>());
508508
var user = CreateOctokitUser(username);
509-
apiClient.GetUser().Returns(Observable.Return(new UserAndScopes(user, null)));
509+
apiClient.GetUser().Returns(Observable.Return(user));
510510
apiClient.GetOrganizations().Returns(Observable.Empty<Organization>());
511511
var act = modelService.GetAccounts().ToEnumerable().First().First();
512512

src/UnitTests/GitHub.App/Models/RepositoryHostTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ public async Task LogsTheUserInSuccessfullyAndCachesRelevantInfo()
2727
apiClient.GetOrCreateApplicationAuthenticationCode(
2828
Args.TwoFactorChallengCallback, Args.String, Args.Boolean)
2929
.Returns(Observable.Return(new ApplicationAuthorization("S3CR3TS")));
30-
apiClient.GetUser().Returns(Observable.Return(CreateUserAndScopes("baymax")));
30+
apiClient.GetUser().Returns(Observable.Return(CreateOctokitUser("baymax")));
3131
var hostCache = new InMemoryBlobCache();
3232
var modelService = new ModelService(apiClient, hostCache, Substitute.For<IAvatarProvider>());
3333
var loginManager = Substitute.For<ILoginManager>();
34-
loginManager.Login(HostAddress.GitHubDotComHostAddress, Arg.Any<IGitHubClient>(), "baymax", "aPassword").Returns(CreateUserAndScopes("baymax").User);
34+
loginManager.Login(HostAddress.GitHubDotComHostAddress, Arg.Any<IGitHubClient>(), "baymax", "aPassword").Returns(CreateOctokitUser("baymax"));
3535
var loginCache = new TestLoginCache();
3636
var usage = Substitute.For<IUsageTracker>();
3737
var host = new RepositoryHost(apiClient, modelService, loginManager, loginCache, usage);
@@ -52,11 +52,11 @@ public async Task IncrementsLoginCount()
5252
apiClient.GetOrCreateApplicationAuthenticationCode(
5353
Args.TwoFactorChallengCallback, Args.String, Args.Boolean)
5454
.Returns(Observable.Return(new ApplicationAuthorization("S3CR3TS")));
55-
apiClient.GetUser().Returns(Observable.Return(CreateUserAndScopes("baymax")));
55+
apiClient.GetUser().Returns(Observable.Return(CreateOctokitUser("baymax")));
5656
var hostCache = new InMemoryBlobCache();
5757
var modelService = Substitute.For<IModelService>();
5858
var loginManager = Substitute.For<ILoginManager>();
59-
loginManager.Login(HostAddress.GitHubDotComHostAddress, Arg.Any<IGitHubClient>(), "baymax", "aPassword").Returns(CreateUserAndScopes("baymax").User);
59+
loginManager.Login(HostAddress.GitHubDotComHostAddress, Arg.Any<IGitHubClient>(), "baymax", "aPassword").Returns(CreateOctokitUser("baymax"));
6060
var loginCache = new TestLoginCache();
6161
var usage = Substitute.For<IUsageTracker>();
6262
var host = new RepositoryHost(apiClient, modelService, loginManager, loginCache, usage);
@@ -97,7 +97,7 @@ public async Task LogsTheUserInSuccessfullyAndCachesRelevantInfo()
9797
var hostCache = new InMemoryBlobCache();
9898
var modelService = new ModelService(apiClient, hostCache, Substitute.For<IAvatarProvider>());
9999
var loginManager = Substitute.For<ILoginManager>();
100-
loginManager.LoginFromCache(HostAddress.GitHubDotComHostAddress, Arg.Any<IGitHubClient>()).Returns(CreateUserAndScopes("baymax").User);
100+
loginManager.LoginFromCache(HostAddress.GitHubDotComHostAddress, Arg.Any<IGitHubClient>()).Returns(CreateOctokitUser("baymax"));
101101
var loginCache = new TestLoginCache();
102102
var usage = Substitute.For<IUsageTracker>();
103103
var host = new RepositoryHost(apiClient, modelService, loginManager, loginCache, usage);
@@ -118,7 +118,7 @@ public async Task IncrementsLoginCount()
118118
var hostCache = new InMemoryBlobCache();
119119
var modelService = Substitute.For<IModelService>();
120120
var loginManager = Substitute.For<ILoginManager>();
121-
loginManager.LoginFromCache(HostAddress.GitHubDotComHostAddress, Arg.Any<IGitHubClient>()).Returns(CreateUserAndScopes("baymax").User);
121+
loginManager.LoginFromCache(HostAddress.GitHubDotComHostAddress, Arg.Any<IGitHubClient>()).Returns(CreateOctokitUser("baymax"));
122122
var loginCache = new TestLoginCache();
123123
var usage = Substitute.For<IUsageTracker>();
124124
var host = new RepositoryHost(apiClient, modelService, loginManager, loginCache, usage);

src/UnitTests/Helpers/TestBaseClass.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@ public virtual void OnExit()
2424
{
2525
}
2626

27-
protected static UserAndScopes CreateUserAndScopes(string login, string[] scopes = null)
28-
{
29-
return new UserAndScopes(CreateOctokitUser(login), scopes);
30-
}
31-
3227
protected static User CreateOctokitUser(string login = "login", string url = "https://url")
3328
{
3429
return new User("https://url", "bio", "blog", 1, "GitHub",

0 commit comments

Comments
 (0)