tgstation-server  4.4.0
The /tg/station 13 server suite
PullRequestsCommand.cs
Go to the documentation of this file.
1 using Microsoft.EntityFrameworkCore;
2 using System;
3 using System.Collections.Generic;
4 using System.Globalization;
5 using System.Linq;
6 using System.Threading;
7 using System.Threading.Tasks;
12 
13 namespace Tgstation.Server.Host.Components.Chat.Commands
14 {
19  {
21  public string Name => "prs";
22 
24  public string HelpText => "Display live test merge pull request numbers. Add --repo to view repository test merges";
25 
27  public bool AdminOnly => false;
28 
32  readonly IWatchdog watchdog;
33 
38 
43 
47  readonly Models.Instance instance;
48 
56  public PullRequestsCommand(IWatchdog watchdog, IRepositoryManager repositoryManager, IDatabaseContextFactory databaseContextFactory, Models.Instance instance)
57  {
58  this.watchdog = watchdog ?? throw new ArgumentNullException(nameof(watchdog));
59  this.repositoryManager = repositoryManager ?? throw new ArgumentNullException(nameof(repositoryManager));
60  this.databaseContextFactory = databaseContextFactory ?? throw new ArgumentNullException(nameof(databaseContextFactory));
61  this.instance = instance ?? throw new ArgumentNullException(nameof(instance));
62  }
63 
65  // TODO: Decomplexify
66  #pragma warning disable CA1506
67  public async Task<string> Invoke(string arguments, ChatUser user, CancellationToken cancellationToken)
68  {
69  IEnumerable<Models.TestMerge> results = null;
70  if (arguments.Split(' ').Any(x => x.ToUpperInvariant() == "--REPO"))
71  {
72  string head;
73  using (var repo = await repositoryManager.LoadRepository(cancellationToken).ConfigureAwait(false))
74  {
75  if (repo == null)
76  return "Repository unavailable!";
77  head = repo.Head;
78  }
79 
80  await databaseContextFactory.UseContext(
81  async db => results = await db
82  .RevisionInformations
83  .AsQueryable()
84  .Where(x => x.Instance.Id == instance.Id && x.CommitSha == head)
85  .SelectMany(x => x.ActiveTestMerges)
86  .Select(x => x.TestMerge)
87  .Select(x => new Models.TestMerge
88  {
89  Number = x.Number,
90  PullRequestRevision = x.PullRequestRevision
91  })
92  .ToListAsync(cancellationToken)
93  .ConfigureAwait(false))
94  .ConfigureAwait(false);
95  }
96  else
97  {
98  if (watchdog.Status == WatchdogStatus.Offline)
99  return "Server offline!";
100  results = watchdog.ActiveCompileJob?.RevisionInformation.ActiveTestMerges.Select(x => x.TestMerge).ToList() ?? new List<Models.TestMerge>();
101  }
102 
103  return !results.Any() ? "None!" : String.Join(", ", results.Select(x => String.Format(CultureInfo.InvariantCulture, "#{0} at {1}", x.Number, x.PullRequestRevision.Substring(0, 7))));
104  }
105  #pragma warning restore CA1506
106  }
107 }
readonly IDatabaseContextFactory databaseContextFactory
The IDatabaseContextFactory for the PullRequestsCommand
Factory for scoping usage of IDatabaseContexts. Meant for use by Components
readonly IRepositoryManager repositoryManager
The IRepositoryManager for the PullRequestsCommand
WatchdogStatus
The current status of the watchdog.
readonly Models.Instance instance
The Models.Instance for the PullRequestsCommand
Represents a tgs_chat_user datum
Definition: ChatUser.cs:10
readonly IWatchdog watchdog
The IWatchdog for the PullRequestsCommand
Factory for creating and loading IRepositorys
async Task< string > Invoke(string arguments, ChatUser user, CancellationToken cancellationToken)
Invoke the ICommand
PullRequestsCommand(IWatchdog watchdog, IRepositoryManager repositoryManager, IDatabaseContextFactory databaseContextFactory, Models.Instance instance)
Construct a PullRequestsCommand
Represents a command that can be invoked by talking to chat bots
Definition: ICommand.cs:9
Runs and monitors the twin server controllers
Definition: IWatchdog.cs:15