From 2e189ff2753abe5a9eae7033bd8fcf11e4666609 Mon Sep 17 00:00:00 2001 From: Robert Dailey Date: Wed, 12 Oct 2022 14:38:34 -0500 Subject: [PATCH] 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. --- CHANGELOG.md | 1 + src/Recyclarr/Command/SonarrCommand.cs | 3 ++ .../Sonarr/SonarrCompatibilityTest.cs | 18 ++++----- .../Sonarr/ISonarrVersionEnforcement.cs | 8 ++++ .../ReleaseProfile/ReleaseProfileUpdater.cs | 28 -------------- .../Services/Sonarr/SonarrAutofacModule.cs | 2 + .../Sonarr/SonarrVersionEnforcement.cs | 38 +++++++++++++++++++ 7 files changed, 61 insertions(+), 37 deletions(-) create mode 100644 src/TrashLib/Services/Sonarr/ISonarrVersionEnforcement.cs create mode 100644 src/TrashLib/Services/Sonarr/SonarrVersionEnforcement.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c7b6aeb..e0750f9a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 scroll through. +- Sonarr: Run version enforcement logic when using CFs instead of RPs. ## [2.5.0] - 2022-09-11 diff --git a/src/Recyclarr/Command/SonarrCommand.cs b/src/Recyclarr/Command/SonarrCommand.cs index 2ee139ab..4477fcfd 100644 --- a/src/Recyclarr/Command/SonarrCommand.cs +++ b/src/Recyclarr/Command/SonarrCommand.cs @@ -49,6 +49,7 @@ public class SonarrCommand : ServiceCommand var log = container.Resolve(); var customFormatUpdaterFactory = container.Resolve>(); var guideService = container.Resolve(); + var versionEnforcement = container.Resolve(); if (ListReleaseProfiles) { @@ -87,6 +88,8 @@ public class SonarrCommand : ServiceCommand { log.Information("Processing server {Url}", FlurlLogging.SanitizeUrl(config.BaseUrl)); + await versionEnforcement.DoVersionEnforcement(config); + if (config.ReleaseProfiles.Count > 0) { await profileUpdaterFactory().Process(Preview, config); diff --git a/src/TrashLib.Tests/Sonarr/SonarrCompatibilityTest.cs b/src/TrashLib.Tests/Sonarr/SonarrCompatibilityTest.cs index 48407028..d3762433 100644 --- a/src/TrashLib.Tests/Sonarr/SonarrCompatibilityTest.cs +++ b/src/TrashLib.Tests/Sonarr/SonarrCompatibilityTest.cs @@ -7,6 +7,7 @@ using Newtonsoft.Json.Linq; using Newtonsoft.Json.Serialization; using NSubstitute; using NUnit.Framework; +using Recyclarr.Command; using TestLibrary.AutoFixture; using TrashLib.Config.Services; using TrashLib.ExceptionTypes; @@ -14,7 +15,6 @@ using TrashLib.Services.Sonarr; using TrashLib.Services.Sonarr.Api; using TrashLib.Services.Sonarr.Api.Objects; using TrashLib.Services.Sonarr.Config; -using TrashLib.Services.Sonarr.ReleaseProfile; using TrashLib.Startup; namespace TrashLib.Tests.Sonarr; @@ -122,7 +122,7 @@ public class SonarrCompatibilityTest public async Task Failure_when_release_profiles_used_with_sonarr_v4( [Frozen] ISonarrApi api, [Frozen(Matching.ImplementedInterfaces)] SonarrCompatibility compatibility, - ReleaseProfileUpdater updater) + SonarrVersionEnforcement enforcement) { api.GetVersion().Returns(new Version(4, 0)); @@ -131,7 +131,7 @@ public class SonarrCompatibilityTest ReleaseProfiles = new List {new()} }; - var act = () => updater.Process(false, config); + var act = () => enforcement.DoVersionEnforcement(config); await act.Should().ThrowAsync().WithMessage("Sonarr v4*"); } @@ -140,7 +140,7 @@ public class SonarrCompatibilityTest public async Task No_failure_when_release_profiles_used_with_sonarr_v3( [Frozen] ISonarrApi api, [Frozen(Matching.ImplementedInterfaces)] SonarrCompatibility compatibility, - ReleaseProfileUpdater updater) + SonarrVersionEnforcement enforcement) { api.GetVersion().Returns(new Version(3, 9)); @@ -149,7 +149,7 @@ public class SonarrCompatibilityTest ReleaseProfiles = new List {new()} }; - var act = () => updater.Process(false, config); + var act = () => enforcement.DoVersionEnforcement(config); await act.Should().NotThrowAsync(); } @@ -158,7 +158,7 @@ public class SonarrCompatibilityTest public async Task Failure_when_custom_formats_used_with_sonarr_v3( [Frozen] ISonarrApi api, [Frozen(Matching.ImplementedInterfaces)] SonarrCompatibility compatibility, - ReleaseProfileUpdater updater) + SonarrVersionEnforcement enforcement) { api.GetVersion().Returns(new Version(3, 9)); @@ -167,7 +167,7 @@ public class SonarrCompatibilityTest CustomFormats = new List {new()} }; - var act = () => updater.Process(false, config); + var act = () => enforcement.DoVersionEnforcement(config); await act.Should().ThrowAsync().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( [Frozen] ISonarrApi api, [Frozen(Matching.ImplementedInterfaces)] SonarrCompatibility compatibility, - ReleaseProfileUpdater updater) + SonarrVersionEnforcement enforcement) { api.GetVersion().Returns(new Version(4, 0)); @@ -185,7 +185,7 @@ public class SonarrCompatibilityTest CustomFormats = new List {new()} }; - var act = () => updater.Process(false, config); + var act = () => enforcement.DoVersionEnforcement(config); await act.Should().NotThrowAsync(); } diff --git a/src/TrashLib/Services/Sonarr/ISonarrVersionEnforcement.cs b/src/TrashLib/Services/Sonarr/ISonarrVersionEnforcement.cs new file mode 100644 index 00000000..34ef8730 --- /dev/null +++ b/src/TrashLib/Services/Sonarr/ISonarrVersionEnforcement.cs @@ -0,0 +1,8 @@ +using TrashLib.Services.Sonarr.Config; + +namespace Recyclarr.Command; + +public interface ISonarrVersionEnforcement +{ + Task DoVersionEnforcement(SonarrConfiguration config); +} diff --git a/src/TrashLib/Services/Sonarr/ReleaseProfile/ReleaseProfileUpdater.cs b/src/TrashLib/Services/Sonarr/ReleaseProfile/ReleaseProfileUpdater.cs index a84a16c7..33e37fb6 100644 --- a/src/TrashLib/Services/Sonarr/ReleaseProfile/ReleaseProfileUpdater.cs +++ b/src/TrashLib/Services/Sonarr/ReleaseProfile/ReleaseProfileUpdater.cs @@ -1,8 +1,6 @@ -using System.Reactive.Linq; using CliFx.Infrastructure; using Common.Extensions; using Serilog; -using TrashLib.ExceptionTypes; using TrashLib.Services.Sonarr.Api; using TrashLib.Services.Sonarr.Api.Objects; using TrashLib.Services.Sonarr.Config; @@ -14,7 +12,6 @@ namespace TrashLib.Services.Sonarr.ReleaseProfile; public class ReleaseProfileUpdater : IReleaseProfileUpdater { private readonly IReleaseProfileApiService _releaseProfileApi; - private readonly ISonarrCompatibility _compatibility; private readonly IReleaseProfileFilterPipeline _pipeline; private readonly IConsole _console; private readonly ISonarrGuideService _guide; @@ -26,7 +23,6 @@ public class ReleaseProfileUpdater : IReleaseProfileUpdater ISonarrGuideService guide, ISonarrApi api, IReleaseProfileApiService releaseProfileApi, - ISonarrCompatibility compatibility, IReleaseProfileFilterPipeline pipeline, IConsole console) { @@ -34,15 +30,12 @@ public class ReleaseProfileUpdater : IReleaseProfileUpdater _guide = guide; _api = api; _releaseProfileApi = releaseProfileApi; - _compatibility = compatibility; _pipeline = pipeline; _console = console; } public async Task Process(bool isPreview, SonarrConfiguration config) { - await DoVersionEnforcement(config); - var profilesFromGuide = _guide.GetReleaseProfileData(); var filteredProfiles = new List<(ReleaseProfileData Profile, IReadOnlyCollection Tags)>(); @@ -196,27 +189,6 @@ public class ReleaseProfileUpdater : IReleaseProfileUpdater .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 sonarrTags, IEnumerable configTags) { var missingTags = configTags.Where(t => !sonarrTags.Any(t2 => t2.Label.EqualsIgnoreCase(t))); diff --git a/src/TrashLib/Services/Sonarr/SonarrAutofacModule.cs b/src/TrashLib/Services/Sonarr/SonarrAutofacModule.cs index e76bf8dc..b8414c21 100644 --- a/src/TrashLib/Services/Sonarr/SonarrAutofacModule.cs +++ b/src/TrashLib/Services/Sonarr/SonarrAutofacModule.cs @@ -1,5 +1,6 @@ using Autofac; using Autofac.Extras.Ordering; +using Recyclarr.Command; using TrashLib.Services.Sonarr.Api; using TrashLib.Services.Sonarr.Config; using TrashLib.Services.Sonarr.QualityDefinition; @@ -17,6 +18,7 @@ public class SonarrAutofacModule : Module builder.RegisterType().As(); builder.RegisterType().As(); builder.RegisterType().As().InstancePerLifetimeScope(); + builder.RegisterType().As(); builder.RegisterType().As(); // Release Profile Support diff --git a/src/TrashLib/Services/Sonarr/SonarrVersionEnforcement.cs b/src/TrashLib/Services/Sonarr/SonarrVersionEnforcement.cs new file mode 100644 index 00000000..ebc97590 --- /dev/null +++ b/src/TrashLib/Services/Sonarr/SonarrVersionEnforcement.cs @@ -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."); + } + } +}