Split IGitHubService into IAuthenticatedGitHubService

This commit is contained in:
Dominion
2023-06-04 12:07:10 -04:00
parent 2751abc44d
commit 174d899f70
6 changed files with 80 additions and 56 deletions
@@ -73,9 +73,18 @@ namespace Tgstation.Server.Host.Components.Deployment.Remote
.FirstAsync(cancellationToken));
var instanceAuthenticated = repositorySettings.AccessToken != null;
var gitHubService = !instanceAuthenticated
? gitHubServiceFactory.CreateService()
: gitHubServiceFactory.CreateService(repositorySettings.AccessToken);
IAuthenticatedGitHubService authenticatedGitHubService;
IGitHubService gitHubService;
if (instanceAuthenticated)
{
authenticatedGitHubService = gitHubServiceFactory.CreateService(repositorySettings.AccessToken);
gitHubService = authenticatedGitHubService;
}
else
{
authenticatedGitHubService = null;
gitHubService = gitHubServiceFactory.CreateService();
}
var repositoryIdTask = gitHubService.GetRepositoryId(
remoteInformation.RemoteRepositoryOwner,
@@ -91,7 +100,7 @@ namespace Tgstation.Server.Host.Components.Deployment.Remote
Logger.LogTrace("Creating deployment...");
try
{
compileJob.GitHubDeploymentId = await gitHubService.CreateDeployment(
compileJob.GitHubDeploymentId = await authenticatedGitHubService.CreateDeployment(
new NewDeployment(compileJob.RevisionInformation.CommitSha)
{
AutoMerge = false,
@@ -106,7 +115,7 @@ namespace Tgstation.Server.Host.Components.Deployment.Remote
Logger.LogDebug("Created deployment ID {deploymentId}", compileJob.GitHubDeploymentId);
await gitHubService.CreateDeploymentStatus(
await authenticatedGitHubService.CreateDeploymentStatus(
new NewDeploymentStatus(DeploymentState.InProgress)
{
Description = "The project is being deployed",
@@ -13,8 +13,10 @@ using Tgstation.Server.Host.Extensions;
namespace Tgstation.Server.Host.Utils.GitHub
{
/// <inheritdoc />
sealed class GitHubService : IGitHubService
/// <summary>
/// Service for interacting with GitHub. Authenticated or otherwise.
/// </summary>
sealed class GitHubService : IAuthenticatedGitHubService
{
/// <summary>
/// The <see cref="IGitHubClient"/> for the <see cref="GitHubService"/>.
@@ -47,7 +47,7 @@ namespace Tgstation.Server.Host.Utils.GitHub
public IGitHubService CreateService() => CreateServiceImpl(gitHubClientFactory.CreateClient());
/// <inheritdoc />
public IGitHubService CreateService(string accessToken)
public IAuthenticatedGitHubService CreateService(string accessToken)
=> CreateServiceImpl(
gitHubClientFactory.CreateClient(
accessToken ?? throw new ArgumentNullException(nameof(accessToken))));
@@ -0,0 +1,55 @@
using System.Threading;
using System.Threading.Tasks;
using Octokit;
namespace Tgstation.Server.Host.Utils.GitHub
{
/// <summary>
/// <see cref="IGitHubService"/> that exposes functions that require authentication.
/// </summary>
public interface IAuthenticatedGitHubService : IGitHubService
{
/// <summary>
/// Create a comment on a given <paramref name="issueNumber"/>.
/// </summary>
/// <param name="repoOwner">The owner of the target repository.</param>
/// <param name="repoName">The name of the target repository.</param>
/// <param name="comment">The text of the comment.</param>
/// <param name="issueNumber">The number of the issue to comment on.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation.</param>
/// <returns>A <see cref="Task"/> representing the running operation.</returns>
Task CommentOnIssue(string repoOwner, string repoName, string comment, int issueNumber, CancellationToken cancellationToken);
/// <summary>
/// Create a <paramref name="newDeployment"/> on a target repostiory.
/// </summary>
/// <param name="newDeployment">The <see cref="NewDeployment"/>.</param>
/// <param name="repoOwner">The owner of the target repository.</param>
/// <param name="repoName">The name of the target repository.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation.</param>
/// <returns>A <see cref="Task{TResult}"/> resulting in the new deployment's ID.</returns>
Task<int> CreateDeployment(NewDeployment newDeployment, string repoOwner, string repoName, CancellationToken cancellationToken);
/// <summary>
/// Create a <paramref name="newDeploymentStatus"/> on a target deployment.
/// </summary>
/// <param name="newDeploymentStatus">The <see cref="NewDeploymentStatus"/>.</param>
/// <param name="repoOwner">The owner of the target repository.</param>
/// <param name="repoName">The name of the target repository.</param>
/// <param name="deploymentId">The ID of the parent deployment.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation.</param>
/// <returns>A <see cref="Task"/> representing the running operation.</returns>
Task CreateDeploymentStatus(NewDeploymentStatus newDeploymentStatus, string repoOwner, string repoName, int deploymentId, CancellationToken cancellationToken);
/// <summary>
/// Create a <paramref name="newDeploymentStatus"/> on a target deployment.
/// </summary>
/// <param name="newDeploymentStatus">The <see cref="NewDeploymentStatus"/>.</param>
/// <param name="repoId">The ID of the target repository.</param>
/// <param name="deploymentId">The ID of the parent deployment.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation.</param>
/// <returns>A <see cref="Task"/> representing the running operation.</returns>
Task CreateDeploymentStatus(NewDeploymentStatus newDeploymentStatus, long repoId, int deploymentId, CancellationToken cancellationToken);
}
}
@@ -38,13 +38,6 @@ namespace Tgstation.Server.Host.Utils.GitHub
/// <returns>A <see cref="Task{TResult}"/> resulting in a <see cref="string"/> representing the returned OAuth code from GitHub on success, <see langword="null"/> otherwise.</returns>
Task<string> CreateOAuthAccessToken(OAuthConfiguration oAuthConfiguration, string code, CancellationToken cancellationToken);
/// <summary>
/// Get the current user's ID.
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation.</param>
/// <returns>A <see cref="Task{TResult}"/> resulting in the current user's ID.</returns>
Task<int> GetCurrentUserId(CancellationToken cancellationToken);
/// <summary>
/// Get a target repostiory's ID.
/// </summary>
@@ -55,46 +48,11 @@ namespace Tgstation.Server.Host.Utils.GitHub
Task<long> GetRepositoryId(string repoOwner, string repoName, CancellationToken cancellationToken);
/// <summary>
/// Create a comment on a given <paramref name="issueNumber"/>.
/// Get the current user's ID.
/// </summary>
/// <param name="repoOwner">The owner of the target repository.</param>
/// <param name="repoName">The name of the target repository.</param>
/// <param name="comment">The text of the comment.</param>
/// <param name="issueNumber">The number of the issue to comment on.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation.</param>
/// <returns>A <see cref="Task"/> representing the running operation.</returns>
Task CommentOnIssue(string repoOwner, string repoName, string comment, int issueNumber, CancellationToken cancellationToken);
/// <summary>
/// Create a <paramref name="newDeployment"/> on a target repostiory.
/// </summary>
/// <param name="newDeployment">The <see cref="NewDeployment"/>.</param>
/// <param name="repoOwner">The owner of the target repository.</param>
/// <param name="repoName">The name of the target repository.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation.</param>
/// <returns>A <see cref="Task{TResult}"/> resulting in the new deployment's ID.</returns>
Task<int> CreateDeployment(NewDeployment newDeployment, string repoOwner, string repoName, CancellationToken cancellationToken);
/// <summary>
/// Create a <paramref name="newDeploymentStatus"/> on a target deployment.
/// </summary>
/// <param name="newDeploymentStatus">The <see cref="NewDeploymentStatus"/>.</param>
/// <param name="repoOwner">The owner of the target repository.</param>
/// <param name="repoName">The name of the target repository.</param>
/// <param name="deploymentId">The ID of the parent deployment.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation.</param>
/// <returns>A <see cref="Task"/> representing the running operation.</returns>
Task CreateDeploymentStatus(NewDeploymentStatus newDeploymentStatus, string repoOwner, string repoName, int deploymentId, CancellationToken cancellationToken);
/// <summary>
/// Create a <paramref name="newDeploymentStatus"/> on a target deployment.
/// </summary>
/// <param name="newDeploymentStatus">The <see cref="NewDeploymentStatus"/>.</param>
/// <param name="repoId">The ID of the target repository.</param>
/// <param name="deploymentId">The ID of the parent deployment.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation.</param>
/// <returns>A <see cref="Task"/> representing the running operation.</returns>
Task CreateDeploymentStatus(NewDeploymentStatus newDeploymentStatus, long repoId, int deploymentId, CancellationToken cancellationToken);
/// <returns>A <see cref="Task{TResult}"/> resulting in the current user's ID.</returns>
Task<int> GetCurrentUserId(CancellationToken cancellationToken);
/// <summary>
/// Get a given <paramref name="pullRequestNumber"/>.
@@ -12,10 +12,10 @@
public IGitHubService CreateService();
/// <summary>
/// Create a <see cref="IGitHubService"/>.
/// Create an <see cref="IAuthenticatedGitHubService"/>.
/// </summary>
/// <param name="accessToken">The access token to use for communication with GitHub.</param>
/// <returns>A new <see cref="IGitHubService"/>.</returns>
public IGitHubService CreateService(string accessToken);
/// <returns>A new <see cref="IAuthenticatedGitHubService"/>.</returns>
public IAuthenticatedGitHubService CreateService(string accessToken);
}
}