Skip to content

Commit 2a37974

Browse files
committed
gitlab: add in-proc UI implementation
1 parent 5c8d929 commit 2a37974

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

src/shared/GitLab.Tests/GitLabAuthenticationTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public async Task GitLabAuthentication_GetAuthenticationAsync_SingleChoice_Termi
2626
context.Settings.IsTerminalPromptsEnabled = false;
2727
context.Settings.IsInteractionAllowed = false;
2828
context.SessionManager.IsDesktopSession = true; // necessary for browser
29+
context.Settings.IsGuiPromptsEnabled = false;
2930
var auth = new GitLabAuthentication(context);
3031
var result = await auth.GetAuthenticationAsync(null, null, modes);
3132
Assert.Equal(modes, result.AuthenticationMode);
@@ -49,6 +50,7 @@ public async Task GitLabAuthentication_GetAuthenticationAsync_Terminal()
4950
var context = new TestCommandContext();
5051
var auth = new GitLabAuthentication(context);
5152
context.SessionManager.IsDesktopSession = true;
53+
context.Settings.IsGuiPromptsEnabled = false;
5254
context.Terminal.Prompts["option (enter for default)"] = "";
5355
var result = await auth.GetAuthenticationAsync(null, null, AuthenticationModes.All);
5456
Assert.Equal(AuthenticationModes.Browser, result.AuthenticationMode);

src/shared/GitLab/GitLabAuthentication.cs

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
using GitCredentialManager;
88
using GitCredentialManager.Authentication;
99
using GitCredentialManager.Authentication.OAuth;
10+
using GitCredentialManager.UI;
11+
using GitLab.UI.ViewModels;
12+
using GitLab.UI.Views;
1013

1114
namespace GitLab
1215
{
@@ -67,15 +70,66 @@ public async Task<AuthenticationPromptResult> GetAuthenticationAsync(Uri targetU
6770
throw new ArgumentException(@$"Must specify at least one {nameof(AuthenticationModes)}", nameof(modes));
6871
}
6972

70-
if (Context.Settings.IsGuiPromptsEnabled && Context.SessionManager.IsDesktopSession &&
71-
TryFindHelperCommand(out string helperCommand, out string args))
73+
if (Context.Settings.IsGuiPromptsEnabled && Context.SessionManager.IsDesktopSession)
7274
{
73-
return await GetAuthenticationViaHelperAsync(targetUri, userName, modes, helperCommand, args);
75+
if (TryFindHelperCommand(out string helperCommand, out string args))
76+
{
77+
return await GetAuthenticationViaHelperAsync(targetUri, userName, modes, helperCommand, args);
78+
}
79+
80+
return await GetAuthenticationViaUiAsync(targetUri, userName, modes);
7481
}
7582

7683
return GetAuthenticationViaTty(targetUri, userName, modes);
7784
}
7885

86+
private async Task<AuthenticationPromptResult> GetAuthenticationViaUiAsync(
87+
Uri targetUri, string userName, AuthenticationModes modes)
88+
{
89+
var viewModel = new CredentialsViewModel(Context.Environment)
90+
{
91+
ShowBrowserLogin = (modes & AuthenticationModes.Browser) != 0,
92+
ShowTokenLogin = (modes & AuthenticationModes.Pat) != 0,
93+
ShowBasicLogin = (modes & AuthenticationModes.Basic) != 0,
94+
};
95+
96+
if (!GitLabConstants.IsGitLabDotCom(targetUri))
97+
{
98+
viewModel.Url = targetUri.ToString();
99+
}
100+
101+
if (!string.IsNullOrWhiteSpace(userName))
102+
{
103+
viewModel.UserName = userName;
104+
viewModel.TokenUserName = userName;
105+
}
106+
107+
await AvaloniaUi.ShowViewAsync<CredentialsView>(viewModel, GetParentWindowHandle(), CancellationToken.None);
108+
109+
ThrowIfWindowCancelled(viewModel);
110+
111+
switch (viewModel.SelectedMode)
112+
{
113+
case AuthenticationModes.Basic:
114+
return new AuthenticationPromptResult(
115+
AuthenticationModes.Basic,
116+
new GitCredential(viewModel.UserName, viewModel.Password)
117+
);
118+
119+
case AuthenticationModes.Browser:
120+
return new AuthenticationPromptResult(AuthenticationModes.Browser);
121+
122+
case AuthenticationModes.Pat:
123+
return new AuthenticationPromptResult(
124+
AuthenticationModes.Pat,
125+
new GitCredential(viewModel.TokenUserName, viewModel.Token)
126+
);
127+
128+
default:
129+
throw new ArgumentOutOfRangeException();
130+
}
131+
}
132+
79133
private AuthenticationPromptResult GetAuthenticationViaTty(Uri targetUri, string userName, AuthenticationModes modes)
80134
{
81135
switch (modes)

0 commit comments

Comments
 (0)