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

Commit 7b4080f

Browse files
committed
Merge branch 'master' into fixes/1176-duplicate-login-classes
Conflicts: src/GitHub.App/Models/RepositoryHost.cs src/UnitTests/GitHub.App/Models/RepositoryHostTests.cs
2 parents 95f2c0b + 4407e3c commit 7b4080f

34 files changed

+85
-519
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/Controllers/UIController.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,6 @@ void ConfigureUIHandlingStates()
319319
ConfigureEntriesExitsForView(UIViewType.Login);
320320
ConfigureEntriesExitsForView(UIViewType.TwoFactor);
321321
ConfigureEntriesExitsForView(UIViewType.Gist);
322-
ConfigureEntriesExitsForView(UIViewType.LogoutRequired);
323322
ConfigureEntriesExitsForView(UIViewType.StartPageClone);
324323

325324
uiStateMachine.Configure(UIViewType.End)
@@ -421,7 +420,6 @@ void ConfigureLogicStates()
421420
ConfigureSingleViewLogic(UIControllerFlow.Create, UIViewType.Create);
422421
ConfigureSingleViewLogic(UIControllerFlow.Gist, UIViewType.Gist);
423422
ConfigureSingleViewLogic(UIControllerFlow.Home, UIViewType.PRList);
424-
ConfigureSingleViewLogic(UIControllerFlow.LogoutRequired, UIViewType.LogoutRequired);
425423
ConfigureSingleViewLogic(UIControllerFlow.Publish, UIViewType.Publish);
426424
ConfigureSingleViewLogic(UIControllerFlow.PullRequestList, UIViewType.PRList);
427425
ConfigureSingleViewLogic(UIControllerFlow.PullRequestDetail, UIViewType.PRDetail);
@@ -448,11 +446,7 @@ UIControllerFlow SelectActiveFlow()
448446
{
449447
var host = connection != null ? hosts.LookupHost(connection.HostAddress) : null;
450448
var loggedIn = host?.IsLoggedIn ?? false;
451-
if (!loggedIn || selectedFlow != UIControllerFlow.Gist)
452-
return loggedIn ? selectedFlow : UIControllerFlow.Authentication;
453-
454-
var supportsGist = host?.SupportsGist ?? false;
455-
return supportsGist ? selectedFlow : UIControllerFlow.LogoutRequired;
449+
return loggedIn ? selectedFlow : UIControllerFlow.Authentication;
456450
}
457451

458452
/// <summary>

src/GitHub.App/GitHub.App.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,6 @@
213213
<Compile Include="ViewModels\NotAGitRepositoryViewModel.cs" />
214214
<Compile Include="ViewModels\NotAGitHubRepositoryViewModel.cs" />
215215
<Compile Include="ViewModels\LoggedOutViewModel.cs" />
216-
<Compile Include="ViewModels\LogoutRequiredViewModel.cs" />
217216
<Compile Include="ViewModels\PullRequestCreationViewModel.cs" />
218217
<Compile Include="ViewModels\PullRequestDetailViewModel.cs" />
219218
<Compile Include="ViewModels\PullRequestDirectoryNode.cs" />

src/GitHub.App/Models/DisconnectedRepositoryHosts.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ public DisconnectedRepositoryHost()
2525

2626
public bool IsLoggedIn { get; private set; }
2727
public bool IsLoggingIn { get; private set; }
28-
public bool SupportsGist { get; private set; }
2928
public ReactiveList<IAccount> Organizations { get; private set; }
3029
public ReactiveList<IAccount> Accounts { get; private set; }
3130
public string Title { get; private set; }

src/GitHub.App/Models/RepositoryHost.cs

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ namespace GitHub.Models
2323
public class RepositoryHost : ReactiveObject, IRepositoryHost
2424
{
2525
static readonly Logger log = LogManager.GetCurrentClassLogger();
26-
static readonly UserAndScopes unverifiedUser = new UserAndScopes(null, null);
2726

2827
readonly ILoginManager loginManager;
2928
readonly HostAddress hostAddress;
@@ -61,8 +60,6 @@ public bool IsLoggedIn
6160
private set { this.RaiseAndSetIfChanged(ref isLoggedIn, value); }
6261
}
6362

64-
public bool SupportsGist { get; private set; }
65-
6663
public string Title { get; private set; }
6764

6865
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
@@ -77,7 +74,7 @@ public IObservable<AuthenticationResult> LogInFromCache()
7774
usage.IncrementLoginCount().Forget();
7875
await ModelService.InsertUser(accountCacheItem);
7976

80-
if (user != unverifiedUser.User)
77+
if (user != null)
8178
{
8279
IsLoggedIn = true;
8380
return AuthenticationResult.Success;
@@ -108,7 +105,7 @@ public IObservable<AuthenticationResult> LogIn(string usernameOrEmail, string pa
108105
usage.IncrementLoginCount().Forget();
109106
await ModelService.InsertUser(accountCacheItem);
110107

111-
if (user != unverifiedUser.User)
108+
if (user != null)
112109
{
113110
IsLoggedIn = true;
114111
return Observable.Return(AuthenticationResult.Success);
@@ -145,53 +142,6 @@ public IObservable<Unit> LogOut()
145142
});
146143
}
147144

148-
static IObservable<AuthenticationResult> GetAuthenticationResultForUser(UserAndScopes account)
149-
{
150-
return Observable.Return(account == null ? AuthenticationResult.CredentialFailure
151-
: account == unverifiedUser
152-
? AuthenticationResult.VerificationFailure
153-
: AuthenticationResult.Success);
154-
}
155-
156-
IObservable<AuthenticationResult> LoginWithApiUser(UserAndScopes userAndScopes)
157-
{
158-
return GetAuthenticationResultForUser(userAndScopes)
159-
.SelectMany(result =>
160-
{
161-
if (result.IsSuccess())
162-
{
163-
var accountCacheItem = new AccountCacheItem(userAndScopes.User);
164-
usage.IncrementLoginCount().Forget();
165-
return ModelService.InsertUser(accountCacheItem).Select(_ => result);
166-
}
167-
168-
if (result == AuthenticationResult.VerificationFailure)
169-
{
170-
return keychain.Delete(Address).ToObservable().Select(_ => result);
171-
}
172-
return Observable.Return(result);
173-
})
174-
.ObserveOn(RxApp.MainThreadScheduler)
175-
.Do(result =>
176-
{
177-
if (result.IsSuccess())
178-
{
179-
SupportsGist = userAndScopes.Scopes?.Contains("gist") ?? true;
180-
IsLoggedIn = true;
181-
}
182-
183-
log.Info("Log in from cache for login '{0}' to host '{1}' {2}",
184-
userAndScopes?.User?.Login ?? "(null)",
185-
hostAddress.ApiUri,
186-
result.IsSuccess() ? "SUCCEEDED" : "FAILED");
187-
});
188-
}
189-
190-
IObservable<UserAndScopes> GetUserFromApi()
191-
{
192-
return Observable.Defer(() => ApiClient.GetUser());
193-
}
194-
195145
protected virtual void Dispose(bool disposing)
196146
{}
197147

src/GitHub.App/Resources.Designer.cs

Lines changed: 0 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/GitHub.App/Resources.resx

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,6 @@
159159
<data name="LoginTitle" xml:space="preserve">
160160
<value>Connect To GitHub</value>
161161
</data>
162-
<data name="LogoutRequiredFeatureGist" xml:space="preserve">
163-
<value>create a Gist</value>
164-
</data>
165-
<data name="LogoutRequiredMessage" xml:space="preserve">
166-
<value>To {0} you need to sign out and back in.</value>
167-
</data>
168-
<data name="LogoutRequiredTitle" xml:space="preserve">
169-
<value>Sign Out Required</value>
170-
</data>
171162
<data name="PasswordValidatorEmpty" xml:space="preserve">
172163
<value>Please enter your password</value>
173164
</data>

src/GitHub.App/SampleData/SampleViewModels.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -276,12 +276,6 @@ public bool IsLoggedIn
276276
private set;
277277
}
278278

279-
public bool SupportsGist
280-
{
281-
get;
282-
private set;
283-
}
284-
285279
public IModelService ModelService
286280
{
287281
get;

src/GitHub.App/Services/GitClient.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public Task Fetch(IRepository repository, string remoteName, params string[] ref
9898
var remote = repository.Network.Remotes[remoteName];
9999
repository.Network.Fetch(remote, refspecs, fetchOptions);
100100
}
101-
catch(Exception ex)
101+
catch (Exception ex)
102102
{
103103
log.Error("Failed to fetch", ex);
104104
#if DEBUG
@@ -394,7 +394,7 @@ public async Task<string> GetPullRequestMergeBase(IRepository repo,
394394
baseCommit = repo.Lookup<Commit>(baseSha);
395395
if (baseCommit == null)
396396
{
397-
return null;
397+
throw new NotFoundException($"Couldn't find {baseSha} after fetching from {baseCloneUrl}:{baseRef}.");
398398
}
399399
}
400400

@@ -405,14 +405,14 @@ public async Task<string> GetPullRequestMergeBase(IRepository repo,
405405
headCommit = repo.Lookup<Commit>(headSha);
406406
if (headCommit == null)
407407
{
408-
return null;
408+
throw new NotFoundException($"Couldn't find {headSha} after fetching from {headCloneUrl}:{headRef}.");
409409
}
410410
}
411411

412412
var mergeBaseCommit = repo.ObjectDatabase.FindMergeBase(baseCommit, headCommit);
413-
if(mergeBaseCommit == null)
413+
if (mergeBaseCommit == null)
414414
{
415-
return null;
415+
throw new NotFoundException($"Couldn't find merge base between {baseCommit} and {headCommit}.");
416416
}
417417

418418
return mergeBaseCommit.Sha;

src/GitHub.App/Services/PullRequestService.cs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -308,18 +308,20 @@ public IObservable<string> ExtractFile(
308308
}
309309
else
310310
{
311-
sha = await gitClient.GetPullRequestMergeBase(
312-
repo,
313-
pullRequest.Base.RepositoryCloneUrl,
314-
pullRequest.Head.RepositoryCloneUrl,
315-
pullRequest.Base.Sha,
316-
pullRequest.Head.Sha,
317-
pullRequest.Base.Ref,
318-
pullRequest.Head.Ref);
319-
320-
if (sha == null)
311+
try
321312
{
322-
throw new NotFoundException($"Couldn't find merge base between {pullRequest.Base.Sha} and {pullRequest.Head.Sha}.");
313+
sha = await gitClient.GetPullRequestMergeBase(
314+
repo,
315+
pullRequest.Base.RepositoryCloneUrl,
316+
pullRequest.Head.RepositoryCloneUrl,
317+
pullRequest.Base.Sha,
318+
pullRequest.Head.Sha,
319+
pullRequest.Base.Ref,
320+
pullRequest.Head.Ref);
321+
}
322+
catch (NotFoundException ex)
323+
{
324+
throw new NotFoundException($"The Pull Request file failed to load. Please check your network connection and click refresh to try again. If this issue persists, please let us know at [email protected]", ex);
323325
}
324326
}
325327

@@ -350,7 +352,7 @@ static bool HasPreamble(string file, Encoding encoding)
350352
{
351353
foreach (var b in encoding.GetPreamble())
352354
{
353-
if(b != stream.ReadByte())
355+
if (b != stream.ReadByte())
354356
{
355357
return false;
356358
}
@@ -473,7 +475,7 @@ async Task<bool> IsBranchMarkedAsPullRequest(IRepository repo, string branchName
473475
{
474476
var prConfigKey = $"branch.{branchName}.{SettingGHfVSPullRequest}";
475477
var value = ParseGHfVSConfigKeyValue(await gitClient.GetConfig<string>(repo, prConfigKey));
476-
return value != null &&
478+
return value != null &&
477479
value.Item1 == pullRequest.Base.RepositoryCloneUrl.Owner &&
478480
value.Item2 == pullRequest.Number;
479481
}
@@ -550,4 +552,4 @@ static Tuple<string, int> ParseGHfVSConfigKeyValue(string value)
550552
return null;
551553
}
552554
}
553-
}
555+
}

0 commit comments

Comments
 (0)