mirror of
https://github.com/tgstation/tgstation-server.git
synced 2026-08-27 15:07:03 +01:00
Use access tokens for github clients where possible
This commit is contained in:
@@ -32,9 +32,9 @@ namespace Tgstation.Server.Host.Controllers
|
||||
readonly IInstanceManager instanceManager;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="Octokit.IGitHubClient"/> for the <see cref="RepositoryController"/>
|
||||
/// The <see cref="IGitHubClientFactory"/> for the <see cref="RepositoryController"/>
|
||||
/// </summary>
|
||||
readonly Octokit.IGitHubClient gitHubClient;
|
||||
readonly IGitHubClientFactory gitHubClientFactory;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="IJobManager"/> for the <see cref="RepositoryController"/>
|
||||
@@ -47,13 +47,13 @@ namespace Tgstation.Server.Host.Controllers
|
||||
/// <param name="databaseContext">The <see cref="IDatabaseContext"/> for the <see cref="ApiController"/></param>
|
||||
/// <param name="authenticationContextFactory">The <see cref="IAuthenticationContextFactory"/> for the <see cref="ApiController"/></param>
|
||||
/// <param name="instanceManager">The value of <see cref="instanceManager"/></param>
|
||||
/// <param name="gitHubClient">The value of <see cref="gitHubClient"/></param>
|
||||
/// <param name="gitHubClientFactory">The value of <see cref="gitHubClientFactory"/></param>
|
||||
/// <param name="jobManager">The value of <see cref="jobManager"/></param>
|
||||
/// <param name="logger">The <see cref="ILogger"/> for the <see cref="ApiController"/></param>
|
||||
public RepositoryController(IDatabaseContext databaseContext, IAuthenticationContextFactory authenticationContextFactory, IInstanceManager instanceManager, Octokit.IGitHubClient gitHubClient, IJobManager jobManager, ILogger<RepositoryController> logger) : base(databaseContext, authenticationContextFactory, logger, true)
|
||||
public RepositoryController(IDatabaseContext databaseContext, IAuthenticationContextFactory authenticationContextFactory, IInstanceManager instanceManager, IGitHubClientFactory gitHubClientFactory, IJobManager jobManager, ILogger<RepositoryController> 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
|
||||
|
||||
@@ -40,7 +40,7 @@ namespace Tgstation.Server.Host.Core
|
||||
/// <summary>
|
||||
/// Prefix for string version names
|
||||
/// </summary>
|
||||
const string VersionPrefix = "tgstation-server";
|
||||
public string VersionPrefix => "tgstation-server";
|
||||
|
||||
/// <inheritdoc />
|
||||
public string HostingPath => serverAddresses.Addresses.First();
|
||||
@@ -187,7 +187,9 @@ namespace Tgstation.Server.Host.Core
|
||||
services.AddSingleton<IPasswordHasher<Models.User>, PasswordHasher<Models.User>>();
|
||||
services.AddSingleton<ITokenFactory, TokenFactory>();
|
||||
services.AddSingleton<ISynchronousIOManager, SynchronousIOManager>();
|
||||
services.AddSingleton<IGitHubClient>(x => new GitHubClient(new ProductHeaderValue(VersionPrefix, Version.ToString())));
|
||||
|
||||
services.AddSingleton<IGitHubClientFactory, GitHubClientFactory>();
|
||||
services.AddSingleton(x => x.GetRequiredService<IGitHubClientFactory>().CreateClient());
|
||||
|
||||
if (isWindows)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using Octokit;
|
||||
|
||||
namespace Tgstation.Server.Host.Core
|
||||
{
|
||||
/// <inheritdoc />
|
||||
sealed class GitHubClientFactory : IGitHubClientFactory
|
||||
{
|
||||
/// <summary>
|
||||
/// The <see cref="IApplication"/> for the <see cref="GitHubClientFactory"/>
|
||||
/// </summary>
|
||||
readonly IApplication application;
|
||||
|
||||
/// <summary>
|
||||
/// Construct a <see cref="GitHubClientFactory"/>
|
||||
/// </summary>
|
||||
/// <param name="application">The value of <see cref="application"/></param>
|
||||
public GitHubClientFactory(IApplication application)
|
||||
{
|
||||
this.application = application ?? throw new ArgumentNullException(nameof(application));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a <see cref="GitHubClient"/>
|
||||
/// </summary>
|
||||
/// <returns>A new <see cref="GitHubClient"/></returns>
|
||||
GitHubClient CreateBaseClient() => new GitHubClient(new ProductHeaderValue(application.VersionPrefix, application.Version.ToString()));
|
||||
|
||||
/// <inheritdoc />
|
||||
public IGitHubClient CreateClient() => CreateBaseClient();
|
||||
|
||||
/// <inheritdoc />
|
||||
public IGitHubClient CreateClient(string accessToken)
|
||||
{
|
||||
if (accessToken == null)
|
||||
throw new ArgumentNullException(nameof(accessToken));
|
||||
var result = CreateBaseClient();
|
||||
result.Credentials = new Credentials(accessToken);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,11 @@ namespace Tgstation.Server.Host.Core
|
||||
/// </summary>
|
||||
public interface IApplication
|
||||
{
|
||||
/// <summary>
|
||||
/// Prefix to <see cref="VersionString"/>
|
||||
/// </summary>
|
||||
string VersionPrefix { get; }
|
||||
|
||||
/// <summary>
|
||||
/// A more verbose version of <see cref="Version"/>
|
||||
/// </summary>
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
using Octokit;
|
||||
|
||||
namespace Tgstation.Server.Host.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// For creating <see cref="IGitHubClient"/>s
|
||||
/// </summary>
|
||||
public interface IGitHubClientFactory
|
||||
{
|
||||
/// <summary>
|
||||
/// Create a client with anonymous authentication. Low rate limit
|
||||
/// </summary>
|
||||
/// <returns>A new <see cref="IGitHubClient"/></returns>
|
||||
IGitHubClient CreateClient();
|
||||
|
||||
/// <summary>
|
||||
/// Create a client with authentication using a personal access token
|
||||
/// </summary>
|
||||
/// <param name="accessToken">The GitHub personal access token</param>
|
||||
/// <returns>A new <see cref="IGitHubClient"/></returns>
|
||||
IGitHubClient CreateClient(string accessToken);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user