tgstation-server  4.3.2
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;
11 
12 namespace Tgstation.Server.Host.Components.Chat.Commands
13 {
18  {
20  public string Name => "prs";
21 
23  public string HelpText => "Display live test merge pull request numbers. Add --repo to view repository test merges";
24 
26  public bool AdminOnly => false;
27 
31  readonly IWatchdog watchdog;
32 
37 
42 
46  readonly Models.Instance instance;
47 
55  public PullRequestsCommand(IWatchdog watchdog, IRepositoryManager repositoryManager, IDatabaseContextFactory databaseContextFactory, Models.Instance instance)
56  {
57  this.watchdog = watchdog ?? throw new ArgumentNullException(nameof(watchdog));
58  this.repositoryManager = repositoryManager ?? throw new ArgumentNullException(nameof(repositoryManager));
59  this.databaseContextFactory = databaseContextFactory ?? throw new ArgumentNullException(nameof(databaseContextFactory));
60  this.instance = instance ?? throw new ArgumentNullException(nameof(instance));
61  }
62 
64  // TODO: Decomplexify
65  #pragma warning disable CA1506
66  public async Task<string> Invoke(string arguments, ChatUser user, CancellationToken cancellationToken)
67  {
68  IEnumerable<Models.TestMerge> results = null;
69  if (arguments.Split(' ').Any(x => x.ToUpperInvariant() == "--REPO"))
70  {
71  string head;
72  using (var repo = await repositoryManager.LoadRepository(cancellationToken).ConfigureAwait(false))
73  {
74  if (repo == null)
75  return "Repository unavailable!";
76  head = repo.Head;
77  }
78 
79  await databaseContextFactory.UseContext(
80  async db => results = await db
81  .RevisionInformations
82  .AsQueryable()
83  .Where(x => x.Instance.Id == instance.Id && x.CommitSha == head)
84  .SelectMany(x => x.ActiveTestMerges)
85  .Select(x => x.TestMerge)
86  .Select(x => new Models.TestMerge
87  {
88  Number = x.Number,
89  PullRequestRevision = x.PullRequestRevision
90  })
91  .ToListAsync(cancellationToken)
92  .ConfigureAwait(false))
93  .ConfigureAwait(false);
94  }
95  else
96  {
97  if (!watchdog.Running)
98  return "Server offline!";
99  results = watchdog.ActiveCompileJob?.RevisionInformation.ActiveTestMerges.Select(x => x.TestMerge).ToList() ?? new List<Models.TestMerge>();
100  }
101 
102  return !results.Any() ? "None!" : String.Join(", ", results.Select(x => String.Format(CultureInfo.InvariantCulture, "#{0} at {1}", x.Number, x.PullRequestRevision.Substring(0, 7))));
103  }
104  #pragma warning restore CA1506
105  }
106 }
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
readonly Models.Instance instance
The Models.Instance for the PullRequestsCommand
Command for reading the active Api.Models.TestMerges
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:14