Skip to content

Commit 4a8d5e4

Browse files
committed
bitbucket: add in-proc UI implementation
1 parent f120a12 commit 4a8d5e4

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

src/shared/Atlassian.Bitbucket.Tests/BitbucketAuthenticationTest.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public async Task BitbucketAuthentication_GetCredentialsAsync_All_ShowsMenu_OAut
5151
{
5252
var context = new TestCommandContext();
5353
context.SessionManager.IsDesktopSession = true; // Allow OAuth mode
54+
context.Settings.IsGuiPromptsEnabled = false; // Force text prompts
5455
context.Terminal.Prompts["option (enter for default)"] = "1";
5556
Uri targetUri = null;
5657

@@ -71,6 +72,7 @@ public async Task BitbucketAuthentication_GetCredentialsAsync_All_ShowsMenu_Basi
7172

7273
var context = new TestCommandContext();
7374
context.SessionManager.IsDesktopSession = true; // Allow OAuth mode
75+
context.Settings.IsGuiPromptsEnabled = false; // Force text prompts
7476
context.Terminal.Prompts["option (enter for default)"] = "2";
7577
context.Terminal.Prompts["Username"] = username;
7678
context.Terminal.SecretPrompts["Password"] = password;

src/shared/Atlassian.Bitbucket/BitbucketAuthentication.cs

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
using System.Text;
55
using System.Threading;
66
using System.Threading.Tasks;
7+
using Atlassian.Bitbucket.UI.ViewModels;
8+
using Atlassian.Bitbucket.UI.Views;
79
using GitCredentialManager;
810
using GitCredentialManager.Authentication;
911
using GitCredentialManager.Authentication.OAuth;
12+
using GitCredentialManager.UI;
1013

1114
namespace Atlassian.Bitbucket
1215
{
@@ -89,15 +92,51 @@ public async Task<CredentialsPromptResult> GetCredentialsAsync(Uri targetUri, st
8992
}
9093

9194
// Shell out to the UI helper and show the Bitbucket u/p prompt
92-
if (Context.Settings.IsGuiPromptsEnabled && Context.SessionManager.IsDesktopSession &&
93-
TryFindHelperCommand(out string helperCommand, out string args))
95+
if (Context.Settings.IsGuiPromptsEnabled && Context.SessionManager.IsDesktopSession)
9496
{
95-
return await GetCredentialsViaHelperAsync(targetUri, userName, modes, helperCommand, args);
97+
if (TryFindHelperCommand(out string helperCommand, out string args))
98+
{
99+
return await GetCredentialsViaHelperAsync(targetUri, userName, modes, helperCommand, args);
100+
}
101+
102+
return await GetCredentialsViaUiAsync(targetUri, userName, modes);
96103
}
97104

98105
return GetCredentialsViaTty(targetUri, userName, modes);
99106
}
100107

108+
private async Task<CredentialsPromptResult> GetCredentialsViaUiAsync(
109+
Uri targetUri, string userName, AuthenticationModes modes)
110+
{
111+
var viewModel = new CredentialsViewModel(Context.Environment)
112+
{
113+
Url = targetUri,
114+
UserName = userName,
115+
ShowOAuth = (modes & AuthenticationModes.OAuth) != 0,
116+
ShowBasic = (modes & AuthenticationModes.Basic) != 0
117+
};
118+
119+
await AvaloniaUi.ShowViewAsync<CredentialsView>(viewModel, GetParentWindowHandle(), CancellationToken.None);
120+
121+
ThrowIfWindowCancelled(viewModel);
122+
123+
switch (viewModel.SelectedMode)
124+
{
125+
case AuthenticationModes.OAuth:
126+
return new CredentialsPromptResult(AuthenticationModes.OAuth);
127+
128+
case AuthenticationModes.Basic:
129+
return new CredentialsPromptResult(
130+
AuthenticationModes.Basic,
131+
new GitCredential(viewModel.UserName, viewModel.Password)
132+
);
133+
134+
default:
135+
throw new ArgumentOutOfRangeException(nameof(AuthenticationModes),
136+
"Unknown authentication mode", viewModel.SelectedMode.ToString());
137+
}
138+
}
139+
101140
private CredentialsPromptResult GetCredentialsViaTty(Uri targetUri, string userName, AuthenticationModes modes)
102141
{
103142
ThrowIfTerminalPromptsDisabled();

0 commit comments

Comments
 (0)