fix(sonarr): Run version enforcement always

Version enforcement was only running if the user specified release
profiles because that logic was unintentionally tightly coupled to them.
Now that logic runs regardless of whether the user is using RPs or CFs.
pull/136/head
Robert Dailey 2 years ago
parent ba84b07713
commit 2e189ff275

@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Use compact JSON for HTTP request/response body in debug log files. This makes logs much easier to - Use compact JSON for HTTP request/response body in debug log files. This makes logs much easier to
scroll through. scroll through.
- Sonarr: Run version enforcement logic when using CFs instead of RPs.
## [2.5.0] - 2022-09-11 ## [2.5.0] - 2022-09-11

@ -49,6 +49,7 @@ public class SonarrCommand : ServiceCommand
var log = container.Resolve<ILogger>(); var log = container.Resolve<ILogger>();
var customFormatUpdaterFactory = container.Resolve<Func<ICustomFormatUpdater>>(); var customFormatUpdaterFactory = container.Resolve<Func<ICustomFormatUpdater>>();
var guideService = container.Resolve<ISonarrGuideService>(); var guideService = container.Resolve<ISonarrGuideService>();
var versionEnforcement = container.Resolve<ISonarrVersionEnforcement>();
if (ListReleaseProfiles) if (ListReleaseProfiles)
{ {
@ -87,6 +88,8 @@ public class SonarrCommand : ServiceCommand
{ {
log.Information("Processing server {Url}", FlurlLogging.SanitizeUrl(config.BaseUrl)); log.Information("Processing server {Url}", FlurlLogging.SanitizeUrl(config.BaseUrl));
await versionEnforcement.DoVersionEnforcement(config);
if (config.ReleaseProfiles.Count > 0) if (config.ReleaseProfiles.Count > 0)
{ {
await profileUpdaterFactory().Process(Preview, config); await profileUpdaterFactory().Process(Preview, config);

@ -7,6 +7,7 @@ using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization; using Newtonsoft.Json.Serialization;
using NSubstitute; using NSubstitute;
using NUnit.Framework; using NUnit.Framework;
using Recyclarr.Command;
using TestLibrary.AutoFixture; using TestLibrary.AutoFixture;
using TrashLib.Config.Services; using TrashLib.Config.Services;
using TrashLib.ExceptionTypes; using TrashLib.ExceptionTypes;
@ -14,7 +15,6 @@ using TrashLib.Services.Sonarr;
using TrashLib.Services.Sonarr.Api; using TrashLib.Services.Sonarr.Api;
using TrashLib.Services.Sonarr.Api.Objects; using TrashLib.Services.Sonarr.Api.Objects;
using TrashLib.Services.Sonarr.Config; using TrashLib.Services.Sonarr.Config;
using TrashLib.Services.Sonarr.ReleaseProfile;
using TrashLib.Startup; using TrashLib.Startup;
namespace TrashLib.Tests.Sonarr; namespace TrashLib.Tests.Sonarr;
@ -122,7 +122,7 @@ public class SonarrCompatibilityTest
public async Task Failure_when_release_profiles_used_with_sonarr_v4( public async Task Failure_when_release_profiles_used_with_sonarr_v4(
[Frozen] ISonarrApi api, [Frozen] ISonarrApi api,
[Frozen(Matching.ImplementedInterfaces)] SonarrCompatibility compatibility, [Frozen(Matching.ImplementedInterfaces)] SonarrCompatibility compatibility,
ReleaseProfileUpdater updater) SonarrVersionEnforcement enforcement)
{ {
api.GetVersion().Returns(new Version(4, 0)); api.GetVersion().Returns(new Version(4, 0));
@ -131,7 +131,7 @@ public class SonarrCompatibilityTest
ReleaseProfiles = new List<ReleaseProfileConfig> {new()} ReleaseProfiles = new List<ReleaseProfileConfig> {new()}
}; };
var act = () => updater.Process(false, config); var act = () => enforcement.DoVersionEnforcement(config);
await act.Should().ThrowAsync<VersionException>().WithMessage("Sonarr v4*"); await act.Should().ThrowAsync<VersionException>().WithMessage("Sonarr v4*");
} }
@ -140,7 +140,7 @@ public class SonarrCompatibilityTest
public async Task No_failure_when_release_profiles_used_with_sonarr_v3( public async Task No_failure_when_release_profiles_used_with_sonarr_v3(
[Frozen] ISonarrApi api, [Frozen] ISonarrApi api,
[Frozen(Matching.ImplementedInterfaces)] SonarrCompatibility compatibility, [Frozen(Matching.ImplementedInterfaces)] SonarrCompatibility compatibility,
ReleaseProfileUpdater updater) SonarrVersionEnforcement enforcement)
{ {
api.GetVersion().Returns(new Version(3, 9)); api.GetVersion().Returns(new Version(3, 9));
@ -149,7 +149,7 @@ public class SonarrCompatibilityTest
ReleaseProfiles = new List<ReleaseProfileConfig> {new()} ReleaseProfiles = new List<ReleaseProfileConfig> {new()}
}; };
var act = () => updater.Process(false, config); var act = () => enforcement.DoVersionEnforcement(config);
await act.Should().NotThrowAsync(); await act.Should().NotThrowAsync();
} }
@ -158,7 +158,7 @@ public class SonarrCompatibilityTest
public async Task Failure_when_custom_formats_used_with_sonarr_v3( public async Task Failure_when_custom_formats_used_with_sonarr_v3(
[Frozen] ISonarrApi api, [Frozen] ISonarrApi api,
[Frozen(Matching.ImplementedInterfaces)] SonarrCompatibility compatibility, [Frozen(Matching.ImplementedInterfaces)] SonarrCompatibility compatibility,
ReleaseProfileUpdater updater) SonarrVersionEnforcement enforcement)
{ {
api.GetVersion().Returns(new Version(3, 9)); api.GetVersion().Returns(new Version(3, 9));
@ -167,7 +167,7 @@ public class SonarrCompatibilityTest
CustomFormats = new List<CustomFormatConfig> {new()} CustomFormats = new List<CustomFormatConfig> {new()}
}; };
var act = () => updater.Process(false, config); var act = () => enforcement.DoVersionEnforcement(config);
await act.Should().ThrowAsync<VersionException>().WithMessage("Sonarr v3*custom format*use*v4*"); await act.Should().ThrowAsync<VersionException>().WithMessage("Sonarr v3*custom format*use*v4*");
} }
@ -176,7 +176,7 @@ public class SonarrCompatibilityTest
public async Task No_failure_when_custom_formats_used_with_sonarr_v4( public async Task No_failure_when_custom_formats_used_with_sonarr_v4(
[Frozen] ISonarrApi api, [Frozen] ISonarrApi api,
[Frozen(Matching.ImplementedInterfaces)] SonarrCompatibility compatibility, [Frozen(Matching.ImplementedInterfaces)] SonarrCompatibility compatibility,
ReleaseProfileUpdater updater) SonarrVersionEnforcement enforcement)
{ {
api.GetVersion().Returns(new Version(4, 0)); api.GetVersion().Returns(new Version(4, 0));
@ -185,7 +185,7 @@ public class SonarrCompatibilityTest
CustomFormats = new List<CustomFormatConfig> {new()} CustomFormats = new List<CustomFormatConfig> {new()}
}; };
var act = () => updater.Process(false, config); var act = () => enforcement.DoVersionEnforcement(config);
await act.Should().NotThrowAsync(); await act.Should().NotThrowAsync();
} }

@ -0,0 +1,8 @@
using TrashLib.Services.Sonarr.Config;
namespace Recyclarr.Command;
public interface ISonarrVersionEnforcement
{
Task DoVersionEnforcement(SonarrConfiguration config);
}

@ -1,8 +1,6 @@
using System.Reactive.Linq;
using CliFx.Infrastructure; using CliFx.Infrastructure;
using Common.Extensions; using Common.Extensions;
using Serilog; using Serilog;
using TrashLib.ExceptionTypes;
using TrashLib.Services.Sonarr.Api; using TrashLib.Services.Sonarr.Api;
using TrashLib.Services.Sonarr.Api.Objects; using TrashLib.Services.Sonarr.Api.Objects;
using TrashLib.Services.Sonarr.Config; using TrashLib.Services.Sonarr.Config;
@ -14,7 +12,6 @@ namespace TrashLib.Services.Sonarr.ReleaseProfile;
public class ReleaseProfileUpdater : IReleaseProfileUpdater public class ReleaseProfileUpdater : IReleaseProfileUpdater
{ {
private readonly IReleaseProfileApiService _releaseProfileApi; private readonly IReleaseProfileApiService _releaseProfileApi;
private readonly ISonarrCompatibility _compatibility;
private readonly IReleaseProfileFilterPipeline _pipeline; private readonly IReleaseProfileFilterPipeline _pipeline;
private readonly IConsole _console; private readonly IConsole _console;
private readonly ISonarrGuideService _guide; private readonly ISonarrGuideService _guide;
@ -26,7 +23,6 @@ public class ReleaseProfileUpdater : IReleaseProfileUpdater
ISonarrGuideService guide, ISonarrGuideService guide,
ISonarrApi api, ISonarrApi api,
IReleaseProfileApiService releaseProfileApi, IReleaseProfileApiService releaseProfileApi,
ISonarrCompatibility compatibility,
IReleaseProfileFilterPipeline pipeline, IReleaseProfileFilterPipeline pipeline,
IConsole console) IConsole console)
{ {
@ -34,15 +30,12 @@ public class ReleaseProfileUpdater : IReleaseProfileUpdater
_guide = guide; _guide = guide;
_api = api; _api = api;
_releaseProfileApi = releaseProfileApi; _releaseProfileApi = releaseProfileApi;
_compatibility = compatibility;
_pipeline = pipeline; _pipeline = pipeline;
_console = console; _console = console;
} }
public async Task Process(bool isPreview, SonarrConfiguration config) public async Task Process(bool isPreview, SonarrConfiguration config)
{ {
await DoVersionEnforcement(config);
var profilesFromGuide = _guide.GetReleaseProfileData(); var profilesFromGuide = _guide.GetReleaseProfileData();
var filteredProfiles = new List<(ReleaseProfileData Profile, IReadOnlyCollection<string> Tags)>(); var filteredProfiles = new List<(ReleaseProfileData Profile, IReadOnlyCollection<string> Tags)>();
@ -196,27 +189,6 @@ public class ReleaseProfileUpdater : IReleaseProfileUpdater
.ToList(); .ToList();
} }
private async Task DoVersionEnforcement(SonarrConfiguration config)
{
var capabilities = await _compatibility.Capabilities.LastAsync();
if (!capabilities.SupportsNamedReleaseProfiles)
{
throw new VersionException(
$"Your Sonarr version {capabilities.Version} does not meet the minimum " +
$"required version of {_compatibility.MinimumVersion} to use this program");
}
switch (capabilities.SupportsCustomFormats)
{
case true when config.ReleaseProfiles.Any():
throw new VersionException(
"Sonarr v4 does not support Release Profiles. Please use Sonarr v3 instead.");
case false when config.CustomFormats.Any():
throw new VersionException("Sonarr v3 does not support Custom Formats. Please use Sonarr v4 instead.");
}
}
private async Task CreateMissingTags(ICollection<SonarrTag> sonarrTags, IEnumerable<string> configTags) private async Task CreateMissingTags(ICollection<SonarrTag> sonarrTags, IEnumerable<string> configTags)
{ {
var missingTags = configTags.Where(t => !sonarrTags.Any(t2 => t2.Label.EqualsIgnoreCase(t))); var missingTags = configTags.Where(t => !sonarrTags.Any(t2 => t2.Label.EqualsIgnoreCase(t)));

@ -1,5 +1,6 @@
using Autofac; using Autofac;
using Autofac.Extras.Ordering; using Autofac.Extras.Ordering;
using Recyclarr.Command;
using TrashLib.Services.Sonarr.Api; using TrashLib.Services.Sonarr.Api;
using TrashLib.Services.Sonarr.Config; using TrashLib.Services.Sonarr.Config;
using TrashLib.Services.Sonarr.QualityDefinition; using TrashLib.Services.Sonarr.QualityDefinition;
@ -17,6 +18,7 @@ public class SonarrAutofacModule : Module
builder.RegisterType<ReleaseProfileApiService>().As<IReleaseProfileApiService>(); builder.RegisterType<ReleaseProfileApiService>().As<IReleaseProfileApiService>();
builder.RegisterType<SonarrValidationMessages>().As<ISonarrValidationMessages>(); builder.RegisterType<SonarrValidationMessages>().As<ISonarrValidationMessages>();
builder.RegisterType<SonarrCompatibility>().As<ISonarrCompatibility>().InstancePerLifetimeScope(); builder.RegisterType<SonarrCompatibility>().As<ISonarrCompatibility>().InstancePerLifetimeScope();
builder.RegisterType<SonarrVersionEnforcement>().As<ISonarrVersionEnforcement>();
builder.RegisterType<SonarrGuideDataLister>().As<ISonarrGuideDataLister>(); builder.RegisterType<SonarrGuideDataLister>().As<ISonarrGuideDataLister>();
// Release Profile Support // Release Profile Support

@ -0,0 +1,38 @@
using System.Reactive.Linq;
using TrashLib.ExceptionTypes;
using TrashLib.Services.Sonarr;
using TrashLib.Services.Sonarr.Config;
namespace Recyclarr.Command;
public class SonarrVersionEnforcement : ISonarrVersionEnforcement
{
private readonly ISonarrCompatibility _compatibility;
public SonarrVersionEnforcement(ISonarrCompatibility compatibility)
{
_compatibility = compatibility;
}
public async Task DoVersionEnforcement(SonarrConfiguration config)
{
var capabilities = await _compatibility.Capabilities.LastAsync();
if (!capabilities.SupportsNamedReleaseProfiles)
{
throw new VersionException(
$"Your Sonarr version {capabilities.Version} does not meet the minimum " +
$"required version of {_compatibility.MinimumVersion} to use this program");
}
switch (capabilities.SupportsCustomFormats)
{
case true when config.ReleaseProfiles.Any():
throw new VersionException(
"Sonarr v4 does not support Release Profiles. Please use Sonarr v3 instead.");
case false when config.CustomFormats.Any():
throw new VersionException(
"Sonarr v3 does not support Custom Formats. Please use Sonarr v4 instead.");
}
}
}
Loading…
Cancel
Save