Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

Fix detecting GitHub Enterprise Server behind a HAProxy #2562

Merged
merged 9 commits into from
Mar 18, 2021
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
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ jobs:
shell: bash
run: |
vstest.console /TestAdapterPath:test /Settings:test/test.runsettings \
test/GitHub.Api.UnitTests/bin/${{ env.config }}/net46/GitHub.Api.UnitTests.dll \
test/GitHub.App.UnitTests/bin/${{ env.config }}/net46/GitHub.App.UnitTests.dll \
test/GitHub.Exports.Reactive.UnitTests/bin/${{ env.config }}/net46/GitHub.Exports.Reactive.UnitTests.dll \
test/GitHub.Exports.UnitTests/bin/${{ env.config }}/net46/GitHub.Exports.UnitTests.dll \
Expand Down
9 changes: 7 additions & 2 deletions src/GitHub.Api/LoginManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,14 @@ async Task<LoginResult> GetUserAndCheckScopes(IGitHubClient client)
var response = await client.Connection.Get<User>(
UserEndpoint, null, null).ConfigureAwait(false);

if (response.HttpResponse.Headers.ContainsKey(ScopesHeader))
var scopes = response.HttpResponse.Headers
.Where(h => string.Equals(h.Key, ScopesHeader, StringComparison.OrdinalIgnoreCase))
.Select(h => h.Value)
.FirstOrDefault();

if (scopes != null)
{
var returnedScopes = new ScopesCollection(response.HttpResponse.Headers[ScopesHeader]
var returnedScopes = new ScopesCollection(scopes
.Split(',')
.Select(x => x.Trim())
.ToArray());
Expand Down
4 changes: 2 additions & 2 deletions src/GitHub.Exports/ExceptionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using Octokit;

namespace GitHub.Extensions
Expand All @@ -9,8 +10,7 @@ public static class ApiExceptionExtensions
public static bool IsGitHubApiException(this Exception ex)
{
var apiex = ex as ApiException;
return apiex?.HttpResponse?.Headers.ContainsKey(GithubHeader) ?? false;
return apiex?.HttpResponse?.Headers.Keys.Contains(GithubHeader, StringComparer.OrdinalIgnoreCase) ?? false;
}
}

}
2 changes: 1 addition & 1 deletion submodules/octokit.net
21 changes: 19 additions & 2 deletions test/GitHub.Api.UnitTests/LoginManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,30 @@ public void InvalidResponseScopesCauseException()
Assert.ThrowsAsync<IncorrectScopesException>(() => target.Login(host, client, "foo", "bar"));
}

IGitHubClient CreateClient(User user = null, string[] responseScopes = null)
[TestCase("X-OAuth-Scopes")]
[TestCase("x-oauth-scopes")]
public void ValidResponseScopesDoesNotThrow(string scopesHeader)
{
var client = CreateClient(responseScopes: scopes, scopesHeader: scopesHeader);
client.Authorization.GetOrCreateApplicationAuthentication("id", "secret", Arg.Any<NewAuthorization>())
.Returns(CreateApplicationAuthorization("123abc"));

var keychain = Substitute.For<IKeychain>();
var tfa = new Lazy<ITwoFactorChallengeHandler>(() => Substitute.For<ITwoFactorChallengeHandler>());
var oauthListener = Substitute.For<IOAuthCallbackListener>();

var target = new LoginManager(keychain, tfa, oauthListener, "id", "secret", scopes, scopes);

Assert.DoesNotThrowAsync(() => target.Login(host, client, "foo", "bar"));
}

IGitHubClient CreateClient(User user = null, string[] responseScopes = null, string scopesHeader = "X-OAuth-Scopes")
{
var result = Substitute.For<IGitHubClient>();
var userResponse = Substitute.For<IApiResponse<User>>();
userResponse.HttpResponse.Headers.Returns(new Dictionary<string, string>
{
{ "X-OAuth-Scopes", string.Join(",", responseScopes ?? scopes) }
{ scopesHeader, string.Join(",", responseScopes ?? scopes) }
});
userResponse.Body.Returns(user ?? new User());
result.Connection.Get<User>(new Uri("user", UriKind.Relative), null, null).Returns(userResponse);
Expand Down
42 changes: 42 additions & 0 deletions test/GitHub.Exports.UnitTests/ApiExceptionExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using Octokit;
using NSubstitute;
using NUnit.Framework;
using GitHub.Extensions;

public class ApiExceptionExtensionsTests
{
public class TheIsGitHubApiExceptionMethod
{
[TestCase("Not-GitHub-Request-Id", false)]
[TestCase("X-GitHub-Request-Id", true)]
[TestCase("x-github-request-id", true)]
public void NoGitHubRequestId(string key, bool expect)
{
var ex = CreateApiException(new Dictionary<string, string> { { key, "ANYTHING" } });

var result = ApiExceptionExtensions.IsGitHubApiException(ex);

Assert.That(result, Is.EqualTo(expect));
}

[Test]
public void NoResponse()
{
var ex = new ApiException();

var result = ApiExceptionExtensions.IsGitHubApiException(ex);

Assert.That(result, Is.EqualTo(false));
}

static ApiException CreateApiException(Dictionary<string, string> headers)
{
var response = Substitute.For<IResponse>();
response.Headers.Returns(headers.ToImmutableDictionary());
var ex = new ApiException(response);
return ex;
}
}
}