tgstation-server 5.12.7
The /tg/station 13 server suite
Loading...
Searching...
No Matches
GitLabRemoteDeploymentManager.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Globalization;
4using System.Linq;
5using System.Threading;
6using System.Threading.Tasks;
7
8using GitLabApiClient;
9using GitLabApiClient.Models.MergeRequests.Responses;
10using GitLabApiClient.Models.Notes.Requests;
11using Microsoft.Extensions.Logging;
12
16
18{
23 {
29 public GitLabRemoteDeploymentManager(ILogger<GitLabRemoteDeploymentManager> logger, Api.Models.Instance metadata)
30 : base(logger, metadata)
31 {
32 }
33
35 public override async Task<IReadOnlyCollection<TestMerge>> RemoveMergedTestMerges(
36 IRepository repository,
37 RepositorySettings repositorySettings,
38 RevisionInformation revisionInformation,
39 CancellationToken cancellationToken)
40 {
41 ArgumentNullException.ThrowIfNull(repository);
42 ArgumentNullException.ThrowIfNull(repositorySettings);
43 ArgumentNullException.ThrowIfNull(revisionInformation);
44
45 if (revisionInformation.ActiveTestMerges?.Any() != true)
46 {
47 Logger.LogTrace("No test merges to remove.");
48 return Array.Empty<TestMerge>();
49 }
50
51 var client = repositorySettings.AccessToken != null
52 ? new GitLabClient(GitLabRemoteFeatures.GitLabUrl, repositorySettings.AccessToken)
53 : new GitLabClient(GitLabRemoteFeatures.GitLabUrl);
54
55 var tasks = revisionInformation
57 .Select(x => client
58 .MergeRequests
59 .GetAsync(
60 $"{repository.RemoteRepositoryOwner}/{repository.RemoteRepositoryName}",
61 x.TestMerge.Number)
62 .WithToken(cancellationToken));
63 try
64 {
65 await Task.WhenAll(tasks);
66 }
67 catch (Exception ex) when (ex is not OperationCanceledException)
68 {
69 Logger.LogWarning(ex, "Merge requests update check failed!");
70 }
71
72 var newList = revisionInformation.ActiveTestMerges.Select(x => x.TestMerge).ToList();
73
74 MergeRequest lastMerged = null;
75 async Task CheckRemoveMR(Task<MergeRequest> task)
76 {
77 var mergeRequest = await task;
78 if (mergeRequest.State != MergeRequestState.Merged)
79 return;
80
81 // We don't just assume, actually check the repo contains the merge commit.
82 if (await repository.ShaIsParent(mergeRequest.MergeCommitSha, cancellationToken))
83 {
84 if (lastMerged == null || lastMerged.ClosedAt < mergeRequest.ClosedAt)
85 lastMerged = mergeRequest;
86 newList.Remove(
87 newList.First(
88 potential => potential.Number == mergeRequest.Id));
89 }
90 }
91
92 foreach (var prTask in tasks)
93 await CheckRemoveMR(prTask);
94
95 return newList;
96 }
97
99 public override Task ApplyDeployment(
100 CompileJob compileJob,
101 CompileJob oldCompileJob,
102 CancellationToken cancellationToken) => Task.CompletedTask;
103
105 public override Task FailDeployment(
106 CompileJob compileJob,
107 string errorMessage,
108 CancellationToken cancellationToken) => Task.CompletedTask;
109
111 public override Task MarkInactive(CompileJob compileJob, CancellationToken cancellationToken) => Task.CompletedTask;
112
114 public override Task StageDeployment(CompileJob compileJob, CancellationToken cancellationToken) => Task.CompletedTask;
115
117 public override Task StartDeployment(
118 Api.Models.Internal.IGitRemoteInformation remoteInformation,
119 CompileJob compileJob,
120 CancellationToken cancellationToken) => Task.CompletedTask;
121
123 protected override async Task CommentOnTestMergeSource(
124 RepositorySettings repositorySettings,
125 string remoteRepositoryOwner,
126 string remoteRepositoryName,
127 string comment,
128 int testMergeNumber,
129 CancellationToken cancellationToken)
130 {
131 var client = repositorySettings.AccessToken != null
132 ? new GitLabClient(GitLabRemoteFeatures.GitLabUrl, repositorySettings.AccessToken)
133 : new GitLabClient(GitLabRemoteFeatures.GitLabUrl);
134
135 try
136 {
137 await client
138 .MergeRequests
139 .CreateNoteAsync(
140 $"{remoteRepositoryOwner}/{remoteRepositoryName}",
141 testMergeNumber,
142 new CreateMergeRequestNoteRequest(comment))
143 .WithToken(cancellationToken);
144 }
145 catch (Exception ex) when (ex is not OperationCanceledException)
146 {
147 Logger.LogWarning(ex, "Error posting GitHub comment!");
148 }
149 }
150
152 protected override string FormatTestMerge(
153 RepositorySettings repositorySettings,
154 CompileJob compileJob,
155 TestMerge testMerge,
156 string remoteRepositoryOwner,
157 string remoteRepositoryName,
158 bool updated) => String.Format(
159 CultureInfo.InvariantCulture,
160 "#### Test Merge {4}{0}{0}##### Server Instance{0}{5}{1}{0}{0}##### Revision{0}Origin: {6}{0}Merge Request: {2}{0}Server: {7}{3}",
161 Environment.NewLine,
162 repositorySettings.ShowTestMergeCommitters.Value
163 ? String.Format(
164 CultureInfo.InvariantCulture,
165 "{0}{0}##### Merged By{0}{1}",
166 Environment.NewLine,
167 testMerge.MergedBy.Name)
168 : String.Empty,
169 testMerge.TargetCommitSha,
170 testMerge.Comment != null
171 ? String.Format(
172 CultureInfo.InvariantCulture,
173 "{0}{0}##### Comment{0}{1}",
174 Environment.NewLine,
175 testMerge.Comment)
176 : String.Empty,
177 updated ? "Updated" : "Deployed",
178 Metadata.Name,
179 compileJob.RevisionInformation.OriginCommitSha,
180 compileJob.RevisionInformation.CommitSha);
181 }
182}
string? AccessToken
The token/password to access the git repository with.
Api.Models.Instance Metadata
The Api.Models.Instance for the BaseRemoteDeploymentManager.
ILogger< BaseRemoteDeploymentManager > Logger
The ILogger for the BaseRemoteDeploymentManager.
override string FormatTestMerge(RepositorySettings repositorySettings, CompileJob compileJob, TestMerge testMerge, string remoteRepositoryOwner, string remoteRepositoryName, bool updated)
Formats a comment for a given testMerge . A Task representing the running operation.
override Task ApplyDeployment(CompileJob compileJob, CompileJob oldCompileJob, CancellationToken cancellationToken)
Stage a given compileJob 's deployment. A Task representing the running operation.
GitLabRemoteDeploymentManager(ILogger< GitLabRemoteDeploymentManager > logger, Api.Models.Instance metadata)
Initializes a new instance of the GitLabRemoteDeploymentManager class.
override async Task CommentOnTestMergeSource(RepositorySettings repositorySettings, string remoteRepositoryOwner, string remoteRepositoryName, string comment, int testMergeNumber, CancellationToken cancellationToken)
Create a comment of a given testMergeNumber 's source. A Task representing the running operation.
override Task MarkInactive(CompileJob compileJob, CancellationToken cancellationToken)
Mark the deplotment for a given compileJob as inactive. A Task representing the running operation.
override Task StageDeployment(CompileJob compileJob, CancellationToken cancellationToken)
Stage a given compileJob 's deployment. A Task representing the running operation.
override Task StartDeployment(Api.Models.Internal.IGitRemoteInformation remoteInformation, CompileJob compileJob, CancellationToken cancellationToken)
Start a deployment for a given compileJob . A Task representing the running operation.
override async Task< IReadOnlyCollection< TestMerge > > RemoveMergedTestMerges(IRepository repository, RepositorySettings repositorySettings, RevisionInformation revisionInformation, CancellationToken cancellationToken)
Get the updated list of TestMerges for an origin merge. A Task<TResult> resulting in the IReadOnlyCol...
override Task FailDeployment(CompileJob compileJob, string errorMessage, CancellationToken cancellationToken)
Fail a deployment for a given compileJob . A Task representing the running operation.
ICollection< RevInfoTestMerge > ActiveTestMerges
See Api.Models.RevisionInformation.ActiveTestMerges.
Represents an on-disk git repository.
Definition: IRepository.cs:14
Task< bool > ShaIsParent(string sha, CancellationToken cancellationToken)
Check if a given sha is a parent of the current Head.