Final options conversions

This commit is contained in:
Jordan Dominion
2025-08-16 01:09:10 -04:00
parent 2e0d1f5422
commit 741144ae45
6 changed files with 47 additions and 47 deletions
@@ -50,16 +50,16 @@ namespace Tgstation.Server.Host.Utils.GitHub
/// </summary>
readonly IHttpMessageHandlerFactory httpMessageHandlerFactory;
/// <summary>
/// The <see cref="IOptionsMonitor{TOptions}"/> of <see cref="GeneralConfiguration"/> for the <see cref="GitHubClientFactory"/>.
/// </summary>
readonly IOptionsMonitor<GeneralConfiguration> generalConfigurationOptions;
/// <summary>
/// The <see cref="ILogger"/> for the <see cref="GitHubClientFactory"/>.
/// </summary>
readonly ILogger<GitHubClientFactory> logger;
/// <summary>
/// The <see cref="GeneralConfiguration"/> for the <see cref="GitHubClientFactory"/>.
/// </summary>
readonly GeneralConfiguration generalConfiguration;
/// <summary>
/// Cache of created <see cref="GitHubClient"/>s and last used/expiry times, keyed by access token.
/// </summary>
@@ -76,17 +76,17 @@ namespace Tgstation.Server.Host.Utils.GitHub
/// <param name="assemblyInformationProvider">The value of <see cref="assemblyInformationProvider"/>.</param>
/// <param name="httpMessageHandlerFactory">The value of <see cref="httpMessageHandlerFactory"/>.</param>
/// <param name="logger">The value of <see cref="logger"/>.</param>
/// <param name="generalConfigurationOptions">The <see cref="IOptions{TOptions}"/> containing the value of <see cref="generalConfiguration"/>.</param>
/// <param name="generalConfigurationOptions">The <see cref="IOptions{TOptions}"/> containing the value of <see cref="generalConfigurationOptions"/>.</param>
public GitHubClientFactory(
IAssemblyInformationProvider assemblyInformationProvider,
IHttpMessageHandlerFactory httpMessageHandlerFactory,
ILogger<GitHubClientFactory> logger,
IOptions<GeneralConfiguration> generalConfigurationOptions)
IOptionsMonitor<GeneralConfiguration> generalConfigurationOptions,
ILogger<GitHubClientFactory> logger)
{
this.assemblyInformationProvider = assemblyInformationProvider ?? throw new ArgumentNullException(nameof(assemblyInformationProvider));
this.httpMessageHandlerFactory = httpMessageHandlerFactory ?? throw new ArgumentNullException(nameof(httpMessageHandlerFactory));
this.generalConfigurationOptions = generalConfigurationOptions ?? throw new ArgumentNullException(nameof(generalConfigurationOptions));
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
generalConfiguration = generalConfigurationOptions?.Value ?? throw new ArgumentNullException(nameof(generalConfigurationOptions));
clientCache = new Dictionary<string, (GitHubClient, DateTimeOffset, DateTimeOffset?)>();
clientCacheSemaphore = new SemaphoreSlim(1, 1);
@@ -98,7 +98,7 @@ namespace Tgstation.Server.Host.Utils.GitHub
/// <inheritdoc />
public async ValueTask<IGitHubClient> CreateClient(CancellationToken cancellationToken)
=> (await GetOrCreateClient(
generalConfiguration.GitHubAccessToken,
generalConfigurationOptions.CurrentValue.GitHubAccessToken,
null,
cancellationToken))!;
@@ -27,22 +27,22 @@ namespace Tgstation.Server.Host.Utils.GitHub
/// <summary>
/// The <see cref="UpdatesConfiguration"/> for the <see cref="GitHubServiceFactory"/>.
/// </summary>
readonly UpdatesConfiguration updatesConfiguration;
readonly IOptionsMonitor<UpdatesConfiguration> updatesConfigurationOptions;
/// <summary>
/// Initializes a new instance of the <see cref="GitHubServiceFactory"/> class.
/// </summary>
/// <param name="gitHubClientFactory">The value of <see cref="gitHubClientFactory"/>.</param>
/// <param name="loggerFactory">The value of <see cref="loggerFactory"/>.</param>
/// <param name="updatesConfigurationOptions">The <see cref="IOptions{TOptions}"/> containing value of <see cref="updatesConfiguration"/>.</param>
/// <param name="updatesConfigurationOptions">The value of <see cref="updatesConfigurationOptions"/>.</param>
public GitHubServiceFactory(
IGitHubClientFactory gitHubClientFactory,
ILoggerFactory loggerFactory,
IOptions<UpdatesConfiguration> updatesConfigurationOptions)
IOptionsMonitor<UpdatesConfiguration> updatesConfigurationOptions)
{
this.gitHubClientFactory = gitHubClientFactory ?? throw new ArgumentNullException(nameof(gitHubClientFactory));
this.loggerFactory = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory));
updatesConfiguration = updatesConfigurationOptions?.Value ?? throw new ArgumentNullException(nameof(updatesConfigurationOptions));
this.updatesConfigurationOptions = updatesConfigurationOptions ?? throw new ArgumentNullException(nameof(updatesConfigurationOptions));
}
/// <inheritdoc />
@@ -79,6 +79,6 @@ namespace Tgstation.Server.Host.Utils.GitHub
=> new(
gitHubClient,
loggerFactory.CreateLogger<GitHubService>(),
updatesConfiguration);
updatesConfigurationOptions.CurrentValue);
}
}
@@ -34,16 +34,16 @@ namespace Tgstation.Server.Host.Utils
/// </summary>
readonly IPlatformIdentifier platformIdentifier;
/// <summary>
/// The <see cref="IOptions{TOptions}"/> of <see cref="SwarmConfiguration"/> for the <see cref="PortAllocator"/>.
/// </summary>
readonly IOptions<SwarmConfiguration> swarmConfigurationOptions;
/// <summary>
/// The <see cref="ILogger"/> for the <see cref="PortAllocator"/>.
/// </summary>
readonly ILogger<PortAllocator> logger;
/// <summary>
/// The <see cref="SwarmConfiguration"/> for the <see cref="PortAllocator"/>.
/// </summary>
readonly SwarmConfiguration swarmConfiguration;
/// <summary>
/// The <see cref="SemaphoreSlim"/> used to serialized port requisition requests.
/// </summary>
@@ -55,7 +55,7 @@ namespace Tgstation.Server.Host.Utils
/// <param name="serverPortProvider">The value of <see cref="serverPortProvider"/>.</param>
/// <param name="databaseContextFactory">The value of <see cref="databaseContextFactory"/>.</param>
/// <param name="platformIdentifier">The value of <see cref="platformIdentifier"/>.</param>
/// <param name="swarmConfigurationOptions">The <see cref="IOptions{TOptions}"/> containing the value of <see cref="swarmConfiguration"/>.</param>
/// <param name="swarmConfigurationOptions">The value of <see cref="swarmConfigurationOptions"/>.</param>
/// <param name="logger">The value of <see cref="logger"/>.</param>
public PortAllocator(
IServerPortProvider serverPortProvider,
@@ -67,7 +67,7 @@ namespace Tgstation.Server.Host.Utils
this.serverPortProvider = serverPortProvider ?? throw new ArgumentNullException(nameof(serverPortProvider));
this.databaseContextFactory = databaseContextFactory ?? throw new ArgumentNullException(nameof(databaseContextFactory));
this.platformIdentifier = platformIdentifier ?? throw new ArgumentNullException(nameof(platformIdentifier));
swarmConfiguration = swarmConfigurationOptions?.Value ?? throw new ArgumentNullException(nameof(swarmConfigurationOptions));
this.swarmConfigurationOptions = swarmConfigurationOptions ?? throw new ArgumentNullException(nameof(swarmConfigurationOptions));
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
allocatorLock = new SemaphoreSlim(1);
@@ -99,7 +99,7 @@ namespace Tgstation.Server.Host.Utils
logger.LogTrace("Port allocation >= {basePort} requested...", basePort);
var ddPorts = await databaseContext
.DreamDaemonSettings
.Where(x => x.Instance!.SwarmIdentifer == swarmConfiguration.Identifier)
.Where(x => x.Instance!.SwarmIdentifer == swarmConfigurationOptions.Value.Identifier)
.Select(x => new
{
Port = x.Port!.Value,
@@ -109,7 +109,7 @@ namespace Tgstation.Server.Host.Utils
var dmPorts = await databaseContext
.DreamMakerSettings
.Where(x => x.Instance!.SwarmIdentifer == swarmConfiguration.Identifier)
.Where(x => x.Instance!.SwarmIdentifer == swarmConfigurationOptions.Value.Identifier)
.Select(x => new
{
ApiValidationPort = x.ApiValidationPort!.Value,
@@ -47,7 +47,7 @@ namespace Tgstation.Server.Host.Utils.GitHub.Tests
Assert.ThrowsExactly<ArgumentNullException>(() => new GitHubClientFactory(null, null, null, null));
Assert.ThrowsExactly<ArgumentNullException>(() => new GitHubClientFactory(Mock.Of<IAssemblyInformationProvider>(), null, null, null));
Assert.ThrowsExactly<ArgumentNullException>(() => new GitHubClientFactory(Mock.Of<IAssemblyInformationProvider>(), Mock.Of<IHttpMessageHandlerFactory>(), null, null));
Assert.ThrowsExactly<ArgumentNullException>(() => new GitHubClientFactory(Mock.Of<IAssemblyInformationProvider>(), Mock.Of<IHttpMessageHandlerFactory>(), Mock.Of<ILogger<GitHubClientFactory>>(), null));
Assert.ThrowsExactly<ArgumentNullException>(() => new GitHubClientFactory(Mock.Of<IAssemblyInformationProvider>(), Mock.Of<IHttpMessageHandlerFactory>(), Mock.Of<IOptionsMonitor<GeneralConfiguration>>(), null));
}
[TestMethod]
@@ -56,12 +56,12 @@ namespace Tgstation.Server.Host.Utils.GitHub.Tests
var mockApp = new Mock<IAssemblyInformationProvider>();
mockApp.SetupGet(x => x.ProductInfoHeaderValue).Returns(new ProductInfoHeaderValue("TGSTests", "1.2.3")).Verifiable();
var mockOptions = new Mock<IOptions<GeneralConfiguration>>();
var mockOptions = new Mock<IOptionsMonitor<GeneralConfiguration>>();
var gc = new GeneralConfiguration();
Assert.IsNull(gc.GitHubAccessToken);
mockOptions.SetupGet(x => x.Value).Returns(gc);
var factory = new GitHubClientFactory(mockApp.Object, new BasicHttpMessageHandlerFactory(), loggerFactory.CreateLogger<GitHubClientFactory>(), mockOptions.Object);
mockOptions.SetupGet(x => x.CurrentValue).Returns(gc);
var factory = new GitHubClientFactory(mockApp.Object, new BasicHttpMessageHandlerFactory(), mockOptions.Object, loggerFactory.CreateLogger<GitHubClientFactory>());
var client = await factory.CreateClient(CancellationToken.None);
Assert.IsNotNull(client);
@@ -85,9 +85,9 @@ namespace Tgstation.Server.Host.Utils.GitHub.Tests
var mockApp = new Mock<IAssemblyInformationProvider>();
mockApp.SetupGet(x => x.ProductInfoHeaderValue).Returns(new ProductInfoHeaderValue("TGSTests", "1.2.3")).Verifiable();
var mockOptions = new Mock<IOptions<GeneralConfiguration>>();
mockOptions.SetupGet(x => x.Value).Returns(new GeneralConfiguration());
var factory = new GitHubClientFactory(mockApp.Object, new BasicHttpMessageHandlerFactory(), loggerFactory.CreateLogger<GitHubClientFactory>(), mockOptions.Object);
var mockOptions = new Mock<IOptionsMonitor<GeneralConfiguration>>();
mockOptions.SetupGet(x => x.CurrentValue).Returns(new GeneralConfiguration());
var factory = new GitHubClientFactory(mockApp.Object, new BasicHttpMessageHandlerFactory(), mockOptions.Object, loggerFactory.CreateLogger<GitHubClientFactory>());
await Assert.ThrowsExactlyAsync<ArgumentNullException>(() => factory.CreateClient(null, CancellationToken.None).AsTask());
@@ -107,9 +107,9 @@ namespace Tgstation.Server.Host.Utils.GitHub.Tests
var mockApp = new Mock<IAssemblyInformationProvider>();
mockApp.SetupGet(x => x.ProductInfoHeaderValue).Returns(new ProductInfoHeaderValue("TGSTests", "1.2.3")).Verifiable();
var mockOptions = new Mock<IOptions<GeneralConfiguration>>();
mockOptions.SetupGet(x => x.Value).Returns(new GeneralConfiguration());
var factory = new GitHubClientFactory(mockApp.Object, new BasicHttpMessageHandlerFactory(), loggerFactory.CreateLogger<GitHubClientFactory>(), mockOptions.Object);
var mockOptions = new Mock<IOptionsMonitor<GeneralConfiguration>>();
mockOptions.SetupGet(x => x.CurrentValue).Returns(new GeneralConfiguration());
var factory = new GitHubClientFactory(mockApp.Object, new BasicHttpMessageHandlerFactory(), mockOptions.Object, loggerFactory.CreateLogger<GitHubClientFactory>());
var appID = Environment.GetEnvironmentVariable("TGS_TEST_APP_ID");
var privateKey = Environment.GetEnvironmentVariable("TGS_TEST_APP_PRIVATE_KEY");
@@ -145,9 +145,9 @@ namespace Tgstation.Server.Host.Utils.GitHub.Tests
var mockApp = new Mock<IAssemblyInformationProvider>();
mockApp.SetupGet(x => x.ProductInfoHeaderValue).Returns(new ProductInfoHeaderValue("TGSTests", "1.2.3")).Verifiable();
var mockOptions = new Mock<IOptions<GeneralConfiguration>>();
mockOptions.SetupGet(x => x.Value).Returns(new GeneralConfiguration());
var factory = new GitHubClientFactory(mockApp.Object, new BasicHttpMessageHandlerFactory(), loggerFactory.CreateLogger<GitHubClientFactory>(), mockOptions.Object);
var mockOptions = new Mock<IOptionsMonitor<GeneralConfiguration>>();
mockOptions.SetupGet(x => x.CurrentValue).Returns(new GeneralConfiguration());
var factory = new GitHubClientFactory(mockApp.Object, new BasicHttpMessageHandlerFactory(), mockOptions.Object, loggerFactory.CreateLogger<GitHubClientFactory>());
await Assert.ThrowsExactlyAsync<ArgumentNullException>(() => factory.CreateClient(null, CancellationToken.None).AsTask());
@@ -193,9 +193,9 @@ vTdVAoGBAI/jjUMdjkY43zhe3w2piwT0fhGfqm9ikdAB9IcgcptuS0ML0ZaWV/eO
var mockApp = new Mock<IAssemblyInformationProvider>();
mockApp.SetupGet(x => x.ProductInfoHeaderValue).Returns(new ProductInfoHeaderValue("TGSTests", "1.2.3")).Verifiable();
var mockOptions = new Mock<IOptions<GeneralConfiguration>>();
mockOptions.SetupGet(x => x.Value).Returns(new GeneralConfiguration());
var factory = new GitHubClientFactory(mockApp.Object, new BasicHttpMessageHandlerFactory(), loggerFactory.CreateLogger<GitHubClientFactory>(), mockOptions.Object);
var mockOptions = new Mock<IOptionsMonitor<GeneralConfiguration>>();
mockOptions.SetupGet(x => x.CurrentValue).Returns(new GeneralConfiguration());
var factory = new GitHubClientFactory(mockApp.Object, new BasicHttpMessageHandlerFactory(), mockOptions.Object, loggerFactory.CreateLogger<GitHubClientFactory>());
var client1 = await factory.CreateClient(CancellationToken.None);
var client2 = await factory.CreateClient("asdf", CancellationToken.None);
@@ -23,8 +23,8 @@ namespace Tgstation.Server.Host.Utils.GitHub.Tests
Assert.ThrowsExactly<ArgumentNullException>(() => new GitHubServiceFactory(null, null, null));
Assert.ThrowsExactly<ArgumentNullException>(() => new GitHubServiceFactory(Mock.Of<IGitHubClientFactory>(), null, null));
Assert.ThrowsExactly<ArgumentNullException>(() => new GitHubServiceFactory(Mock.Of<IGitHubClientFactory>(), Mock.Of<ILoggerFactory>(), null));
var mockOptions = new Mock<IOptions<UpdatesConfiguration>>();
mockOptions.SetupGet(x => x.Value).Returns(new UpdatesConfiguration());
var mockOptions = new Mock<IOptionsMonitor<UpdatesConfiguration>>();
mockOptions.SetupGet(x => x.CurrentValue).Returns(new UpdatesConfiguration());
_ = new GitHubServiceFactory(Mock.Of<IGitHubClientFactory>(), Mock.Of<ILoggerFactory>(), mockOptions.Object);
}
@@ -41,8 +41,8 @@ namespace Tgstation.Server.Host.Utils.GitHub.Tests
mockFactory.Setup(x => x.CreateClient(mockToken, It.IsAny<CancellationToken>())).Returns(ValueTask.FromResult(Mock.Of<IGitHubClient>())).Verifiable();
#pragma warning restore CA2012 // Use ValueTasks correctly
var mockOptions = new Mock<IOptions<UpdatesConfiguration>>();
mockOptions.SetupGet(x => x.Value).Returns(new UpdatesConfiguration());
var mockOptions = new Mock<IOptionsMonitor<UpdatesConfiguration>>();
mockOptions.SetupGet(x => x.CurrentValue).Returns(new UpdatesConfiguration());
var factory = new GitHubServiceFactory(mockFactory.Object, Mock.Of<ILoggerFactory>(), mockOptions.Object);
@@ -34,13 +34,13 @@ namespace Tgstation.Server.Tests.Live
static TestingGitHubService()
{
var mockOptions = new Mock<IOptions<GeneralConfiguration>>();
mockOptions.SetupGet(x => x.Value).Returns(new GeneralConfiguration
var mockOptions = new Mock<IOptionsMonitor<GeneralConfiguration>>();
mockOptions.SetupGet(x => x.CurrentValue).Returns(new GeneralConfiguration
{
GitHubAccessToken = Environment.GetEnvironmentVariable("TGS_TEST_GITHUB_TOKEN")
});
var gitHubClientFactory = new GitHubClientFactory(new AssemblyInformationProvider(), new BasicHttpMessageHandlerFactory(), Mock.Of<ILogger<GitHubClientFactory>>(), mockOptions.Object);
var gitHubClientFactory = new GitHubClientFactory(new AssemblyInformationProvider(), new BasicHttpMessageHandlerFactory(), mockOptions.Object, Mock.Of<ILogger<GitHubClientFactory>>());
RealClient = gitHubClientFactory.CreateClient(CancellationToken.None).GetAwaiter().GetResult();
}