diff --git a/src/Tgstation.Server.Host/Controllers/RepositoryController.cs b/src/Tgstation.Server.Host/Controllers/RepositoryController.cs index 61904c5b99..c919b417fa 100644 --- a/src/Tgstation.Server.Host/Controllers/RepositoryController.cs +++ b/src/Tgstation.Server.Host/Controllers/RepositoryController.cs @@ -32,9 +32,9 @@ namespace Tgstation.Server.Host.Controllers readonly IInstanceManager instanceManager; /// - /// The for the + /// The for the /// - readonly Octokit.IGitHubClient gitHubClient; + readonly IGitHubClientFactory gitHubClientFactory; /// /// The for the @@ -47,13 +47,13 @@ namespace Tgstation.Server.Host.Controllers /// The for the /// The for the /// The value of - /// The value of + /// The value of /// The value of /// The for the - public RepositoryController(IDatabaseContext databaseContext, IAuthenticationContextFactory authenticationContextFactory, IInstanceManager instanceManager, Octokit.IGitHubClient gitHubClient, IJobManager jobManager, ILogger logger) : base(databaseContext, authenticationContextFactory, logger, true) + public RepositoryController(IDatabaseContext databaseContext, IAuthenticationContextFactory authenticationContextFactory, IInstanceManager instanceManager, IGitHubClientFactory gitHubClientFactory, IJobManager jobManager, ILogger logger) : base(databaseContext, authenticationContextFactory, logger, true) { this.instanceManager = instanceManager ?? throw new ArgumentNullException(nameof(instanceManager)); - this.gitHubClient = gitHubClient ?? throw new ArgumentNullException(nameof(gitHubClient)); + this.gitHubClientFactory = gitHubClientFactory ?? throw new ArgumentNullException(nameof(gitHubClientFactory)); this.jobManager = jobManager ?? throw new ArgumentNullException(nameof(jobManager)); } @@ -386,6 +386,7 @@ namespace Tgstation.Server.Host.Controllers //test merging if (newTestMerges) { + var gitHubClient = currentModel.AccessToken != null ? gitHubClientFactory.CreateClient(currentModel.AccessToken) : gitHubClientFactory.CreateClient(); var contextUser = new Models.User { Id = AuthenticationContext.User.Id diff --git a/src/Tgstation.Server.Host/Core/Application.cs b/src/Tgstation.Server.Host/Core/Application.cs index dd5f307a5d..5dc34e3462 100644 --- a/src/Tgstation.Server.Host/Core/Application.cs +++ b/src/Tgstation.Server.Host/Core/Application.cs @@ -40,7 +40,7 @@ namespace Tgstation.Server.Host.Core /// /// Prefix for string version names /// - const string VersionPrefix = "tgstation-server"; + public string VersionPrefix => "tgstation-server"; /// public string HostingPath => serverAddresses.Addresses.First(); @@ -187,7 +187,9 @@ namespace Tgstation.Server.Host.Core services.AddSingleton, PasswordHasher>(); services.AddSingleton(); services.AddSingleton(); - services.AddSingleton(x => new GitHubClient(new ProductHeaderValue(VersionPrefix, Version.ToString()))); + + services.AddSingleton(); + services.AddSingleton(x => x.GetRequiredService().CreateClient()); if (isWindows) { diff --git a/src/Tgstation.Server.Host/Core/GitHubClientFactory.cs b/src/Tgstation.Server.Host/Core/GitHubClientFactory.cs new file mode 100644 index 0000000000..029482ed83 --- /dev/null +++ b/src/Tgstation.Server.Host/Core/GitHubClientFactory.cs @@ -0,0 +1,42 @@ +using System; +using Octokit; + +namespace Tgstation.Server.Host.Core +{ + /// + sealed class GitHubClientFactory : IGitHubClientFactory + { + /// + /// The for the + /// + readonly IApplication application; + + /// + /// Construct a + /// + /// The value of + public GitHubClientFactory(IApplication application) + { + this.application = application ?? throw new ArgumentNullException(nameof(application)); + } + + /// + /// Create a + /// + /// A new + GitHubClient CreateBaseClient() => new GitHubClient(new ProductHeaderValue(application.VersionPrefix, application.Version.ToString())); + + /// + public IGitHubClient CreateClient() => CreateBaseClient(); + + /// + public IGitHubClient CreateClient(string accessToken) + { + if (accessToken == null) + throw new ArgumentNullException(nameof(accessToken)); + var result = CreateBaseClient(); + result.Credentials = new Credentials(accessToken); + return result; + } + } +} diff --git a/src/Tgstation.Server.Host/Core/IApplication.cs b/src/Tgstation.Server.Host/Core/IApplication.cs index 1cbe820b4c..e720c32c8f 100644 --- a/src/Tgstation.Server.Host/Core/IApplication.cs +++ b/src/Tgstation.Server.Host/Core/IApplication.cs @@ -7,6 +7,11 @@ namespace Tgstation.Server.Host.Core /// public interface IApplication { + /// + /// Prefix to + /// + string VersionPrefix { get; } + /// /// A more verbose version of /// diff --git a/src/Tgstation.Server.Host/Core/IGitHubClientFactory.cs b/src/Tgstation.Server.Host/Core/IGitHubClientFactory.cs new file mode 100644 index 0000000000..7066ed1d29 --- /dev/null +++ b/src/Tgstation.Server.Host/Core/IGitHubClientFactory.cs @@ -0,0 +1,23 @@ +using Octokit; + +namespace Tgstation.Server.Host.Core +{ + /// + /// For creating s + /// + public interface IGitHubClientFactory + { + /// + /// Create a client with anonymous authentication. Low rate limit + /// + /// A new + IGitHubClient CreateClient(); + + /// + /// Create a client with authentication using a personal access token + /// + /// The GitHub personal access token + /// A new + IGitHubClient CreateClient(string accessToken); + } +} \ No newline at end of file