Add revision command

This commit is contained in:
Jordan Brown
2018-08-18 11:07:55 -04:00
parent bd889af82c
commit a1eb9622e0
2 changed files with 76 additions and 0 deletions
@@ -77,6 +77,7 @@ namespace Tgstation.Server.Host.Components.Chat.Commands
{
new VersionCommand(application),
new ByondCommand(byondManager),
new RevisionCommand(watchdog, repositoryManager, instance),
new PullRequestsCommand(watchdog, repositoryManager, databaseContextFactory, instance),
new KekCommand()
};
@@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Tgstation.Server.Host.Components.Repository;
using Tgstation.Server.Host.Components.Watchdog;
namespace Tgstation.Server.Host.Components.Chat.Commands
{
/// <summary>
/// For displaying <see cref="Api.Models.Internal.RevisionInformation"/>
/// </summary>
sealed class RevisionCommand : ICommand
{
/// <inheritdoc />
public string Name => "revision";
/// <inheritdoc />
public string HelpText => "Display live commit sha. Add --repo to view repository revision";
/// <inheritdoc />
public bool AdminOnly => false;
/// <summary>
/// The <see cref="IWatchdog"/> for the <see cref="PullRequestsCommand"/>
/// </summary>
readonly IWatchdog watchdog;
/// <summary>
/// The <see cref="IRepositoryManager"/> for the <see cref="PullRequestsCommand"/>
/// </summary>
readonly IRepositoryManager repositoryManager;
/// <summary>
/// The <see cref="Models.Instance"/> for the <see cref="PullRequestsCommand"/>
/// </summary>
readonly Models.Instance instance;
/// <summary>
/// Construct a <see cref="RevisionCommand"/>
/// </summary>
/// <param name="watchdog">The value of <see cref="watchdog"/></param>
/// <param name="repositoryManager">The value of <see cref="repositoryManager"/></param>
/// <param name="instance">The value of <see cref="instance"/></param>
public RevisionCommand(IWatchdog watchdog, IRepositoryManager repositoryManager, Models.Instance instance)
{
this.watchdog = watchdog ?? throw new ArgumentNullException(nameof(watchdog));
this.repositoryManager = repositoryManager ?? throw new ArgumentNullException(nameof(repositoryManager));
this.instance = instance ?? throw new ArgumentNullException(nameof(instance));
}
/// <inheritdoc />
public async Task<string> Invoke(string arguments, User user, CancellationToken cancellationToken)
{
string result;
if (arguments.Split(' ').Any(x => x.ToUpperInvariant() == "--REPO"))
using (var repo = await repositoryManager.LoadRepository(cancellationToken).ConfigureAwait(false))
{
if (repo == null)
return "Repository unavailable!";
result = repo.Head;
}
else
{
if (!watchdog.Running)
return "Server offline!";
result = watchdog.ActiveCompileJob?.RevisionInformation.CommitSha;
}
return String.Format(CultureInfo.InvariantCulture, "^{0}", result);
}
}
}