tgstation-server 6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
PullRequestsCommand.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Threading;
5using System.Threading.Tasks;
6
7using Microsoft.EntityFrameworkCore;
8
15
17{
22 {
24 public string Name => "prs";
25
27 public string HelpText => "Display live test merge numbers. Add --repo to view test merges in the repository as opposed to live. Add --staged to view PRs in the upcoming deployment.";
28
30 public bool AdminOnly => false;
31
36
41
46
51
56
70 Models.Instance instance)
71 {
72 this.watchdog = watchdog ?? throw new ArgumentNullException(nameof(watchdog));
73 this.repositoryManager = repositoryManager ?? throw new ArgumentNullException(nameof(repositoryManager));
74 this.databaseContextFactory = databaseContextFactory ?? throw new ArgumentNullException(nameof(databaseContextFactory));
75 this.compileJobProvider = compileJobProvider ?? throw new ArgumentNullException(nameof(compileJobProvider));
76 this.instance = instance ?? throw new ArgumentNullException(nameof(instance));
77 }
78
80 // TODO: Decomplexify
81#pragma warning disable CA1506
82 public async ValueTask<MessageContent> Invoke(string arguments, ChatUser user, CancellationToken cancellationToken)
83 {
84 IEnumerable<Models.TestMerge> results;
85 var splits = arguments.Split(' ');
86 var hasRepo = splits.Any(x => x.Equals("--repo", StringComparison.OrdinalIgnoreCase));
87 var hasStaged = splits.Any(x => x.Equals("--staged", StringComparison.OrdinalIgnoreCase));
88
89 if (hasStaged && hasRepo)
90 return new MessageContent
91 {
92 Text = "Please use only one of `--staged` or `--repo`",
93 };
94
95 if (hasRepo)
96 {
98 return new MessageContent
99 {
100 Text = "Repository busy! Try again later",
101 };
102
103 string head;
104 using (var repo = await repositoryManager.LoadRepository(cancellationToken))
105 {
106 if (repo == null)
107 return new MessageContent
108 {
109 Text = "Repository unavailable!",
110 };
111
112 head = repo.Head;
113 }
114
115 results = null!;
117 async db => results = await db
118 .RevisionInformations
119 .AsQueryable()
120 .Where(x => x.Instance!.Id == instance.Id && x.CommitSha == head)
121 .SelectMany(x => x.ActiveTestMerges!)
122 .Select(x => x.TestMerge)
123 .Select(x => new Models.TestMerge
124 {
125 Number = x.Number,
126 TargetCommitSha = x.TargetCommitSha,
127 })
128 .ToListAsync(cancellationToken));
129 }
130 else if (watchdog.Status == WatchdogStatus.Offline)
131 return new MessageContent
132 {
133 Text = "Server offline!",
134 };
135 else
136 {
137 var compileJobToUse = watchdog.ActiveCompileJob;
138 if (hasStaged)
139 {
140 var latestCompileJob = await compileJobProvider.LatestCompileJob();
141 if (latestCompileJob?.Id != compileJobToUse?.Id)
142 compileJobToUse = latestCompileJob;
143 else
144 compileJobToUse = null;
145 }
146
147 results = compileJobToUse?.RevisionInformation.ActiveTestMerges?.Select(x => x.TestMerge).ToList() ?? Enumerable.Empty<Models.TestMerge>();
148 }
149
150 return new MessageContent
151 {
152 Text = !results.Any()
153 ? "None!"
154 : String.Join(
155 ", ",
156 results.Select(
157 x => $"#{x.Number} at {x.TargetCommitSha![..7]}")),
158 };
159 }
160#pragma warning restore CA1506
161 }
162}
Represents a tgs_chat_user datum.
Definition ChatUser.cs:12
readonly IDatabaseContextFactory databaseContextFactory
The IDatabaseContextFactory for the PullRequestsCommand.
string Name
The text to invoke the command. May not be "?" or "help" (case-insensitive).
readonly Models.Instance instance
The Models.Instance for the PullRequestsCommand.
string HelpText
The help text to display when queires are made about the command.
bool AdminOnly
If the command should only be available to ChatUsers who's ChatUser.Channel has ChannelRepresentation...
async ValueTask< MessageContent > Invoke(string arguments, ChatUser user, CancellationToken cancellationToken)
Invoke the ICommand.A ValueTask<TResult> resulting in a MessageContent to send to the invoker.
readonly IWatchdog watchdog
The IWatchdog for the PullRequestsCommand.
PullRequestsCommand(IWatchdog watchdog, IRepositoryManager repositoryManager, IDatabaseContextFactory databaseContextFactory, ILatestCompileJobProvider compileJobProvider, Models.Instance instance)
Initializes a new instance of the PullRequestsCommand class.
readonly IRepositoryManager repositoryManager
The IRepositoryManager for the PullRequestsCommand.
readonly ILatestCompileJobProvider compileJobProvider
The ILatestCompileJobProvider for the PullRequestsCommand.
Represents a message to send to a chat provider.
Represents an Api.Models.Instance in the database.
Definition Instance.cs:11
Represents a command that can be invoked by talking to chat bots.
Definition ICommand.cs:12
ValueTask< CompileJob?> LatestCompileJob()
Gets the latest CompileJob.
bool CloneInProgress
If a CloneRepository(Uri, string, string, string, JobProgressReporter, bool, CancellationToken) opera...
ValueTask< IRepository?> LoadRepository(CancellationToken cancellationToken)
Attempt to load the IRepository from the default location.
bool InUse
If something is holding a lock on the repository.
Runs and monitors the twin server controllers.
Definition IWatchdog.cs:16
Models.? CompileJob ActiveCompileJob
Retrieves the Models.CompileJob currently running on the server.
Definition IWatchdog.cs:50
WatchdogStatus Status
The current WatchdogStatus.
Definition IWatchdog.cs:35
Factory for scoping usage of IDatabaseContexts. Meant for use by Components.
ValueTask UseContext(Func< IDatabaseContext, ValueTask > operation)
Run an operation in the scope of an IDatabaseContext.
WatchdogStatus
The current status of the watchdog.