Use configured GitHubAccessToken when available for Octokit operations

This commit is contained in:
Cyberboss
2018-09-10 09:55:30 -04:00
parent 554627226f
commit 992ab646d2
2 changed files with 25 additions and 6 deletions
@@ -31,9 +31,9 @@ namespace Tgstation.Server.Host.Controllers
const string RestartNotSupportedException = "This deployment of tgstation-server is lacking the Tgstation.Server.Host.Watchdog component. Restarts and version changes cannot be completed!";
/// <summary>
/// The <see cref="IGitHubClient"/> for the <see cref="AdministrationController"/>
/// The <see cref="IGitHubClientFactory"/> for the <see cref="AdministrationController"/>
/// </summary>
readonly IGitHubClient gitHubClient;
readonly IGitHubClientFactory gitHubClientFactory;
/// <summary>
/// The <see cref="IServerControl"/> for the <see cref="AdministrationController"/>
@@ -55,6 +55,11 @@ namespace Tgstation.Server.Host.Controllers
/// </summary>
readonly UpdatesConfiguration updatesConfiguration;
/// <summary>
/// The <see cref="GeneralConfiguration"/> for the <see cref="AdministrationController"/>
/// </summary>
readonly GeneralConfiguration generalConfiguration;
/// <summary>
/// Construct an <see cref="AdministrationController"/>
/// </summary>
@@ -66,13 +71,15 @@ namespace Tgstation.Server.Host.Controllers
/// <param name="ioManager">The value of <see cref="ioManager"/></param>
/// <param name="logger">The <see cref="ILogger"/> for the <see cref="ApiController"/></param>
/// <param name="updatesConfigurationOptions">The <see cref="IOptions{TOptions}"/> containing value of <see cref="updatesConfiguration"/></param>
public AdministrationController(IDatabaseContext databaseContext, IAuthenticationContextFactory authenticationContextFactory, IGitHubClient gitHubClient, IServerControl serverUpdater, IApplication application, IIOManager ioManager, ILogger<AdministrationController> logger, IOptions<UpdatesConfiguration> updatesConfigurationOptions) : base(databaseContext, authenticationContextFactory, logger, false)
/// <param name="generalConfigurationOptions">The <see cref="IOptions{TOptions}"/> containing value of <see cref="generalConfiguration"/></param>
public AdministrationController(IDatabaseContext databaseContext, IAuthenticationContextFactory authenticationContextFactory, IGitHubClientFactory gitHubClientFactory, IServerControl serverUpdater, IApplication application, IIOManager ioManager, ILogger<AdministrationController> logger, IOptions<UpdatesConfiguration> updatesConfigurationOptions, IOptions<GeneralConfiguration> generalConfigurationOptions) : base(databaseContext, authenticationContextFactory, logger, false)
{
this.gitHubClient = gitHubClient ?? throw new ArgumentNullException(nameof(gitHubClient));
this.gitHubClientFactory = gitHubClientFactory ?? throw new ArgumentNullException(nameof(gitHubClientFactory));
this.serverUpdater = serverUpdater ?? throw new ArgumentNullException(nameof(serverUpdater));
this.application = application ?? throw new ArgumentNullException(nameof(application));
this.ioManager = ioManager ?? throw new ArgumentNullException(nameof(ioManager));
updatesConfiguration = updatesConfigurationOptions?.Value ?? throw new ArgumentNullException(nameof(updatesConfigurationOptions));
generalConfiguration = generalConfigurationOptions?.Value ?? throw new ArgumentNullException(nameof(generalConfigurationOptions));
}
StatusCodeResult RateLimit(RateLimitExceededException exception)
@@ -83,6 +90,8 @@ namespace Tgstation.Server.Host.Controllers
return StatusCode(429);
}
IGitHubClient GetGitHubClient() => String.IsNullOrEmpty(generalConfiguration.GitHubAccessToken) ? gitHubClientFactory.CreateClient() : gitHubClientFactory.CreateClient(generalConfiguration.GitHubAccessToken);
/// <inheritdoc />
[TgsAuthorize]
public override async Task<IActionResult> Read(CancellationToken cancellationToken)
@@ -93,6 +102,7 @@ namespace Tgstation.Server.Host.Controllers
Uri repoUrl = null;
try
{
var gitHubClient = GetGitHubClient();
var repositoryTask = gitHubClient.Repository.Get(updatesConfiguration.GitHubRepositoryId);
var releases = (await gitHubClient.Repository.Release.GetAll(updatesConfiguration.GitHubRepositoryId).ConfigureAwait(false)).Where(x => x.TagName.StartsWith(updatesConfiguration.GitTagPrefix, StringComparison.InvariantCulture));
@@ -137,6 +147,7 @@ namespace Tgstation.Server.Host.Controllers
IEnumerable<Release> releases;
try
{
var gitHubClient = GetGitHubClient();
releases = (await gitHubClient.Repository.Release.GetAll(updatesConfiguration.GitHubRepositoryId).ConfigureAwait(false)).Where(x => x.TagName.StartsWith(updatesConfiguration.GitTagPrefix, StringComparison.InvariantCulture));
}
catch (RateLimitExceededException e)
@@ -2,6 +2,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Globalization;
@@ -15,6 +16,7 @@ using Tgstation.Server.Api;
using Tgstation.Server.Api.Models;
using Tgstation.Server.Api.Rights;
using Tgstation.Server.Host.Components;
using Tgstation.Server.Host.Configuration;
using Tgstation.Server.Host.Core;
using Tgstation.Server.Host.Models;
using Tgstation.Server.Host.Security;
@@ -42,6 +44,11 @@ namespace Tgstation.Server.Host.Controllers
/// </summary>
readonly IJobManager jobManager;
/// <summary>
/// The <see cref="GeneralConfiguration"/> for the <see cref="RepositoryController"/>
/// </summary>
readonly GeneralConfiguration generalConfiguration;
/// <summary>
/// Construct a <see cref="RepositoryController"/>
/// </summary>
@@ -51,11 +58,12 @@ namespace Tgstation.Server.Host.Controllers
/// <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, IGitHubClientFactory gitHubClientFactory, 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, IOptions<GeneralConfiguration> generalConfigurationOptions) : base(databaseContext, authenticationContextFactory, logger, true)
{
this.instanceManager = instanceManager ?? throw new ArgumentNullException(nameof(instanceManager));
this.gitHubClientFactory = gitHubClientFactory ?? throw new ArgumentNullException(nameof(gitHubClientFactory));
this.jobManager = jobManager ?? throw new ArgumentNullException(nameof(jobManager));
generalConfiguration = generalConfigurationOptions?.Value ?? throw new ArgumentNullException(nameof(generalConfigurationOptions));
}
static async Task<bool> LoadRevisionInformation(Components.Repository.IRepository repository, IDatabaseContext databaseContext, Models.Instance instance, string lastOriginCommitSha, Action<Models.RevisionInformation> revInfoSink, CancellationToken cancellationToken)
@@ -474,7 +482,7 @@ namespace Tgstation.Server.Host.Controllers
foreach (var I in model.NewTestMerges.Where(x => String.IsNullOrWhiteSpace(x.PullRequestRevision)))
I.PullRequestRevision = null;
var gitHubClient = currentModel.AccessToken != null ? gitHubClientFactory.CreateClient(currentModel.AccessToken) : gitHubClientFactory.CreateClient();
var gitHubClient = String.IsNullOrEmpty(generalConfiguration.GitHubAccessToken) ? (currentModel.AccessToken != null ? gitHubClientFactory.CreateClient(currentModel.AccessToken) : gitHubClientFactory.CreateClient()) : gitHubClientFactory.CreateClient(generalConfiguration.GitHubAccessToken);
var repoOwner = repo.GitHubOwner;
var repoName = repo.GitHubRepoName;