Setup GitLab GraphQL client and replace one call

This commit is contained in:
Jordan Dominion
2025-02-13 19:07:58 -05:00
parent 61a604b79f
commit 653b82538d
12 changed files with 3754 additions and 26 deletions
+2
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
@@ -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 @@
query GetMergeRequests($project: ID!, $numbers: [String!]!) {
project(fullPath: $project) {
mergeRequests(iids: $numbers) {
nodes {
state
mergeCommitSha
closedAt
iid
}
}
}
}
@@ -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);
}
});
ServiceProvider 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,47 @@
<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>
<GraphQL Remove="gql\queries\GetMergeRequests.graphql" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="StrawberryShake.Server" Version="15.0.3" />
</ItemGroup>
<ItemGroup>
<Folder Include="GQL\Queries\" />
</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
@@ -7,12 +7,15 @@ 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 +55,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;
}
@@ -164,6 +164,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>
+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