Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only set credential.provider for dynamic matches #535

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions src/shared/Core.Tests/HostProviderRegistryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public async Task HostProviderRegistry_GetProvider_Auto_HasProviders_ReturnsSupp
}

[Fact]
public async Task HostProviderRegistry_GetProvider_Auto_HasProviders_SetsProviderGlobalConfig()
public async Task HostProviderRegistry_GetProvider_Auto_HasProviders_StaticMatch_DoesNotSetProviderGlobalConfig()
{
var context = new TestCommandContext();
var registry = new HostProviderRegistry(context);
Expand All @@ -90,14 +90,41 @@ public async Task HostProviderRegistry_GetProvider_Auto_HasProviders_SetsProvide

IHostProvider result = await registry.GetProviderAsync(input);

Assert.Same(providerMock.Object, result);
Assert.False(context.Git.Configuration.Global.TryGetValue(configKey, out _));
}

[Fact]
public async Task HostProviderRegistry_GetProvider_Auto_HasProviders_DynamicMatch_SetsProviderGlobalConfig()
{
var context = new TestCommandContext();
var registry = new HostProviderRegistry(context);
var remote = new Uri("https://example.com");
InputArguments input = CreateInputArguments(remote);

string providerId = "myProvider";
string configKey = string.Format(CultureInfo.InvariantCulture,
"{0}.https://example.com.{1}",
Constants.GitConfiguration.Credential.SectionName,
Constants.GitConfiguration.Credential.Provider);

var providerMock = new Mock<IHostProvider>();
providerMock.Setup(x => x.Id).Returns(providerId);
providerMock.Setup(x => x.IsSupported(It.IsAny<InputArguments>())).Returns(false);
providerMock.Setup(x => x.IsSupported(It.IsAny<HttpResponseMessage>())).Returns(true);

registry.Register(providerMock.Object, HostProviderPriority.Normal);

IHostProvider result = await registry.GetProviderAsync(input);

Assert.Same(providerMock.Object, result);
Assert.True(context.Git.Configuration.Global.TryGetValue(configKey, out IList<string> config));
Assert.Equal(1, config.Count);
Assert.Equal(providerId, config[0]);
}

[Fact]
public async Task HostProviderRegistry_GetProvider_Auto_HasProviders_SetsProviderGlobalConfig_HostWithPath()
public async Task HostProviderRegistry_GetProvider_Auto_HasProviders_DynamicMatch_SetsProviderGlobalConfig_HostWithPath()
{
var context = new TestCommandContext();
var registry = new HostProviderRegistry(context);
Expand All @@ -112,7 +139,8 @@ public async Task HostProviderRegistry_GetProvider_Auto_HasProviders_SetsProvide

var providerMock = new Mock<IHostProvider>();
providerMock.Setup(x => x.Id).Returns(providerId);
providerMock.Setup(x => x.IsSupported(It.IsAny<InputArguments>())).Returns(true);
providerMock.Setup(x => x.IsSupported(It.IsAny<InputArguments>())).Returns(false);
providerMock.Setup(x => x.IsSupported(It.IsAny<HttpResponseMessage>())).Returns(true);

registry.Register(providerMock.Object, HostProviderPriority.Normal);

Expand Down
36 changes: 20 additions & 16 deletions src/shared/Core/HostProviderRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,24 +220,28 @@ await MatchProviderAsync(HostProviderPriority.Normal) ??
await MatchProviderAsync(HostProviderPriority.Low) ??
throw new Exception("No host provider available to service this request.");

// Set the host provider explicitly for future calls
IGitConfiguration gitConfig = _context.Git.GetConfiguration();
var keyName = string.Format(CultureInfo.InvariantCulture, "{0}.{1}.{2}",
Constants.GitConfiguration.Credential.SectionName, uri.ToString().TrimEnd('/'),
Constants.GitConfiguration.Credential.Provider);

try
{
_context.Trace.WriteLine($"Remembering host provider for '{uri}' as '{match.Id}'...");
gitConfig.Set(GitConfigurationLevel.Global, keyName, match.Id);
}
catch (Exception ex)
// If we ended up making a network call then set the host provider explicitly
// to avoid future calls!
if (probeResponse != null)
{
_context.Trace.WriteLine("Failed to set host provider!");
_context.Trace.WriteException(ex);
IGitConfiguration gitConfig = _context.Git.GetConfiguration();
var keyName = string.Format(CultureInfo.InvariantCulture, "{0}.{1}.{2}",
Constants.GitConfiguration.Credential.SectionName, uri.ToString().TrimEnd('/'),
Constants.GitConfiguration.Credential.Provider);

try
{
_context.Trace.WriteLine($"Remembering host provider for '{uri}' as '{match.Id}'...");
gitConfig.Set(GitConfigurationLevel.Global, keyName, match.Id);
}
catch (Exception ex)
{
_context.Trace.WriteLine("Failed to set host provider!");
_context.Trace.WriteException(ex);

_context.Streams.Error.WriteLine("warning: failed to remember result of host provider detection!");
_context.Streams.Error.WriteLine($"warning: try setting this manually: `git config --global {keyName} {match.Id}`");
_context.Streams.Error.WriteLine("warning: failed to remember result of host provider detection!");
_context.Streams.Error.WriteLine($"warning: try setting this manually: `git config --global {keyName} {match.Id}`");
}
}

return match;
Expand Down