Skip to content

Commit 7c4381c

Browse files
authored
Merge pull request #535 from mjcheetham/dynamic-provider-setting-only
Only set `credential.provider` for dynamic matches
2 parents 0d360ac + 402c9d2 commit 7c4381c

File tree

2 files changed

+51
-19
lines changed

2 files changed

+51
-19
lines changed

src/shared/Core.Tests/HostProviderRegistryTests.cs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public async Task HostProviderRegistry_GetProvider_Auto_HasProviders_ReturnsSupp
6969
}
7070

7171
[Fact]
72-
public async Task HostProviderRegistry_GetProvider_Auto_HasProviders_SetsProviderGlobalConfig()
72+
public async Task HostProviderRegistry_GetProvider_Auto_HasProviders_StaticMatch_DoesNotSetProviderGlobalConfig()
7373
{
7474
var context = new TestCommandContext();
7575
var registry = new HostProviderRegistry(context);
@@ -90,14 +90,41 @@ public async Task HostProviderRegistry_GetProvider_Auto_HasProviders_SetsProvide
9090

9191
IHostProvider result = await registry.GetProviderAsync(input);
9292

93+
Assert.Same(providerMock.Object, result);
94+
Assert.False(context.Git.Configuration.Global.TryGetValue(configKey, out _));
95+
}
96+
97+
[Fact]
98+
public async Task HostProviderRegistry_GetProvider_Auto_HasProviders_DynamicMatch_SetsProviderGlobalConfig()
99+
{
100+
var context = new TestCommandContext();
101+
var registry = new HostProviderRegistry(context);
102+
var remote = new Uri("https://example.com");
103+
InputArguments input = CreateInputArguments(remote);
104+
105+
string providerId = "myProvider";
106+
string configKey = string.Format(CultureInfo.InvariantCulture,
107+
"{0}.https://example.com.{1}",
108+
Constants.GitConfiguration.Credential.SectionName,
109+
Constants.GitConfiguration.Credential.Provider);
110+
111+
var providerMock = new Mock<IHostProvider>();
112+
providerMock.Setup(x => x.Id).Returns(providerId);
113+
providerMock.Setup(x => x.IsSupported(It.IsAny<InputArguments>())).Returns(false);
114+
providerMock.Setup(x => x.IsSupported(It.IsAny<HttpResponseMessage>())).Returns(true);
115+
116+
registry.Register(providerMock.Object, HostProviderPriority.Normal);
117+
118+
IHostProvider result = await registry.GetProviderAsync(input);
119+
93120
Assert.Same(providerMock.Object, result);
94121
Assert.True(context.Git.Configuration.Global.TryGetValue(configKey, out IList<string> config));
95122
Assert.Equal(1, config.Count);
96123
Assert.Equal(providerId, config[0]);
97124
}
98125

99126
[Fact]
100-
public async Task HostProviderRegistry_GetProvider_Auto_HasProviders_SetsProviderGlobalConfig_HostWithPath()
127+
public async Task HostProviderRegistry_GetProvider_Auto_HasProviders_DynamicMatch_SetsProviderGlobalConfig_HostWithPath()
101128
{
102129
var context = new TestCommandContext();
103130
var registry = new HostProviderRegistry(context);
@@ -112,7 +139,8 @@ public async Task HostProviderRegistry_GetProvider_Auto_HasProviders_SetsProvide
112139

113140
var providerMock = new Mock<IHostProvider>();
114141
providerMock.Setup(x => x.Id).Returns(providerId);
115-
providerMock.Setup(x => x.IsSupported(It.IsAny<InputArguments>())).Returns(true);
142+
providerMock.Setup(x => x.IsSupported(It.IsAny<InputArguments>())).Returns(false);
143+
providerMock.Setup(x => x.IsSupported(It.IsAny<HttpResponseMessage>())).Returns(true);
116144

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

src/shared/Core/HostProviderRegistry.cs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -220,24 +220,28 @@ await MatchProviderAsync(HostProviderPriority.Normal) ??
220220
await MatchProviderAsync(HostProviderPriority.Low) ??
221221
throw new Exception("No host provider available to service this request.");
222222

223-
// Set the host provider explicitly for future calls
224-
IGitConfiguration gitConfig = _context.Git.GetConfiguration();
225-
var keyName = string.Format(CultureInfo.InvariantCulture, "{0}.{1}.{2}",
226-
Constants.GitConfiguration.Credential.SectionName, uri.ToString().TrimEnd('/'),
227-
Constants.GitConfiguration.Credential.Provider);
228-
229-
try
230-
{
231-
_context.Trace.WriteLine($"Remembering host provider for '{uri}' as '{match.Id}'...");
232-
gitConfig.Set(GitConfigurationLevel.Global, keyName, match.Id);
233-
}
234-
catch (Exception ex)
223+
// If we ended up making a network call then set the host provider explicitly
224+
// to avoid future calls!
225+
if (probeResponse != null)
235226
{
236-
_context.Trace.WriteLine("Failed to set host provider!");
237-
_context.Trace.WriteException(ex);
227+
IGitConfiguration gitConfig = _context.Git.GetConfiguration();
228+
var keyName = string.Format(CultureInfo.InvariantCulture, "{0}.{1}.{2}",
229+
Constants.GitConfiguration.Credential.SectionName, uri.ToString().TrimEnd('/'),
230+
Constants.GitConfiguration.Credential.Provider);
231+
232+
try
233+
{
234+
_context.Trace.WriteLine($"Remembering host provider for '{uri}' as '{match.Id}'...");
235+
gitConfig.Set(GitConfigurationLevel.Global, keyName, match.Id);
236+
}
237+
catch (Exception ex)
238+
{
239+
_context.Trace.WriteLine("Failed to set host provider!");
240+
_context.Trace.WriteException(ex);
238241

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

243247
return match;

0 commit comments

Comments
 (0)