1
0
mirror of https://github.com/ncblakely/GiantsTools synced 2024-11-05 14:55:38 +01:00
GiantsTools/Giants.Services/Services/VersioningService.cs

98 lines
3.7 KiB
C#
Raw Normal View History

2022-09-05 22:42:41 +02:00
using Giants.DataContract.V1;
2022-09-09 07:31:24 +02:00
using Giants.Services.Utility;
2022-09-05 22:42:41 +02:00
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System;
2022-09-09 07:31:24 +02:00
using System.Collections.Generic;
using System.Linq;
2022-09-05 22:42:41 +02:00
using System.Threading.Tasks;
namespace Giants.Services
{
public class VersioningService : IVersioningService
{
private readonly IVersioningStore versioningStore;
private readonly IConfiguration configuration;
2022-09-09 07:31:24 +02:00
private readonly ISimpleMemoryCache<VersionInfo> versionCache;
2022-09-05 22:42:41 +02:00
private readonly ILogger<VersioningService> logger;
2022-09-09 07:31:24 +02:00
2022-09-05 22:42:41 +02:00
private const string InstallerContainerName = "public";
public VersioningService(
2022-09-09 07:31:24 +02:00
ILogger<VersioningService> logger,
2022-09-05 22:42:41 +02:00
IVersioningStore updaterStore,
IConfiguration configuration,
2022-09-09 07:31:24 +02:00
ISimpleMemoryCache<VersionInfo> versionCache)
2022-09-05 22:42:41 +02:00
{
2022-09-09 07:31:24 +02:00
this.logger = logger;
2022-09-05 22:42:41 +02:00
this.versioningStore = updaterStore;
this.configuration = configuration;
2022-09-09 07:31:24 +02:00
this.versionCache = versionCache;
2022-09-05 22:42:41 +02:00
}
2022-09-09 07:31:24 +02:00
public async Task<VersionInfo> GetVersionInfo(string appName, string branchName)
2022-09-05 22:42:41 +02:00
{
ArgumentUtility.CheckStringForNullOrEmpty(appName);
2022-09-09 07:31:24 +02:00
branchName ??= BranchConstants.DefaultBranchName;
var versions = await this.versionCache.GetItems();
return versions
.Where(x => x.AppName.Equals(appName, StringComparison.Ordinal)
&& !string.IsNullOrEmpty(x.BranchName)
&& x.BranchName.Equals(branchName, StringComparison.OrdinalIgnoreCase))
2022-09-09 07:31:24 +02:00
.FirstOrDefault();
2022-09-05 22:42:41 +02:00
}
2022-09-09 07:31:24 +02:00
public async Task UpdateVersionInfo(string appName, AppVersion appVersion, string fileName, string branchName, bool force)
2022-09-05 22:42:41 +02:00
{
ArgumentUtility.CheckStringForNullOrEmpty(appName);
ArgumentUtility.CheckForNull(appVersion);
ArgumentUtility.CheckStringForNullOrEmpty(fileName);
2022-09-09 07:31:24 +02:00
ArgumentUtility.CheckStringForNullOrEmpty(branchName);
2022-09-05 22:42:41 +02:00
2022-09-09 07:31:24 +02:00
var storageAccountUri = new Uri(this.configuration["StorageAccountUri"]);
2022-09-05 22:42:41 +02:00
2022-09-09 07:31:24 +02:00
VersionInfo versionInfo = await this.GetVersionInfo(appName, branchName);
2022-09-05 22:42:41 +02:00
if (versionInfo == null)
{
2022-09-09 07:31:24 +02:00
throw new ArgumentException($"No version information for {appName} ({branchName}) found.");
2022-09-05 22:42:41 +02:00
}
2022-09-09 07:31:24 +02:00
if (!force && (appVersion < versionInfo.Version))
2022-09-05 22:42:41 +02:00
{
throw new ArgumentException($"Version {appVersion.SerializeToJson()} is less than current version {versionInfo.Version.SerializeToJson()}", nameof(appVersion));
}
if (fileName.Contains('/') || fileName.Contains('\\'))
{
throw new ArgumentException("File name must be relative to configured storage account.", nameof(fileName));
}
var installerUri = new Uri(storageAccountUri, $"{InstallerContainerName}/{fileName}");
var newVersionInfo = new VersionInfo()
{
AppName = appName,
Version = appVersion,
InstallerUri = installerUri,
2022-09-09 07:31:24 +02:00
BranchName = branchName,
2022-09-05 22:42:41 +02:00
};
this.logger.LogInformation("Updating version info for {appName}: {versionInfo}", appName, newVersionInfo.SerializeToJson());
await this.versioningStore.UpdateVersionInfo(newVersionInfo);
2022-09-09 07:31:24 +02:00
this.versionCache.Invalidate();
}
public async Task<IEnumerable<string>> GetBranches(string appName)
{
var allVersions = await this.versionCache.GetItems();
return allVersions
.Where(x => x.AppName.Equals(appName, StringComparison.OrdinalIgnoreCase))
.Select(x => x.BranchName).ToList();
2022-09-05 22:42:41 +02:00
}
}
}