tgstation-server
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  public async Task<string> Invoke(string arguments, User user, CancellationToken cancellationToken)
65  {
66  IEnumerable<Models.TestMerge> results = null;
67  if (arguments.Split(' ').Any(x => x.ToUpperInvariant() == "--REPO"))
68  {
69  string head;
70  using (var repo = await repositoryManager.LoadRepository(cancellationToken).ConfigureAwait(false))
71  {
72  if (repo == null)
73  return "Repository unavailable!";
74  head = repo.Head;
75  }
76  await databaseContextFactory.UseContext(async db => results = await db.RevisionInformations.Where(x => x.Instance.Id == instance.Id && x.CommitSha == head)
77  .SelectMany(x => x.ActiveTestMerges)
78  .Select(x => x.TestMerge)
79  .Select(x => new Models.TestMerge
80  {
81  Number = x.Number,
82  PullRequestRevision = x.PullRequestRevision
83  }).ToListAsync(cancellationToken).ConfigureAwait(false)).ConfigureAwait(false);
84  }
85  else
86  {
87  if (!watchdog.Running)
88  return "Server offline!";
89  results = watchdog.ActiveCompileJob?.RevisionInformation.ActiveTestMerges.Select(x => x.TestMerge).ToList() ?? new List<Models.TestMerge>();
90  }
91 
92  if (!results.Any())
93  return "None!";
94 
95  return String.Join(", ", results.Select(x => String.Format(CultureInfo.InvariantCulture, "#{0} at {1}", x.Number, x.PullRequestRevision.Substring(0, 7))));
96  }
97  }
98 }
readonly IDatabaseContextFactory databaseContextFactory
The IDatabaseContextFactory for the PullRequestsCommand
readonly IRepositoryManager repositoryManager
The IRepositoryManager for the PullRequestsCommand
Factory for scoping usage of IDatabaseContexts. Meant for use by Components
readonly Models.Instance instance
The Models.Instance for the PullRequestsCommand
Command for reading the active Api.Models.TestMerges
readonly IWatchdog watchdog
The IWatchdog for the PullRequestsCommand
Represents a tgs_chat_user datum
Definition: User.cs:10
Factory for creating and loading IRepositorys
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
async Task< string > Invoke(string arguments, User user, CancellationToken cancellationToken)
Invoke the ICommand
Runs and monitors the twin server controllers
Definition: IWatchdog.cs:12