This commit is contained in:
Jordan Dominion
2025-03-15 09:10:38 -04:00
34 changed files with 4253 additions and 168 deletions
+3
View File
@@ -21,6 +21,8 @@ artifacts/
/src/Tgstation.Server.Host/wwwroot
/src/Tgstation.Server.Host/--applicationName
/src/Tgstation.Server.Host/ClientApp
/src/Tgstation.Server.Host.Utils.GitLab.GraphQL/node_modules
/src/Tgstation.Server.Host.Utils.GitLab.GraphQL/schema.graphql
/tools/Tgstation.Server.ReleaseNotes/release_notes.md
launchSettings.json
release_notes.md
@@ -29,3 +31,4 @@ changelog.yml
*.sqlite3
packaging/
yarn-error.log*
.graphqlrc.json
@@ -28,7 +28,7 @@
<!-- Usage: HTTP constants reference -->
<PackageReference Include="Microsoft.AspNetCore.Http.Extensions" Version="2.3.0" />
<!-- Usage: Decoding the 'nbf' property of JWTs -->
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="8.6.0" />
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="8.6.1" />
<!-- Usage: Data model annotating -->
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
</ItemGroup>
@@ -0,0 +1,21 @@
{
"schema": "schema.graphql",
"documents": "**/*.graphql",
"extensions": {
"strawberryShake": {
"name": "GraphQLClient",
"url": "../../artifacts/gitlab-api.graphql",
"namespace": "Tgstation.Server.Host.Utils.GitLab.GraphQL",
"records": {
"inputs": false,
"entities": false
},
"transportProfiles": [
{
"default": "Http",
"subscription": "Http"
}
]
}
}
}
@@ -0,0 +1,12 @@
mutation CreateNote($id: NoteableID!, $body: String!) {
createNote(input: { noteableId: $id, body: $body }) {
note {
id
body
discussion {
id
}
}
errors
}
}
@@ -0,0 +1,9 @@
mutation ModifyNote($id: NoteID!, $body: String!) {
updateNote(input: { id: $id, body: $body }) {
note {
id
body
}
errors
}
}
@@ -0,0 +1,6 @@
query GetCurrentUser {
currentUser
{
username
}
}
@@ -0,0 +1,14 @@
query GetMergeRequest($project: ID!, $number: String!) {
project(fullPath: $project) {
mergeRequest(iid: $number) {
author { username }
description
title
diffHeadSha
mergeCommitSha
webUrl
iid
id
}
}
}
@@ -0,0 +1,15 @@
query GetMergeRequestNotes($project: ID!, $number: String!) {
project(fullPath: $project) {
mergeRequest(iid: $number) {
iid
id
notes {
nodes {
author { username }
body
id
}
}
}
}
}
@@ -0,0 +1,14 @@
query GetMergeRequests($project: ID!, $numbers: [String!]!) {
project(fullPath: $project) {
mergeRequests(iids: $numbers) {
nodes {
state
diffHeadSha
mergeCommitSha
closedAt
iid
id
}
}
}
}
@@ -0,0 +1,33 @@
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
namespace Tgstation.Server.Host.Utils.GitLab.GraphQL
{
/// <inheritdoc />
sealed class GraphQLGitLabClient : IGraphQLGitLabClient
{
/// <inheritdoc />
public IGraphQLClient GraphQL { get; }
/// <summary>
/// The <see cref="ServiceProvider"/> containing the <see cref="GraphQL"/> client.
/// </summary>
readonly ServiceProvider serviceProvider;
/// <summary>
/// Initializes a new instance of the <see cref="GraphQLGitLabClient"/> class.
/// </summary>
/// <param name="serviceProvider">The value of <see cref="serviceProvider"/>.</param>
public GraphQLGitLabClient(ServiceProvider serviceProvider)
{
this.serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
GraphQL = serviceProvider.GetService<IGraphQLClient>() ?? throw new ArgumentException($"Expected an {nameof(IGraphQLClient)} service in the provider!", nameof(serviceProvider));
}
/// <inheritdoc />
public ValueTask DisposeAsync()
=> serviceProvider.DisposeAsync();
}
}
@@ -0,0 +1,49 @@
using System;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
namespace Tgstation.Server.Host.Utils.GitLab.GraphQL
{
/// <summary>
/// Factory for creating <see cref="IGraphQLGitLabClient"/>s.
/// </summary>
public sealed class GraphQLGitLabClientFactory
{
/// <summary>
/// Sets up a <see cref="IGraphQLGitLabClient"/>.
/// </summary>
/// <param name="bearerToken">The token to use for authentication, if any.</param>
/// <returns>A <see cref="ValueTask{TResult}"/> resulting in a new <see cref="IGraphQLGitLabClient"/>.</returns>
public static async ValueTask<IGraphQLGitLabClient> CreateClient(string? bearerToken = null)
{
var serviceCollection = new ServiceCollection();
var clientBuilder = serviceCollection
.AddGraphQLClient();
var graphQLEndpoint = new Uri("https://gitlab.com/api/graphql");
clientBuilder.ConfigureHttpClient(
client =>
{
client.BaseAddress = new Uri("https://gitlab.com/api/graphql");
if (bearerToken != null)
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", bearerToken);
}
});
var serviceProvider = serviceCollection.BuildServiceProvider();
try
{
return new GraphQLGitLabClient(serviceProvider);
}
catch
{
await serviceProvider.DisposeAsync();
throw;
}
}
}
}
@@ -0,0 +1,15 @@
using System;
namespace Tgstation.Server.Host.Utils.GitLab.GraphQL
{
/// <summary>
/// Wrapper for using a GitLab <see cref="IGraphQLClient"/>.
/// </summary>
public interface IGraphQLGitLabClient : IAsyncDisposable
{
/// <summary>
/// Gets the underlying <see cref="IGraphQLClient"/>.
/// </summary>
IGraphQLClient GraphQL { get; }
}
}
@@ -0,0 +1,44 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="../../build/SrcCommon.props" />
<PropertyGroup>
<TargetFrameworks>$(TgsFrameworkVersion)</TargetFrameworks>
<Version>$(TgsCoreVersion)</Version>
<Nullable>enable</Nullable>
</PropertyGroup>
<Target Name="InstallApollo" Inputs="package.json;yarn.lock" Outputs="node_modules/.bin/apollo">
<Message Text="Installing Apollo..." Importance="high" />
<Exec Command="yarn install --immutable --ignore-engines" />
</Target>
<Target Name="GetApi" DependsOnTargets="InstallApollo" Inputs="node_modules/.bin/apollo" Outputs="../../artifacts/gitlab-api.graphql">
<Message Text="Fetching GitLab GraphQL API schema..." Importance="high" />
<MakeDir Directories="../../artifacts" />
<Exec Command="node_modules/.bin/apollo client:download-schema --endpoint=https://gitlab.com/api/graphql ../../artifacts/gitlab-api.graphql" />
</Target>
<!-- https://github.com/ChilliCream/graphql-platform/blob/c0c8df525ca0f47bf3b3b409a8b22cbe37f7a9c0/src/StrawberryShake/MetaPackages/Common/MSBuild/StrawberryShake.targets#L20 -->
<Target Name="ImportGraphQLApiSchema" DependsOnTargets="GetApi" BeforeTargets="_GraphQLCodeGenerationRoot" Inputs="../../artifacts/gitlab-api.graphql" Outputs="schema.graphql">
<Copy SkipUnchangedFiles="true" SourceFiles="../../artifacts/gitlab-api.graphql" DestinationFiles="schema.graphql" />
<WriteLinesToFile File="schema.graphql" Lines="$([System.IO.File]::ReadAllText('schema.graphql').Replace('\', ''))" Overwrite="true" Encoding="UTF-8" />
</Target>
<Target Name="FixWarningsInGeneratedSchema" AfterTargets="GenerateGraphQLCode">
<PropertyGroup>
<InputFile>$(IntermediateOutputPath)berry/GraphQLClient.Client.cs</InputFile>
<OutputFile>$(IntermediateOutputPath)berry/GraphQLClient.Client.cs</OutputFile>
</PropertyGroup>
<WriteLinesToFile File="$(OutputFile)" Lines="$([System.IO.File]::ReadAllText($(InputFile)).Replace('/ &lt;auto-generated/&gt;','/ &lt;auto-generated /&gt;%0d%0a#pragma warning disable'))" Overwrite="true" Encoding="Unicode" />
</Target>
<ItemGroup>
<PackageReference Include="StrawberryShake.Server" Version="15.0.3" />
</ItemGroup>
<ItemGroup>
<Folder Include="GQL\Queries\" />
<Folder Include="GQL\Mutations\" />
</ItemGroup>
</Project>
@@ -0,0 +1,6 @@
{
"devDependencies": {
"apollo": "^2.34.0"
},
"packageManager": "yarn@1.22.22+sha1.ac34549e6aa8e7ead463a7407e1c7390f61a6610"
}
File diff suppressed because it is too large Load Diff
@@ -377,6 +377,7 @@ namespace Tgstation.Server.Host.Components.Chat
/// <inheritdoc />
public Func<string?, string, Action<bool>> QueueDeploymentMessage(
Models.RevisionInformation revisionInformation,
Models.RevisionInformation? previousRevisionInformation,
EngineVersion engineVersion,
DateTimeOffset? estimatedCompletionTime,
string? gitHubOwner,
@@ -407,6 +408,7 @@ namespace Tgstation.Server.Host.Components.Chat
{
var callback = await provider.SendUpdateMessage(
revisionInformation,
previousRevisionInformation,
engineVersion,
estimatedCompletionTime,
gitHubOwner,
@@ -61,6 +61,7 @@ namespace Tgstation.Server.Host.Components.Chat
/// Send the message for a deployment to configured deployment channels.
/// </summary>
/// <param name="revisionInformation">The <see cref="RevisionInformation"/> of the deployment.</param>
/// <param name="previousRevisionInformation">The optional <see cref="RevisionInformation"/> of the previous deployment.</param>
/// <param name="engineVersion">The <see cref="Api.Models.EngineVersion"/> of the deployment.</param>
/// <param name="estimatedCompletionTime">The optional <see cref="DateTimeOffset"/> the deployment is expected to be completed at.</param>
/// <param name="gitHubOwner">The repository GitHub owner, if any.</param>
@@ -69,6 +70,7 @@ namespace Tgstation.Server.Host.Components.Chat
/// <returns>A <see cref="Func{T1, T2, TResult}"/> to call to update the message at the deployment's conclusion. Parameters: Error message if any, DreamMaker output if any. Returns an <see cref="Action"/> to call to mark the deployment as active/inactive. Parameter: If the deployment is being activated or inactivated.</returns>
Func<string?, string, Action<bool>> QueueDeploymentMessage(
Models.RevisionInformation revisionInformation,
Models.RevisionInformation? previousRevisionInformation,
Api.Models.EngineVersion engineVersion,
DateTimeOffset? estimatedCompletionTime,
string? gitHubOwner,
@@ -303,6 +303,7 @@ namespace Tgstation.Server.Host.Components.Chat.Providers
/// <inheritdoc />
public override async ValueTask<Func<string?, string, ValueTask<Func<bool, ValueTask>>>> SendUpdateMessage(
Models.RevisionInformation revisionInformation,
Models.RevisionInformation? previousRevisionInformation,
EngineVersion engineVersion,
DateTimeOffset? estimatedCompletionTime,
string? gitHubOwner,
@@ -316,7 +317,7 @@ namespace Tgstation.Server.Host.Components.Chat.Providers
localCommitPushed |= revisionInformation.CommitSha == revisionInformation.OriginCommitSha;
var fields = BuildUpdateEmbedFields(revisionInformation, engineVersion, gitHubOwner, gitHubRepo, localCommitPushed);
var fields = BuildUpdateEmbedFields(revisionInformation, previousRevisionInformation, engineVersion, gitHubOwner, gitHubRepo, localCommitPushed);
Optional<IEmbedAuthor> author = new EmbedAuthor(assemblyInformationProvider.VersionPrefix)
{
Url = "https://github.com/tgstation/tgstation-server",
@@ -900,6 +901,7 @@ namespace Tgstation.Server.Host.Components.Chat.Providers
/// Create a <see cref="List{T}"/> of <see cref="IEmbedField"/>s for a discord update embed.
/// </summary>
/// <param name="revisionInformation">The <see cref="RevisionInformation"/> of the deployment.</param>
/// <param name="previousRevisionInformation">The optional <see cref="RevisionInformation"/> of the previous deployment.</param>
/// <param name="engineVersion">The <see cref="EngineVersion"/> of the deployment.</param>
/// <param name="gitHubOwner">The repository GitHub owner, if any.</param>
/// <param name="gitHubRepo">The repository GitHub name, if any.</param>
@@ -907,6 +909,7 @@ namespace Tgstation.Server.Host.Components.Chat.Providers
/// <returns>A new <see cref="List{T}"/> of <see cref="IEmbedField"/>s to use.</returns>
List<IEmbedField> BuildUpdateEmbedFields(
Models.RevisionInformation revisionInformation,
Models.RevisionInformation? previousRevisionInformation,
EngineVersion engineVersion,
string? gitHubOwner,
string? gitHubRepo,
@@ -936,6 +939,31 @@ namespace Tgstation.Server.Host.Components.Chat.Providers
if (gitHubOwner == null || gitHubRepo == null)
return fields;
var previousTestMerges = (IEnumerable<RevInfoTestMerge>?)previousRevisionInformation?.ActiveTestMerges ?? Enumerable.Empty<RevInfoTestMerge>();
var currentTestMerges = (IEnumerable<RevInfoTestMerge>?)revisionInformation.ActiveTestMerges ?? Enumerable.Empty<RevInfoTestMerge>();
// determine what TMs were changed and how
var addedTestMerges = currentTestMerges
.Select(x => x.TestMerge)
.Where(x => !previousTestMerges
.Any(y => y.TestMerge.Number == x.Number))
.ToList();
var removedTestMerges = previousTestMerges
.Select(x => x.TestMerge)
.Where(x => !currentTestMerges
.Any(y => y.TestMerge.Number == x.Number))
.ToList();
var updatedTestMerges = currentTestMerges
.Select(x => x.TestMerge)
.Where(x => previousTestMerges
.Any(y => y.TestMerge.Number == x.Number && y.TestMerge.TargetCommitSha != x.TargetCommitSha))
.ToList();
var unchangedTestMerges = currentTestMerges
.Select(x => x.TestMerge)
.Where(x => previousTestMerges
.Any(y => y.TestMerge.Number == x.Number && y.TestMerge.TargetCommitSha == x.TargetCommitSha))
.ToList();
fields.Add(
new EmbedField(
"Local Commit",
@@ -952,13 +980,33 @@ namespace Tgstation.Server.Host.Components.Chat.Providers
: revisionOriginSha[..7],
true));
fields.AddRange((revisionInformation.ActiveTestMerges ?? Enumerable.Empty<RevInfoTestMerge>())
.Select(x => x.TestMerge)
fields.AddRange(addedTestMerges
.Select(x => new EmbedField(
$"#{x.Number} (Added)",
$"[{x.TitleAtMerge}]({x.Url}) by _[@{x.Author}](https://github.com/{x.Author})_{Environment.NewLine}Commit: [{x.TargetCommitSha![..7]}](https://github.com/{gitHubOwner}/{gitHubRepo}/commit/{x.TargetCommitSha}){(String.IsNullOrWhiteSpace(x.Comment) ? String.Empty : $"{Environment.NewLine}_**{x.Comment}**_")}",
false)));
fields.AddRange(updatedTestMerges
.Select(x => new EmbedField(
$"#{x.Number} (Updated)",
$"[{x.TitleAtMerge}]({x.Url}) by _[@{x.Author}](https://github.com/{x.Author})_{Environment.NewLine}Commit: [{x.TargetCommitSha![..7]}](https://github.com/{gitHubOwner}/{gitHubRepo}/commit/{x.TargetCommitSha}){(String.IsNullOrWhiteSpace(x.Comment) ? String.Empty : $"{Environment.NewLine}_**{x.Comment}**_")}",
false)));
fields.AddRange(unchangedTestMerges
.Select(x => new EmbedField(
$"#{x.Number}",
$"[{x.TitleAtMerge}]({x.Url}) by _[@{x.Author}](https://github.com/{x.Author})_{Environment.NewLine}Commit: [{x.TargetCommitSha![..7]}](https://github.com/{gitHubOwner}/{gitHubRepo}/commit/{x.TargetCommitSha}){(String.IsNullOrWhiteSpace(x.Comment) ? String.Empty : $"{Environment.NewLine}_**{x.Comment}**_")}",
false)));
if (removedTestMerges.Count != 0)
fields.Add(
new EmbedField(
"Removed:",
String.Join(
Environment.NewLine,
removedTestMerges
.Select(x => $"- #{x.Number} [{x.TitleAtMerge}]({x.Url}) by _[@{x.Author}](https://github.com/{x.Author})_"))));
return fields;
}
@@ -84,6 +84,7 @@ namespace Tgstation.Server.Host.Components.Chat.Providers
/// Send the message for a deployment.
/// </summary>
/// <param name="revisionInformation">The <see cref="RevisionInformation"/> of the deployment.</param>
/// <param name="previousRevisionInformation">The <see cref="RevisionInformation"/> of the previous deployment if any.</param>
/// <param name="engineVersion">The <see cref="Api.Models.EngineVersion"/> of the deployment.</param>
/// <param name="estimatedCompletionTime">The optional <see cref="DateTimeOffset"/> the deployment is expected to be completed at.</param>
/// <param name="gitHubOwner">The repository GitHub owner, if any.</param>
@@ -94,6 +95,7 @@ namespace Tgstation.Server.Host.Components.Chat.Providers
/// <returns>A <see cref="ValueTask{TResult}"/> resulting in a <see cref="Func{T1, T2, TResult}"/> to call to update the message at the deployment's conclusion. Parameters: Error message if any, DreamMaker output if any. Returns another callback which should be called to mark the deployment as active.</returns>
ValueTask<Func<string?, string, ValueTask<Func<bool, ValueTask>>>> SendUpdateMessage(
Models.RevisionInformation revisionInformation,
Models.RevisionInformation? previousRevisionInformation,
Api.Models.EngineVersion engineVersion,
DateTimeOffset? estimatedCompletionTime,
string? gitHubOwner,
@@ -17,6 +17,7 @@ using Tgstation.Server.Host.Configuration;
using Tgstation.Server.Host.Extensions;
using Tgstation.Server.Host.IO;
using Tgstation.Server.Host.Jobs;
using Tgstation.Server.Host.Models;
using Tgstation.Server.Host.System;
using Tgstation.Server.Host.Utils;
@@ -217,6 +218,7 @@ namespace Tgstation.Server.Host.Components.Chat.Providers
/// <inheritdoc />
public override async ValueTask<Func<string?, string, ValueTask<Func<bool, ValueTask>>>> SendUpdateMessage(
Models.RevisionInformation revisionInformation,
Models.RevisionInformation? previousRevisionInformation,
EngineVersion engineVersion,
DateTimeOffset? estimatedCompletionTime,
string? gitHubOwner,
@@ -228,6 +230,9 @@ namespace Tgstation.Server.Host.Components.Chat.Providers
ArgumentNullException.ThrowIfNull(revisionInformation);
ArgumentNullException.ThrowIfNull(engineVersion);
var previousTestMerges = (IEnumerable<RevInfoTestMerge>?)previousRevisionInformation?.ActiveTestMerges ?? Enumerable.Empty<RevInfoTestMerge>();
var currentTestMerges = (IEnumerable<RevInfoTestMerge>?)revisionInformation.ActiveTestMerges ?? Enumerable.Empty<RevInfoTestMerge>();
var commitInsert = revisionInformation.CommitSha![..7];
string remoteCommitInsert;
if (revisionInformation.CommitSha == revisionInformation.OriginCommitSha)
@@ -238,21 +243,35 @@ namespace Tgstation.Server.Host.Components.Chat.Providers
else
remoteCommitInsert = String.Format(CultureInfo.InvariantCulture, ". Remote commit: ^{0}", revisionInformation.OriginCommitSha![..7]);
var testmergeInsert = (revisionInformation.ActiveTestMerges?.Count ?? 0) == 0
var testmergeInsert = !currentTestMerges.Any()
? String.Empty
: String.Format(
CultureInfo.InvariantCulture,
" (Test Merges: {0})",
String.Join(
", ",
revisionInformation
.ActiveTestMerges!
currentTestMerges
.Select(x => x.TestMerge)
.Select(x =>
{
var result = String.Format(CultureInfo.InvariantCulture, "#{0} at {1}", x.Number, x.TargetCommitSha![..7]);
if (x.Comment != null)
result += String.Format(CultureInfo.InvariantCulture, " ({0})", x.Comment);
var status = string.Empty;
if (!previousTestMerges.Any(y => y.TestMerge.Number == x.Number))
status = "Added";
else if (previousTestMerges.Any(y => y.TestMerge.Number == x.Number && y.TestMerge.TargetCommitSha != x.TargetCommitSha))
status = "Updated";
var result = $"#{x.Number} at {x.TargetCommitSha![..7]}";
if (!string.IsNullOrEmpty(x.Comment))
{
if (!string.IsNullOrEmpty(status))
result += $" ({status} - {x.Comment})";
else
result += $" ({x.Comment})";
}
else if (!string.IsNullOrEmpty(status))
result += $" ({status})";
return result;
})));
@@ -199,6 +199,7 @@ namespace Tgstation.Server.Host.Components.Chat.Providers
/// <inheritdoc />
public abstract ValueTask<Func<string?, string, ValueTask<Func<bool, ValueTask>>>> SendUpdateMessage(
RevisionInformation revisionInformation,
RevisionInformation? previousRevisionInformation,
Api.Models.EngineVersion engineVersion,
DateTimeOffset? estimatedCompletionTime,
string? gitHubOwner,
@@ -336,13 +336,17 @@ namespace Tgstation.Server.Host.Components.Deployment
}
});
var likelyPushedTestMergeCommit =
repositorySettings!.PushTestMergeCommits!.Value
&& repositorySettings.AccessToken != null
&& repositorySettings.AccessUser != null;
Models.CompileJob? oldCompileJob;
using (repo)
{
var likelyPushedTestMergeCommit =
repositorySettings!.PushTestMergeCommits!.Value
&& repositorySettings.AccessToken != null
&& repositorySettings.AccessUser != null;
oldCompileJob = await compileJobConsumer.LatestCompileJob();
compileJob = await Compile(
job,
oldCompileJob,
revInfo!,
dreamMakerSettings!,
ddSettings!,
@@ -352,8 +356,8 @@ namespace Tgstation.Server.Host.Components.Deployment
averageSpan,
likelyPushedTestMergeCommit,
cancellationToken);
}
var activeCompileJob = await compileJobConsumer.LatestCompileJob();
try
{
await databaseContextFactory.UseContext(
@@ -402,7 +406,7 @@ namespace Tgstation.Server.Host.Components.Deployment
var commentsTask = remoteDeploymentManager!.PostDeploymentComments(
compileJob,
activeCompileJob?.RevisionInformation,
oldCompileJob?.RevisionInformation,
repositorySettings,
repoOwner,
repoName,
@@ -479,6 +483,7 @@ namespace Tgstation.Server.Host.Components.Deployment
/// Run the compile implementation.
/// </summary>
/// <param name="job">The currently running <see cref="Job"/>.</param>
/// <param name="oldCompileJob">The optional <see cref="CompileJob"/> of the previous deployment.</param>
/// <param name="revisionInformation">The <see cref="RevisionInformation"/>.</param>
/// <param name="dreamMakerSettings">The <see cref="Api.Models.Internal.DreamMakerSettings"/>.</param>
/// <param name="launchParameters">The <see cref="DreamDaemonLaunchParameters"/>.</param>
@@ -491,6 +496,7 @@ namespace Tgstation.Server.Host.Components.Deployment
/// <returns>A <see cref="ValueTask{TResult}"/> resulting in the completed <see cref="CompileJob"/>.</returns>
async ValueTask<Models.CompileJob> Compile(
Models.Job job,
Models.CompileJob? oldCompileJob,
Models.RevisionInformation revisionInformation,
Api.Models.Internal.DreamMakerSettings dreamMakerSettings,
DreamDaemonLaunchParameters launchParameters,
@@ -512,6 +518,7 @@ namespace Tgstation.Server.Host.Components.Deployment
using var engineLock = await engineManager.UseExecutables(null, null, cancellationToken);
currentChatCallback = chatManager.QueueDeploymentMessage(
revisionInformation,
oldCompileJob?.RevisionInformation,
engineLock.Version,
DateTimeOffset.UtcNow + estimatedDuration,
repository.RemoteRepositoryOwner,
@@ -18,6 +18,11 @@ namespace Tgstation.Server.Host.Components.Deployment.Remote
/// </summary>
abstract class BaseRemoteDeploymentManager : IRemoteDeploymentManager
{
/// <summary>
/// The header comment that begins every deployment message comment/note.
/// </summary>
public const string DeploymentMsgHeaderStart = "<!-- tgs_test_merge_comment -->";
/// <summary>
/// The <see cref="Api.Models.Instance"/> for the <see cref="BaseRemoteDeploymentManager"/>.
/// </summary>
@@ -66,36 +71,28 @@ namespace Tgstation.Server.Host.Components.Deployment.Remote
if (repositorySettings.AccessToken == null)
return;
var deployedRevisionInformation = compileJob.RevisionInformation;
if ((previousRevisionInformation != null && previousRevisionInformation.CommitSha == deployedRevisionInformation.CommitSha)
var revisionInformation = compileJob.RevisionInformation;
if ((previousRevisionInformation != null && previousRevisionInformation.CommitSha == revisionInformation.CommitSha)
|| !repositorySettings.PostTestMergeComment!.Value)
return;
previousRevisionInformation ??= new RevisionInformation();
previousRevisionInformation.ActiveTestMerges ??= new List<RevInfoTestMerge>();
var previousTestMerges = (IEnumerable<RevInfoTestMerge>?)previousRevisionInformation?.ActiveTestMerges ?? Enumerable.Empty<RevInfoTestMerge>();
var currentTestMerges = (IEnumerable<RevInfoTestMerge>?)revisionInformation.ActiveTestMerges ?? Enumerable.Empty<RevInfoTestMerge>();
deployedRevisionInformation.ActiveTestMerges ??= new List<RevInfoTestMerge>();
// added prs
var addedTestMerges = deployedRevisionInformation
.ActiveTestMerges
// determine what TMs were changed and how
var addedTestMerges = currentTestMerges
.Select(x => x.TestMerge)
.Where(x => !previousRevisionInformation
.ActiveTestMerges
.Where(x => !previousTestMerges
.Any(y => y.TestMerge.Number == x.Number))
.ToList();
var removedTestMerges = previousRevisionInformation
.ActiveTestMerges
var removedTestMerges = previousTestMerges
.Select(x => x.TestMerge)
.Where(x => !deployedRevisionInformation
.ActiveTestMerges
.Where(x => !currentTestMerges
.Any(y => y.TestMerge.Number == x.Number))
.ToList();
var updatedTestMerges = deployedRevisionInformation
.ActiveTestMerges
var updatedTestMerges = currentTestMerges
.Select(x => x.TestMerge)
.Where(x => previousRevisionInformation
.ActiveTestMerges
.Where(x => previousTestMerges
.Any(y => y.TestMerge.Number == x.Number))
.ToList();
@@ -133,7 +130,12 @@ namespace Tgstation.Server.Host.Components.Deployment.Remote
repositorySettings,
repoOwner,
repoName,
"#### Test Merge Removed",
FormatTestMergeRemoval(
repositorySettings,
compileJob,
removedTestMerge,
repoOwner,
repoName),
removedTestMerge.Number,
cancellationToken);
tasks.Add(removeCommentTask);
@@ -253,6 +255,22 @@ namespace Tgstation.Server.Host.Components.Deployment.Remote
string remoteRepositoryName,
bool updated);
/// <summary>
/// Formats a comment for a given <paramref name="testMerge"/> removal.
/// </summary>
/// <param name="repositorySettings">The <see cref="RepositorySettings"/> to use.</param>
/// <param name="compileJob">The test merge's <see cref="CompileJob"/>.</param>
/// <param name="testMerge">The <see cref="TestMerge"/>.</param>
/// <param name="remoteRepositoryOwner">The <see cref="Api.Models.Internal.IGitRemoteInformation.RemoteRepositoryOwner"/>.</param>
/// <param name="remoteRepositoryName">The <see cref="Api.Models.Internal.IGitRemoteInformation.RemoteRepositoryName"/>.</param>
/// <returns>A formatted <see cref="string"/> for posting a informative comment about the <paramref name="testMerge"/> removal.</returns>
protected abstract string FormatTestMergeRemoval(
RepositorySettings repositorySettings,
CompileJob compileJob,
TestMerge testMerge,
string remoteRepositoryOwner,
string remoteRepositoryName);
/// <summary>
/// Create a comment of a given <paramref name="testMergeNumber"/>'s source.
/// </summary>
@@ -282,7 +282,17 @@ namespace Tgstation.Server.Host.Components.Deployment.Remote
try
{
await gitHubService.CommentOnIssue(remoteRepositoryOwner, remoteRepositoryName, comment, testMergeNumber, cancellationToken);
string header = String.Format(CultureInfo.InvariantCulture, "{1}{0}## Test merge deployment history:{0}{0}", Environment.NewLine, DeploymentMsgHeaderStart);
var existingComment = await gitHubService.GetExistingCommentOnIssue(remoteRepositoryOwner, remoteRepositoryName, DeploymentMsgHeaderStart, testMergeNumber, cancellationToken);
if (existingComment != null)
{
await gitHubService.AppendCommentOnIssue(remoteRepositoryOwner, remoteRepositoryName, comment, existingComment, cancellationToken);
}
else
{
await gitHubService.CommentOnIssue(remoteRepositoryOwner, remoteRepositoryName, header + comment, testMergeNumber, cancellationToken);
}
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
@@ -299,30 +309,43 @@ namespace Tgstation.Server.Host.Components.Deployment.Remote
string remoteRepositoryName,
bool updated) => String.Format(
CultureInfo.InvariantCulture,
"#### Test Merge {4}{0}{0}<details><summary>Details</summary>{0}{0}##### Server Instance{0}{5}{1}{0}{0}##### Revision{0}Origin: {6}{0}Pull Request: {2}{0}Server: {7}{3}{8}{0}</details>",
Environment.NewLine,
"<details><summary>Test Merge {4} @ {8}:</summary>{0}{0}##### Server Instance{0}{5}{1}{0}{0}##### Revision{0}Origin: {6}{0}Pull Request: {2}{0}Server: {7}{3}{0}</details>{0}",
Environment.NewLine, // 0
repositorySettings.ShowTestMergeCommitters!.Value
? String.Format(
CultureInfo.InvariantCulture,
"{0}{0}##### Merged By{0}{1}",
Environment.NewLine,
testMerge.MergedBy!.Name)
: String.Empty,
testMerge.TargetCommitSha,
testMerge.Comment != null
? String.Format(
: String.Empty, // 1
testMerge.TargetCommitSha, // 2
String.IsNullOrEmpty(testMerge.Comment)
? String.Empty
: String.Format(
CultureInfo.InvariantCulture,
"{0}{0}##### Comment{0}{1}",
Environment.NewLine,
testMerge.Comment)
: String.Empty,
updated ? "Updated" : "Deployed",
Metadata.Name,
compileJob.RevisionInformation.OriginCommitSha,
compileJob.RevisionInformation.CommitSha,
testMerge.Comment), // 3
updated ? "Updated" : "Deployed", // 4
compileJob.GitHubDeploymentId.HasValue
? $"{Environment.NewLine}[GitHub Deployments](https://github.com/{remoteRepositoryOwner}/{remoteRepositoryName}/deployments/activity_log?environment=TGS%3A+{Metadata.Name!.Replace(" ", "+", StringComparison.Ordinal)})"
: String.Empty);
? $"{Environment.NewLine}[{Metadata.Name}](https://github.com/{remoteRepositoryOwner}/{remoteRepositoryName}/deployments/activity_log?environment=TGS%3A+{Metadata.Name!.Replace(" ", "+", StringComparison.Ordinal)})"
: Metadata.Name, // 5
compileJob.RevisionInformation.OriginCommitSha, // 6
compileJob.RevisionInformation.CommitSha, // 7
compileJob.Job.StartedAt); // 8
/// <inheritdoc />
protected override string FormatTestMergeRemoval(
RepositorySettings repositorySettings,
CompileJob compileJob,
TestMerge testMerge,
string remoteRepositoryOwner,
string remoteRepositoryName) => String.Format(
CultureInfo.InvariantCulture,
"<details><summary>Test Merge Removed @ {2}:</summary>{0}{0}##### Server Instance{0}{1}{0}</details>{0}",
Environment.NewLine, // 0
Metadata.Name, // 1
compileJob.Job.StartedAt); // 2
/// <summary>
/// Update the deployment for a given <paramref name="compileJob"/>.
@@ -6,13 +6,13 @@ using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using GitLabApiClient;
using GitLabApiClient.Models.MergeRequests.Responses;
using GitLabApiClient.Models.Notes.Requests;
using Microsoft.Extensions.Logging;
using StrawberryShake;
using Tgstation.Server.Host.Components.Repository;
using Tgstation.Server.Host.Models;
using Tgstation.Server.Host.Utils.GitLab.GraphQL;
namespace Tgstation.Server.Host.Components.Deployment.Remote
{
@@ -52,49 +52,78 @@ namespace Tgstation.Server.Host.Components.Deployment.Remote
return Array.Empty<TestMerge>();
}
var client = repositorySettings.AccessToken != null
? new GitLabClient(GitLabRemoteFeatures.GitLabUrl, repositorySettings.AccessToken)
: new GitLabClient(GitLabRemoteFeatures.GitLabUrl);
var newList = revisionInformation.ActiveTestMerges.Select(x => x.TestMerge).ToList();
var tasks = revisionInformation
.ActiveTestMerges
.Select(x => client
.MergeRequests
.GetAsync(
$"{repository.RemoteRepositoryOwner}/{repository.RemoteRepositoryName}",
x.TestMerge.Number)
.WaitAsync(cancellationToken));
await using var client = await GraphQLGitLabClientFactory.CreateClient(repositorySettings.AccessToken);
IOperationResult<IGetMergeRequestsResult> operationResult;
try
{
await Task.WhenAll(tasks);
operationResult = await client.GraphQL.GetMergeRequests.ExecuteAsync(
$"{repository.RemoteRepositoryOwner}/{repository.RemoteRepositoryName}",
revisionInformation.ActiveTestMerges.Select(revInfoTestMerge => revInfoTestMerge.TestMerge.Number.ToString(CultureInfo.InvariantCulture)).ToList(),
cancellationToken);
operationResult.EnsureNoErrors();
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
Logger.LogWarning(ex, "Merge requests update check failed!");
return newList;
}
var newList = revisionInformation.ActiveTestMerges.Select(x => x.TestMerge).ToList();
MergeRequest? lastMerged = null;
async ValueTask CheckRemoveMR(Task<MergeRequest> task)
var data = operationResult.Data?.Project?.MergeRequests?.Nodes;
if (data == null)
{
var mergeRequest = await task;
Logger.LogWarning("GitLab MergeRequests check returned null!");
return newList;
}
async ValueTask CheckRemoveMR(IGetMergeRequests_Project_MergeRequests_Nodes? mergeRequest)
{
if (mergeRequest == null)
{
Logger.LogWarning("GitLab MergeRequest node was null!");
return;
}
if (mergeRequest.State != MergeRequestState.Merged)
return;
// We don't just assume, actually check the repo contains the merge commit.
if (await repository.CommittishIsParent(mergeRequest.MergeCommitSha, cancellationToken))
var mergeCommitSha = mergeRequest.MergeCommitSha;
if (mergeCommitSha == null)
{
if (lastMerged == null || lastMerged.ClosedAt < mergeRequest.ClosedAt)
lastMerged = mergeRequest;
Logger.LogWarning("MergeRequest #{id} had no MergeCommitSha!", mergeRequest.Iid);
return;
}
var closedAtStr = mergeRequest.ClosedAt;
if (closedAtStr == null)
{
Logger.LogWarning("MergeRequest #{id} had no ClosedAt!", mergeRequest.Iid);
return;
}
if (!DateTimeOffset.TryParseExact(closedAtStr, "O", CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out DateTimeOffset closedAt))
{
Logger.LogWarning("MergeRequest #{id} had invalid ClosedAt: {closedAt}", mergeRequest.Iid, closedAtStr);
return;
}
if (!Int64.TryParse(mergeRequest.Iid, out long number))
{
Logger.LogWarning("MergeRequest #{id} is non-numeric!", mergeRequest.Iid);
return;
}
// We don't just assume, actually check the repo contains the merge commit.
if (await repository.CommittishIsParent(mergeCommitSha, cancellationToken))
newList.Remove(
newList.First(
potential => potential.Number == mergeRequest.Id));
}
potential => potential.Number == number));
}
foreach (var prTask in tasks)
await CheckRemoveMR(prTask);
foreach (var mergeRequest in data)
await CheckRemoveMR(mergeRequest);
return newList;
}
@@ -131,23 +160,69 @@ namespace Tgstation.Server.Host.Components.Deployment.Remote
int testMergeNumber,
CancellationToken cancellationToken)
{
var client = repositorySettings.AccessToken != null
? new GitLabClient(GitLabRemoteFeatures.GitLabUrl, repositorySettings.AccessToken)
: new GitLabClient(GitLabRemoteFeatures.GitLabUrl);
await using var client = await GraphQLGitLabClientFactory.CreateClient(repositorySettings.AccessToken);
try
{
await client
.MergeRequests
.CreateNoteAsync(
$"{remoteRepositoryOwner}/{remoteRepositoryName}",
testMergeNumber,
new CreateMergeRequestNoteRequest(comment))
.WaitAsync(cancellationToken);
string header = String.Format(CultureInfo.InvariantCulture, "{1}{0}## Test merge deployment history:{0}{0}", Environment.NewLine, DeploymentMsgHeaderStart);
// Try to find an existing note
var notesQueryResult = await client.GraphQL.GetMergeRequestNotes.ExecuteAsync(
$"{remoteRepositoryOwner}/{remoteRepositoryName}",
testMergeNumber.ToString(CultureInfo.InvariantCulture),
cancellationToken);
notesQueryResult.EnsureNoErrors();
var mergeRequest = notesQueryResult.Data?.Project?.MergeRequest;
if (mergeRequest == null)
{
Logger.LogWarning("GitLab GetMergeRequestNotes mergeRequest returned null!");
return;
}
var comments = mergeRequest.Notes?.Nodes;
IGetMergeRequestNotes_Project_MergeRequest_Notes_Nodes? existingComment = null;
if (comments != null)
{
for (int i = comments.Count - 1; i > -1; i--)
{
var currentComment = comments[i];
if (currentComment?.Author?.Username == repositorySettings.AccessUser && (currentComment?.Body?.StartsWith(DeploymentMsgHeaderStart) ?? false))
{
if (currentComment.Body.Length > 987856)
{ // Limit should be 999,999 so we'll leave a 12,143 buffer
break;
}
existingComment = currentComment;
break;
}
}
}
// Either amend or create the note
if (existingComment != null)
{
var noteModificationResult = await client.GraphQL.ModifyNote.ExecuteAsync(
existingComment.Id,
existingComment.Body + comment,
cancellationToken);
notesQueryResult.EnsureNoErrors();
}
else
{
var noteCreationResult = await client.GraphQL.CreateNote.ExecuteAsync(
mergeRequest.Id,
header + comment,
cancellationToken);
noteCreationResult.EnsureNoErrors();
}
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
Logger.LogWarning(ex, "Error posting GitHub comment!");
Logger.LogWarning(ex, "Error posting GitLab comment!");
}
}
@@ -160,26 +235,40 @@ namespace Tgstation.Server.Host.Components.Deployment.Remote
string remoteRepositoryName,
bool updated) => String.Format(
CultureInfo.InvariantCulture,
"#### Test Merge {4}{0}{0}##### Server Instance{0}{5}{1}{0}{0}##### Revision{0}Origin: {6}{0}Merge Request: {2}{0}Server: {7}{3}",
Environment.NewLine,
"<details><summary>Test Merge {4} @ {8}</summary>{0}{0}##### Server Instance{0}{5}{1}{0}{0}##### Revision{0}Origin: {6}{0}Merge Request: {2}{0}Server: {7}{3}</details>{0}",
Environment.NewLine, // 0
repositorySettings.ShowTestMergeCommitters!.Value
? String.Format(
CultureInfo.InvariantCulture,
"{0}{0}##### Merged By{0}{1}",
Environment.NewLine,
testMerge.MergedBy!.Name)
: String.Empty,
testMerge.TargetCommitSha,
testMerge.Comment != null
? String.Format(
: String.Empty, // 1
testMerge.TargetCommitSha, // 2
String.IsNullOrEmpty(testMerge.Comment)
? String.Empty
: String.Format(
CultureInfo.InvariantCulture,
"{0}{0}##### Comment{0}{1}",
Environment.NewLine,
testMerge.Comment)
: String.Empty,
updated ? "Updated" : "Deployed",
Metadata.Name,
compileJob.RevisionInformation.OriginCommitSha,
compileJob.RevisionInformation.CommitSha);
testMerge.Comment), // 3
updated ? "Updated" : "Deployed", // 4
Metadata.Name, // 5
compileJob.RevisionInformation.OriginCommitSha, // 6
compileJob.RevisionInformation.CommitSha, // 7
compileJob.Job.StartedAt); // 8
/// <inheritdoc />
protected override string FormatTestMergeRemoval(
RepositorySettings repositorySettings,
CompileJob compileJob,
TestMerge testMerge,
string remoteRepositoryOwner,
string remoteRepositoryName) => String.Format(
CultureInfo.InvariantCulture,
"<details><summary>Test Merge Removed @ {2}:</summary>{0}{0}##### Server Instance{0}{1}{0}</details>{0}",
Environment.NewLine, // 0
Metadata.Name, // 1
compileJob.Job.StartedAt); // 2
}
}
@@ -65,6 +65,15 @@ namespace Tgstation.Server.Host.Components.Deployment.Remote
bool updated)
=> String.Empty;
/// <inheritdoc />
protected override string FormatTestMergeRemoval(
Models.RepositorySettings repositorySettings,
Models.CompileJob compileJob,
TestMerge testMerge,
string remoteRepositoryOwner,
string remoteRepositoryName)
=> String.Empty;
/// <inheritdoc />
protected override ValueTask MarkInactiveImpl(Models.CompileJob compileJob, CancellationToken cancellationToken) => ValueTask.CompletedTask;
@@ -1,11 +1,14 @@
using System;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
using GitLabApiClient;
using Microsoft.Extensions.Logging;
using StrawberryShake;
using Tgstation.Server.Api.Models;
using Tgstation.Server.Host.Utils.GitLab.GraphQL;
namespace Tgstation.Server.Host.Components.Repository
{
@@ -45,30 +48,25 @@ namespace Tgstation.Server.Host.Components.Repository
RepositorySettings repositorySettings,
CancellationToken cancellationToken)
{
var client = repositorySettings.AccessToken != null
? new GitLabClient(GitLabUrl, repositorySettings.AccessToken)
: new GitLabClient(GitLabUrl);
await using var client = await GraphQLGitLabClientFactory.CreateClient(repositorySettings.AccessToken);
try
{
var mr = await client
.MergeRequests
.GetAsync($"{RemoteRepositoryOwner}/{RemoteRepositoryName}", parameters.Number)
.WaitAsync(cancellationToken);
var operationResult = await client.GraphQL.GetMergeRequest.ExecuteAsync(
$"{RemoteRepositoryOwner}/{RemoteRepositoryName}",
parameters.Number.ToString(CultureInfo.InvariantCulture),
cancellationToken);
var revisionToUse = parameters.TargetCommitSha == null
|| mr.Sha.StartsWith(parameters.TargetCommitSha, StringComparison.OrdinalIgnoreCase)
? mr.Sha
: parameters.TargetCommitSha;
operationResult.EnsureNoErrors();
var mr = operationResult.Data?.Project?.MergeRequest ?? throw new InvalidOperationException("GitLab MergeRequest check returned null!");
return new Models.TestMerge
{
Author = mr.Author.Username,
Author = mr.Author?.Username,
BodyAtMerge = mr.Description,
TitleAtMerge = mr.Title,
Comment = parameters.Comment,
Number = parameters.Number,
TargetCommitSha = mr.Sha,
TargetCommitSha = mr.DiffHeadSha,
Url = mr.WebUrl,
};
}
@@ -7,12 +7,12 @@ using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using GitLabApiClient;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using StrawberryShake;
using Tgstation.Server.Api;
using Tgstation.Server.Api.Models;
using Tgstation.Server.Api.Models.Request;
@@ -27,6 +27,7 @@ using Tgstation.Server.Host.Models;
using Tgstation.Server.Host.Security;
using Tgstation.Server.Host.Utils;
using Tgstation.Server.Host.Utils.GitHub;
using Tgstation.Server.Host.Utils.GitLab.GraphQL;
namespace Tgstation.Server.Host.Controllers
{
@@ -585,70 +586,84 @@ namespace Tgstation.Server.Host.Controllers
switch (remoteFeatures.RemoteGitProvider!.Value)
{
case RemoteGitProvider.GitHub:
var gitHubClient = await gitHubClientFactory.CreateClientForRepository(
{
var gitHubClient = await gitHubClientFactory.CreateClientForRepository(
model.AccessToken,
new RepositoryIdentifier(
remoteFeatures.RemoteRepositoryOwner!,
remoteFeatures.RemoteRepositoryName!),
cancellationToken);
if (gitHubClient == null)
{
return this.StatusCode(HttpStatusCode.FailedDependency, new ErrorMessageResponse(ErrorCode.RemoteApiError)
if (gitHubClient == null)
{
AdditionalData = "GitHub authentication failed!",
});
}
try
{
string username;
if (!model.AccessToken.StartsWith(Api.Models.RepositorySettings.TgsAppPrivateKeyPrefix))
{
var user = await gitHubClient.User.Current();
username = user.Login;
}
else
{
// we literally need to app auth again to get the damn bot username
var appClient = gitHubClientFactory.CreateAppClient(model.AccessToken)!;
var app = await appClient.GitHubApps.GetCurrent();
username = app.Name;
return this.StatusCode(HttpStatusCode.FailedDependency, new ErrorMessageResponse(ErrorCode.RemoteApiError)
{
AdditionalData = "GitHub authentication failed!",
});
}
if (username != model.AccessUser)
return Conflict(new ErrorMessageResponse(ErrorCode.RepoTokenUsernameMismatch));
}
catch (Exception ex)
{
return this.StatusCode(HttpStatusCode.FailedDependency, new ErrorMessageResponse(ErrorCode.RemoteApiError)
try
{
AdditionalData = $"GitHub Authentication Failure: {ex.Message}",
});
string username;
if (!model.AccessToken.StartsWith(Api.Models.RepositorySettings.TgsAppPrivateKeyPrefix))
{
var user = await gitHubClient.User.Current();
username = user.Login;
}
else
{
// we literally need to app auth again to get the damn bot username
var appClient = gitHubClientFactory.CreateAppClient(model.AccessToken)!;
var app = await appClient.GitHubApps.GetCurrent();
username = app.Name;
}
if (username != model.AccessUser)
return Conflict(new ErrorMessageResponse(ErrorCode.RepoTokenUsernameMismatch));
}
catch (Exception ex)
{
return this.StatusCode(HttpStatusCode.FailedDependency, new ErrorMessageResponse(ErrorCode.RemoteApiError)
{
AdditionalData = $"GitHub Authentication Failure: {ex.Message}",
});
}
break;
}
break;
case RemoteGitProvider.GitLab:
// need to abstract this eventually
var gitLabClient = new GitLabClient(GitLabRemoteFeatures.GitLabUrl, model.AccessToken);
try
{
var user = await gitLabClient.Users.GetCurrentSessionAsync();
if (user.Username != model.AccessUser)
return Conflict(new ErrorMessageResponse(ErrorCode.RepoTokenUsernameMismatch));
}
catch (Exception ex)
{
return this.StatusCode(HttpStatusCode.FailedDependency, new ErrorMessageResponse(ErrorCode.RemoteApiError)
// need to abstract this eventually
await using var gitLabClient = await GraphQLGitLabClientFactory.CreateClient(model.AccessToken);
try
{
AdditionalData = $"GitLab Authentication Failure: {ex.Message}",
});
var operationResult = await gitLabClient.GraphQL.GetCurrentUser.ExecuteAsync(cancellationToken);
operationResult.EnsureNoErrors();
var user = operationResult.Data?.CurrentUser;
if (user == null || user.Username != model.AccessUser)
{
return Conflict(new ErrorMessageResponse(ErrorCode.RepoTokenUsernameMismatch));
}
}
catch (Exception ex)
{
return this.StatusCode(HttpStatusCode.FailedDependency, new ErrorMessageResponse(ErrorCode.RemoteApiError)
{
AdditionalData = $"GitLab Authentication Failure: {ex.Message}",
});
}
break;
}
break;
case RemoteGitProvider.Unknown:
default:
Logger.LogWarning("RemoteGitProvider is {provider}, no auth check implemented!", remoteFeatures.RemoteGitProvider.Value);
break;
{
Logger.LogWarning("RemoteGitProvider is {provider}, no auth check implemented!", remoteFeatures.RemoteGitProvider.Value);
break;
}
}
return null;
@@ -97,8 +97,6 @@
<PackageReference Include="DotEnv.Core" Version="3.1.0" />
<!-- Usage: Text formatter for Elasticsearch logging plugin -->
<PackageReference Include="Elastic.CommonSchema.Serilog" Version="8.12.3" />
<!-- Usage: GitLab interop -->
<PackageReference Include="GitLabApiClient" Version="1.8.0" />
<!-- Usage: GraphQL API Engine -->
<PackageReference Include="HotChocolate.AspNetCore" Version="15.0.3" />
<!-- Usage: GraphQL Authorization Plugin -->
@@ -171,6 +169,7 @@
<ItemGroup>
<ProjectReference Include="..\Tgstation.Server.Api\Tgstation.Server.Api.csproj" />
<ProjectReference Include="..\Tgstation.Server.Host.Common\Tgstation.Server.Host.Common.csproj" />
<ProjectReference Include="..\Tgstation.Server.Host.Utils.GitLab.GraphQL\Tgstation.Server.Host.Utils.GitLab.GraphQL.csproj" />
<ProjectReference Include="..\Tgstation.Server.Shared\Tgstation.Server.Shared.csproj" />
</ItemGroup>
@@ -163,6 +163,73 @@ namespace Tgstation.Server.Host.Utils.GitHub
.WaitAsync(cancellationToken);
}
/// <inheritdoc />
public Task AppendCommentOnIssue(string repoOwner, string repoName, string comment, IssueComment issueComment, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(repoOwner);
ArgumentNullException.ThrowIfNull(repoName);
ArgumentNullException.ThrowIfNull(comment);
ArgumentNullException.ThrowIfNull(issueComment);
logger.LogTrace("AppendCommentOnIssue");
return gitHubClient
.Issue
.Comment
.Update(
repoOwner,
repoName,
issueComment.Id,
issueComment.Body + comment)
.WaitAsync(cancellationToken);
}
/// <inheritdoc />
public async ValueTask<IssueComment?> GetExistingCommentOnIssue(string repoOwner, string repoName, string header, int issueNumber, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(repoOwner);
ArgumentNullException.ThrowIfNull(repoName);
ArgumentNullException.ThrowIfNull(header);
logger.LogTrace("GetExistingCommentOnIssue");
var comments = await gitHubClient
.Issue
.Comment
.GetAllForIssue(
repoOwner,
repoName,
issueNumber)
.WaitAsync(cancellationToken);
if (comments == null)
{
return null;
}
long userId = await GetCurrentUserId(cancellationToken);
for (int i = comments.Count - 1; i > -1; i--)
{
var currentComment = comments[i];
if (currentComment.User?.Id == userId && (currentComment.Body?.StartsWith(header) ?? false))
{
if (currentComment.Body.Length > 250000)
{ // Limit should be 262,143 so we'll leave a 12,143 buffer
return null;
}
return currentComment;
}
}
return null;
}
/// <inheritdoc />
public async ValueTask<long> GetRepositoryId(string repoOwner, string repoName, CancellationToken cancellationToken)
{
@@ -21,6 +21,28 @@ namespace Tgstation.Server.Host.Utils.GitHub
/// <returns>A <see cref="Task"/> representing the running operation.</returns>
Task CommentOnIssue(string repoOwner, string repoName, string comment, int issueNumber, CancellationToken cancellationToken);
/// <summary>
/// Append a comment on an existing <paramref name="issueComment"/>.
/// </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="issueComment">The <see cref="IssueComment"/> to amend.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation.</param>
/// <returns>A <see cref="Task"/> representing the running operation.</returns>
Task AppendCommentOnIssue(string repoOwner, string repoName, string comment, IssueComment issueComment, CancellationToken cancellationToken);
/// <summary>
/// Gets an <see cref="IssueComment"/> for a particular <paramref name="issueNumber"/> with the provided <paramref name="header"/> if it exists and is not too large.
/// </summary>
/// <param name="repoOwner">The owner of the target repository.</param>
/// <param name="repoName">The name of the target repository.</param>
/// <param name="header">The starting text of the comment to search for.</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="ValueTask{TResult}"/> of an existing comment if its currently under 250k characters, otherwise the <see cref="IssueComment"/> will be null.</returns>
ValueTask<IssueComment?> GetExistingCommentOnIssue(string repoOwner, string repoName, string header, int issueNumber, CancellationToken cancellationToken);
/// <summary>
/// Create a <paramref name="newDeployment"/> on a target repostiory.
/// </summary>
@@ -105,6 +105,7 @@ namespace Tgstation.Server.Tests.Live
public override ValueTask<Func<string, string, ValueTask<Func<bool, ValueTask>>>> SendUpdateMessage(
RevisionInformation revisionInformation,
RevisionInformation previousRevisionInformation,
Api.Models.EngineVersion engineVersion,
DateTimeOffset? estimatedCompletionTime,
string gitHubOwner,
@@ -91,6 +91,18 @@ namespace Tgstation.Server.Tests.Live
return Task.CompletedTask;
}
public Task AppendCommentOnIssue(string repoOwner, string repoName, string comment, IssueComment issueComment, CancellationToken cancellationToken)
{
logger.LogTrace("AppendCommentOnIssue");
return Task.CompletedTask;
}
public ValueTask<IssueComment> GetExistingCommentOnIssue(string repoOwner, string repoName, string header, int issueNumber, CancellationToken cancellationToken)
{
logger.LogTrace("GetExistingCommentOnIssue");
return ValueTask.FromResult<IssueComment>(null);
}
public ValueTask<long> CreateDeployment(NewDeployment newDeployment, string repoOwner, string repoName, CancellationToken cancellationToken)
{
logger.LogTrace("CreateDeployment");
+14
View File
@@ -275,6 +275,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "nix", "nix", "{5130526C-A55
build\package\nix\tgstation-server.nix = build\package\nix\tgstation-server.nix
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tgstation.Server.Host.Utils.GitLab.GraphQL", "src\Tgstation.Server.Host.Utils.GitLab.GraphQL\Tgstation.Server.Host.Utils.GitLab.GraphQL.csproj", "{BF02BCC5-735C-4FF1-8EEF-FF78EA42FC85}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -557,6 +559,18 @@ Global
{8BF95E2D-FD27-470C-82B7-C21AC01BFBD7}.ReleaseNoWindows|Any CPU.Build.0 = Release|Any CPU
{8BF95E2D-FD27-470C-82B7-C21AC01BFBD7}.ReleaseNoWix|Any CPU.ActiveCfg = Release|Any CPU
{8BF95E2D-FD27-470C-82B7-C21AC01BFBD7}.ReleaseNoWix|Any CPU.Build.0 = Release|Any CPU
{BF02BCC5-735C-4FF1-8EEF-FF78EA42FC85}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BF02BCC5-735C-4FF1-8EEF-FF78EA42FC85}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BF02BCC5-735C-4FF1-8EEF-FF78EA42FC85}.DebugNoWindows|Any CPU.ActiveCfg = Debug|Any CPU
{BF02BCC5-735C-4FF1-8EEF-FF78EA42FC85}.DebugNoWindows|Any CPU.Build.0 = Debug|Any CPU
{BF02BCC5-735C-4FF1-8EEF-FF78EA42FC85}.DebugNoWix|Any CPU.ActiveCfg = Debug|Any CPU
{BF02BCC5-735C-4FF1-8EEF-FF78EA42FC85}.DebugNoWix|Any CPU.Build.0 = Debug|Any CPU
{BF02BCC5-735C-4FF1-8EEF-FF78EA42FC85}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BF02BCC5-735C-4FF1-8EEF-FF78EA42FC85}.Release|Any CPU.Build.0 = Release|Any CPU
{BF02BCC5-735C-4FF1-8EEF-FF78EA42FC85}.ReleaseNoWindows|Any CPU.ActiveCfg = Release|Any CPU
{BF02BCC5-735C-4FF1-8EEF-FF78EA42FC85}.ReleaseNoWindows|Any CPU.Build.0 = Release|Any CPU
{BF02BCC5-735C-4FF1-8EEF-FF78EA42FC85}.ReleaseNoWix|Any CPU.ActiveCfg = Release|Any CPU
{BF02BCC5-735C-4FF1-8EEF-FF78EA42FC85}.ReleaseNoWix|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE