Skip to content

Commit edc322b

Browse files
authored
feat: support multiple bearer tokens (#13)
* Update PullRequestStatusCheckController.cs * Fix tests
1 parent f455055 commit edc322b

File tree

7 files changed

+63
-5
lines changed

7 files changed

+63
-5
lines changed

.idea/.idea.Testably.Server/.idea/.gitignore

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/.idea.Testably.Server/.idea/encodings.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/.idea.Testably.Server/.idea/vcs.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Source/Testably.Server/Controllers/PullRequestStatusCheckController.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,20 @@ public async Task<IActionResult> OnPullRequestChanged(
6767
return BadRequest($"Only public repositories from '{string.Join(", ", RepositoryOwners)}' are supported!");
6868
}
6969

70-
var bearerToken = _configuration.GetValue<string>("GithubBearerToken");
70+
var bearerToken = pullRequestModel.Repository.Owner.Login switch
71+
{
72+
"Testably" => _configuration.GetValue<string>("testablyToken"),
73+
"aweXpect" => _configuration.GetValue<string>("aweXpectToken"),
74+
_ => ""
75+
};
76+
if (string.IsNullOrEmpty(bearerToken))
77+
{
78+
_logger.LogWarning("Could not find valid bearer token for {Organization}",
79+
pullRequestModel.Repository.Owner.Login);
80+
return StatusCode(StatusCodes.Status403Forbidden,
81+
$"Could not find valid bearer token for {pullRequestModel.Repository.Owner.Login}");
82+
}
83+
7184
using var client = _clientFactory.CreateClient("Proxied");
7285
client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("Testably",
7386
Assembly.GetExecutingAssembly().GetName().Version.ToString()));
@@ -85,6 +98,7 @@ public async Task<IActionResult> OnPullRequestChanged(
8598
if (!response.IsSuccessStatusCode)
8699
{
87100
var responseContent = await response.Content.ReadAsStringAsync();
101+
_logger.LogWarning("GitHub API '{RequestUri}' not available: {ResponseContent}", requestUri, responseContent);
88102
return StatusCode(StatusCodes.Status500InternalServerError,
89103
$"GitHub API '{requestUri}' not available: {responseContent}");
90104
}
@@ -96,6 +110,7 @@ await response.Content.ReadAsStreamAsync(cancellationToken),
96110
if (!jsonDocument.RootElement.TryGetProperty("title", out var titleProperty) ||
97111
titleProperty.GetString() == null)
98112
{
113+
_logger.LogWarning("GitHub API '{RequestUri}' returned an invalid response (missing title).", requestUri);
99114
return StatusCode(StatusCodes.Status500InternalServerError,
100115
$"GitHub API '{requestUri}' returned an invalid response (missing title).");
101116
}
@@ -104,6 +119,7 @@ await response.Content.ReadAsStreamAsync(cancellationToken),
104119
!headProperty.TryGetProperty("sha", out var shaProperty) ||
105120
shaProperty.GetString() == null)
106121
{
122+
_logger.LogWarning("GitHub API '{RequestUri}' returned an invalid response (missing head.sha).", requestUri);
107123
return StatusCode(StatusCodes.Status500InternalServerError,
108124
$"GitHub API '{requestUri}' returned an invalid response (missing head.sha).");
109125
}

Testably.Server.sln.DotSettings

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@
449449
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpPlaceEmbeddedOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
450450
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpRenamePlacementToArrangementMigration/@EntryIndexedValue">True</s:Boolean>
451451
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpUseContinuousIndentInsideBracesMigration/@EntryIndexedValue">True</s:Boolean>
452+
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002EMemberReordering_002EMigrations_002ECSharpFileLayoutPatternRemoveIsAttributeUpgrade/@EntryIndexedValue">True</s:Boolean>
452453
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EAddAccessorOwnerDeclarationBracesMigration/@EntryIndexedValue">True</s:Boolean>
453454
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EAlwaysTreatStructAsNotReorderableMigration/@EntryIndexedValue">True</s:Boolean>
454455
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002ECSharpPlaceAttributeOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
using Microsoft.AspNetCore.Hosting;
22
using Microsoft.AspNetCore.Mvc.Testing;
33
using Microsoft.AspNetCore.TestHost;
4+
using Microsoft.Extensions.Configuration;
45
using Microsoft.Extensions.DependencyInjection;
6+
using Microsoft.Extensions.Hosting;
57

68
namespace Testably.Server.Tests;
79

810
public class TestFactory : WebApplicationFactory<Program>
911
{
12+
private readonly IConfiguration _configuration;
1013
private readonly IHttpClientFactory? _httpClientFactory;
1114

12-
public TestFactory(IHttpClientFactory? httpClientFactory = null)
15+
public TestFactory(IHttpClientFactory? httpClientFactory = null,
16+
Action<Dictionary<string, string>>? configuration = null)
1317
{
1418
_httpClientFactory = httpClientFactory;
19+
var builder = new ConfigurationBuilder();
20+
var inMemoryConfiguration = new Dictionary<string, string>();
21+
configuration?.Invoke(inMemoryConfiguration);
22+
_configuration = builder.AddInMemoryCollection(inMemoryConfiguration!).Build();
1523
}
1624

1725
/// <inheritdoc />
@@ -22,15 +30,26 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
2230
{
2331
if (_httpClientFactory != null)
2432
{
25-
var descriptor =
26-
services.SingleOrDefault(d => d.ServiceType == _httpClientFactory.GetType());
33+
var descriptor = services
34+
.SingleOrDefault(d => d.ServiceType == _httpClientFactory.GetType());
2735
if (descriptor != null)
2836
{
2937
services.Remove(descriptor);
3038
}
3139

3240
services.AddSingleton(_httpClientFactory);
3341
}
42+
43+
services.AddSingleton(_configuration);
3444
});
3545
}
46+
47+
/// <summary>
48+
/// https://stackoverflow.com/a/69825605
49+
/// </summary>
50+
protected override IHost CreateHost(IHostBuilder builder)
51+
{
52+
builder.UseContentRoot(Directory.GetCurrentDirectory());
53+
return base.CreateHost(builder);
54+
}
3655
}

Tests/Testably.Server.Tests/WebhookTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ public async Task HappyCase_ShouldSucceed(string title, string status)
3333
sentStatusCheck = c;
3434
return "";
3535
}));
36-
await using var factory = new TestFactory(httpClientFactoryMock.Object);
36+
await using var factory = new TestFactory(httpClientFactoryMock.Object,
37+
c => c.Add("testablyToken", "foo"));
3738
using var client = factory.CreateClient();
3839
client.DefaultRequestHeaders.Add("x-github-event", "pull_request");
3940
using var content = new StringContent(webhookPayload, mediaType: "application/json",

0 commit comments

Comments
 (0)