Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit a3b896a

Browse files
Merge pull request #820 from github-for-unity/fixes/login-logout
Fix sign in and out
2 parents 5040bdc + ad23dcd commit a3b896a

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

src/GitHub.Api/Primitives/UriString.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ static string GetSerializedValue(SerializationInfo info)
267267

268268
static string NormalizePath(string path)
269269
{
270-
return path?.Replace('\\', '/');
270+
return path?.Replace('\\', '/').TrimEnd('/');
271271
}
272272

273273
static string GetRepositoryName(string repositoryNameSegment)

src/UnityExtension/Assets/Editor/GitHub.Unity/UI/Window.cs

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class Window : BaseWindow
5454
[SerializeField] private string repositoryProgressMessage;
5555
[SerializeField] private float appManagerProgressValue;
5656
[SerializeField] private string appManagerProgressMessage;
57+
[SerializeField] private Connection connection;
5758

5859
[MenuItem(Menu_Window_GitHub)]
5960
public static void Window_GitHub()
@@ -210,6 +211,8 @@ private void ValidateCachedData(IRepository repository)
210211

211212
private void MaybeUpdateData()
212213
{
214+
connection = Platform.Keychain.Connections.FirstOrDefault();
215+
213216
if (repositoryProgressHasUpdate)
214217
{
215218
if (repositoryProgress != null)
@@ -351,6 +354,7 @@ private void AttachHandlers(IRepository repository)
351354
repository.TrackingStatusChanged += RepositoryOnTrackingStatusChanged;
352355
repository.StatusEntriesChanged += RepositoryOnStatusEntriesChanged;
353356
repository.OnProgress += UpdateProgress;
357+
Platform.Keychain.ConnectionsChanged += ConnectionsChanged;
354358
}
355359

356360
private void DetachHandlers(IRepository repository)
@@ -362,6 +366,7 @@ private void DetachHandlers(IRepository repository)
362366
repository.StatusEntriesChanged -= RepositoryOnStatusEntriesChanged;
363367
repository.OnProgress -= UpdateProgress;
364368
Manager.OnProgress -= ApplicationManagerOnProgress;
369+
Platform.Keychain.ConnectionsChanged -= ConnectionsChanged;
365370
}
366371

367372
private void RepositoryOnCurrentBranchAndRemoteChanged(CacheUpdateEvent cacheUpdateEvent)
@@ -419,6 +424,14 @@ private void ApplicationManagerOnProgress(IProgress progress)
419424
appManagerProgressHasUpdate = true;
420425
}
421426

427+
private void ConnectionsChanged()
428+
{
429+
if (!ThreadingHelper.InUIThread)
430+
TaskManager.RunInUI(Redraw);
431+
else
432+
Redraw();
433+
}
434+
422435
public override void OnUI()
423436
{
424437
base.OnUI();
@@ -610,8 +623,18 @@ private void DoActionbarGUI()
610623

611624
GUILayout.FlexibleSpace();
612625

613-
if (GUILayout.Button(Localization.AccountButton, EditorStyles.toolbarDropDown))
614-
DoAccountDropdown();
626+
if (connection == null)
627+
{
628+
if (GUILayout.Button("Sign in", EditorStyles.toolbarButton))
629+
SignIn(null);
630+
}
631+
else
632+
{
633+
if (GUILayout.Button(connection.Username, EditorStyles.toolbarDropDown))
634+
{
635+
DoAccountDropdown();
636+
}
637+
}
615638
}
616639
EditorGUILayout.EndHorizontal();
617640
}
@@ -770,23 +793,15 @@ private void SwitchView(Subview fromView, Subview toView)
770793
toView.OnDataUpdate();
771794

772795
// this triggers a repaint
773-
Repaint();
796+
Redraw();
774797
}
775798

776799
private void DoAccountDropdown()
777800
{
778801
GenericMenu accountMenu = new GenericMenu();
779-
780-
if (!Platform.Keychain.HasKeys)
781-
{
782-
accountMenu.AddItem(new GUIContent("Sign in"), false, SignIn, "sign in");
783-
}
784-
else
785-
{
786-
accountMenu.AddItem(new GUIContent("Go to Profile"), false, GoToProfile, "profile");
787-
accountMenu.AddSeparator("");
788-
accountMenu.AddItem(new GUIContent("Sign out"), false, SignOut, "sign out");
789-
}
802+
accountMenu.AddItem(new GUIContent("Go to Profile"), false, GoToProfile, "profile");
803+
accountMenu.AddSeparator("");
804+
accountMenu.AddItem(new GUIContent("Sign out"), false, SignOut, "sign out");
790805
accountMenu.ShowAsContext();
791806
}
792807

@@ -809,15 +824,15 @@ private void SignOut(object obj)
809824
if (Repository != null && Repository.CloneUrl != null && Repository.CloneUrl.IsValidUri)
810825
{
811826
host = new UriString(Repository.CloneUrl.ToRepositoryUri()
812-
.GetComponents(UriComponents.SchemeAndServer, UriFormat.SafeUnescaped));
827+
.GetComponents(UriComponents.SchemeAndServer, UriFormat.SafeUnescaped));
813828
}
814829
else
815830
{
816831
host = UriString.ToUriString(HostAddress.GitHubDotComHostAddress.WebUri);
817832
}
818833

819834
var apiClient = new ApiClient(host, Platform.Keychain, null, null, NPath.Default, NPath.Default);
820-
apiClient.Logout(host);
835+
apiClient.Logout(host).FinallyInUI((s, e) => Redraw());
821836
}
822837

823838
public new void ShowNotification(GUIContent content)

src/tests/UnitTests/Authentication/KeychainTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ public void ShouldConnectSetCredentialsTokenAndSave()
305305
fileSystem.DidNotReceive().FileDelete(Args.String);
306306
fileSystem.DidNotReceive().ReadAllText(Args.String);
307307
fileSystem.DidNotReceive().ReadAllLines(Args.String);
308-
fileSystem.Received(1).WriteAllText(connectionsCacheFile, @"[{""Host"":""https://github.com/"",""Username"":""SomeUser""}]");
308+
fileSystem.Received(1).WriteAllText(connectionsCacheFile, @"[{""Host"":""https://github.com"",""Username"":""SomeUser""}]");
309309

310310
credentialManager.DidNotReceive().Load(Args.UriString);
311311
credentialManager.DidNotReceive().HasCredentials();

0 commit comments

Comments
 (0)